vincbeck opened a new pull request, #77548:
URL: https://github.com/apache/airflow/pull/77548
## Why
Each time the scheduler looks for task instances to queue, it counts the
active ones for `max_active_tasks`,
`max_active_tis_per_dag` and `max_active_tis_per_dagrun`. It does that with
one query grouped by Dag, task, Dag run
and state, which returns about one row per active task instance. With 70,000
running task instances, the scheduler
pulls about 70,000 rows into Python each time just to add them up, although
the per-task counts are only needed for
Dags with task-level limits. In a laptop profile this query was 13% of the
scheduler's time.
## What changes
- The running and queued task instances per Dag run (for `max_active_tasks`)
are counted by the database: one row
per Dag run, 700 instead of 70,000 in the example above. On MySQL the
query carries a `USE INDEX (ti_state)` hint,
because MySQL otherwise sometimes scans every task instance (1.1-1.3 s).
- The per-task counts are loaded only for the candidate Dags that have
task-level limits, with at most one query
per candidate query.
- Given the same database state, every limit decision is the same as before.
`ConcurrencyMap` is internal and is
replaced without a shim.
## Results
`main` is `bada0686ef`. The queries alone ran on local containers (70,000
running, 500,000 finished task
instances); end to end ran on a laptop (4 schedulers, real `CeleryExecutor`,
PostgreSQL 17, 200,000 tasks of 2 to 6
minutes) and on AWS (2 schedulers, real `AwsEcsExecutor` on Fargate, RDS
PostgreSQL 14, 10,000 tasks).
| | main | this PR | change |
|---|---:|---:|---:|
| Count query, fetched into Python (PostgreSQL 17) | 114 ms, 70,000 rows |
18 ms, 700 rows | **-84%** |
| Count query (MySQL 8.0) | 266 ms | 130 ms | **-51%** |
| `critical_section_duration` p50 (laptop, Celery) | 183 ms | 62 ms | **3x
shorter** |
| Selection query p95 (AWS, ECS) | 141-143 ms | 117 ms | **-17%** |
| Task limits on every task, 1 scheduler, all 70,000 running (laptop) | 618
s | 606 s | no slower |
End to end, the time to finish does not change (1,347 s against 1,355 s on
the laptop, the same 99% time on ECS):
the tasks run for minutes, so the scheduler is not the limit there.
## Trade-offs
- More round trips when nothing can be selected: each retry that brings new
Dags with task-level limits runs one
per-task query. For example, 6 Dags at their task limit with a batch of 1
run 6 per-task queries, where `main` ran
its count query once.
- Worst case: a cluster policy sets task-level limits on every Dag, and a
few large Dags hold most active task
instances. The per-task queries then return about as many rows as the old
query. The last row of the table is
this case: no slower end to end.
## Tests
New tests cover the per-Dag-run counts, the per-Dag load of the task counts
on first use, the MySQL hint, and at
most one per-task query per candidate query. The existing concurrency-limit
tests pass unchanged, and the scheduler
tests pass on SQLite, PostgreSQL and MySQL.
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes (please specify the tool below)
Generated-by: Claude Code (Opus 5.5) following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
---
* Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information. Note: commit author/co-author name and email in commits
become permanently public when merged.
* For fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License
Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment:
`{pr_number}.significant.rst`, in
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
You can add this file in a follow-up commit after the PR is created so you
know the PR number.
--
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]