Repository navigation
Restore clearing ExternalTaskMarker targets in other Dags - #75874
Conversation
|
So this is just the functionality but no UI or config settings to enable |
shahar1
left a comment
There was a problem hiding this comment.
The worker-recorded design holds up and removes the provider coupling from core, but three things look like bugs: a default marker now fails in runs with no logical date, the API server rebuilds objects from worker-written XCom, and a target Dag's later loop passes get retried instead of archived.
uranusjr's question on #65314 about a default for getattr(task, "recursion_depth") doesn't carry over here: core no longer reads that attribute from a serialized operator, and ExternalClearTarget.recursion_depth defaults to 10. The strict data["recursion_depth"] read is covered by the inline comment on _recorded_targets.
bbovenzi's question above about a UI or config setting is still unanswered. It connects to the inline comment on _editable_task_instances.
The only red CI job is React UI tests, in SearchDagRuns.test.tsx, which this PR doesn't touch, so it needs a rerun.
Smaller observations
These two aren't anchored inline because the code isn't in the diff:
- minor: the clear preview can't tell Dags apart.
taskInstanceKeyinairflow-core/src/airflow/ui/src/components/ActionAccordion/columns.tsx:32is built from run ID, task ID, map index and region, with nodag_id, and the preview has no Dag column. Those rows can now span Dags. Two Dags on the same schedule share ascheduled__...run ID, so a same-named task in each collides, and unticking one excludes both. - minor: release notes. Clears now cross Dags by default and markers use a worker slot on every run.
AGENTS.mdsays: "Inairflow-core/use onlysignificantnewsfragments, and only for a change users must read before upgrading: a behavior change, a removal or a new default, or migration steps." This looks like it qualifies. Dag-run clear also doesn't cascade (routes/public/dag_run.pyis untouched), and Known issues only listsairflow tasks clear.
I read the code and ran two small local checks on main, using code this PR doesn't change (noted on those comments). I didn't run the PR's tests.
This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The findings
below are observations, not blockers; an Apache Airflow
maintainer — a real person — will take the next look at the
PR. If you think a finding is mis-applied, please reply on
the PR and a maintainer will weigh in.More on how Apache Airflow handles maintainer review:
Contributing guide.
2f52a09 to
c3dccf1
Compare
shahar1
left a comment
There was a problem hiding this comment.
Thanks for the follow-up. Everything from my first pass is addressed on c3dccf1e2, and each fix comes with a test: the default marker now takes the run's logical date and records nothing when there is none, the API server reads the record with deserialize_value_primitives and validates it with a pydantic model, dependent runs clear through SerializedDAG.clear so later loop passes are archived, the authorization check now runs before traversal, the plan is rebuilt under one lock batch, and the clear dialogs have the "Clear tasks in dependent Dags" checkbox with a Dag column in the preview. The newsfragment covers the new default and the worker slot. CI is green on the head.
bbovenzi's question above is answered by the checkbox, so a short reply there would close it. The PR body still has no mention of the UI change, and "Known issues" could note that clearing a whole Dag run does not cascade, as the newsfragment does.
Two nits inline, neither blocks.
I read the code and the new tests at the head; I didn't run them locally and relied on CI.
This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.More on how Apache Airflow handles maintainer review:
Contributing guide.
@bbovenzi Added a "Clear tasks in dependent Dags" checkbox to both clear dialogs. It's on by default and only active with Downstream, matching the API, where an unset Wdyt? or do you want to take a stab at it when you are available. |
uranusjr
left a comment
There was a problem hiding this comment.
Most significantly, I wonder if there’s a way we could save the worker if there’s nothing to calculate at runtime at all. I feel this could be possible.
Also, this still leaves some gaps to how Airflow 2 did things. Do we intend to fix them eventually? If so, we should open some follow-up issues for tracking.
cff1f18 to
397243f
Compare
@uranusjr Done, an untemplated marker never runs now. Its target is stored with the Dag version, and only templated markers take a worker slot.
Yes. Airflow 2 also cascaded when clearing a whole Dag run (UI and REST) and from |
Clearing a task with its downstream tasks clears the task an ExternalTaskMarker points at, and that task's downstream tasks, in the other Dag, as it did in Airflow 2. The marker now runs on a worker, where its templates render, and records the resolved target. The API server follows those records when clearing, so core needs no knowledge of the provider's operator.
A default marker no longer fails in a run without a logical date: its logical_date defaults to the run's own date and such a run records nothing. The API server reads the recorded target as plain JSON and validates it. Target Dags clear through SerializedDAG.clear, so loop gates archive their later passes and prevent_running_task applies. Targets in Dags the caller cannot clear are not followed, source and dependent runs are locked in one batch, and the cascade is worked out again under that lock. The clear dialogs get a "Clear tasks in dependent Dags" option, and the preview shows a Dag ID column when it spans Dags.
Look up each target's run once, not once per task instance row: a mapped target or one with archived attempts returned a row each, and every row repeated the Dag lookup and the dry-run clear for the same run. Clear dependent runs from the plan made under the route's run locks instead of locking them again and repeating the dry run.
…rgets An ExternalTaskMarker whose target needs no rendering stays an empty operator again, and its target is stored with the Dag version, so it never takes a worker slot. Only a templated marker runs, and it now reports its target with its success instead of pushing an XCom. The API server validates the report and writes the record under a key the Execution and public XCom APIs both reject. When the live attempt of a task has not succeeded yet, clearing uses the target an earlier attempt of the same execution reported, so clearing twice before the marker reruns still reaches the other Dag.
…itly A mapped operator counted as empty only when its class attribute inherits_from_empty_operator was exactly True. ExternalTaskMarker defines it as a property, so a mapped marker ran on a worker only because the class attribute is a property object. Check SupportsExternalClearTarget too, since a mapped task's clear target is known only once it runs.
The Go SDK now decodes supervisor date-times through its own Time type, so the external clear target's logical_date uses it too.
7f342c2 to
260ee6f
Compare
|
@kaxil should the |
In Airflow 2, `airflow tasks clear` also cleared the tasks the selected ExternalTaskMarkers point at, and their downstream tasks, in the other Dags. apache#75874 brought that back for task instance clears through the REST API and the UI only; the CLI command cleared the selected Dags and nothing else. The command now follows the same targets with find_dependent_clears, so a clear from the CLI and one from the UI reach the same tasks. The confirmation prompt lists the tasks in the other Dags too. The new `--exclude-dependent-dags` flag, which takes the place of the old `--exclude-subdags`, keeps the clear inside the selected Dags. related: apache#76779 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Fixes #61451. An alternative to #65314.
In Airflow 2, clearing a task with its downstream tasks also cleared the task an
ExternalTaskMarkerpoints at, and everything downstream of it in the other Dag. Airflow 3 dropped it, because the Airflow 2 code rendered the marker's templates inside the webserver and the Airflow 3 API server can't run user code.With this change a marker whose target needs no rendering stores it with the Dag version and still never runs. A templated marker works out its target on a worker and reports it with its success. The API server follows either one when it clears.
Design rationale
Core does not look for
ExternalTaskMarkerby name. #65314 does, which ties core to a provider class name and to provider attributes on a serialized operator. That approach also misses templated targets: the marker is a trivialEmptyOperator, so the scheduler marks it successful without running it and no rendered-fields row is ever written. With a customlogical_datetemplate (an hourly Dag marking its daily child's run, say) or a templatedexternal_dag_id, the clear lands on the wrong run or on none.How it works:
ExternalClearTarget(dag_id, task_id, logical_date, recursion_depth), where aNonedate means the run's own logical date. An operator opts in by name, throughruntime_checkableprotocols, the way OpenLineage findsget_openlineage_facets_on_complete;BaseOperatoris unchanged. A target known at parse time goes in anexternal_clear_targetproperty, and the Dag processor serializes it with the task. A target that depends on the run comes fromget_external_clear_target(context), which the task runner calls after a successful run and sends with the success report (external_clear_targetonTISuccessStatePayload, new in the 2027-02-28 Execution API version). ReturningNonereports nothing for that run. If the method raises, the task fails, so a bad template surfaces where it was written._airflow_external_clear_target, a key the Execution API and the public XCom API both reject, like the loop decision keys.find_dependent_clearsreads a task's target from its Dag version first, then from its reports. An attempt that has not succeeded yet uses the last earlier attempt's report, so clearing twice before the marker reruns still reaches the other Dag. It follows the targets level by level, capped byrecursion_depth, and takes the downstream tasks from each target run's own Dag version. It reads records as plain JSON and validates them, so the API server never rebuilds an object from a value a task wrote; a record that does not validate is skipped with a warning. It does not follow targets into Dags the caller cannot clear, or into a run with no serialized Dag. Targets in the same run merge into one clear, and executions the source selection already clears are excluded, so nothing is cleared twice.SerializedDAG.clear, so a loop gate in a target Dag archives its later passes exactly as a clear of that Dag would, andprevent_running_taskapplies there too. The runs a cascade reaches are locked in the same ordered batch as the source runs, and the cascade is worked out again under that lock before anything is cleared.ClearTaskInstancesBody.include_dependent_dags: bool | None. Left unset, it followsinclude_downstream.falsekeeps the clear in one Dag. Each target Dag uses its ownrerun_with_latest_version.ExternalTaskMarkerdefines both. When its Dag and task IDs hold no Jinja (checked with the Dag's own Jinja environment, so custom delimiters count) and itslogical_dateis unset or a literal, the target is stored with the Dag version and the marker stays an empty operator. Otherwise it runs on a worker. Itslogical_datenow defaults toNone, meaning the logical date of the marker's own run. A run without a logical date (asset-triggered, or triggered with a nulllogical_date) reports nothing instead of failing on an undefined template variable. A template that renders to a datetime, underrender_template_as_native_obj, is used as is. On Airflow 2 the old template default stays, because its webserver still renders it.With the checkbox ticked, the clear dialog previews the child Dag's tasks, with a Dag ID column. Unticked, it lists only the parent's:
The runner calls the method;
execute()does not raise or push. I also tried theTriggerDagRunOperatorstyle, where the marker'sexecute()raises a signal for the task runner to catch. There, a subclass whoseexecute()skipssuper()or wraps it intry/except Exceptiondrops the cascade, and the marker still shows success. Pushing an XCom fromexecute()has the samesuper()problem, and any task could push the same key. Having the runner call the method afterexecute()returns, and the server write the record from the validated success report, avoids both.Nothing on
BaseOperator. Only one operator needs this today, so I didn't add a base-class method or a mixin for it.Mapped markers.
OperatorPartial._expanddecidedis_emptywithissubclass(..., EmptyOperator). It ignored a subclass that setsinherits_from_empty_operator = False, so mapped markers never ran. It now reads the attribute, the same way an unmapped operator does, which also removes the Task SDK's import of the standard provider'sEmptyOperator.Gotchas
significantnewsfragment says so.recursion_depthamong the markers in the first level (Airflow 2 took it from whichever marker it hit first).Known issues
airflow tasks clearfrom the CLI (airflow tasks cleardoes not clear ExternalTaskMarker targets #76779), do not cascade yet.