-
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
Open
jroachgolf84
wants to merge
11
commits into
apache:main
Choose a base branch
from
jroachgolf84:issue-61451
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.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
550eb73
issue-61451: Restoring include_downstream_dags logic
jroachgolf84 9805cce
issue-61451: Error handling, adding tests
jroachgolf84 86a75fe
issue-61451: Generating datamodels
jroachgolf84 70c4154
issue-61451: Fixing CI failures
jroachgolf84 1950d94
issue-61451: Fixing CI failures
jroachgolf84 4eaeb1b
issue-63451: Implementing feedback
jroachgolf84 bd41ac1
issue-61451: Implementing feedback for clearing TI's
jroachgolf84 5ea452a
issue-61451: Passing through DagBag on clear
jroachgolf84 29ae371
Merge branch 'main' into issue-61451
jroachgolf84 1456d06
Apply suggestions from code review
jroachgolf84 c2df99c
Apply suggestions from code review
jroachgolf84 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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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, | ||||||
|
|
@@ -108,10 +110,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 | ||||||
|
|
@@ -831,7 +834,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)), | ||||||
|
|
@@ -925,30 +930,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.
|
||||||
|
|
||||||
| 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: | ||||||
| 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 | ||||||
|
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
|
||||||
| 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=other_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] | ||||||
|
|
||||||
| if not dry_run: | ||||||
| try: | ||||||
|
|
||||||
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.
Uh oh!
There was an error while loading. Please reload this page.