1#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18from __future__ import annotations
19
20from collections.abc import Iterator
21from typing import TYPE_CHECKING
22
23from sqlalchemy import select
24
25from airflow.models.taskinstance import TaskInstance
26from airflow.ti_deps.deps.base_ti_dep import BaseTIDep
27from airflow.utils.state import State, TaskInstanceState
28
29if TYPE_CHECKING:
30 from sqlalchemy.orm import Session
31
32 from airflow.ti_deps.dep_context import DepContext
33 from airflow.ti_deps.deps.base_ti_dep import TIDepStatus
34
35
36class MappedTaskUpstreamDep(BaseTIDep):
37 """
38 Determines if the task, if mapped, is allowed to run based on its mapped dependencies.
39
40 In particular, check if upstream tasks that provide XComs used by this task for task mapping are in
41 states that allow the task instance to run.
42 """
43
44 NAME = "Mapped dependencies have succeeded"
45 IGNORABLE = True
46 IS_TASK_DEP = True
47
48 def _get_dep_statuses(
49 self,
50 ti: TaskInstance,
51 session: Session,
52 dep_context: DepContext,
53 ) -> Iterator[TIDepStatus]:
54 from airflow.models.mappedoperator import MappedOperator
55
56 if isinstance(ti.task, MappedOperator):
57 mapped_dependencies = ti.task.iter_mapped_dependencies()
58 elif ti.task is not None and (task_group := ti.task.get_closest_mapped_task_group()) is not None:
59 mapped_dependencies = task_group.iter_mapped_dependencies()
60 else:
61 return
62
63 # Get the tis of all mapped dependencies. In case a mapped dependency is itself mapped, we are
64 # only interested in it if it hasn't been expanded yet, i.e., we filter by map_index=-1. This is
65 # because if it has been expanded, it did not fail and was not skipped outright which is all we need
66 # to know for the purposes of this check.
67 mapped_dependency_tis = (
68 session.scalars(
69 select(TaskInstance).where(
70 TaskInstance.task_id.in_(operator.task_id for operator in mapped_dependencies),
71 TaskInstance.dag_id == ti.dag_id,
72 TaskInstance.run_id == ti.run_id,
73 TaskInstance.map_index == -1,
74 )
75 ).all()
76 if mapped_dependencies
77 else []
78 )
79 if not mapped_dependency_tis:
80 yield self._passing_status(reason="There are no (unexpanded) mapped dependencies!")
81 return
82
83 finished_states = {ti.state for ti in mapped_dependency_tis if ti.state in State.finished}
84 if not finished_states:
85 return
86 if finished_states == {TaskInstanceState.SUCCESS}:
87 # Mapped dependencies are at least partially done and only feature successes
88 return
89
90 # At least one mapped dependency was not successful
91 if ti.state not in {TaskInstanceState.FAILED, TaskInstanceState.UPSTREAM_FAILED}:
92 # If another dependency (such as the trigger rule dependency) has not already marked the task as
93 # FAILED or UPSTREAM_FAILED then we update the state
94 new_state = None
95 if (
96 TaskInstanceState.FAILED in finished_states
97 or TaskInstanceState.UPSTREAM_FAILED in finished_states
98 ):
99 new_state = TaskInstanceState.UPSTREAM_FAILED
100 elif TaskInstanceState.SKIPPED in finished_states:
101 new_state = TaskInstanceState.SKIPPED
102 if new_state is not None and ti.set_state(new_state, session):
103 dep_context.have_changed_ti_states = True
104 yield self._failing_status(reason="At least one of task's mapped dependencies has not succeeded!")