Skip to content

Support a JoinPythonOperator to Work with the BranchPythonOperator Already Supported #167

Closed

Description

@r39132

I need a branch operator to do the following

Currently, in our QA environment, our Spark job has been split into 2 steps, let's call them A and C. In our PROD environment however, it is still one step, A. I don't want to maintain 2 different DAGs, one for PROD and one for QA. Instead, I would like to leverage both Variables and the new BranchPythonOperator.

Specifically, I have a new Variable called ENV with values of QA or PROD. I would like the PythonBranchOperator to execute A-->B-->C-->D-->E in QA and A-->B-->D-->E in PROD. I would like to make step C optional based on ENV.

My DAG is below:
screenshot 2015-07-22 19 16 26

I tried to leverage the new BranchPythonOperator for this, but it doesn't work. The reason is that the DAG is not allowed to rejoin after a branch. Once one leg of the branch is taken, all downstream descendants are marked as skipped. However, if the DAG rejoins, then this is not the case.

In my example, C is a BashOperator.

Activity

  1. changed the title [-]Need the possibility of skipping a BashOperator at Execution Time[/-] [+]Need the Ability to skip a BashOperator at Execution Time[/+] on Jul 23, 2015
  2. changed the title [-]Need the Ability to skip a BashOperator at Execution Time[/-] [+]Need the Ability to skip a BashOperator at Execution Time Based on Airflow Read-Only Variable Values[/+] on Jul 23, 2015
  3. changed the title [-]Need the Ability to skip a BashOperator at Execution Time Based on Airflow Read-Only Variable Values[/-] [+]Need the Ability to Optionally Execute a (Bash) Operator based on a Read-only Variable Value[/+] on Jul 23, 2015
  4. mistercrunch commented on Jul 23, 2015

    @mistercrunch
    Member

    For the templated operators like BashOperator it's pretty easy to skip and succeed:

    {% if params.is_dev %}
    echo "success"
    {% else %}
    bash spark_command.sh
    {% endif %}
    

    We've been doing this for skipping tasks before a certain date for instance as in {% if ds >= '2015-01-01' %}

    A more generic way could be (not a feature yet) to pass a skip_and_succeed param that expects a callable that receives the context object and skips and succeeds if the callable returns True. It may look something like this:

    FooOperator(
        task_id='bar',
        skip_and_succeed=lambda context: return context['ds']<'2015-01-01',
        dag=dag,
    )
    
  5. r39132 commented on Jul 23, 2015

    @r39132
    ContributorAuthor

    Cool. This works.

    # Operator : Read the parquet data and generate aggregate data
    optional_aggregation_templated_command = """
        {% if params.ENV == "EP_PROD" %}
            echo "Skipping Aggregation in Prod Env"
        {% else %}
            ssh -i ~/.ssh/id_rsa_spark [email protected] '. /root/.bash_profile; /root/run_aggregation.sh {{ macros.ds_format(macros.ds_add(ds, -3), '%Y-%m-%d', '%s') }} {{ execution_date.strftime('%s') }}'
        {% endif %}
    """
    aggregate_data_spark_job = BashOperator(
        task_id='aggregate_data_spark_job',
        execution_timeout=timedelta(hours=3),
        bash_command=optional_aggregation_templated_command,
        params={'ENV': ENV},
        dag=dag)
    aggregate_data_spark_job.set_upstream(generate_new_parquet_files_spark_job)
    

    It's a handy solution, but I guess I was thinking of the more general problem of handling branched flows that need to be joined later. Branching is a great feature, but with branching often comes joining. The legs of the branch could be different lengths. Currently, you set all descendants to "Skipped", but perhaps the code can be changed to set only those descendants to Skipped that do not join with a Non-skipped branch. In other words, we would need to set descendants to skipped until we come across a node that has multiple parents and one of those parents is not skipped.

  6. r39132 commented on Jul 23, 2015

    @r39132
    ContributorAuthor

    BTW, I was not able to follow your FooOperator example. I'd like to keep this open in hopes there is a more generic join approach!

  7. changed the title [-]Need the Ability to Optionally Execute a (Bash) Operator based on a Read-only Variable Value[/-] [+]Support a JoinPythonOperator to Parallel the BranchPythonOperator Already Supported[/+] on Jul 25, 2015
  8. r39132 commented on Jul 25, 2015

    @r39132
    ContributorAuthor

    Hi Folks!
    I've changed the title of this issue to Support a JoinPythonOperator to Work with the BranchPythonOperator Already Supported. Prior to the creation of the BranchPythonOperator, we had ability to execute DAGs containing "diamond" topologies. With the advent and use of the BranchPythonOperator, we lost that ability, since every downstream node is labeled as skipped. I think the support of branching sets Airflow apart from other workflow engines, but it does require support for joining, as shown above. If this were available I would use it instead of the 'if-else' templating method, which I feel is less elegant.

  9. changed the title [-]Support a JoinPythonOperator to Parallel the BranchPythonOperator Already Supported[/-] [+]Support a JoinPythonOperator to Work with the BranchPythonOperator Already Supported[/+] on Jul 25, 2015
  10. mistercrunch commented on Jul 27, 2015

    @mistercrunch
    Member

    Sounds right. We'd need the skipped state to propagate downstream until it hits a JoinOperator of some sort.

  11. azaroth42 commented on Aug 14, 2015

    @azaroth42

    👍

  12. mistercrunch commented on Aug 14, 2015

    @mistercrunch
    Member

    Joining will be possible using any operator once this PR is merged!
    #261

  13. r39132 commented on Aug 14, 2015

    @r39132
    ContributorAuthor

    👍

  14. smnaveen commented on Aug 24, 2015

    @smnaveen

    Btw, when attempting to use the branch operator, I am getting a "import error" on the line trying to import the branch python operator. (from airflow.operators import BranchPythonOperator)

    i am using v1.1.1. is the branch operator only available in a specific version or release of airflow ?

  15. artwr commented on Aug 24, 2015

    @artwr
    Contributor

    @smnaveen the operator showed up in 1.2.0 if I am correct, but I would recommend using the latest release (There have been a decent amount of bug fixes since)

  16. r39132 commented on Nov 21, 2015

    @r39132
    ContributorAuthor

    I finally got to a point where I needed to use this.. excellent!

    Thanks!

    screenshot 2015-11-20 23 41 20

  17. added a commit that references this issue on Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions