airflow触发规则"all_done"和"all_success"有什么区别?

What is the difference between airflow trigger rule "all_done" and "all_success"?

我正在处理的工作流中的一个要求是在给定时间内等待某个事件发生,如果没有发生,则将任务标记为失败,下游任务仍应执行。

我想知道"all_done"是否意味着所有依赖任务都已完成,无论它们是否成功。

https://airflow.apache.org/docs/apache-airflow/stable/concepts/dags.html#concepts-trigger-rules

all_done 表示所有操作都已完成。也许他们成功了,也许没有。

all_success 表示所有操作都已完成且没有错误

所以你猜对了

考虑将 ShortCircuitOperator 用于您所述的目的。

摘要
如果 SUCCESS、FAILED、UPSTREAM_FAILED、SKIPPED 任务的计数大于或等于所有上游任务的计数,则任务为 "all done"。

不确定为什么会大于?也许子标签对计数做了一些奇怪的事情。

如果上游任务的计数与成功的上游任务的计数相同,则任务为"all success"。

详情
评估触发规则的代码在这里https://github.com/apache/incubator-airflow/blob/master/airflow/ti_deps/deps/trigger_rule_dep.py#L72

  1. ALL_DONE

以下代码将 qry 和 return 的第一行(查询是一个聚合,无论如何只会 return 一行)运行到以下变量中:

successes, skipped, failed, upstream_failed, done = qry.first()

查询中的 "done" 列对应于此:func.count(TI.task_id) 换句话说,所有与过滤器匹配的任务的计数。 过滤器指定它只计算上游任务,从当前 dag,从当前执行日期开始,这个:

 TI.state.in_([
                    State.SUCCESS, State.FAILED,
                    State.UPSTREAM_FAILED, State.SKIPPED])

所以 done 是具有这 4 个状态之一的上游任务的计数。

后面还有这段代码

upstream = len(task.upstream_task_ids)
...
upstream_done = done >= upstream

而实际的触发规则仅在此

上失败
if not upstream_done
  1. ALL_SUCCESS

代码相当简单,概念也很直观

num_failures = upstream - successes
if num_failures > 0:
... it fails

所有运算符都有一个 trigger_rule 参数,用于定义触发生成的任务的规则。

我在以下用例中使用了这些触发规则:

all_success:(默认)所有parent都成功了

all_done:所有 parent 已执行完毕。

To carry out cleanups irrespective of the upstream tasks
succeeded or failed then setting this trigger_rule to ALL_DONE is always useful.

one_success:只要至少一个 parent 成功就触发,它不会等待所有 parent 完成

To trigger external DAG after successful completion of the single upstream parent.

one_failed:只要至少一个 parent 失败就触发,它不会等待所有 parent 失败完成

To trigger the alerts once at least one parent fails or for any other use case.

Reference