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


##########
airflow-core/src/airflow/jobs/scheduler_job_runner.py:
##########
@@ -1031,7 +1031,12 @@ def _build_schedulable_tis_query(
                 isouter=True,
             )
             
.where(func.coalesce(dr_task_concurrency_subquery.c.task_per_dr_count, 0) < 
DM.max_active_tasks)
-            .order_by(-TI.priority_weight, DR.logical_date, TI.region_index)
+            # Asset-triggered dag runs have logical_date=NULL, so coalesce to 
run_after
+            # (never NULL; the asset event time for such runs) and tiebreak on 
DR.id to
+            # keep the ordering — and thus which rows reach the LIMIT — 
deterministic.
+            .order_by(
+                -TI.priority_weight, func.coalesce(DR.logical_date, 
DR.run_after), DR.id, TI.region_index

Review Comment:
   Neither added test exercises the new `DR.id` tie-breaker: their effective 
run dates differ, so removing this key would leave both tests passing. Please 
add a regression case with equal effective dates and priorities, with region 
indexes ordered opposite to the numeric Dag run IDs, and assert that the lower 
`DR.id` is queued first. This should verify both the tie-breaker and its 
precedence over `region_index`.



##########
airflow-core/tests/unit/jobs/test_scheduler_job.py:
##########
@@ -2304,6 +2304,88 @@ def 
test_find_executable_task_instances_order_logical_date(self, dag_maker):
         assert [ti.key for ti in res] == [tis[1].key]
         session.rollback()
 
+    def 
test_find_executable_task_instances_order_run_after_asset_triggered(self, 
dag_maker):
+        """
+        Asset-triggered dagruns have logical_date=None, so the scheduler falls 
back to
+        run_after (the asset event time) to keep them FIFO. The run with the 
earliest
+        run_after must be queued first, regardless of creation order.
+        """
+        dag_id_1 = 
"SchedulerJobTest.test_find_executable_task_instances_order_run_after_asset-a"
+        dag_id_2 = 
"SchedulerJobTest.test_find_executable_task_instances_order_run_after_asset-b"
+        task_id = "task-a"
+        session = settings.Session()
+        with dag_maker(dag_id=dag_id_1, max_active_tasks=16, session=session):
+            EmptyOperator(task_id=task_id)
+        dr1 = dag_maker.create_dagrun(
+            run_type=DagRunType.ASSET_TRIGGERED,
+            logical_date=None,
+            run_after=DEFAULT_DATE + timedelta(hours=2),
+        )
+
+        with dag_maker(dag_id=dag_id_2, max_active_tasks=16, session=session):
+            EmptyOperator(task_id=task_id)
+        dr2 = dag_maker.create_dagrun(
+            run_type=DagRunType.ASSET_TRIGGERED,
+            logical_date=None,
+            run_after=DEFAULT_DATE + timedelta(hours=1),
+        )
+
+        dr1 = session.merge(dr1, load=False)
+
+        scheduler_job = Job()
+        self.job_runner = SchedulerJobRunner(job=scheduler_job)
+
+        tis = dr1.task_instances + dr2.task_instances
+        for ti in tis:
+            ti.state = State.SCHEDULED
+            session.merge(ti)
+        session.flush()
+
+        res = self.job_runner._select_task_instances_to_queue(1, 
make_pool_stats(), set(), session=session)
+        session.flush()
+        assert [ti.key for ti in res] == [tis[1].key]
+        session.rollback()
+
+    def 
test_find_executable_task_instances_order_mixed_asset_triggered_and_dated(self, 
dag_maker):
+        """
+        A mix of dated and asset-triggered dagruns interleaves by
+        coalesce(logical_date, run_after) rather than one run type 
categorically
+        preceding the other (which NULL logical_date otherwise causes,
+        backend-dependently). The asset-triggered run with the earlier 
run_after
+        must beat the dated run with the later logical_date.
+        """
+        dag_id_1 = 
"SchedulerJobTest.test_find_executable_task_instances_order_mixed-a"
+        dag_id_2 = 
"SchedulerJobTest.test_find_executable_task_instances_order_mixed-b"
+        task_id = "task-a"
+        session = settings.Session()
+        with dag_maker(dag_id=dag_id_1, max_active_tasks=16, session=session):
+            EmptyOperator(task_id=task_id)
+        dr1 = dag_maker.create_dagrun(logical_date=DEFAULT_DATE + 
timedelta(hours=2))
+
+        with dag_maker(dag_id=dag_id_2, max_active_tasks=16, session=session):
+            EmptyOperator(task_id=task_id)
+        dr2 = dag_maker.create_dagrun(
+            run_type=DagRunType.ASSET_TRIGGERED,
+            logical_date=None,
+            run_after=DEFAULT_DATE + timedelta(hours=1),
+        )

Review Comment:
   This mixed-run case also passes with the old ordering on SQLite and MySQL: 
ascending `logical_date` already puts the NULL asset-triggered run before the 
dated run, regardless of `run_after`. Please parameterize both relative date 
orders and assert that the dated run wins when its logical date is earlier. 
Otherwise the test does not detect removal of the fix on those backends.



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