kaxil commented on code in PR #77197:
URL: https://github.com/apache/airflow/pull/77197#discussion_r4710880622


##########
providers/cncf/kubernetes/docs/changelog.rst:
##########
@@ -27,6 +27,19 @@
 Changelog
 ---------
 
+.. note::
+    When a retry reattaches to the pod of a previous try and finds it 
``Succeeded``,
+    ``KubernetesPodOperator`` now treats that pod as the finished run instead 
of deleting it and
+    starting a new pod. This applies only with ``do_xcom_push=False``; with 
XCom on, the pod is still
+    deleted and recreated because its result can no longer be read. Set 
``reattach_on_restart=False``
+    (or ``durable=False`` on Airflow 3.3+) to always start a new pod on retry.
+
+.. note::
+    With ``on_kill_action="keep_pod"`` and tries left, a pod whose try hits 
``execution_timeout`` is
+    now left running and the retry reattaches to it, in sync mode with 
``durable=True`` (or
+    ``reattach_on_restart=True`` below Airflow 3.3) and no ``retry_policy`` 
set. Set
+    ``on_kill_action="delete_pod"`` or ``durable=False`` to delete the pod on 
timeout as before.

Review Comment:
   Below 3.3 `durable=False` only warns and `reattach_on_restart` stays True, 
so this opt-out still keeps the pod there. That was my wording, sorry. Maybe 
``durable=False`` (``reattach_on_restart=False`` below Airflow 3.3), like the 
note above?



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/pod.py:
##########
@@ -1435,6 +1461,15 @@ def cleanup(
         if self._killed or not remote_pod:
             return
 
+        if self._keep_pod_after_timeout:
+            self.log.info(
+                "Task interrupted by execution_timeout and on_kill_action is 
%r, keeping pod %s "
+                "so the next try can reattach.",
+                self.on_kill_action.value,
+                cast("k8s.V1ObjectMeta", remote_pod.metadata).name,
+            )
+            return

Review Comment:
   This return only skips `cleanup`. `post_complete_action` still calls 
`on_pod_cleanup` right after, on a pod that is still running and that the retry 
reattaches to. Before this change `cleanup` raised on a running pod, so those 
callbacks didn't run when a try timed out with its pod running. The hook is 
documented as called after cleaning/deleting the pod, so a callback that tears 
down something the pod uses would now do it under the retry. Should the 
callbacks be skipped while the pod is kept? I missed this earlier.



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/pod.py:
##########
@@ -942,6 +956,18 @@ def execute_sync(self, context: Context):
             self.remote_pod = self.pod_manager.await_pod_completion(
                 self.pod, istio_enabled, self.base_container_name, 
self.do_xcom_push
             )
+        except AirflowTaskTimeout:
+            # The task runner calls on_kill only after this propagates, so 
cleanup has to know now
+            # that the pod must be left running for the next try to reattach. 
A retry_policy (Airflow
+            # 3.3+) can fail the task with tries left, which would leave the 
kept pod running forever.
+            ti = context["ti"]
+            self._keep_pod_after_timeout = (
+                self.reattach_on_restart
+                and self.on_kill_action == OnKillAction.KEEP_POD
+                and ti.try_number <= ti.max_tries
+                and getattr(self, "retry_policy", None) is None

Review Comment:
   When this declines the keep for a `keep_pod` user (last try, or a 
`retry_policy` is set), cleanup handles the pod per `on_finish_action` and 
raises "Pod ... returned a failure.", and nothing in the log says `keep_pod` 
was overridden or why. With a retry policy that happens on every timeout, so 
someone who set `keep_pod` gets a fresh pod each try with no explanation. Could 
this log which condition declined it, only when `on_kill_action` is `keep_pod`?



##########
providers/cncf/kubernetes/src/airflow/providers/cncf/kubernetes/operators/pod.py:
##########
@@ -779,6 +788,11 @@ def get_or_create_pod(self, pod_request_obj: k8s.V1Pod, 
context: Context) -> k8s
 
                 if pod_phase == PodPhase.SUCCEEDED:
                     Stats.incr("resumable_job.already_succeeded", 
tags=stats_tags)
+                    # Without XCom there is nothing left to read from the pod, 
so a pod that finished
+                    # after its try was interrupted (e.g. a kept pod after a 
timeout) is this try's run.
+                    if not self.do_xcom_push:

Review Comment:
   A Clear gets here too, which I didn't think about when I asked for this. Say 
a try times out and the pod is kept, then someone clears the task to start 
over. The next try still finds that pod (the state store can survive a clear, 
and the label search has no try number), and with `do_xcom_push` off a pod that 
finished in the meantime marks the cleared task successful without running 
anything. I can't see a clean way to tell a clear from a retry here. Maybe the 
Durable execution docs should say a clear reattaches to the kept pod too?



##########
providers/cncf/kubernetes/docs/operators.rst:
##########
@@ -243,6 +243,12 @@ persisted, the operator falls back to the same label 
search ``reattach_on_restar
 used, so a running pod from a prior attempt is still found and reattached to 
rather than
 duplicated. Once an identity is persisted, subsequent retries skip the label 
search entirely.
 
+By default, a try interrupted by ``execution_timeout`` deletes the pod (per 
``on_finish_action``), so the retry
+starts a fresh pod. To let the retry reattach instead, set 
``on_kill_action="keep_pod"``: the pod is left running
+and is not marked as already checked. On the last try no retry follows, so the 
pod is cleaned up as usual.

Review Comment:
   The worker can only clean up for retries it knows won't happen. If the retry 
never starts, because `dagrun_timeout` passes during `retry_delay` (the 
scheduler marks the waiting TI skipped) or someone marks the task failed while 
it's up for retry, nothing runs `cleanup` or `on_kill`, and the kept pod runs 
until it finishes, or forever if it's hung. Could the docs say that and point 
at `active_deadline_seconds` to bound it? It would have to cover the whole job 
across tries, since Kubernetes counts it from the pod's start and the retry 
reattaches to the same pod.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to