uranusjr commented on code in PR #69849:
URL: https://github.com/apache/airflow/pull/69849#discussion_r4691252767
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -339,6 +342,53 @@ def _build_scheduled_asset_event_clause(dag_id: str) ->
ColumnElement[bool]:
)
+def _associate_asset_events_with_dag_run(
+ *, dag_run: DagRun, asset_event_ids_select: Select[tuple[int]], session:
Session
+) -> int:
+ """Associate selected asset events with a Dag run without loading them,
returning the row count."""
+ selected_event_ids = asset_event_ids_select.subquery()
+ result = session.execute(
+ insert(association_table).from_select(
+ ["dag_run_id", "event_id"], select(literal(dag_run.id),
selected_event_ids.c.event_id)
+ ),
+ # SQLAlchemy only keeps rowcount for UPDATE/DELETE by default;
PostgreSQL reports -1 for this INSERT without it.
+ execution_options={"preserve_rowcount": True},
+ )
+ # The Core insert bypasses the ORM collection, which create_dagrun left as
a loaded empty list.
+ session.expire(dag_run, ["consumed_asset_events"])
+ return cast("CursorResult", result).rowcount
+
+
+def _build_consumable_asset_event_clause(
+ dag: SerializedDAG, *, locked_records: Iterable[AssetDagRunQueue]
+) -> ColumnElement[bool]:
+ """
+ Match unconsumed events referenced by a Dag's locked ADRQ rows.
+
+ ADRQ rows are matched up to the largest event ID in *locked_records*.
+ This does not exclude every event queued after the rows were locked.
+ With catchup, events are also matched by asset regardless of ID, and
+ event IDs are not assigned in commit order. Concurrent schedulers can't
+ consume this Dag's events since the Dag's own row is locked.
+ """
+ event_predicate: ColumnElement[bool] = (
+ select(AssetDagRunQueue.asset_event_id)
+ .where(
+ AssetDagRunQueue.target_dag_id == dag.dag_id,
+ AssetDagRunQueue.asset_event_id == AssetEvent.id,
+ AssetDagRunQueue.asset_event_id <= max(record.asset_event_id for
record in locked_records),
+ )
+ .exists()
+ )
+ if dag.catchup:
+ event_predicate |= _build_scheduled_asset_event_clause(dag.dag_id)
+ return and_(event_predicate, ~build_consumed_by_dag_clause(dag.dag_id,
AssetEvent.id))
+
+
+def _has_default_asset_event_grouping(timetable: Timetable) -> bool:
+ return type(timetable).group_asset_events is Timetable.group_asset_events
Review Comment:
This is slightly fragile because functions in Python can be individually
monkey-patched, and in this case it would make the detection flip
unintentionally. It’s _probably_ fine, monkey-patching is always a hack at the
end of the day, but I wonder where we need to call this out.
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2960,9 +3011,13 @@ def _create_dag_runs(self, dag_models:
Collection[DagModel], session: Session) -
session=session,
active_non_backfill_runs=active_runs_of_dags[dag_model.dag_id],
)
- if gated_locked_records is not None:
-
created_run.consumed_asset_events.extend(gated_asset_events)
- session.flush() # Make consumed events visible to ADRQ
cleanup.
+ if gated_asset_event_ids is not None:
+ if TYPE_CHECKING:
+ assert gated_locked_records is not None
+ _associate_asset_events_with_dag_run(
+ dag_run=created_run,
asset_event_ids_select=gated_asset_event_ids, session=session
+ )
+ session.flush()
Review Comment:
Why is this flush needed?
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2999,7 +3054,11 @@ def _collect_gated_asset_events(
if not self._is_asset_condition_met(dag, locked_records=records,
session=session):
self._delete_consumed_asset_records(dag, locked_records=records,
session=session)
return None
- return records, self._select_consumed_asset_events(dag,
locked_records=records, session=session)
+ return records, (
+ select(AssetEvent.id.label("event_id"))
+ .where(_build_consumable_asset_event_clause(dag,
locked_records=records))
+ .order_by(AssetEvent.timestamp.asc(), AssetEvent.id.asc())
+ )
Review Comment:
Since the `select` is now delay-executed, the window it picks up events is
slightly different between gated and non-gated paths. We might need to add an
additional bound.
##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -2960,9 +3011,13 @@ def _create_dag_runs(self, dag_models:
Collection[DagModel], session: Session) -
session=session,
active_non_backfill_runs=active_runs_of_dags[dag_model.dag_id],
)
- if gated_locked_records is not None:
-
created_run.consumed_asset_events.extend(gated_asset_events)
- session.flush() # Make consumed events visible to ADRQ
cleanup.
+ if gated_asset_event_ids is not None:
+ if TYPE_CHECKING:
+ assert gated_locked_records is not None
Review Comment:
These two variables are only used from this point below anyway, so we could
avoid jumping through this loop by moving the unpacking here.
```python
if dag_model.timetable_asset_gated:
if gate := ... is None:
continue
else:
gate = None
...
if gate is not None:
gated_locked_records, gated_asset_event_ids = gate
associate_asset_events_with_dag_run(...)
...
```
--
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]