Lee-W commented on code in PR #76982:
URL: https://github.com/apache/airflow/pull/76982#discussion_r4638684562


##########
airflow-core/src/airflow/dag_processing/collection.py:
##########
@@ -1452,6 +1452,9 @@ def add_asset_trigger_references(
         refs_to_add: dict[tuple[str, str], set[int]] = {}
         refs_to_remove: dict[tuple[str, str], set[int]] = {}
         triggers: dict[int, dict] = {}
+        # An identical trigger on several assets shares one entry in 
``triggers``, so watcher names
+        # must be tracked per asset.
+        watcher_names: dict[tuple[str, str], dict[int, str]] = {}

Review Comment:
   ```suggestion
           # {(name, uri): {trigger_hash: watcher_name}}
           watcher_names: dict[tuple[str, str], dict[int, str]] = {}
   ```



##########
airflow-core/src/airflow/dag_processing/collection.py:
##########
@@ -1471,12 +1474,22 @@ def add_asset_trigger_references(
                 for trigger in asset_watcher_triggers
             }
             triggers.update(trigger_hash_to_trigger_dict)
+            watcher_names[name_uri] = {
+                trigger_hash: trigger["watcher_name"]
+                for trigger_hash, trigger in 
trigger_hash_to_trigger_dict.items()
+            }
             trigger_hash_from_asset: set[int] = 
set(trigger_hash_to_trigger_dict.keys())
 
             asset_model = assets[name_uri]
-            trigger_hash_from_asset_model: set[int] = {
-                BaseEventTrigger.hash(trigger.classpath, trigger.kwargs) for 
trigger in asset_model.triggers
-            }
+            trigger_hash_from_asset_model: set[int] = set()
+            for watcher in asset_model.watchers:
+                trigger_hash = 
BaseEventTrigger.hash(watcher.trigger.classpath, watcher.trigger.kwargs)
+                trigger_hash_from_asset_model.add(trigger_hash)
+                # Names are otherwise only written when a reference is 
created, so a watcher renamed
+                # in the Dag file (or a wrong name stored by an older version) 
would never be corrected.
+                watcher_name = watcher_names[name_uri].get(trigger_hash)
+                if watcher_name is not None and watcher.name != watcher_name:
+                    watcher.name = watcher_name

Review Comment:
   since the name is now rewritten on every sync, two Dags that declare the 
same asset with an identical trigger but different watcher names make the 
stored name alternate with whichever Dag was synced last (a local run gives 
`name_a`, `name_b`, `name_a`, `name_b`; before this change it stayed `name_a`). 
or maybe we can add a warning? This is something i would consider an edge case 
and doesn't really need to be in this PR. 



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to