Nataneljpwd commented on code in PR #64294:
URL: https://github.com/apache/airflow/pull/64294#discussion_r3282898745


##########
airflow-core/src/airflow/config_templates/config.yml:
##########
@@ -2568,6 +2568,17 @@ scheduler:
       type: integer
       default: "20"
       see_also: ":ref:`scheduler:ha:tunables`"
+    max_new_dagruns_per_loop_to_schedule:
+      description: |
+        How many NEW dagruns should be scheduled and examined (and locked) 
when scheduling
+        and queuing tasks.
+        If set, select `max_dagruns_per_loop_to_schedule` old dagruns (that 
have been
+        examined before)
+        And `max_new_dagruns_per_loop_to_schedule` new dagruns (that have not 
yet been examined).
+      example: ~
+      version_added: 3.2.2
+      type: integer
+      default: "0"

Review Comment:
   ok, done



##########
airflow-core/src/airflow/config_templates/config.yml:
##########
@@ -2568,6 +2568,17 @@ scheduler:
       type: integer
       default: "20"
       see_also: ":ref:`scheduler:ha:tunables`"
+    max_new_dagruns_per_loop_to_schedule:
+      description: |
+        How many NEW dagruns should be scheduled and examined (and locked) 
when scheduling
+        and queuing tasks.
+        If set, select `max_dagruns_per_loop_to_schedule` old dagruns (that 
have been
+        examined before)
+        And `max_new_dagruns_per_loop_to_schedule` new dagruns (that have not 
yet been examined).
+      example: ~

Review Comment:
   done



##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -628,27 +642,61 @@ def get_running_dag_runs_to_examine(cls, session: 
Session) -> ScalarResult[DagRu
         from airflow.models.backfill import BackfillDagRun
         from airflow.models.dag import DagModel
 
-        query = (
-            select(cls)
-            .with_hint(cls, "USE INDEX (idx_dag_run_running_dags)", 
dialect_name="mysql")
-            .where(cls.state == DagRunState.RUNNING)
-            .join(DagModel, DagModel.dag_id == cls.dag_id)
-            .join(BackfillDagRun, BackfillDagRun.dag_run_id == DagRun.id, 
isouter=True)
-            .where(
-                DagModel.is_paused == false(),
-                DagModel.is_stale == false(),
-            )
-            .order_by(
-                nulls_first(cast("ColumnElement[Any]", 
BackfillDagRun.sort_ordinal), session=session),
-                nulls_first(cast("ColumnElement[Any]", 
cls.last_scheduling_decision), session=session),
-                cls.run_after,
+        def _get_dagrun_query(
+            filters: list[ColumnElement[bool]], order_by: 
list[SQLColumnExpression[Any]], limit: int
+        ):
+            return (
+                select(DagRun)
+                .with_hint(DagRun, "USE INDEX (idx_dag_run_running_dags)", 
dialect_name="mysql")
+                .where(DagRun.state == DagRunState.RUNNING)
+                .join(DagModel, DagModel.dag_id == cls.dag_id)
+                .join(BackfillDagRun, BackfillDagRun.dag_run_id == DagRun.id, 
isouter=True)
+                .where(*filters)
+                .order_by(*order_by)
+                .limit(limit)
             )
-            .limit(cls.DEFAULT_DAGRUNS_TO_EXAMINE)
+
+        filters = [
+            DagRun.run_after <= func.now(),
+            DagModel.is_paused == false(),
+            DagModel.is_stale == false(),
+        ]
+
+        order = [
+            nulls_first(cast("ColumnElement[Any]", 
BackfillDagRun.sort_ordinal), session=session),
+            nulls_first(cast("ColumnElement[Any]", 
DagRun.last_scheduling_decision), session=session),
+            DagRun.run_after,
+        ]
+
+        new_dagruns_to_examine = max(cls.DEFAULT_NEW_DAGRUNS_TO_EXAMINE, 0)
+        dagruns_to_examine = cls.DEFAULT_DAGRUNS_TO_EXAMINE
+
+        query = _get_dagrun_query(
+            filters=filters
+            if new_dagruns_to_examine == 0
+            else [*filters, DagRun.last_scheduling_decision.is_not(None)],

Review Comment:
   done



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