Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/airflow/utils/dag_cycle_tester.py: 28%
40 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License. You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17"""DAG Cycle tester."""
18from __future__ import annotations
20from collections import defaultdict, deque
21from typing import TYPE_CHECKING, Deque
23from airflow.exceptions import AirflowDagCycleException, RemovedInAirflow3Warning
25if TYPE_CHECKING:
26 from airflow.models.dag import DAG
28CYCLE_NEW = 0
29CYCLE_IN_PROGRESS = 1
30CYCLE_DONE = 2
33def test_cycle(dag: DAG) -> None:
34 """
35 A wrapper function of `check_cycle` for backward compatibility purpose.
36 New code should use `check_cycle` instead since this function name `test_cycle` starts with 'test_' and
37 will be considered as a unit test by pytest, resulting in failure.
38 """
39 from warnings import warn
41 warn(
42 "Deprecated, please use `check_cycle` at the same module instead.",
43 RemovedInAirflow3Warning,
44 stacklevel=2,
45 )
46 return check_cycle(dag)
49def check_cycle(dag: DAG) -> None:
50 """Check to see if there are any cycles in the DAG.
52 :raises AirflowDagCycleException: If cycle is found in the DAG.
53 """
54 # default of int is 0 which corresponds to CYCLE_NEW
55 visited: dict[str, int] = defaultdict(int)
56 path_stack: Deque[str] = deque()
57 task_dict = dag.task_dict
59 def _check_adjacent_tasks(task_id, current_task):
60 """Returns first untraversed child task, else None if all tasks traversed."""
61 for adjacent_task in current_task.get_direct_relative_ids():
62 if visited[adjacent_task] == CYCLE_IN_PROGRESS:
63 msg = f"Cycle detected in DAG: {dag.dag_id}. Faulty task: {task_id}"
64 raise AirflowDagCycleException(msg)
65 elif visited[adjacent_task] == CYCLE_NEW:
66 return adjacent_task
67 return None
69 for dag_task_id in dag.task_dict.keys():
70 if visited[dag_task_id] == CYCLE_DONE:
71 continue
72 path_stack.append(dag_task_id)
73 while path_stack:
74 current_task_id = path_stack[-1]
75 if visited[current_task_id] == CYCLE_NEW:
76 visited[current_task_id] = CYCLE_IN_PROGRESS
77 task = task_dict[current_task_id]
78 child_to_check = _check_adjacent_tasks(current_task_id, task)
79 if not child_to_check:
80 visited[current_task_id] = CYCLE_DONE
81 path_stack.pop()
82 else:
83 path_stack.append(child_to_check)