buhuipao opened a new pull request, #77761:
URL: https://github.com/apache/airflow/pull/77761

   The scheduler examines running Dag runs from one queue for all Dags. Each 
loop takes the `max_dagruns_per_loop_to_schedule` running Dag runs that were 
examined least recently (20 by default). When one Dag has thousands of running 
Dag runs, it takes most of every batch, and the Dag runs of every other Dag 
wait behind it for a full round.
   
   This adds the `[scheduler] max_dagruns_per_dag_per_loop` option and 
`DagRun.get_running_dag_runs_per_dag`. When the option is set to K, 
`_do_scheduling` runs a second query right after the usual one. It locks up to 
K more running Dag runs of each Dag, up to `max_dagruns_per_loop_to_schedule` 
in total. It takes Dags least recently examined first, and in each Dag the Dag 
runs examined least recently. The option is 0 (off) by default, so nothing 
changes unless it is set.
   
   Notes for review:
   
   - A Dag's last examination is the newest `last_scheduling_decision` among 
its running Dag runs. So a Dag that was just examined moves behind the others. 
Ordering Dags by their oldest Dag run does not do this: a Dag with a big 
backlog keeps an old Dag run until its whole backlog is examined, so it would 
stay first.
   - The per-Dag subquery is `LATERAL` and holds the row lock. Dag runs that 
another scheduler holds are skipped before the per-Dag limit, so a second 
scheduler gets the next Dag runs of the same Dag. A window function cannot do 
this, because Postgres does not allow row locks with window functions.
   - SQLite has no `LATERAL`, so the option has no effect there. The asset 
events route already skips its `LATERAL` query on SQLite in the same way.
   - Runs picked by the first query are excluded by id. A session's own row 
locks do not make `SKIP LOCKED` skip them, so without this the same run would 
be examined twice in one loop.
   - The new method has no `@retry_db_transaction`. Its rollback would release 
the row locks that the first query took in the same transaction.
   - When the option is set, one loop can examine up to twice 
`max_dagruns_per_loop_to_schedule` Dag runs. The config description and the 
scheduler docs say so.
   - On MySQL 8.0, the existing query returned 3 of 16 running Dag runs and 
locked all 16, and the new query locked every running Dag run of the Dags it 
visited. The existing query runs first in the same transaction, so the new 
query adds no locks there.
   - #76017 adds another query at the same place in `_do_scheduling`. The two 
changes do not depend on each other. The one that merges second needs a small 
rebase.
   
   #### Effect
   
   Simulated scheduler loops on Postgres: 10,000 running Dag runs, 8,000 of one 
busy Dag and 20 of each of 100 other Dags. Each loop runs the queries and sets 
`last_scheduling_decision` of the Dag runs it got. 1,500 loops per row. The 
last two columns show how many loops pass between two examinations of the same 
Dag run (median).
   
   | | Dag runs examined per loop | busy Dag | other Dags |
   |---|---|---|---|
   | `max_dagruns_per_loop_to_schedule = 20` | 20 | 500 | 500 |
   | `max_dagruns_per_loop_to_schedule = 40` | 40 | 250 | 250 |
   | `max_dagruns_per_loop_to_schedule = 20`, `max_dagruns_per_dag_per_loop = 
4` | 40 | 400 | 100 |
   
   With the same number of Dag runs examined per loop, the Dag runs of the 
other Dags are examined 2.5 times as often, and those of the busy Dag 1.6 times 
less often.
   
   #### Query cost
   
   Postgres 15 in Breeze (arm64), the same data plus 20,000 finished Dag runs, 
`max_dagruns_per_dag_per_loop = 4`. 9 alternating runs; medians:
   
   | | median | Dag runs returned |
   |---|---|---|
   | base: `get_running_dag_runs_to_examine` | 6.78 ms | 20 |
   | this PR: both queries | 10.78 ms | 40 |
   | new query alone | 4.48 ms | 20 |
   | new query alone, when it visits the busy Dag | 7.86 ms | 20 |
   
   The new query reads every running Dag run once to order the Dags, as the 
existing query does to order the Dag runs. Then it reads the running Dag runs 
of each Dag it visits, which is 8,000 rows for the busy Dag. `EXPLAIN ANALYZE` 
shows that it stops after the Dags it needs (5 of 101) and adds no sort over 
the join.
   
   #### Tests run
   
   - New tests in `test_dagrun.py` and `test_scheduler_job.py`: pass on 
Postgres and MySQL 8.0 (Breeze). On SQLite, a test checks that the new method 
returns nothing.
   - All of `test_dagrun.py` and `test_scheduler_job.py`: 1023 passed, 3 
skipped, 1 xfailed on Postgres; 997 passed, 29 skipped, 1 xfailed on SQLite.
   - `prek` pre-commit hooks for the changed files: pass.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes — Claude Code (Fable 5.2)
   
   Generated-by: Claude Code (Fable 5.2) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


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