airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Asset watcher triggers shared by several assets can't use the asset state store

Open seanghaeli opened this issue 1 month ago • 20 comments

Apache Airflow version

main (development)

What happened

When two assets declare a watcher with the same trigger class and arguments, the Dag processor stores a single trigger linked to both assets. The triggerer then hands that trigger an asset state store covering every linked asset, and the store's get/set shortcuts (and aget/aset) refuse when there is more than one:

ValueError: Task has 2 concrete inlets and outlets — use context['asset_state_store'][MY_ASSET] to specify which

The trigger can't follow that advice either: it isn't told which assets it watches, and the accessor has no public way to list them. So a shared watcher trigger has no supported way to persist state.

Today this means:

  • KinesisTrigger and IcebergTableSnapshotTrigger catch the ValueError and keep their cursor in memory only, so for a shared trigger every triggerer restart re-reads from the initial position (Kinesis) or re-emits the current snapshot (Iceberg).
  • A trigger that doesn't catch it crashes on its first read and is restarted every couple of seconds without ever firing. That's what S3KeyUpdateTrigger in #74293 does as of now.
  • Every new watcher that wants restart-safe state has to copy the same workaround.

What you think should happen instead

A watcher trigger should be able to persist state whether it serves one asset or several.

How to reproduce

from airflow.providers.amazon.aws.triggers.s3 import S3KeyUpdateTrigger  # any watcher trigger that calls store.get
from airflow.sdk import DAG, Asset, AssetWatcher

def t():
    return S3KeyUpdateTrigger(bucket_name="b", bucket_key="k.txt")

x = Asset("asset_x", watchers=[AssetWatcher(name="wx", trigger=t())])
y = Asset("asset_y", watchers=[AssetWatcher(name="wy", trigger=t())])

with DAG("two_assets", schedule=[x, y]):
    ...

There is one trigger row linked to asset_x and asset_y, and store.get(...) raises on the trigger's first step.

Anything else

Where it comes from:

  • airflow-core/src/airflow/dag_processing/collection.py, add_asset_trigger_references: watchers are deduplicated by BaseEventTrigger.hash(classpath, kwargs).
  • airflow-core/src/airflow/jobs/triggerer_job_runner.py: the trigger gets an accessor built from every asset linked to it.
  • task-sdk/src/airflow/sdk/execution_time/context.py: AssetStateStoreAccessors._single_accessor raises when there is more than one.

Some possible directions:

  1. Give event triggers a store that works when shared, e.g. set writes to every watched asset and get returns the value only if they all agree.
  2. Let the trigger see which assets it watches, so it can pick.
  3. Don't share a trigger across assets for watchers that use the state store.

At the very least, documenting this for trigger authors would stop new triggers from crash-looping.

Are you willing to submit PR?

  • [X] Yes I am willing to submit a PR!

Drafted-by: Claude Code (Opus 5.5)

seanghaeli avatar Oct 21 '26 20:10 seanghaeli

can I work on this?

simar-rekhi avatar Oct 21 '26 21:10 simar-rekhi

Go for it @simar-rekhi

seanghaeli avatar Oct 22 '26 06:10 seanghaeli

I am wondering something. Why does it matter if a trigger is associated to one asset or multiple assets. If 2 assets are sharing the same trigger, what does it change for the trigger. Let's look at it from the other direction and use S3KeyUpdateTrigger for example. S3KeyUpdateTrigger is polling an S3 file to check whether this file has been updated, to do that, it uses the Etag to figure whether the file was modified. I do not understand why it matters whether there is one or 20 assets associated to this trigger, to me it does not. The trigger's mission is to notify us when something happens outside of Airflow, their implementation should be isolated from assets. Maybe @amoghrajesh can help us here?

vincbeck avatar Oct 22 '26 13:10 vincbeck

Commenting to follow along.

jroachgolf84 avatar Oct 22 '26 20:10 jroachgolf84

+1 to @vincbeck. It seems like it was implemented this way because of triggerer re-using the same asset store accessors as tasks so I think we should make a trigger-specific accessor that would allow for multiple assets?

ramitkataria avatar Oct 22 '26 22:10 ramitkataria

Agreed with both of you @vincbeck @ramitkataria , the trigger should not have to care how many assets it's attached to. The reason it does today is that when there are multiple assets, a trigger asks for the ETag, but the state store, having multiple assets, doesn't know which asset's ETag to pass back, so it just errors out. So, it's not really the trigger that needs to know, it's that the state store needs to know. I like the idea of using a trigger-specific accessor to handle this.

seanghaeli avatar Oct 22 '26 23:10 seanghaeli

@simar-rekhi If you have not started, I'm going to reclaim assignment on getting a PR for this. https://github.com/apache/airflow/pull/74293 depends on this, so I want to maintain agency on its completion status.

seanghaeli avatar Oct 22 '26 23:10 seanghaeli

@seanghaeli go ahead!

simar-rekhi avatar Oct 23 '26 01:10 simar-rekhi

Thanks @seanghaeli , please pull @amoghrajesh in the loop. He created the asset state store

vincbeck avatar Oct 23 '26 12:10 vincbeck

@amoghrajesh please let me know if you agree on this approach of implementing a trigger-specific accessor. This way, when several assets share a watcher, set writes to every asset it watches, and get returns the value from the first asset that has the key. Right now, on a get it doesn't know which asset to retrieve the key from, and errors out.

Under this approach, the trigger code remains unchanged and it will be true that the "trigger does not care how many assets are attached to it".

seanghaeli avatar Oct 23 '26 19:10 seanghaeli

https://github.com/apache/airflow/pull/75521 <- draft implementation of this approach

seanghaeli avatar Oct 23 '26 21:10 seanghaeli

Hey folks, coming in late.

I think I agree with this approach.

  • set saves the value on every asset.
  • get reads from the first asset (by name) that has the value.

This is better than asking all assets to match. A new asset starts empty. With this approach, it gets skipped, so the trigger does not fire again for no reason. Trigger code stays the same.

amoghrajesh avatar Oct 26 '26 11:10 amoghrajesh

But we should keep having one asset state store per trigger/asset as opposed to one asset store per trigger? Is there any reason why we want, when a trigger is shared across multiple asset watchers, to have access to multiple access state store? Any use case needs this? If not, I think we should rather update the model than trying to fix it (thanks @seanghaeli for drafting this fix though). @amoghrajesh

vincbeck avatar Oct 26 '26 14:10 vincbeck

I think changing the model is a bit too steep for this @vincbeck ! The only situation where a trigger is sharing multiple assets is in the situation where the source file the assets refer to are equivalent.

Right now, we already have per-asset state stores that ETag the underlying source. The only issue is that each of these state stores don't update in lock-step, and if they did (which #75521 introduces), then we could read ANY of them and they would be equivalent, so we can just pick the first one. I think that's not a bad fix for an edge case as thin as this!

seanghaeli avatar Oct 27 '26 00:10 seanghaeli

I am just challenging the decision of having one asset state store per watcher VS having one asset store per trigger. Why do we need an asset state store per asset? This store is used by the trigger and specific to the trigger implementation, so to me I do not think we need one store per asset. I would like to have @amoghrajesh opinion on this one. I understand this might be a big change but if confirmed, I think we should fix the root cause.

vincbeck avatar Oct 28 '26 20:10 vincbeck

@amoghrajesh

vincbeck avatar Nov 17 '26 16:11 vincbeck

Hey @vincbeck @seanghaeli @jroachgolf84, I thought about this a bit more today, and I am still aligned with the approach in #75521, with one simplification that I have in mind.

My proposal is a state store accessor used only by the triggerer:

  • set and delete write to every asset the trigger watches.
  • get returns the value from the first asset (sorted by name) that has the key.
  • clear still needs exactly one asset, so it doesn't remove keys that tasks wrote.
  • Tasks keep the current accessor and the current error. For a task, two assets really do hold different state, so the error is correct there but not here.

Trigger code doesn't change. S3KeyUpdateTrigger in #74293 works as is, and the Kinesis and Iceberg triggers can drop their except ValueError fallbacks.

Ok coming to the important part: why not the other options ....?

  • get only when all assets agree: a newly linked asset starts empty, so the assets will never agree. get returns the default, and the trigger fires again for a change it already reported.

  • Tell the trigger which assets it watches: the triggerer already has the list today, and it is quite easy to build. But every copy holds the same value, so there is nothing to choose between. Each trigger author would end up writing "pick the first one", which is this fix copied into every provider for not much value.

  • Don't share triggers that use the store: we would poll the same source twice, and Airflow would need to know ahead of time which triggers use the store.

  • One store per trigger: I see the appeal honestly, since an etag describes the s3 object, not the asset. Although what holds me back is:

    1. The asset state store concept has a promise that "Values written by the trigger are visible to any task that declares my_asset as an inlet or outlet, and vice versa." Keeping state on the trigger does not seem right and is overkill for a not-very-generic use case.
    2. A trigger's identity is the hash of its classpath and kwargs. If one kwarg changes, we get a new trigger row and the state is lost. Asset names survive those edits.

    And to add - it also is a schema change. If we still want state scoped to the trigger, maybe lets take it up as its own design discussion and not block this bug on it.

amoghrajesh avatar Nov 20 '26 05:11 amoghrajesh

Alright, lets go with that then, @seanghaeli can you then update your PR to reflect these changes?

vincbeck avatar Nov 20 '26 16:11 vincbeck

Will do

seanghaeli avatar Nov 20 '26 16:11 seanghaeli

Re-opened https://github.com/apache/airflow/pull/75521 , thoughts @vincbeck @amoghrajesh ?

seanghaeli avatar Nov 20 '26 19:11 seanghaeli