Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2bce943
added new_dagruns_to_examine configuration
Nataneljpwd Mar 23, 2026
03bacfb
added an option to choose new dagruns to schedule amount
Nataneljpwd Mar 27, 2026
0399261
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Mar 27, 2026
c70108c
separated to 2 queries
Nataneljpwd Mar 27, 2026
c5431b9
removed subquery
Nataneljpwd Mar 27, 2026
36e0514
fixed mypy
Mar 27, 2026
25ced1c
fixed mypy
Mar 27, 2026
bdf59c7
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Mar 27, 2026
920f06f
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Mar 29, 2026
0f6948c
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Mar 31, 2026
eec2612
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Apr 4, 2026
9de6190
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Apr 9, 2026
db00151
Merge branch 'main' of https://github.com/apache/airflow into feature…
Apr 15, 2026
e727f7e
Update airflow-core/tests/unit/models/test_dagrun.py
Nataneljpwd Apr 15, 2026
39b126b
fixed cr comments
Apr 15, 2026
1a7062b
Merge branch 'feature/add-max-new-dagruns-to-schedule' of https://git…
Apr 15, 2026
4fe0a64
Update airflow-core/tests/unit/models/test_dagrun.py
Nataneljpwd Apr 15, 2026
33b88d1
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Apr 18, 2026
fa53835
Update airflow-core/src/airflow/config_templates/config.yml
Nataneljpwd Apr 22, 2026
9289eac
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Apr 22, 2026
1491844
resolve CR comments
May 21, 2026
06261d2
Merge branch 'main' of https://github.com/apache/airflow into feature…
May 21, 2026
316f4af
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd May 27, 2026
28874df
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd May 27, 2026
4007c65
Merge branch 'main' of https://github.com/apache/airflow into feature…
Nataneljpwd Jun 1, 2026
fa873ff
address CR comments
Nataneljpwd Jun 1, 2026
e647963
move the create_dagruns to a fixture
Nataneljpwd Jun 1, 2026
9f9a2e8
Merge branch 'main' of https://github.com/apache/airflow into feature…
Nataneljpwd Jun 3, 2026
f577267
fixed failing tests
Nataneljpwd Jun 7, 2026
3043732
Merge branch 'main' into feature/add-max-new-dagruns-to-schedule
Nataneljpwd Jun 7, 2026
177ee0b
fixed failing tests
Nataneljpwd Jun 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airflow-core/newsfragments/64294.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a new ``max_new_dagruns_per_loop_to_schedule`` configuration to control how many new dagruns are scheduled each scheduler iteration
1 change: 1 addition & 0 deletions airflow-core/newsfragments/64294.significant.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``get_running_dag_runs_to_examine`` now returns a ``Sequence[DagRun]`` type instead of ``ScalarResult[Dagrun]``
13 changes: 13 additions & 0 deletions airflow-core/src/airflow/config_templates/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,19 @@ scheduler:
type: integer
default: "20"
see_also: ":ref:`scheduler:ha:tunables`"
max_new_dagruns_per_loop_to_schedule:
description: |
When set > 0, the scheduler runs a second query per loop that fetches up to this
many dagruns that have never been examined (``last_scheduling_decision IS NULL``),
in addition to the ``max_dagruns_per_loop_to_schedule`` already-examined ones.

This prevents starvation of older dagruns when large batches of new dagruns are
created at once (for example, via ``TriggerDagRunOperator``). Note that the total
number of dagruns locked and scheduled per loop becomes the sum of both limits.
example: ~
version_added: 3.3.0
type: integer
default: "0"
Comment thread
Nataneljpwd marked this conversation as resolved.
use_job_schedule:
description: |
Turn off scheduler use of cron intervals by setting this to ``False``.
Expand Down
93 changes: 72 additions & 21 deletions airflow-core/src/airflow/models/dagrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
Index,
Integer,
PrimaryKeyConstraint,
SQLColumnExpression,
String,
Text,
UniqueConstraint,
Expand All @@ -57,7 +58,15 @@
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.orm import Mapped, declared_attr, joinedload, mapped_column, relationship, synonym, validates
from sqlalchemy.orm import (
Mapped,
declared_attr,
joinedload,
mapped_column,
relationship,
synonym,
validates,
)
from sqlalchemy.orm.exc import StaleDataError
from sqlalchemy.sql.expression import false, select
from sqlalchemy.sql.functions import coalesce
Expand Down Expand Up @@ -319,6 +328,11 @@ class DagRun(Base, LoggingMixin):
"max_dagruns_per_loop_to_schedule",
fallback=20,
)
DEFAULT_NEW_DAGRUNS_TO_EXAMINE = airflow_conf.getint(
"scheduler",
"max_new_dagruns_per_loop_to_schedule",
fallback=0,
)
_ti_dag_versions = association_proxy("task_instances", "dag_version")
_tih_dag_versions = association_proxy("task_instances_histories", "dag_version")

Expand Down Expand Up @@ -623,40 +637,77 @@ def active_runs_of_dags(

@classmethod
@retry_db_transaction
def get_running_dag_runs_to_examine(cls, session: Session) -> ScalarResult[DagRun]:
def get_running_dag_runs_to_examine(cls, session: Session) -> Sequence[DagRun]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things on this signature change:

  1. The docstring below wasn't updated for the new two-query behavior (fetch already-examined + new dagruns separately).
  2. Changing the return type from ScalarResult[DagRun] to Sequence[DagRun] is a breaking change for any downstream caller (custom jobs, plugins, tests outside this repo) that does .all() on the result -- ScalarResult has .all(), a plain list/Sequence doesn't need it. Worth calling out in the docstring and adding a newsfragment.

This PR is also missing a newsfragment for the new max_new_dagruns_per_loop_to_schedule config option.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point 1 here is still open. The docstring (line 641) still describes a single SELECT ... FOR UPDATE query. With max_new_dagruns_per_loop_to_schedule > 0 the method now runs a second FOR UPDATE SKIP LOCKED query for never-examined runs (last_scheduling_decision IS NULL) and adds IS NOT NULL to the first one. A sentence covering that two-query split would close this out. Worth noting that "never examined" also covers cleared-and-requeued runs, since clearing a queued DagRun resets last_scheduling_decision to NULL.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

"""
Return the next DagRuns that the scheduler should attempt to schedule.
Return the next DagRuns (as a list) that the scheduler should attempt to schedule.

This will return zero or more DagRun rows that are row-level-locked with a "SELECT ... FOR UPDATE"
This will return zero or more DagRuns that are row-level-locked with a "SELECT ... FOR UPDATE"
query, you should ensure that any scheduling decisions are made in a single transaction -- as soon as
the transaction is committed it will be unlocked.
With max_new_dagruns_per_loop_to_schedule > 0, this runs 2 queries, one for new dagruns (where
last_scheduling_decision is None) and for old (already examined) dagruns, cleared / requeued dagruns
will appear in the new dagruns query.

:meta private:
"""
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

old_filters = (
[*filters, DagRun.last_scheduling_decision.is_not(None)]
if new_dagruns_to_examine > 0
else filters
)
query = _get_dagrun_query(filters=old_filters, order_by=order, limit=dagruns_to_examine)

query = query.where(DagRun.run_after <= func.now())
result: list[DagRun] = cast(
"list[DagRun]",
session.scalars(with_row_locks(query, of=cls, session=session, skip_locked=True)).unique().all(),
)

if new_dagruns_to_examine > 0:
new_dagruns_query = _get_dagrun_query(
filters=[*filters, DagRun.last_scheduling_decision.is_(None)],
order_by=order,
limit=new_dagruns_to_examine,
)
new_dagruns: Sequence[DagRun] = (
session.scalars(with_row_locks(new_dagruns_query, of=cls, session=session, skip_locked=True))
.unique()
.all()
)

result.extend(new_dagruns)

result = session.scalars(with_row_locks(query, of=cls, session=session, skip_locked=True)).unique()
return result

@classmethod
Expand Down
Loading
Loading