Copilot commented on code in PR #76214:
URL: https://github.com/apache/airflow/pull/76214#discussion_r4744382910


##########
airflow-core/src/airflow/models/trigger.py:
##########
@@ -415,27 +416,34 @@ def submit_event(
         ):
             handle_event_submit(event, task_instance=task_instance, 
session=session, dag_bag=dag_bag)
 
-        # Send an event to assets
-        trigger = session.scalars(
-            select(cls)
-            .where(cls.id == trigger_id)
-            
.options(selectinload(cls.asset_watchers).selectinload(AssetWatcherModel.asset))
-        ).one_or_none()
-        if trigger is None:
-            # Already deleted for some reason
-            return
-        for asset in trigger.assets:
+        # Query by trigger_id rather than loading the Trigger: its 
lazy="selectin" task_instance
+        # relationship would add an unused task_instance query (joined to 
dag_run) to every event.
+        assets = session.scalars(
+            select(AssetModel)
+            .join(AssetWatcherModel, AssetWatcherModel.asset_id == 
AssetModel.id)
+            .where(AssetWatcherModel.trigger_id == trigger_id)
+        )
+        for asset in assets:
             AssetManager.register_asset_change(
                 asset=asset.to_serialized(),
                 extra={"from_trigger": True, "payload": event.payload},
                 partition_key=event.partition_key if isinstance(event, 
AssetTriggerEvent) else None,
                 session=session,
                 callback_sink=callback_sink,
             )
-        if trigger.callback:
-            trigger.callback.handle_event(event, session)
+
+        callback = session.scalar(
+            select(TriggererCallback).where(TriggererCallback.trigger_id == 
trigger_id).limit(1)
+        )
+        if callback is not None:
+            callback.handle_event(event, session)
         # Recorded for every kind of trigger; written with the rest of this 
transaction.
-        trigger.last_event_at = timezone.utcnow()
+        session.execute(
+            update(cls)
+            .where(cls.id == trigger_id)
+            .values(last_event_at=timezone.utcnow())
+            .execution_options(synchronize_session=False)

Review Comment:
   Disabling session synchronization leaves any already-loaded 
`Trigger.last_event_at` unchanged after this call, unlike the previous ORM 
assignment. Airflow sessions use `expire_on_commit=False` (`settings.py:514`), 
so even committing does not repair the stale value; a subsequent 
`session.get(Trigger, trigger_id)` can still return the old timestamp. Use 
`synchronize_session="evaluate"` for this primary-key update to preserve 
identity-map consistency without adding a SELECT, and cover a caller-supplied 
session that retains the trigger without expiring it.



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