-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Requeue KubernetesExecutor tasks whose pod failed before execution started #69058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seanmuth
wants to merge
2
commits into
apache:main
Choose a base branch
from
seanmuth:feature/k8s-executor-pre-execution-requeue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+344
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -45,6 +45,7 @@ | |||||||||||||||||||||||||||||||||
| from airflow.providers.cncf.kubernetes.executors.kubernetes_executor_types import ( | ||||||||||||||||||||||||||||||||||
| ADOPTED, | ||||||||||||||||||||||||||||||||||
| POD_EXECUTOR_DONE_KEY, | ||||||||||||||||||||||||||||||||||
| FailureDetails, | ||||||||||||||||||||||||||||||||||
| KubernetesJob, | ||||||||||||||||||||||||||||||||||
| KubernetesResults, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
@@ -113,6 +114,19 @@ def __init__(self, *args, **kwargs): | |||||||||||||||||||||||||||||||||
| self.task_publish_max_retries = self.conf.getint( | ||||||||||||||||||||||||||||||||||
| "kubernetes_executor", "task_publish_max_retries", fallback=0 | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| self.pod_launch_failure_attempts: Counter[TaskInstanceKey] = Counter() | ||||||||||||||||||||||||||||||||||
| self.pod_launch_failure_max_retries = self.conf.getint( | ||||||||||||||||||||||||||||||||||
| "kubernetes_executor", "pod_launch_failure_retries", fallback=1 | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| excluded_reasons = self.conf.get( | ||||||||||||||||||||||||||||||||||
| "kubernetes_executor", "pod_launch_failure_excluded_container_reasons", fallback="Error" | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| self.pod_launch_failure_excluded_container_reasons = frozenset( | ||||||||||||||||||||||||||||||||||
| reason.strip() for reason in excluded_reasons.split(",") if reason.strip() | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| # Job specs are retained by key so a pod that fails before the task process starts can be | ||||||||||||||||||||||||||||||||||
| # requeued without the scheduler observing the failure or consuming a task-level retry. | ||||||||||||||||||||||||||||||||||
| self.last_known_jobs: dict[TaskInstanceKey, KubernetesJob] = {} | ||||||||||||||||||||||||||||||||||
| self.completed: set[KubernetesResults] = set() | ||||||||||||||||||||||||||||||||||
| self.create_pods_after: datetime | None = None | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -226,7 +240,9 @@ def execute_async( | |||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| pod_template_file = None | ||||||||||||||||||||||||||||||||||
| self.event_buffer[key] = (TaskInstanceState.QUEUED, self.scheduler_job_id) | ||||||||||||||||||||||||||||||||||
| self.task_queue.put(KubernetesJob(key, command, kube_executor_config, pod_template_file)) | ||||||||||||||||||||||||||||||||||
| job = KubernetesJob(key, command, kube_executor_config, pod_template_file) | ||||||||||||||||||||||||||||||||||
| self.last_known_jobs[key] = job | ||||||||||||||||||||||||||||||||||
| self.task_queue.put(job) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def queue_workload(self, workload: workloads.All, session: Session | None) -> None: | ||||||||||||||||||||||||||||||||||
| from airflow.executors import workloads | ||||||||||||||||||||||||||||||||||
|
|
@@ -455,13 +471,17 @@ def _change_state( | |||||||||||||||||||||||||||||||||
| if state == ADOPTED: | ||||||||||||||||||||||||||||||||||
| # When the task pod is adopted by another executor, | ||||||||||||||||||||||||||||||||||
| # then remove the task from the current executor running queue. | ||||||||||||||||||||||||||||||||||
| self.last_known_jobs.pop(key, None) | ||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||
| self.running.remove(key) | ||||||||||||||||||||||||||||||||||
| except KeyError: | ||||||||||||||||||||||||||||||||||
| self.log.debug("TI key not in running: %s", key) | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if state == TaskInstanceState.RUNNING: | ||||||||||||||||||||||||||||||||||
| # The task process started, so any later failure is an execution failure that should | ||||||||||||||||||||||||||||||||||
| # not be requeued by the pre-execution path below. | ||||||||||||||||||||||||||||||||||
| self.last_known_jobs.pop(key, None) | ||||||||||||||||||||||||||||||||||
| self.event_buffer[key] = state, None | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -478,6 +498,37 @@ def _change_state( | |||||||||||||||||||||||||||||||||
| self.kube_scheduler.patch_pod_executor_done(pod_name=pod_name, namespace=namespace) | ||||||||||||||||||||||||||||||||||
| self.log.info("Patched pod %s in namespace %s to mark it as done", key, namespace) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if state == TaskInstanceState.FAILED and self._is_pre_execution_failure( | ||||||||||||||||||||||||||||||||||
| state, | ||||||||||||||||||||||||||||||||||
| self._get_task_instance_state(key, session=session), | ||||||||||||||||||||||||||||||||||
| failure_details, | ||||||||||||||||||||||||||||||||||
| self.pod_launch_failure_excluded_container_reasons, | ||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+501
to
+506
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This saves some db queries when no retries are allowed. Not a big deal, I think? (It shouldn’t be too common to explicitly disallow retries.) |
||||||||||||||||||||||||||||||||||
| attempts = self.pod_launch_failure_attempts[key] | ||||||||||||||||||||||||||||||||||
| job = self.last_known_jobs.get(key) | ||||||||||||||||||||||||||||||||||
| can_requeue = ( | ||||||||||||||||||||||||||||||||||
| self.pod_launch_failure_max_retries == -1 or attempts < self.pod_launch_failure_max_retries | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| if can_requeue and job is not None: | ||||||||||||||||||||||||||||||||||
| self.pod_launch_failure_attempts[key] = attempts + 1 | ||||||||||||||||||||||||||||||||||
| self.log.warning( | ||||||||||||||||||||||||||||||||||
| "[Try %s of %s] Pod %s/%s for task %s failed before the task process started " | ||||||||||||||||||||||||||||||||||
| "(container_reason: %s). Requeuing without consuming a task retry.", | ||||||||||||||||||||||||||||||||||
| attempts + 1, | ||||||||||||||||||||||||||||||||||
| self.pod_launch_failure_max_retries, | ||||||||||||||||||||||||||||||||||
| namespace, | ||||||||||||||||||||||||||||||||||
| pod_name, | ||||||||||||||||||||||||||||||||||
| key, | ||||||||||||||||||||||||||||||||||
| failure_details.get("container_reason") if failure_details else None, | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| # Leave the key in self.running and do not write to event_buffer: the scheduler | ||||||||||||||||||||||||||||||||||
| # never observes this failure, so no task-level retry is consumed. | ||||||||||||||||||||||||||||||||||
| self.task_queue.put(job) | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| self.last_known_jobs.pop(key, None) | ||||||||||||||||||||||||||||||||||
| self.pod_launch_failure_attempts.pop(key, None) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||
| self.running.remove(key) | ||||||||||||||||||||||||||||||||||
| except KeyError: | ||||||||||||||||||||||||||||||||||
|
|
@@ -486,17 +537,51 @@ def _change_state( | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # If we don't have a TI state, look it up from the db. event_buffer expects the TI state | ||||||||||||||||||||||||||||||||||
| if state is None: | ||||||||||||||||||||||||||||||||||
| from airflow.models.taskinstance import TaskInstance | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| filter_for_tis = TaskInstance.filter_for_tis([key]) | ||||||||||||||||||||||||||||||||||
| if filter_for_tis is not None: | ||||||||||||||||||||||||||||||||||
| state = session.scalar(select(TaskInstance.state).where(filter_for_tis)) | ||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| state = None | ||||||||||||||||||||||||||||||||||
| state = TaskInstanceState(state) if state else None | ||||||||||||||||||||||||||||||||||
| state = self._get_task_instance_state(key, session=session) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| self.event_buffer[key] = state, termination_reason | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _get_task_instance_state(self, key: TaskInstanceKey, *, session: Session) -> TaskInstanceState | None: | ||||||||||||||||||||||||||||||||||
| """Look up the current task instance state from the metadata database.""" | ||||||||||||||||||||||||||||||||||
| from airflow.models.taskinstance import TaskInstance | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| filter_for_tis = TaskInstance.filter_for_tis([key]) | ||||||||||||||||||||||||||||||||||
| if filter_for_tis is None: | ||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||
| db_state = session.scalar(select(TaskInstance.state).where(filter_for_tis)) | ||||||||||||||||||||||||||||||||||
| return TaskInstanceState(db_state) if db_state else None | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||
| def _is_pre_execution_failure( | ||||||||||||||||||||||||||||||||||
| state: TaskInstanceState | str | None, | ||||||||||||||||||||||||||||||||||
| ti_state: TaskInstanceState | None, | ||||||||||||||||||||||||||||||||||
| failure_details: FailureDetails | None, | ||||||||||||||||||||||||||||||||||
| excluded_container_reasons: frozenset[str], | ||||||||||||||||||||||||||||||||||
| ) -> bool: | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
| Return ``True`` if a failed pod's task process never started running. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Both conditions are required: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - ``state`` is ``FAILED``: the pod actually terminated. | ||||||||||||||||||||||||||||||||||
| - ``ti_state`` is ``QUEUED``: the task instance never transitioned to ``running``, so no | ||||||||||||||||||||||||||||||||||
| task code ran. This is the authoritative signal and holds regardless of the specific | ||||||||||||||||||||||||||||||||||
| container failure reason (node drain, autoscaler scale-down, transient image pull | ||||||||||||||||||||||||||||||||||
| error, deferrable resume pod killed before ``execute_complete`` started, etc.). | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Pods whose ``container_reason`` is in ``excluded_container_reasons`` are not treated as | ||||||||||||||||||||||||||||||||||
| pre-execution failures. The default exclusion of ``Error`` covers a container that | ||||||||||||||||||||||||||||||||||
| started executing but whose worker process exited before writing ``running`` to the | ||||||||||||||||||||||||||||||||||
| database, which is most likely an Airflow-specific startup error. | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
| if state != TaskInstanceState.FAILED or ti_state != TaskInstanceState.QUEUED: | ||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||
| if failure_details: | ||||||||||||||||||||||||||||||||||
| container_reason = failure_details.get("container_reason") | ||||||||||||||||||||||||||||||||||
| if container_reason and container_reason in excluded_container_reasons: | ||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def _get_pod_namespace(self, ti: TaskInstance): | ||||||||||||||||||||||||||||||||||
| pod_override = (ti.executor_config or {}).get("pod_override") | ||||||||||||||||||||||||||||||||||
| namespace = None | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should probably also clean
pod_launch_failure_max_retries?I wonder if we should wrap the containers into an object so they are always handled together.