uranusjr commented on code in PR #64109:
URL: https://github.com/apache/airflow/pull/64109#discussion_r4639488126


##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -830,37 +846,42 @@ def get_queued_dag_runs_to_set_running(cls, session: 
Session) -> ScalarResult[Da
                 ),
                 isouter=True,
             )
-            .join(Backfill, isouter=True)
+            .subquery()
+        )
+
+        query = (
+            select(cls)
             .join(
-                running_drs,
+                available_dagruns_rn,
+                available_dagruns_rn.c.id == DagRun.id,
+            )
+            .join(
+                DagModel,
                 and_(
-                    running_drs.c.dag_id == DagRun.dag_id,
-                    coalesce(running_drs.c.backfill_id, text("-1"))
-                    == coalesce(DagRun.backfill_id, text("-1")),
+                    DagModel.dag_id == cls.dag_id,
+                    DagModel.is_paused == false(),
+                    DagModel.is_stale == false(),
                 ),
-                isouter=True,
             )
+            .order_by(
+                nulls_first(cast("ColumnElement[Any]", 
cls.last_scheduling_decision), session=session),
+                nulls_first(available_dagruns_rn.c.num_running, 
session=session),
+                DagRun.run_after,
+            )

Review Comment:
   On main, the first sort key is `nulls_first(BackfillDagRun.sort_ordinal)`, 
putting every non-backfill run ahead of every backfill run before the LIMIT is 
applied. `sort_ordinal` is now used only inside the window, which orders runs 
within a single `(dag_id, backfill_id)` partition. This does nothing for 
ranking across partitions.
   
   One possible fix is to select `BackfillDagRun.sort_ordinal` in 
`available_dagruns_rn` and put 
`nulls_first(available_dagruns_rn.c.sort_ordinal)` first. A test where the 
capped candidates exceed the query’s limit is also needed. No current tests 
cover this.



##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -830,37 +846,42 @@ def get_queued_dag_runs_to_set_running(cls, session: 
Session) -> ScalarResult[Da
                 ),
                 isouter=True,
             )
-            .join(Backfill, isouter=True)
+            .subquery()
+        )
+
+        query = (
+            select(cls)
             .join(
-                running_drs,
+                available_dagruns_rn,
+                available_dagruns_rn.c.id == DagRun.id,
+            )
+            .join(
+                DagModel,
                 and_(
-                    running_drs.c.dag_id == DagRun.dag_id,
-                    coalesce(running_drs.c.backfill_id, text("-1"))
-                    == coalesce(DagRun.backfill_id, text("-1")),
+                    DagModel.dag_id == cls.dag_id,
+                    DagModel.is_paused == false(),
+                    DagModel.is_stale == false(),
                 ),
-                isouter=True,
             )
+            .order_by(
+                nulls_first(cast("ColumnElement[Any]", 
cls.last_scheduling_decision), session=session),
+                nulls_first(available_dagruns_rn.c.num_running, 
session=session),
+                DagRun.run_after,
+            )

Review Comment:
   A separate minor issue: This order_by probably should be moved to after 
`.where()` to follow SQL conventions. (SQLAlchemy doesn’t actually care, so 
this is more of a readability issue.)



##########
airflow-core/tests/unit/jobs/test_scheduler_job.py:
##########
@@ -3015,7 +3015,10 @@ def 
test_find_executable_task_instances_max_active_tis_per_dagrun_deferred(self,
         dag_id = "SchedulerJobTest.test_max_active_tis_per_dagrun_deferred"
         with dag_maker(dag_id=dag_id, max_active_tasks=16, session=session):
             task_a = EmptyOperator.partial(task_id="task_a", 
max_active_tis_per_dagrun=1).expand_kwargs(
-                [{"inputs": 1}, {"inputs": 2}]
+                [
+                    {"inputs": 1},
+                    {"inputs": 2},
+                ]

Review Comment:
   Unrelated?



##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -830,37 +846,42 @@ def get_queued_dag_runs_to_set_running(cls, session: 
Session) -> ScalarResult[Da
                 ),
                 isouter=True,
             )
-            .join(Backfill, isouter=True)
+            .subquery()
+        )
+
+        query = (
+            select(cls)
             .join(
-                running_drs,
+                available_dagruns_rn,
+                available_dagruns_rn.c.id == DagRun.id,
+            )
+            .join(
+                DagModel,
                 and_(
-                    running_drs.c.dag_id == DagRun.dag_id,
-                    coalesce(running_drs.c.backfill_id, text("-1"))
-                    == coalesce(DagRun.backfill_id, text("-1")),
+                    DagModel.dag_id == cls.dag_id,
+                    DagModel.is_paused == false(),
+                    DagModel.is_stale == false(),
                 ),
-                isouter=True,
             )
+            .order_by(
+                nulls_first(cast("ColumnElement[Any]", 
cls.last_scheduling_decision), session=session),
+                nulls_first(available_dagruns_rn.c.num_running, 
session=session),
+                DagRun.run_after,
+            )
+            .join(Backfill, isouter=True)
             .where(
-                # there are two levels of checks for num_running
-                # the one done in this query verifies that the dag is not 
maxed out
-                # it could return many more dag runs than runnable if there is 
even
-                # capacity for 1.  this could be improved.
-                coalesce(running_drs.c.num_running, text("0"))
-                < coalesce(Backfill.max_active_runs, DagModel.max_active_runs),
+                # this check returns strictly only the amount of dagruns
+                # which can run according to the max active runs limit
+                available_dagruns_rn.c.rn
+                <= coalesce(
+                    Backfill.max_active_runs,
+                    DagModel.max_active_runs,
+                    airflow_conf.getint("core", "max_active_runs_per_dag"),

Review Comment:
   This reads conf on every scheduler loop. It should be at the class level 
simialr to DEFAULT_DAGRUNS_TO_EXAMINE.
   
   It is also inconsistent with `_start_queued_dagruns, which treats None as 
unlimited. SQL now caps at the config default, and Python applies no cap. Both 
are defensible, but they should agree.



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