You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/02/13 13:00:20 UTC

[GitHub] [airflow] yuqian90 commented on a change in pull request #7276: [AIRFLOW-5391] Do not run skipped tasks when they are cleared

yuqian90 commented on a change in pull request #7276: [AIRFLOW-5391] Do not run skipped tasks when they are cleared
URL: https://github.com/apache/airflow/pull/7276#discussion_r378844501
 
 

 ##########
 File path: airflow/ti_deps/deps/branch_dep.py
 ##########
 @@ -0,0 +1,63 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from airflow.ti_deps.deps.base_ti_dep import BaseTIDep
+
+
+class BranchDep(BaseTIDep):
+    """
+    Determines if any of the task's direct upstream relatives have decided this task should
+    be skipped.
+    """
+    NAME = "Branch Rule"
+    IGNORABLE = True
+    IS_TASK_DEP = True
+
+    def _get_dep_statuses(self, ti, session, dep_context=None):
+        from airflow.models.skipmixin import SkipMixin
+        from airflow.utils.state import State
+
+        upstream = ti.task.get_direct_relatives(upstream=True)
+
+        finished_tasks = dep_context.finished_tasks if dep_context else None
+        if finished_tasks is None:
+            # this is for the strange feature of running tasks without dag_run
+            finished_tasks = ti.task.dag.get_task_instances(
+                start_date=ti.execution_date,
+                end_date=ti.execution_date,
+                state=State.finished() + [State.UPSTREAM_FAILED],
+                session=session)
+
+        finished_task_ids = {t.task_id for t in finished_tasks}
+
+        for parent in upstream:
+            if isinstance(parent, SkipMixin):
+                xcom_value = ti.xcom_pull(task_ids=parent.task_id)
 
 Review comment:
   Hi, @ashb  here's the reason I'm using XCom, using the same example I previously provided:
   
   In this example, `condition_false` is a `ShortCircuitOperator` that decided to skip its downstream task. But If the user clicks `false_1` and hit "Clear", what Airflow currently does is that it'll actually execute `false_1`, which is very counter-intuitive for users.
   
   So how do we improve this behavior?
   
   When `false_1` is cleared, one way to know that it should actually be skipped is by looking at what its parent `ShortCircuitOperator` returned. To get the return value of the `ShortCircuitOperator`, I'm resorting to `XCom`.
   
   ![image](https://user-images.githubusercontent.com/6637585/74437479-283ddc80-4ea3-11ea-8aec-324369733ebb.png)
   
   
   This works fine. But maybe you know a better way than using XCom for this purpose. Definitely let me know I don't mind changing.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services