Asset watcher triggers shared by several assets can't use the asset state store
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:
KinesisTriggerandIcebergTableSnapshotTriggercatch theValueErrorand 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
S3KeyUpdateTriggerin #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 byBaseEventTrigger.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_accessorraises when there is more than one.
Some possible directions:
- Give event triggers a store that works when shared, e.g.
setwrites to every watched asset andgetreturns the value only if they all agree. - Let the trigger see which assets it watches, so it can pick.
- 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)
can I work on this?
Go for it @simar-rekhi
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?
Commenting to follow along.
+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?
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.
@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 go ahead!
Thanks @seanghaeli , please pull @amoghrajesh in the loop. He created the asset state store
@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".
https://github.com/apache/airflow/pull/75521 <- draft implementation of this approach
Hey folks, coming in late.
I think I agree with this approach.
setsaves the value on every asset.getreads 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.
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
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!
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.
@amoghrajesh
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:
setanddeletewrite to every asset the trigger watches.getreturns the value from the first asset (sorted by name) that has the key.clearstill 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 ....?
-
getonly when all assets agree: a newly linked asset starts empty, so the assets will never agree.getreturns 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:
- The asset state store concept has a promise that "Values written by the trigger are visible to any task that declares
my_assetas 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. - 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.
- The asset state store concept has a promise that "Values written by the trigger are visible to any task that declares
Alright, lets go with that then, @seanghaeli can you then update your PR to reflect these changes?
Will do
Re-opened https://github.com/apache/airflow/pull/75521 , thoughts @vincbeck @amoghrajesh ?