Airflow并行EMR任务失败时如何配置操作符发送SNS告警
核心实现原理
配置trigger_rule="all_done"的SNS任务被触发时,所有上游watch_step_*监控任务已经全部执行完毕,无论成功或失败都已经具备最终状态,可在任务执行阶段遍历读取所有上游任务的状态,判断是否存在失败节点,再决定是否发送告警。
具体改造步骤
- 调整任务链路
无需将每个watch_step_*任务直接挂载到SNS任务下游,可在所有watch_step_*任务和SNS任务之间增加一层状态判断节点,实现“存在失败才发送告警、全部成功则无操作”的逻辑。 - 动态收集监控任务状态
在判断节点中,通过DagRun上下文对象遍历所有watch_step_*任务的实例状态,筛选所有标记为失败的任务,将失败信息存入XCOM供后续SNS消息组装使用。 - 分支触发告警流程
状态判断节点根据是否存在失败任务,分支跳转至对应下游:- 存在失败任务:跳转至SNS发送任务,携带收集到的失败信息组装告警内容
- 全部任务执行成功:跳转至空操作的Dummy任务,无额外动作
代码改造示例
新增依赖引入:
from airflow.operators.python import BranchPythonOperator from airflow.operators.dummy import DummyOperator
改造后的工作流核心逻辑:
def check_emr_step_status(**context): ti = context["ti"] dagrun = ti.get_dagrun() # 遍历当前运行实例下所有EMR步骤监控任务的状态 all_watch_tasks = [ task for task in dagrun.get_task_instances() if task.task_id.startswith("watch_step_") ] failed_tasks = [ task.task_id for task in all_watch_tasks if task.state == "failed" ] # 将失败任务列表存入XCOM,供SNS任务使用 ti.xcom_push(key="failed_step_tasks", value=failed_tasks) # 分支判断:有失败则走SNS告警,否则走无操作节点 return "sns_notification" if failed_tasks else "noop_end" def define_workflow(self): # ... 原有cluster_creator、tables定义逻辑保持不变 ... emr_env = Variable.get("consolidation", deserialize_json=True).get("emr") # 新增状态判断分支任务 check_step_status = BranchPythonOperator( task_id="check_step_status", python_callable=check_emr_step_status, trigger_rule="all_done", dag=self.dag ) # 新增无操作终点任务 noop_end = DummyOperator( task_id="noop_end", dag=self.dag ) # 改造原SNS操作,支持从XCOM动态取失败信息生成告警内容 sns_operator = SnsPublishOperator( target_arn=emr_env['snstopic'], message=""" EMR并行步骤执行异常,失败的任务节点: {{ ti.xcom_pull(task_ids='check_step_status', key='failed_step_tasks') | join(', ') }} 请及时排查。 """, subject="EMR步骤执行失败告警", task_id="sns_notification", trigger_rule="all_success", dag=self.dag ) # 分支链路连接 check_step_status >> [sns_operator, noop_end] for table in tables: step_args = copy.copy(self.other_args) step_args['table'] = table step_adder = EmrAddStepsOperator( dag=self.dag, task_id='step_{table}'.format(table=table), job_flow_id="{{ task_instance.xcom_pull(task_ids='create_cluster', key='return_value') }}", aws_conn_id='aws_default', steps=[common_args.step_args(**step_args)] ) step_checker = EmrStepSensor( dag=self.dag, task_id='watch_step_{table}'.format(table=table), job_flow_id="{{ task_instance.xcom_pull(task_ids='create_cluster', key='return_value') }}", step_id="{{ task_instance.xcom_pull(task_ids='step_" + table + "', key='return_value')[0] }}", aws_conn_id='aws_default', ) # 调整下游链路:原step_checker直接连接sns_operator改为连接状态判断节点 cluster_creator.set_downstream(step_adder) step_adder.set_downstream(step_checker) step_checker.set_downstream(check_step_status) return self.dag
关键注意事项
- 状态判断节点的
trigger_rule必须设为all_done,保证所有watch_step_*任务无论成功失败,都能触发后续状态检查,不会因单个步骤失败中断全量状态校验流程 - SNS发送任务的
trigger_rule改回默认all_success即可,因仅当分支判断明确指向它时才会被触发,无需再配置为all_done - 采用任务ID前缀匹配的方式收集状态,无需手动枚举所有
watch_step_*的任务ID,后续新增并行步骤仅需修改DAG的表配置项,无需调整状态判断逻辑
轻量化可选方案
若不想新增额外分支任务,可直接在原有SNS任务的执行逻辑中加入跳过判断:在SNS操作的前置钩子中先遍历所有watch_step_*任务的状态,若不存在失败节点,直接抛出AirflowSkipException跳过当前SNS任务,不发送消息。该方式代码改动量更小,但可观测性稍差,无法在DAG视图上直观看到分支逻辑。
内容的提问来源于stack exchange,提问作者Tomás Denis Reyes Sánchez
相关产品推荐
相关产品推荐

