You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "dstandish (via GitHub)" <gi...@apache.org> on 2023/08/30 00:31:13 UTC
[GitHub] [airflow] dstandish opened a new pull request, #33903: Ensure that tasks wait for running indirect setup
dstandish opened a new pull request, #33903:
URL: https://github.com/apache/airflow/pull/33903
This is yet another approach to solving this one. But I feel it's the best one thus far because it does not make any changes to existing trigger rule logic, apart from pulling out some internal functions into methods.
The indirect setup constraint logic is processed prior to trigger rules and is completely separate.
This _should_ play nicely with all mapping scenarios but more testing is required to verify.
There are a couple choices to make here.
One is in the "setup constraint" logic, should we apply it only to indirect setups or to all relevant setups? I think the only time it makes a difference is when it there is a mixture of skipped and failed upstream setups and how we determine state of the object task.
Currently I only look at indirect setups.
Another thing is, for calculation of state, do we just go based on the state of the first non-succcess? Or should we wait until everything is done and calculate at that point? Currently it just takes the first non-success and fails (or skips) fast. And I think this is consistent with airflow's many-to-one behavior more generally.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on PR #33903:
URL: https://github.com/apache/airflow/pull/33903#issuecomment-1702279365
have to update a couple tests since now we can't test the two constraints independently. i'll do tomorrow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312652181
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
+
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ # set tasks_dict to contain relevant setups
+ # _get_relevant_upstream_map_indexes will use this to look up the task_id
+ tasks_dict = relevant_setups
+ for status, changed in _evaluate_setup_constraint(relevant_setups=relevant_setups):
+ yield status
+ if not status.passed and changed:
+ # no need to evaluate trigger rule; we've already marked as skipped or failed
+ return
+
+ task = ti.task
+ trigger_rule = task.trigger_rule
Review Comment:
yeah true
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] hussein-awala commented on pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on PR #33903:
URL: https://github.com/apache/airflow/pull/33903#issuecomment-1704326323
I understand that `+602 −236 ` is scary for a patch version, but most of the changed code is documentation and tests, and for the changes in `trigger_rule_dep`, @dstandish has encapsulated the processing logic in a method (added an indent) but he changed nothing in the processing behavior for dags that do not use the new feature.
I think the risk level is very low here, especially after all the tests I ran with @dstandish.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312646683
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
No I mean, why can’t we just put all the tasks in that dict? Maybe `task_dict` should just be `ti.task.dag.task_dict`?
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
No I mean, why can’t we just put all the tasks in that dict? Maybe `task_dict` should just be `ti.task.dag.task_dict`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] hussein-awala commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310730884
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,12 +102,124 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if status.passed is False and changed:
Review Comment:
I don't think we have None values in passed:
```suggestion
if not status.passed and changed:
```
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
Review Comment:
Do we really need an expensive group by here?
```suggestion
upstream = session.execute(
select(func.count("*"))
.where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
.where(
or_(
*self._iter_upstream_conditions(
ti=ti, relevant_tasks=indirect_setups, session=session
)
)
)
).scalar_one()
```
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+
+ if new_state is not None:
+ if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
+ )
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
Review Comment:
Oups, I was breaking my little feature in my PR! :+1:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311209892
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,85 +102,247 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if not status.passed and changed:
+ # no need to evaluate trigger rule; we've already marked as skipped or failed
+ return
yield from self._evaluate_trigger_rule(ti=ti, dep_context=dep_context, session=session)
- def _evaluate_trigger_rule(
+ @staticmethod
+ @functools.lru_cache
+ def _get_relevant_upstream_map_indexes(*, ti: TaskInstance, upstream_task, session) -> int | range | None:
Review Comment:
I wonder if we can actually cache this globally; a trigger rule is accessed repeatedly in the schedule in every loop iteration, and the result of this function may change depending on upstream states. We could add the cache decorator because the function was function-local previously, but now it’s on the class I’m not so sure.
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,85 +102,247 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if not status.passed and changed:
+ # no need to evaluate trigger rule; we've already marked as skipped or failed
+ return
yield from self._evaluate_trigger_rule(ti=ti, dep_context=dep_context, session=session)
- def _evaluate_trigger_rule(
+ @staticmethod
+ @functools.lru_cache
+ def _get_relevant_upstream_map_indexes(*, ti: TaskInstance, upstream_task, session) -> int | range | None:
Review Comment:
I wonder if we can actually cache this globally; a trigger rule is accessed repeatedly in the schedule in every loop iteration, and the result of this function may change depending on upstream states. We could add the cache decorator because the function was function-local previously, but now it’s on the class I’m not so sure.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] hussein-awala commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310808269
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
Review Comment:
The main problem here is that the skipped state is used as one of the successful states (along with success), most of the users do not consider it as failure, it simply means that there is no need to run the task, nor to send a notification (on_failure).
And the problem will be much complicated if we discuss scenarios where there is an external task sensor waits for this task...
I'd love to hear from others what they think.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312624945
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
Probably want to move this above `_get_relevant_upstream_map_indexes` (or that function below this). There’s not much difference but better for readability and to prevent accidental access to this variable before it is created.
(Why do we want this dict to be empty when task is teadrown though? Or is there no difference?)
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
Probably want to move this above `_get_relevant_upstream_map_indexes` (or that function below this). There’s not much difference but better for readability and to prevent accidental access to this variable before it is created.
(Why do we want this dict to be empty when task is teadrown though? Or is there no difference?)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311153094
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,12 +102,124 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if status.passed is False and changed:
Review Comment:
Any reason this is resolved without a comment?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310795666
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
Review Comment:
> Also IMHO we need a condition on done, otherwise it will not be deterministic.
yeah, the reason why i felt it is ok is that airflow is already a race when there are many-to-one. so this seems basically consistent with the existing behavior -- to fail or skip fast. i think in practice it is not likely to make much difference. but...
for others, the scenario is, if there are multiple upstream indirect setups, should we wait for all of them to complete before determining the state of the object task (and then e.g. if there is a combination of success / skip / fail, then we fail it) or should we just take the state of the first non-success?
taking the state of the first non-success is how things work currently when there's a many-to-one direct upstream e.g. `[a, b] >> c`. in this way it "fails fast" (or skips). but indirect upstream setup is of course something a little different. we could choose to wait until all upstream setups are done before marking state. this means we do *not* fail fast, and the payoff is there is no race condition w.r.t. state.
cc @jedcunningham @ephraimbuddy @potiuk
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ephraimbuddy commented on pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "ephraimbuddy (via GitHub)" <gi...@apache.org>.
ephraimbuddy commented on PR #33903:
URL: https://github.com/apache/airflow/pull/33903#issuecomment-1704036612
I would prefer that we move this to 2.7.2 considering that it touches the heart of Airflow and we are cutting the RC tomorrow. What do you think?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312632811
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
so, when `_get_relevant_upstream_map_indexes` is called for setup constraint, then it has access to the setup tasks, and when it is called for direct upstreams, it will have access to the direct upstreams.
we could avoid all of this by passing the task instead of the id, but for whatever reason it feels icky to me to do that since the hash functions are so weird, even if there's no practical way the object gets mutated during processing. i can be easily convinced though.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312649247
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
yeah true, that should be fine.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] potiuk commented on pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #33903:
URL: https://github.com/apache/airflow/pull/33903#issuecomment-1704322540
I'd take the risk to be honest @ephraimbuddy. And maybe @hussein-awala might chime-in, but I think this bug was undermining rather important use case of setup/teardown and it also changes a behaviour from 2.7.0 that someone might start relying on. So this is really a bugfix, but for some people it might break their workflows (literally).
If we also keep the old behaviour in 2.7.1 and fix it in 2.7.2, it will be a bit harder to explain that this is a bugfix. Also 2.7.0 had quite a few teething problems and people went back to 2.6.3 and eagerly waiting for 2.7.1 (I hope) so likely not many people had a chance to start relying in 2.7.0 behaviour, so taking a bit of risk now might be worth it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311216463
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,85 +102,247 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if not status.passed and changed:
+ # no need to evaluate trigger rule; we've already marked as skipped or failed
+ return
yield from self._evaluate_trigger_rule(ti=ti, dep_context=dep_context, session=session)
- def _evaluate_trigger_rule(
+ @staticmethod
+ @functools.lru_cache
+ def _get_relevant_upstream_map_indexes(*, ti: TaskInstance, upstream_task, session) -> int | range | None:
Review Comment:
ok so i guess we can enclose it again...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] hussein-awala commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310767047
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+
+ if new_state is not None:
+ if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
+ )
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
Review Comment:
I think we can replace all of this by `done` after adding it:
```suggestion
if not done:
```
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
Review Comment:
```suggestion
elif done and skipped:
new_state = TaskInstanceState.SKIPPED
```
With
```
done = upstream == (success + skipped + failed + upstream_failed + removed)
```
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
Review Comment:
I never understood why why we should have at least one success upstream and the task should not be mapped to change its state to removed.
Also IMHO we need a condition on `done`, otherwise it will not be deterministic.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311164789
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,12 +102,124 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if status.passed is False and changed:
Review Comment:
Fundamentally there's nothing wrong with saying is false when that's really what you're looking for
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311164289
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,12 +102,124 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if status.passed is False and changed:
Review Comment:
I accepted the suggestion but then mypy complained so I reverted it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312647728
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
+
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ # set tasks_dict to contain relevant setups
+ # _get_relevant_upstream_map_indexes will use this to look up the task_id
+ tasks_dict = relevant_setups
+ for status, changed in _evaluate_setup_constraint(relevant_setups=relevant_setups):
+ yield status
+ if not status.passed and changed:
+ # no need to evaluate trigger rule; we've already marked as skipped or failed
+ return
+
+ task = ti.task
+ trigger_rule = task.trigger_rule
Review Comment:
I feel these two should be put back to near the top of the function, or at least before the (nested) function that uses them.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312634679
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
But `relevant_setups` is just a subset of all upstreams, isn’t it? Because I don’t see we catch KeyError anywhere, just passing the entire task dict shouldn’t change the logic at all from what I can tell. Is it just for performance?
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
But `relevant_setups` is just a subset of all upstreams, isn’t it? Because I don’t see we catch KeyError anywhere, just passing the entire task dict shouldn’t change the logic at all from what I can tell. Is it just for performance?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312630377
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
see line 554, where we set the dict equal to `relevant_setups` when processing setup constraint. then at 566 (if we get there) we reassign it to direct upstream tasks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310778855
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
Review Comment:
i would rather leave this to future.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311210791
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,85 +102,247 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if not status.passed and changed:
+ # no need to evaluate trigger rule; we've already marked as skipped or failed
+ return
yield from self._evaluate_trigger_rule(ti=ti, dep_context=dep_context, session=session)
- def _evaluate_trigger_rule(
+ @staticmethod
+ @functools.lru_cache
+ def _get_relevant_upstream_map_indexes(*, ti: TaskInstance, upstream_task, session) -> int | range | None:
+ """Get the given task's map indexes relevant to the current ti.
+
+ This extra closure allows us to query the database only when needed,
+ and at most once for each task (instead of once for each expanded
+ task instance of the same task).
+ """
+ from airflow.models.abstractoperator import NotMapped
+ from airflow.models.expandinput import NotFullyPopulated
+
+ @functools.lru_cache
+ def _get_expanded_ti_count() -> int:
Review Comment:
And this cache has no effect at all since it is local to this function and only accessed exactly once during its lifetime.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312636933
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
no, cus the bug we're solving here, is the case of _indirect_ setups -- setups that are not direct upstream relatives. in normal operation this is a non issue. but when you clear a work task, it clears its setups; if you don't add a check for the indirect setups then they will run concurrently. that's the problem here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311171205
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,12 +102,124 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if status.passed is False and changed:
Review Comment:
i guess mypy mighta been unrelated
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ephraimbuddy merged pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "ephraimbuddy (via GitHub)" <gi...@apache.org>.
ephraimbuddy merged PR #33903:
URL: https://github.com/apache/airflow/pull/33903
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312640538
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
separately, just a note, i expect there's a way to optimize this and reduce queries from 2 back down to 1; but i don't want to "change multiple things at once" in this way; i just want to layer the setup constraint on top of the existing trigger rule, and apply it only when dag has setups, to reduce potential for impact on dags not using the new feature, particularly given that here we're just trying to introduce a bugfix in a timely manner.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1312636933
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -222,224 +212,356 @@ def _iter_upstream_conditions() -> Iterator[ColumnOperators]:
else:
yield and_(TaskInstance.task_id == upstream_id, TaskInstance.map_index == map_indexes)
- # Optimization: Don't need to hit the database if all upstreams are
- # "simple" tasks (no task or task group mapping involved).
- if not any(needs_expansion(t) for t in upstream_tasks.values()):
- upstream = len(upstream_tasks)
- upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
- else:
- task_id_counts = session.execute(
- select(TaskInstance.task_id, func.count(TaskInstance.task_id))
- .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
- .where(or_(*_iter_upstream_conditions()))
- .group_by(TaskInstance.task_id)
- ).all()
- upstream = sum(count for _, count in task_id_counts)
- upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
-
- upstream_done = done >= upstream
-
- changed = False
- new_state = None
- if dep_context.flag_upstream_failed:
- if trigger_rule == TR.ALL_SUCCESS:
+ def _evaluate_setup_constraint(*, relevant_setups) -> Iterator[tuple[TIDepStatus, bool]]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ task = ti.task
+
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=x, relevant_ids=indirect_setups.keys())
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=indirect_setups)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+
+ new_state = None
+ changed = False
+
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
if upstream_failed or failed:
new_state = TaskInstanceState.UPSTREAM_FAILED
elif skipped:
new_state = TaskInstanceState.SKIPPED
elif removed and success and ti.map_index > -1:
if ti.map_index >= success:
new_state = TaskInstanceState.REMOVED
- elif trigger_rule == TR.ALL_FAILED:
- if success or skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_SUCCESS:
- if upstream_done and done == skipped:
- # if upstream is done and all are skipped mark as skipped
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and success <= 0:
- # if upstream is done and there are no success mark as upstream failed
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.ONE_FAILED:
- if upstream_done and not (failed or upstream_failed):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ONE_DONE:
- if upstream_done and not (failed or success):
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_FAILED:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- if upstream_failed or failed:
- new_state = TaskInstanceState.UPSTREAM_FAILED
- elif skipped == upstream:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.NONE_SKIPPED:
- if skipped:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_SKIPPED:
- if success or failed:
- new_state = TaskInstanceState.SKIPPED
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
- # when there is an upstream setup and they have all skipped, then skip
- new_state = TaskInstanceState.SKIPPED
- elif upstream_done and upstream_setup and success_setup == 0:
- # when there is an upstream setup, if none succeeded, mark upstream failed
- # if at least one setup ran, we'll let it run
- new_state = TaskInstanceState.UPSTREAM_FAILED
- if new_state is not None:
- if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
- past_depends_met = ti.xcom_pull(
- task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
- )
- if not past_depends_met:
- yield self._failing_status(
- reason=("Task should be skipped but the past depends are not met")
+
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- return
- changed = ti.set_state(new_state, session)
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
- if changed:
- dep_context.have_changed_ti_states = True
+ if changed:
+ dep_context.have_changed_ti_states = True
- if trigger_rule == TR.ONE_SUCCESS:
- if success <= 0:
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
yield self._failing_status(
reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
- f"but none were found. upstream_states={upstream_states}, "
+ f"All setup tasks must complete successfully. Relevant setups: {relevant_setups}: "
+ f"upstream_states={upstream_states}, "
f"upstream_task_ids={task.upstream_task_ids}"
+ ),
+ ), changed
+
+ def _evaluate_direct_relatives() -> Iterator[TIDepStatus]:
+ """Evaluate whether ``ti``'s trigger rule was met.
+
+ :param ti: Task instance to evaluate the trigger rule of.
+ :param dep_context: The current dependency context.
+ :param session: Database session.
+ """
+ finished_upstream_tis = (
+ finished_ti
+ for finished_ti in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if _is_relevant_upstream(upstream=finished_ti, relevant_ids=ti.task.upstream_task_ids)
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
+
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
+ done = upstream_states.done
+ success_setup = upstream_states.success_setup
+ skipped_setup = upstream_states.skipped_setup
+
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in upstream_tasks.values()):
+ upstream = len(upstream_tasks)
+ upstream_setup = sum(1 for x in upstream_tasks.values() if x.is_setup)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(or_(*_iter_upstream_conditions(relevant_tasks=upstream_tasks)))
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
+ upstream_setup = sum(c for t, c in task_id_counts if upstream_tasks[t].is_setup)
+
+ upstream_done = done >= upstream
+
+ changed = False
+ new_state = None
+ if dep_context.flag_upstream_failed:
+ if trigger_rule == TR.ALL_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+ elif trigger_rule == TR.ALL_FAILED:
+ if success or skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_SUCCESS:
+ if upstream_done and done == skipped:
+ # if upstream is done and all are skipped mark as skipped
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and success <= 0:
+ # if upstream is done and there are no success mark as upstream failed
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.ONE_FAILED:
+ if upstream_done and not (failed or upstream_failed):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ONE_DONE:
+ if upstream_done and not (failed or success):
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_FAILED:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped == upstream:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_SKIPPED:
+ if success or failed:
+ new_state = TaskInstanceState.SKIPPED
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if upstream_done and upstream_setup and skipped_setup >= upstream_setup:
+ # when there is an upstream setup and they have all skipped, then skip
+ new_state = TaskInstanceState.SKIPPED
+ elif upstream_done and upstream_setup and success_setup == 0:
+ # when there is an upstream setup, if none succeeded, mark upstream failed
+ # if at least one setup ran, we'll let it run
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ if new_state is not None:
+ if (
+ new_state == TaskInstanceState.SKIPPED
+ and dep_context.wait_for_past_depends_before_skipping
+ ):
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
)
- )
- elif trigger_rule == TR.ONE_FAILED:
- if not failed and not upstream_failed:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
- f"but none were found. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ if not past_depends_met:
+ yield self._failing_status(
+ reason=("Task should be skipped but the past depends are not met")
+ )
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ if trigger_rule == TR.ONE_SUCCESS:
+ if success <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task success, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ONE_DONE:
- if success + failed <= 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}'"
- "requires at least one upstream task failure or success"
- f"but none were failed or success. upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_FAILED:
+ if not failed and not upstream_failed:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires one upstream task failure, "
+ f"but none were found. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SUCCESS:
- num_failures = upstream - success
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ONE_DONE:
+ if success + failed <= 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}'"
+ "requires at least one upstream task failure or success"
+ f"but none were failed or success. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_FAILED:
- num_success = upstream - failed - upstream_failed
- if ti.map_index > -1:
- num_success -= removed
- if num_success > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have failed, "
- f"but found {num_success} non-failure(s). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SUCCESS:
+ num_failures = upstream - success
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_FAILED:
+ num_success = upstream - failed - upstream_failed
+ if ti.map_index > -1:
+ num_success -= removed
+ if num_success > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks "
+ f"to have failed, but found {num_success} non-failure(s). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were "
+ f"not done. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
- num_failures = upstream - success - skipped
- if ti.map_index > -1:
- num_failures -= removed
- if num_failures > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"succeeded or been skipped, but found {num_failures} non-success(es). "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.NONE_SKIPPED:
- if not upstream_done or (skipped > 0):
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not have been "
- f"skipped, but found {skipped} task(s) skipped. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_FAILED_MIN_ONE_SUCCESS:
+ num_failures = upstream - success - skipped
+ if ti.map_index > -1:
+ num_failures -= removed
+ if num_failures > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"succeeded or been skipped, but found {num_failures} non-success(es). "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_SKIPPED:
- num_non_skipped = upstream - skipped
- if num_non_skipped > 0:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
- f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.NONE_SKIPPED:
+ if not upstream_done or (skipped > 0):
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to not "
+ f"have been skipped, but found {skipped} task(s) skipped. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
- if not upstream_done:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
- f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_SKIPPED:
+ num_non_skipped = upstream - skipped
+ if num_non_skipped > 0:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have been "
+ f"skipped, but found {num_non_skipped} task(s) in non skipped state. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup is None: # for now, None only happens in mapped case
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif trigger_rule == TR.ALL_DONE_SETUP_SUCCESS:
+ if not upstream_done:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires all upstream tasks to have "
+ f"completed, but found {len(upstream_tasks) - done} task(s) that were not done. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- elif upstream_setup and not success_setup >= 1:
- yield self._failing_status(
- reason=(
- f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task be "
- f"successful, but found {upstream_setup - success_setup} task(s) that were not. "
- f"upstream_states={upstream_states}, "
- f"upstream_task_ids={task.upstream_task_ids}"
+ elif upstream_setup is None: # for now, None only happens in mapped case
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' cannot have mapped tasks as upstream. "
+ f"upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
+ )
+ elif upstream_setup and not success_setup >= 1:
+ yield self._failing_status(
+ reason=(
+ f"Task's trigger rule '{trigger_rule}' requires at least one upstream setup task "
+ f"be successful, but found {upstream_setup - success_setup} task(s) that were "
+ f"not. upstream_states={upstream_states}, "
+ f"upstream_task_ids={task.upstream_task_ids}"
+ )
)
- )
- else:
- yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+ else:
+ yield self._failing_status(reason=f"No strategy to evaluate trigger rule '{trigger_rule}'.")
+
+ # this dictionary enables use of task_id as the key for lru caching
+ # in function _get_relevant_upstream_map_indexes
+ tasks_dict = {}
Review Comment:
no, cus the bug we're solving here, is the case of _indirect_ setups -- setups that are not direct upstream relatives. in normal operation this is a non issue. but when you clear a work task, it clears its setups, though there might be other tasks between the setup and the work task (thus indirect); if you don't add a check for the indirect setups then they will run concurrently. that's the problem here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311166669
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,12 +102,124 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if status.passed is False and changed:
Review Comment:
Cool
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310920402
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
+
+ if new_state is not None:
+ if new_state == TaskInstanceState.SKIPPED and dep_context.wait_for_past_depends_before_skipping:
+ past_depends_met = ti.xcom_pull(
+ task_ids=ti.task_id, key=PAST_DEPENDS_MET, session=session, default=False
+ )
+ if not past_depends_met:
+ yield self._failing_status(
+ reason="Task should be skipped but the past depends are not met"
+ ), changed
+ return
+ changed = ti.set_state(new_state, session)
+
+ if changed:
+ dep_context.have_changed_ti_states = True
+
+ non_successes = upstream - success
+ if ti.map_index > -1:
+ non_successes -= removed
+ if non_successes > 0:
Review Comment:
this could be true but, this logic was added to deal with a deadlock and i don't really want to mess with it here. i'm just keeping the behavior consistent with trigger rule. maybe let's do this in followup?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1311219179
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -102,85 +102,247 @@ def _get_dep_statuses(
if ti.task.trigger_rule == TR.ALWAYS:
yield self._passing_status(reason="The task had a always trigger rule set.")
return
+ if not ti.task.is_teardown:
+ # a teardown cannot have any indirect setups
+ relevant_setups = {t.task_id: t for t in ti.task.get_upstreams_only_setups()}
+ if relevant_setups:
+ for status, changed in self._evaluate_setup_constraint(
+ ti=ti, relevant_setups=relevant_setups, dep_context=dep_context, session=session
+ ):
+ yield status
+ if not status.passed and changed:
+ # no need to evaluate trigger rule; we've already marked as skipped or failed
+ return
yield from self._evaluate_trigger_rule(ti=ti, dep_context=dep_context, session=session)
- def _evaluate_trigger_rule(
+ @staticmethod
+ @functools.lru_cache
+ def _get_relevant_upstream_map_indexes(*, ti: TaskInstance, upstream_task, session) -> int | range | None:
Review Comment:
well, i'll look into this tomorrow i guess
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] ephraimbuddy commented on pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "ephraimbuddy (via GitHub)" <gi...@apache.org>.
ephraimbuddy commented on PR #33903:
URL: https://github.com/apache/airflow/pull/33903#issuecomment-1704343282
Cherrypicked!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310779527
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
Review Comment:
do we need to wait for done if we already have one that is skipped?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] dstandish commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "dstandish (via GitHub)" <gi...@apache.org>.
dstandish commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310814737
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
Review Comment:
so with your example, if the upstream setups are skipped, the task will be skipped. it's only if there's a mixed state, which is actually a pretty odd situation, don't think super common generally, but we must to decide what to do.
the question is take the first one or wait for all and the evaluate.
if your position is "wait for all and evaluate" what would be your logic?
```
if at least one failure, then upstream_failed
else if at least one skip, skip
```
?
i think it's probably fine... add an implicit all_done for the setups and then evaluate. i don't think it makes a _huge_ difference either way.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] jedcunningham commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "jedcunningham (via GitHub)" <gi...@apache.org>.
jedcunningham commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310871504
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
+ elif removed and success and ti.map_index > -1:
+ if ti.map_index >= success:
+ new_state = TaskInstanceState.REMOVED
Review Comment:
I lean more toward consistency here (fast fail), even thought that consistency is a non-deterministic outcome.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] hussein-awala commented on a diff in pull request #33903: Ensure that tasks wait for running indirect setup
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #33903:
URL: https://github.com/apache/airflow/pull/33903#discussion_r1310782368
##########
airflow/ti_deps/deps/trigger_rule_dep.py:
##########
@@ -117,70 +229,120 @@ def _evaluate_trigger_rule(
:param dep_context: The current dependency context.
:param session: Database session.
"""
- from airflow.models.abstractoperator import NotMapped
- from airflow.models.expandinput import NotFullyPopulated
from airflow.models.operator import needs_expansion
from airflow.models.taskinstance import TaskInstance
task = ti.task
- upstream_tasks = {t.task_id: t for t in task.upstream_list}
- trigger_rule = task.trigger_rule
- @functools.lru_cache
- def _get_expanded_ti_count() -> int:
- """Get how many tis the current task is supposed to be expanded into.
+ indirect_setups = {k: v for k, v in relevant_setups.items() if k not in task.upstream_task_ids}
+ finished_upstream_tis = (
+ x
+ for x in dep_context.ensure_finished_tis(ti.get_dagrun(session), session)
+ if self._is_relevant_upstream(
+ ti=ti,
+ upstream=x,
+ relevant_ids=indirect_setups.keys(),
+ session=session,
+ )
+ )
+ upstream_states = _UpstreamTIStates.calculate(finished_upstream_tis)
- This extra closure allows us to query the database only when needed,
- and at most once.
- """
- return task.get_mapped_ti_count(ti.run_id, session=session)
+ # all of these counts reflect indirect setups which are relevant for this ti
+ success = upstream_states.success
+ skipped = upstream_states.skipped
+ failed = upstream_states.failed
+ upstream_failed = upstream_states.upstream_failed
+ removed = upstream_states.removed
- @functools.lru_cache
- def _get_relevant_upstream_map_indexes(upstream_id: str) -> int | range | None:
- """Get the given task's map indexes relevant to the current ti.
+ # Optimization: Don't need to hit the database if all upstreams are
+ # "simple" tasks (no task or task group mapping involved).
+ if not any(needs_expansion(t) for t in indirect_setups.values()):
+ upstream = len(indirect_setups)
+ else:
+ task_id_counts = session.execute(
+ select(TaskInstance.task_id, func.count(TaskInstance.task_id))
+ .where(TaskInstance.dag_id == ti.dag_id, TaskInstance.run_id == ti.run_id)
+ .where(
+ or_(
+ *self._iter_upstream_conditions(
+ ti=ti, relevant_tasks=indirect_setups, session=session
+ )
+ )
+ )
+ .group_by(TaskInstance.task_id)
+ ).all()
+ upstream = sum(count for _, count in task_id_counts)
- This extra closure allows us to query the database only when needed,
- and at most once for each task (instead of once for each expanded
- task instance of the same task).
- """
- try:
- expanded_ti_count = _get_expanded_ti_count()
- except (NotFullyPopulated, NotMapped):
- return None
- return ti.get_relevant_upstream_map_indexes(
- upstream_tasks[upstream_id],
- expanded_ti_count,
- session=session,
- )
+ new_state = None
+ changed = False
- def _is_relevant_upstream(upstream: TaskInstance) -> bool:
- """Whether a task instance is a "relevant upstream" of the current task."""
- # Not actually an upstream task.
- if upstream.task_id not in task.upstream_task_ids:
- return False
- # The current task is not in a mapped task group. All tis from an
- # upstream task are relevant.
- if task.get_closest_mapped_task_group() is None:
- return True
- # The upstream ti is not expanded. The upstream may be mapped or
- # not, but the ti is relevant either way.
- if upstream.map_index < 0:
- return True
- # Now we need to perform fine-grained check on whether this specific
- # upstream ti's map index is relevant.
- relevant = _get_relevant_upstream_map_indexes(upstream.task_id)
- if relevant is None:
- return True
- if relevant == upstream.map_index:
- return True
- if isinstance(relevant, collections.abc.Container) and upstream.map_index in relevant:
- return True
- return False
+ # if there's a failure, we mark upstream_failed; if there's a skip, we mark skipped
+ # in either case, we don't wait for all relevant setups to complete
+ if dep_context.flag_upstream_failed:
+ if upstream_failed or failed:
+ new_state = TaskInstanceState.UPSTREAM_FAILED
+ elif skipped:
+ new_state = TaskInstanceState.SKIPPED
Review Comment:
yes, waiting for a failed setup, in this case we will always have an upstream_failed state if we have at least one failed setup. Without this condition we will have a skipped state once a setup task is skipped, even if it will be followed by a failed setup.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org