Skip to content

When clearing task instances try to get associated DAGs from database - #29065

Merged
potiuk merged 4 commits into
apache:mainfrom
sean-rose:fix-recursive-clear
Apr 14, 2023
Merged

When clearing task instances try to get associated DAGs from database#29065
potiuk merged 4 commits into
apache:mainfrom
sean-rose:fix-recursive-clear

Conversation

@sean-rose

Copy link
Copy Markdown
Contributor

This fixes problems when recursively clearing task instances across multiple DAGs:

  • Task instances in downstream DAGs weren't having their max_tries property incremented, which could cause downstream external task sensors in reschedule mode to instantly time out.
  • Task instances in downstream DAGs could have some of their properties overridden by an unrelated task in the upstream DAG if they had the same task ID.

closes: #29049

This fixes problems when recursively clearing task instances across multiple DAGs:
  * Task instances in downstream DAGs weren't having their `max_tries` property incremented, which could cause downstream external task sensors in reschedule mode to instantly time out (issue apache#29049).
  * Task instances in downstream DAGs could have some of their properties overridden by an unrelated task in the upstream DAG if they had the same task ID.
@boring-cyborg

boring-cyborg Bot commented Jan 20, 2023

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@potiuk potiuk added this to the Airflow 2.5.2 milestone Jan 21, 2023
@potiuk

potiuk commented Jan 21, 2023

Copy link
Copy Markdown
Member

looks like an important one to merge if the diagnosis is correct. I have not yet looked at the details and do not get that deep knowledge about this part but might be worth seriously looking at it @ashb @ephraimbuddy .

@ephraimbuddy ephraimbuddy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread tests/models/test_cleartasks.py Outdated
Comment thread tests/models/test_cleartasks.py
task_id_by_key: dict[str, dict[str, dict[int, dict[int, set[str]]]]] = defaultdict(
lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(set)))
)
dag_bag = DagBag(read_dags_from_db=True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why dag is not a required argument to begin with. It really should be.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ti_dag = dag if dag and dag.dag_id == ti.dag_id else dag_bag.get_dag(ti.dag_id, session=session)

Looks like dag is optional because if there is no DAG we can just get the DAG from the TaskInstance

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change yes, but without this PR dag=None would have a very weird behaviour and not particularly useful. This makes me think the signature may be considered a bug in the first place and fixed without introducing DagBag here.

@dstandish dstandish May 1, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blast from a past, but I just had reason to encounter this headscratcher of a function also. I think it can be that we can deal with many different dags in this function, because e.g. there is no restriction that the TIs passed in must be from the same dag. And I think this gets used in real life when you have external task sensors and you clear the upstream task. Too stinkin complicated.

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one!

@potiuk potiuk modified the milestones: Airflow 2.6.1, Airflow 2.6.0 Apr 14, 2023
@potiuk
potiuk merged commit 0d2e6dc into apache:main Apr 14, 2023
@boring-cyborg

boring-cyborg Bot commented Apr 14, 2023

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

ephraimbuddy pushed a commit that referenced this pull request Apr 14, 2023
…#29065)

* When clearing task instances try to get associated DAGs from database.

This fixes problems when recursively clearing task instances across multiple DAGs:
  * Task instances in downstream DAGs weren't having their `max_tries` property incremented, which could cause downstream external task sensors in reschedule mode to instantly time out (issue #29049).
  * Task instances in downstream DAGs could have some of their properties overridden by an unrelated task in the upstream DAG if they had the same task ID.

* Use session fixture for new `test_clear_task_instances_without_dag_param` test.

* Use session fixture for new `test_clear_task_instances_in_multiple_dags` test.

---------

Co-authored-by: eladkal <45845474+eladkal@users.noreply.github.com>
(cherry picked from commit 0d2e6dc)
wookiist pushed a commit to wookiist/airflow that referenced this pull request Apr 19, 2023
…apache#29065)

* When clearing task instances try to get associated DAGs from database.

This fixes problems when recursively clearing task instances across multiple DAGs:
  * Task instances in downstream DAGs weren't having their `max_tries` property incremented, which could cause downstream external task sensors in reschedule mode to instantly time out (issue apache#29049).
  * Task instances in downstream DAGs could have some of their properties overridden by an unrelated task in the upstream DAG if they had the same task ID.

* Use session fixture for new `test_clear_task_instances_without_dag_param` test.

* Use session fixture for new `test_clear_task_instances_in_multiple_dags` test.

---------

Co-authored-by: eladkal <45845474+eladkal@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recursively cleared external task sensors using reschedule mode instantly time out if previous run is older than sensor timeout

8 participants