Skip to content

Dataset events don't trigger downstream DAG #47398

Closed as duplicate
Closed as duplicate

Description

@dominikdAtStampli

Apache Airflow version

2.10.5

If "Other Airflow 2 version" selected, which one?

No response

What happened?

Hey, it seems that Dataset events are not always triggering downstream DAG, even when there is free capacity in the cluster. I'm writing here, as I'm not sure if this is a bug or an expected behavior of Airflow.

I have a small demo to showcase what I mean. The demo consists of two DAGs, producer & consumer, one creating events on a dataset, and the other one running whenever new dataset events happen. A single run of the producer creates 4 new events, each one should be processed by the consumer DAG. But instead only some not all of those events are processed. On the first run 2 out of 4 events were processed. After the second run of the producer, the consumer was triggered with 3 events, two from the previous producer run, and one from the new batch. Three events are left not being processed at all.

Also it seems that sometimes the event source is missing from in the Airflow Dataset events view.

Image

I don't see anything about those remaining events in the Scheduler's logs.

I noticed this problem originally in Amazon's MWAA service, and then I wanted to check if I can reproduce it in docker Airflow, and indeed I was able to reproduce it in Airflow running in Docker Compose.

What you think should happen instead?

I would expect all dataset events to trigger the downstream DAG.

How to reproduce

  1. Run Airflow in Docker Compose: https://airflow.apache.org/docs/apache-airflow/stable/howto/docker-compose/index.html
  2. Create producer.py DAG:
from datetime import datetime

from airflow import DAG
from airflow.decorators import task
from airflow.datasets import Dataset

START_DATASET = Dataset("DA://start")

with DAG(
    dag_id="producer",
    schedule=None,
    start_date=datetime(2024, 12, 3),
    catchup=False,
) as dag:
    @task
    def generate_data():
        return ["football ⚽", "jogging 🏃", "biking 🚴", "hiking 🥾"]
    
    @task(outlets=[START_DATASET])
    def trigger_down_streams(element: str, **context):
        context["outlet_events"][START_DATASET].extra = {"Activity": element}

    generated_data = generate_data()
    trigger_down_streams.expand(element=generated_data)
  1. Create consumer.py DAG:
from datetime import datetime

from airflow import DAG
from airflow.decorators import task
from airflow.datasets import Dataset

START_DATASET = Dataset("DA://start")

with DAG(
    dag_id="consumer",
    schedule=[START_DATASET],
    start_date=datetime(2024, 12, 3),
    catchup=False,
) as dag:
    @task
    def print_triggering_dataset_events(**context):
        triggering_dataset_events = context.get("triggering_dataset_events")
        print(triggering_dataset_events)
        
    print_triggering_dataset_events()

Operating System

MacOS - using docker compose

Versions of Apache Airflow Providers

No response

Deployment

Docker-Compose

Deployment details

No response

Anything else?

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Mar 5, 2025
  2. boring-cyborg commented on Mar 5, 2025

    @boring-cyborg

    Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.

  3. amolsr commented on Mar 6, 2025

    @amolsr
    Contributor

    Assign this issue to me.

  4. amolsr commented on Mar 6, 2025

    @amolsr
    Contributor

    Can you share the logs of execution for the parent dag?

  5. dominikdAtStampli commented on Mar 6, 2025

    @dominikdAtStampli
    Author

    Hey, I recreated Airflow docker compose environment from scratch with the two DAGs I showed above, and ran the producer DAG once. Below I attach screenshots from the UI with the status of the events & dag runs as well as logs.

    logs.zip

    Image Image
  6. amolsr commented on Mar 6, 2025

    @amolsr
    Contributor

    Hi, what is your requirement here? Are you trying to schedule four different runs on the same dataset? I don’t see a real production use case for this.

    If you're using Mapped Tasks, the dataset has already been segmented, meaning it is no longer a single dataset. You can perform a fork and join operation if you need to trigger a downstream DAG and create a mapped task based on specific business logic. This logic may be the same as the upstream DAG or could differ.

    Currently, Mapped Tasks are not mature enough to efficiently handle fork-and-join scenarios with datasets in between.

  7. dominikdAtStampli commented on Mar 7, 2025

    @dominikdAtStampli
    Author

    Hey, I'm trying to create an event driven architecture in Airflow. Maybe I misunderstood the documentation and this video, but I thought that Airflow is able to trigger DAGs based on incoming dataset events. That it can decide if it wants to batch them into a single DAG execution, or run multiple DAG executions each with a subset of events.

    That way I could send events to a Dataset either from other DAGs or from external systems using Airflow API, and have Airflow process all those events utilizing available capacity.

    But I was surprised to see that pending dataset events don't always trigger downstream DAG, they just hang there and are picked up only after more events are sent. I'm not sure I understand what is the value of such an approach? Is this by design? Or is it an unexpected bug?

  8. amolsr commented on Mar 10, 2025

    @amolsr
    Contributor

    I believe this is intentional for airflow 2.5.2. However, Airflow 3.0 takes a more event-driven approach to scheduling. https://blog.det.life/airflow-3-0-promises-us-real-event-driven-scheduling-55fbf1cb5943

  9. dominikdAtStampli commented on Mar 10, 2025

    @dominikdAtStampli
    Author

    Hey, are you sure about your answer? If so, why is there an API that allows creating dataset events? What is the purpose of creating those events if they are not triggering a downstream DAG that depends on a given dataset?

    Also, is there a stable version of Airflow 3.0 where I could test if that was actually improved in the new release?

  10. ferruzzi commented on Mar 12, 2025

    @ferruzzi
    Contributor

    It looks like the code is commented indicating this is a known and intentional decision, along with a proposed solution that I don't understand yet.

    def _queue_dagruns(cls, dataset_id: int, dags_to_queue: set[DagModel], session: Session) -> None:
        # Possible race condition: if multiple dags or multiple (usually
        # mapped) tasks update the same dataset, this can fail with a unique
        # constraint violation.
        #
        # If we support it, use ON CONFLICT to do nothing, otherwise
        # "fallback" to running this in a nested transaction. This is needed
        # so that the adding of these rows happens in the same transaction
        # where `ti.state` is changed.
    

    https://github.com/apache/airflow/blob/2.10.5/airflow/datasets/manager.py#L167

    Seems like we should be able to change the method to be

    while dags_to_queue:
        if session.bind.dialect.name == "postgresql":
            return cls._postgres_queue_dagruns(dataset_id, dags_to_queue, session)
        return cls._slow_path_queue_dagruns(dataset_id, dags_to_queue, session)
    return
    

    But I don't know. If the solution was that easy it would have been done by now; it would have been faster to fix than to write that .

    There is an email on the dev-list with instructions to try out Airflow 3.0.0beta2, maybe you can use that to see if it has already been sorted out there?

  11. added
    area:data-aware-scheduling
    and removed on Mar 25, 2025
  12. Lee-W commented on Nov 19, 2025

    @Lee-W
    Member

    This was intentional and we're now tracking it through #56750

  13. removed
    needs-triage
    on Nov 19, 2025
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

    area:corearea:data-aware-schedulingkind:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions