o-nikolas commented on code in PR #73816:
URL: https://github.com/apache/airflow/pull/73816#discussion_r4619280699
##########
airflow-core/docs/core-concepts/tasks.rst:
##########
@@ -190,6 +190,31 @@ If you want to control your task's state from within
custom Task/Operator code,
These can be useful if your code has extra knowledge about its environment and
wants to fail/skip faster - e.g., skipping when it knows there's no data
available, or fast-failing when it detects its API key is invalid (as that will
not be fixed by a retry).
+Failure causes
+--------------
+
+Airflow reports a failure category when the worker or executor can establish
the cause:
+``infra``, ``application``, ``timeout``, or ``manual``. Otherwise the category
remains unset.
+Failure listeners and scheduler logs can also receive a short producer-owned
reason,
+such as Kubernetes ``PreemptionByScheduler``.
Review Comment:
Since the provider adapters were dropped from this PR, no built-in executor
reports a cause yet, but these paragraphs read as if Airflow already does this
for Kubernetes (`PreemptionByScheduler`, `Evicted`, disruption conditions).
Could we scope the docs to what ships here (`application`/`timeout` from the
SDK, `manual` from the API, and causes a custom executor passes to `fail()`),
and add the Kubernetes guidance with the K8s adapter PR?
---
Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -2005,6 +2006,12 @@ def _terminal_failure(ti: RuntimeTaskInstance) ->
TaskState:
)
+def _get_task_failure_kind(error: BaseException | None) -> TaskFailureKind |
None:
+ if error is None or isinstance(error, AirflowTaskTerminated):
+ return None
+ return TaskFailureKind.TIMEOUT if isinstance(error, AirflowTaskTimeout)
else TaskFailureKind.APPLICATION
Review Comment:
Everything that isn't a timeout or `AirflowTaskTerminated` becomes
`application` here. That includes `AirflowRuntimeError`, which also wraps
`API_SERVER_ERROR` responses from the Execution API. The PR is careful not to
call worker loss `infra` without proof. Should we be just as careful here and
leave errors raised by the Airflow runtime itself (at least `API_SERVER_ERROR`)
unset, rather than counting them as `application`?
---
Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting
##########
airflow-core/docs/administration-and-deployment/listeners.rst:
##########
@@ -103,6 +103,19 @@ of a
:class:`~airflow.sdk.execution_time.task_runner.RuntimeTaskInstance` instan
:start-after: [START howto_listen_ti_skipped_task]
:end-before: [END howto_listen_ti_skipped_task]
+Failure cause
+"""""""""""""
+
+``on_task_instance_failed`` can receive a
:class:`~airflow.executors.base_executor.TaskFailureKind`:
Review Comment:
This points listener authors to
`airflow.executors.base_executor.TaskFailureKind`, but worker-side listeners
run under the Task SDK and shouldn't import from airflow-core, and the SDK copy
is in the private `airflow.sdk._shared.state`. Could we export it from a public
SDK path and reference that? Or, since it's a `str` enum, just say that
comparing against the string values is the supported way.
---
Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting
##########
shared/observability/src/airflow_shared/observability/metrics/metrics_template.yaml:
##########
@@ -40,7 +40,8 @@ metrics:
name_variables: ["job_name"]
- name: "operator_failures"
- description: "Operator ``{operator_name}`` failures."
+ description: "Operator ``{operator_name}`` failures. Tagged backends
include ``failure_kind``:
+ ``infra``, ``application``, ``timeout``, ``manual``, or ``unclassified``."
Review Comment:
`manual` never reaches these metrics: the API's mark-failed path only calls
the listener, not `handle_failure()`. I'd drop it from this list or say it's
listener-only.
---
Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting
##########
shared/listeners/src/airflow_shared/listeners/spec/taskinstance.py:
##########
@@ -52,8 +53,10 @@ def on_task_instance_failed(
previous_state: TaskInstanceState | None,
task_instance: RuntimeTaskInstance | TaskInstance,
error: None | str | BaseException,
-):
- """Execute when task state changes to FAIL. previous_state can be None."""
+ failure_kind: TaskFailureKind | None,
Review Comment:
How do you see listeners that support several Airflow versions (OpenLineage,
say) adopting this? pluggy rejects a hookimpl argument that isn't in the
hookspec, so declaring `failure_kind`/`reason` fails registration on current
Airflow, and declaring them with defaults means they're never passed. The repro
works around it with `inspect.signature` on the spec. A separate hook just for
the cause would avoid that, since older versions would simply never call it. If
we keep it on `on_task_instance_failed`, the listeners docs need a
`versionadded` and a note about this.
---
Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting
##########
airflow-core/tests/unit/executors/test_failure_cause.py:
##########
@@ -0,0 +1,156 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+from typing import TYPE_CHECKING
+from unittest.mock import Mock
+
+import pytest
+
+from airflow._shared.state import TaskFailureKind
+from airflow.callbacks.callback_requests import TaskCallbackRequest
+from airflow.executors.workloads.types import TaskInstanceUuid
+from airflow.jobs.job import Job
+from airflow.jobs.scheduler_job_runner import SchedulerJobRunner
+from airflow.listeners import hookimpl
+from airflow.models.dagrun import DagRunState
+from airflow.providers.standard.operators.empty import EmptyOperator
+from airflow.utils.state import TaskInstanceState
+
+from tests_common.test_utils.mock_executor import MockExecutor
+
+if TYPE_CHECKING:
+ from collections.abc import Callable
+
+ from sqlalchemy.orm import Session
+
+ from airflow.models.taskinstance import TaskInstance
+
+ from tests_common.pytest_plugin import DagMaker
+
+pytestmark = pytest.mark.db_test
+
+
[email protected](
+ ("name", "failure_info"),
+ [
+ ("infra", (TaskFailureKind.INFRA, "PreemptionByScheduler")),
+ ("application", (TaskFailureKind.APPLICATION, None)),
+ ("timeout", (TaskFailureKind.TIMEOUT, None)),
+ ("manual", (TaskFailureKind.MANUAL, None)),
+ *[
+ (f"reason_{reason}", (None, reason))
+ for reason in ("SIGTERM", "SIGKILL", "OOMKilled",
"WorkerLostError", "Evicted")
Review Comment:
This file tests `SchedulerJobRunner.process_executor_events`, so I'd move it
into `tests/unit/jobs/test_scheduler_job.py`. The matrix is also 40 DB tests,
and the five reason-only cases all take the same path, so one would do. Small
thing: the `model_fields` assertions at the end pass without this change too,
and so does `test_failure_cause_is_not_added_to_template_context` in the Task
SDK's `test_task_runner.py`.
---
Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting
--
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]