-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Restoring include_downstream_dags logic to clear downstream Dags
#65314
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
base: main
Are you sure you want to change the base?
Changes from all commits
550eb73
9805cce
86a75fe
70c4154
1950d94
4eaeb1b
bd41ac1
5ea452a
29ae371
1456d06
c2df99c
92946b3
6464c5d
3e87386
bac0797
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,11 +22,13 @@ | |
|
|
||
| import structlog | ||
| from fastapi import Depends, HTTPException, Query, status | ||
| from pendulum.parsing.exceptions import ParserError | ||
| from sqlalchemy import or_, select | ||
| from sqlalchemy.orm import joinedload | ||
| from sqlalchemy.sql.selectable import Select | ||
|
|
||
| from airflow.api_fastapi.auth.managers.models.resource_details import DagAccessEntity | ||
| from airflow.api_fastapi.app import get_auth_manager | ||
| from airflow.api_fastapi.auth.managers.models.resource_details import DagAccessEntity, DagDetails | ||
| from airflow.api_fastapi.common.cursors import ( | ||
| apply_cursor_filter, | ||
| encode_cursor, | ||
|
|
@@ -111,10 +113,11 @@ | |
| _reload_tis_with_rendered_fields, | ||
| ) | ||
| from airflow.api_fastapi.logging.decorators import action_logging | ||
| from airflow.exceptions import AirflowClearRunningTaskException, TaskNotFound | ||
| from airflow.models import Base, DagRun | ||
| from airflow.exceptions import AirflowClearRunningTaskException, DagNotFound, TaskNotFound | ||
| from airflow.models import Base, DagModel, DagRun | ||
| from airflow.models.taskinstance import TaskInstance as TI, clear_task_instances | ||
| from airflow.models.taskinstancehistory import TaskInstanceHistory as TIH | ||
| from airflow.serialization.definitions.dag import MaxRecursionDepthError | ||
| from airflow.ti_deps.dep_context import DepContext | ||
| from airflow.ti_deps.dependencies_deps import SCHEDULER_QUEUED_DEPS | ||
| from airflow.utils.db import get_query_count | ||
|
|
@@ -839,7 +842,9 @@ def get_mapped_task_instance_try_details( | |
|
|
||
| @task_instances_router.post( | ||
| "/clearTaskInstances", | ||
| responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND, status.HTTP_409_CONFLICT]), | ||
| responses=create_openapi_http_exception_doc( | ||
| [status.HTTP_400_BAD_REQUEST, status.HTTP_404_NOT_FOUND, status.HTTP_409_CONFLICT] | ||
| ), | ||
| dependencies=[ | ||
| Depends(action_logging()), | ||
| Depends(requires_access_dag(method="PUT", access_entity=DagAccessEntity.TASK_INSTANCE)), | ||
|
|
@@ -933,30 +938,69 @@ def _collect_relatives(run_id: str, direction: Literal["upstream", "downstream"] | |
| *((t, m) for t, m in mapped_tasks_tuples if t not in normal_task_ids), | ||
| ] | ||
|
|
||
| # Follow ExternalTaskMarker connections when explicitly requested via include_downstream_dags, or | ||
| # automatically whenever downstream clearing is selected (restoring Airflow 2 behavior) | ||
| include_dependent_dags = body.include_downstream_dags or downstream | ||
|
jroachgolf84 marked this conversation as resolved.
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. Coming back to the flag overlap I asked about in #65314 (comment), now that I've read the code this restores.
The UI side is what bothers me more.
include_downstream_dags: bool | None = None
...
downstream if body.include_downstream_dags is None else body.include_downstream_dags |
||
|
|
||
| task_instances: Sequence[TI] | ||
| if dag_run_id is not None and not (past or future): | ||
| # Use run_id-based clearing when we have a specific dag_run_id and not using past/future | ||
| task_instances = dag.clear( | ||
| dry_run=True, | ||
| task_ids=task_markers_to_clear, | ||
| run_id=dag_run_id, | ||
| session=session, | ||
| run_on_latest_version=resolved_run_on_latest, | ||
| only_failed=body.only_failed, | ||
| only_running=body.only_running, | ||
| ) | ||
| else: | ||
| # Use date-based clearing when no dag_run_id or when past/future is specified | ||
| task_instances = dag.clear( | ||
| dry_run=True, | ||
| task_ids=task_markers_to_clear, | ||
| start_date=body.start_date, | ||
| end_date=body.end_date, | ||
| session=session, | ||
| run_on_latest_version=resolved_run_on_latest, | ||
| only_failed=body.only_failed, | ||
| only_running=body.only_running, | ||
| ) | ||
| try: | ||
| if dag_run_id is not None and not (past or future): | ||
| # Use run_id-based clearing when we have a specific dag_run_id and not using past/future | ||
| task_instances = dag.clear( | ||
| dry_run=True, | ||
| task_ids=task_markers_to_clear, | ||
| run_id=dag_run_id, | ||
| session=session, | ||
| run_on_latest_version=resolved_run_on_latest, | ||
| only_failed=body.only_failed, | ||
| only_running=body.only_running, | ||
| include_dependent_dags=include_dependent_dags, | ||
| dag_bag=dag_bag, | ||
| ) | ||
| else: | ||
| # Use date-based clearing when no dag_run_id or when past/future is specified | ||
| task_instances = dag.clear( | ||
| dry_run=True, | ||
| task_ids=task_markers_to_clear, | ||
| start_date=body.start_date, | ||
| end_date=body.end_date, | ||
| session=session, | ||
| run_on_latest_version=resolved_run_on_latest, | ||
| only_failed=body.only_failed, | ||
| only_running=body.only_running, | ||
| include_dependent_dags=include_dependent_dags, | ||
| dag_bag=dag_bag, | ||
| ) | ||
|
|
||
| except MaxRecursionDepthError as e: | ||
| raise HTTPException(status.HTTP_400_BAD_REQUEST, str(e)) from e | ||
| except ParserError as e: | ||
|
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. Still Two shapes reach it on the pinned pendulum 3.2.0. Catching One thing to flag: |
||
| raise HTTPException(status.HTTP_400_BAD_REQUEST, f"Invalid logical_date: {e}") from e | ||
| except DagNotFound as e: | ||
| raise HTTPException(status.HTTP_404_NOT_FOUND, str(e)) from e | ||
|
|
||
| if include_dependent_dags: | ||
| # Ensure proper access to downstream Dags/tasks with dag.clear and include_dependent_dags | ||
| auth_manager = get_auth_manager() | ||
| all_dag_ids = {ti.dag_id for ti in task_instances} # Retrieve all Dag ID's from task instances | ||
|
|
||
| # Used to find a team name from a Dag ID | ||
| dag_id_to_team = DagModel.get_dag_id_to_team_name_mapping(list(all_dag_ids), session=session) | ||
|
|
||
| # set of Dag ID's that can be cleared | ||
| editable_dag_ids = { | ||
| dependent_dag_id | ||
| for dependent_dag_id in all_dag_ids | ||
| if auth_manager.is_authorized_dag( | ||
| method="PUT", | ||
| access_entity=DagAccessEntity.TASK_INSTANCE, | ||
| details=DagDetails(id=dependent_dag_id, team_name=dag_id_to_team.get(dependent_dag_id)), | ||
| user=user, | ||
| ) | ||
| } | ||
|
jroachgolf84 marked this conversation as resolved.
|
||
|
|
||
| # list of all TI's that can be cleared (TI's within the Dags from above) | ||
| task_instances = [ti for ti in task_instances if ti.dag_id in editable_dag_ids] | ||
|
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. Now that this list can span Dags, the single Since the clear dialog always sends the field, the Grouping the TIs by |
||
|
|
||
| if not dry_run: | ||
| try: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.