Skip to content

Fix pre-mature evaluation of tasks in mapped task group - #34337

Merged
ephraimbuddy merged 5 commits into
apache:mainfrom
astronomer:fix-mapped-tg
Nov 1, 2023
Merged

Fix pre-mature evaluation of tasks in mapped task group#34337
ephraimbuddy merged 5 commits into
apache:mainfrom
astronomer:fix-mapped-tg

Conversation

@ephraimbuddy

Copy link
Copy Markdown
Contributor

Getting the relevant upstream indexes of a task instance in a mapped task group should only be done when the task has expanded. If the task has not expanded yet, we should return None so that the task can wait for the upstreams before trying to run.
This issue is more noticeable when the trigger rule is ONE_FAILED because then, the task instance is marked as SKIPPED.
This commit fixes this issue.
closes: #34023

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 this case does not raise NotFullyPopulated

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

From this

"""Get how many tis the current task is supposed to be expanded into.
, it seems like it's the supposed number it could expand into but I think this case is because the group has expanded but the task hasn't?

@uranusjruranusjrSep 15, 2023

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 digged around a bit, this counts as expanded because there is indeed enough information to expand the operator, but the other depended task (t2) has not finished. This makes me think the fix is not right; we should investigate why t2 is not considered relevant instead. We need to somehow distinguish upstreams that are depended on as a DAG structure (e.g. with >>), or depended on for expansion (what _get_relevant_upstream_map_indexes is for); for >> dependencies we need to depend on the entire task regardless of map indexes instead.

@uranusjr

uranusjr commented Sep 15, 2023

Copy link
Copy Markdown
Member

I think the fix to this would be we should check whether an upstream is a dependency used for task expansion. If it is, we go through the normal “relevant map index” check; if not, we need to depend on its entirety i.e. _get_relevant_upstream_map_indexes should return None.

This change seems to work for me:

def_iter_expansion_dependencies() ->Iterator[str]:
fromairflow.models.mappedoperatorimportMappedOperatorifisinstance(ti.task, MappedOperator):
foropinti.task.iter_mapped_dependencies():
yieldop.task_idfortginti.task.task_group.iter_mapped_task_groups():
foropintg.iter_mapped_dependencies():
yieldop.task_iddef_get_relevant_upstream_map_indexes(upstream_id: str) ->int|range|None:
ifupstream_idnotinset(_iter_expansion_dependencies()):
returnNone# Same as before...

This is obviously not optimised and I want to add a better abstraction for this. But @ephraimbuddy maybe you can help check if this can fix the actual DAG in the reported issue.

@ephraimbuddy

ephraimbuddy commented Sep 15, 2023

Copy link
Copy Markdown
ContributorAuthor

I think the fix to this would be we should check whether an upstream is a dependency used for task expansion. If it is, we go through the normal “relevant map index” check; if not, we need to depend on its entirety i.e. _get_relevant_upstream_map_indexes should return None.

This change seems to work for me:

def_iter_expansion_dependencies() ->Iterator[str]:
fromairflow.models.mappedoperatorimportMappedOperatorifisinstance(ti.task, MappedOperator):
foropinti.task.iter_mapped_dependencies():
yieldop.task_idfortginti.task.task_group.iter_mapped_task_groups():
foropintg.iter_mapped_dependencies():
yieldop.task_iddef_get_relevant_upstream_map_indexes(upstream_id: str) ->int|range|None:
ifupstream_idnotinset(_iter_expansion_dependencies()):
returnNone# Same as before...

This is obviously not optimised and I want to add a better abstraction for this. But @ephraimbuddy maybe you can help check if this can fix the actual DAG in the reported issue.

It works. Feel free to optimize it on this PR. Also I modified the code to make mypy happy

Edit:
@uranusjr, this works however, all the upstream tasks have to be completed before the task can run. This might not go well with the fast fail feature where if we have multiple upstream then the first fail will trigger a failure on its downstream even if some upstream tasks are still running. If this behaviour is ok for the mapped task group, I can go ahead and address the failing test.

@uranusjr

Copy link
Copy Markdown
Member

all the upstream tasks have to be completed before the task can run. This might not go well with the fast fail feature where if we have multiple upstream then the first fail will trigger a failure on its downstream even if some upstream tasks are still running.

Does this mean with this PR the downstream will run when the first failure happens, or when all tasks (success or not) finish?

@ephraimbuddy

ephraimbuddy commented Oct 16, 2023

Copy link
Copy Markdown
ContributorAuthor

when all tasks (success or not) finish?

Yes. When all tasks are completed before the downstream

@ephraimbuddy

Copy link
Copy Markdown
ContributorAuthor

@uranusjr, Do you think I should fix the tests and get this ready?

@ephraimbuddy
ephraimbuddyforce-pushed the fix-mapped-tg branch 3 times, most recently from 4dce93c to a9aa3bbCompareOctober 19, 2023 08:49
@eladkaleladkal added this to the Airflow 2.7.3 milestone Oct 27, 2023
@eladkaleladkal added the type:bug-fix Changelog: Bug Fixes label Oct 27, 2023
Getting the relevant upstream indexes of a task instance in a mapped task group
should only be done when the task has expanded. If the task has not expanded yet,
we should return None so that the task can wait for the upstreams before trying
to run.
This issue is more noticeable when the trigger rule is ONE_FAILED because then,
the task instance is marked as SKIPPED.
This commit fixes this issue.
closes: apache#34023
@ephraimbuddy
ephraimbuddy merged commit 69938fd into apache:mainNov 1, 2023
@ephraimbuddy
ephraimbuddy deleted the fix-mapped-tg branch November 1, 2023 20:37
ephraimbuddy added a commit that referenced this pull request Nov 1, 2023
* Fix pre-mature evaluation of tasks in mapped task group
Getting the relevant upstream indexes of a task instance in a mapped task group
should only be done when the task has expanded. If the task has not expanded yet,
we should return None so that the task can wait for the upstreams before trying
to run.
This issue is more noticeable when the trigger rule is ONE_FAILED because then,
the task instance is marked as SKIPPED.
This commit fixes this issue.
closes: #34023
* fixup! Fix pre-mature evaluation of tasks in mapped task group
* fixup! fixup! Fix pre-mature evaluation of tasks in mapped task group
* fixup! fixup! fixup! Fix pre-mature evaluation of tasks in mapped task group
* Fix tests
(cherry picked from commit 69938fd)
romsharon98 pushed a commit to romsharon98/airflow that referenced this pull request Nov 10, 2023
* Fix pre-mature evaluation of tasks in mapped task group
Getting the relevant upstream indexes of a task instance in a mapped task group
should only be done when the task has expanded. If the task has not expanded yet,
we should return None so that the task can wait for the upstreams before trying
to run.
This issue is more noticeable when the trigger rule is ONE_FAILED because then,
the task instance is marked as SKIPPED.
This commit fixes this issue.
closes: apache#34023
* fixup! Fix pre-mature evaluation of tasks in mapped task group
* fixup! fixup! Fix pre-mature evaluation of tasks in mapped task group
* fixup! fixup! fixup! Fix pre-mature evaluation of tasks in mapped task group
* Fix tests
ephraimbuddy added a commit to astronomer/airflow that referenced this pull request Nov 15, 2023
ephraimbuddy added a commit that referenced this pull request Nov 15, 2023
@ephraimbuddyephraimbuddy added the changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) label Nov 20, 2023
@ephraimbuddyephraimbuddy removed the changelog:skip Changes that should be skipped from the changelog (CI, tests, etc..) label Nov 20, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:bug-fixChangelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trigger Rule ONE_FAILED does not work in task group with mapped tasks

3 participants

@ephraimbuddy@uranusjr@eladkal