【发布时间】:2022-01-18 07:58:48
【问题描述】:
我有一个具有以下结构的 AIRFLOW DAG。
所有以“check*”开头的函数都是BranchPythonOperator,函数exceptionControl是一个ExecuteDagRunOperator,它接收每个错误以便处理它们。
这是 DAG 配置:
checkCloudFunctions = BranchPythonOperator(
task_id='checkCloudFunctions',
python_callable=check_cloud_functions,
provide_context=True,
dag=dag)
checkSqlTables = BranchPythonOperator(
task_id='checkSqlTables',
python_callable=check_sql_tables,
provide_context=True,
dag=dag)
checkBigQueryTable = BranchPythonOperator(
task_id='checkBigQueryTable',
python_callable=check_big_query_table,
provide_context=True,
dag=dag)
labBuilt = DummyOperator(
task_id='labBuilt',
dag=dag)
exceptionControl = ExecuteDagRunOperator(
task_id='exceptionControl',
execute_dag_id="SYS_exception_control",
python_callable=mediation.dag_trigger_exception,
trigger_rule='one_success',
dag=dag)
# graphs
checkCloudFunctions >> checkSqlTables
checkCloudFunctions >> exceptionControl
checkSqlTables >> checkBigQueryTable
checkSqlTables >> exceptionControl
checkBigQueryTable >> labBuilt
checkBigQueryTable >> exceptionControl
问题是 checkSqlTables 应该遵循异常控制,但它会跳过并且 DAG 结束。该函数返回“exceptionControl”,我们可以在 checkSqlTables 日志中看到:
{base_task_runner.py:98} INFO - {python_operator.py:90} INFO - Done. Returned value was: exceptionControl
{base_task_runner.py:98} INFO - {python_operator.py:118} INFO - Following branch exceptionControl
{base_task_runner.py:98} INFO - {python_operator.py:119} INFO - Marking other directly downstream tasks as skipped
{base_task_runner.py:98} INFO - {python_operator.py:128} INFO - Done.
我也玩过 trigger_rule 属性(one_success、dummy...),但它似乎不起作用。
如果我删除第一步,它似乎可以工作,所以它似乎应该是我的dag的某种配置问题。
任何想法为什么函数 checkSqlTables 不分支到 exceptionControl?
编辑:在new deep reading to the Airflow Documentation 中,我注意到如果一个步骤将任务标记为已跳过,它将永远被跳过,因此我的代码将永远无法与分支运算符一起使用。
使用分支的解决方案包括在每一步之前的一个虚拟步骤。但是我有一些有超过 10 个步骤的 DAG,架构会完全混乱。
【问题讨论】:
-
解决方案很简单,有一个“最终”阶段,无需执行任何操作。如果您的代码需要结束分支到这个“最终”阶段。