vincbeck opened a new pull request, #77546:
URL: https://github.com/apache/airflow/pull/77546
## Why
The scheduler holds its critical-section lock (only one scheduler at a time)
until the whole batch has been handed
to the executors. That includes building each task's workload: signing a JWT
per task instance, reading what the
log address needs (region rows for mapped and looped tasks), and the
executor's `queue_workload()`. With RSA 2048
keys and a batch of 512, signing alone holds the lock for about 300 ms, and
the other schedulers skip their turn
meanwhile.
## What changes
- The scheduler commits the QUEUED state first, which releases the lock,
then builds the workloads and hands them
to the executors in a second transaction. The workloads themselves are
unchanged.
- The second transaction starts with a plain `SELECT` of the batch's task
instances still QUEUED by this scheduler,
and builds workloads only for those, so task instances changed or deleted
in between are skipped. It never
waits on row locks.
- If building or handing over fails, the scheduler rolls back, resets its
still-QUEUED task instances to SCHEDULED
(`SKIP LOCKED`, so it does not wait either) and removes their workloads
from the executors' queues. After a lock
error it goes on with the next loop; any other error stops it, as before.
## Results
`main` is `bada0686ef`. Laptop, PostgreSQL 17, 4 schedulers, real
`CeleryExecutor`, 70,000 waiting task
instances, `max_tis_per_query` 512.
| | main | this PR | change |
|---|---:|---:|---:|
| `critical_section_duration` p50, RSA 2048 keys | 1,159 ms | 337 ms |
**-71%** |
| `critical_section_busy` per run, RSA 2048 keys | 452 | 328 | **-27%** |
| All 70,000 running, RSA 2048 keys | 262 s | 252 s | **-4%** |
| All 70,000 running, shared secret (default) | 232 s | 240 s (231-249) | no
measurable change |
On AWS with the `AwsEcsExecutor` (2 schedulers, shared secret, unmapped
tasks) there is no measurable change: the
gain is for large batches, RSA keys, mapped or looped tasks, and several
schedulers.
## Trade-offs
- Two transactions instead of one. A scheduler killed between the two
commits (a window of about 1 ms to a few
hundred ms) leaves the batch QUEUED with no workload sent. It is recovered
as today when a scheduler dies after
its commit: adoption or reset, then `[scheduler] task_queued_timeout`.
- Writes an executor makes in `queue_workload()` (the `EdgeExecutor`'s
`edge_job` rows, and third-party executors)
now commit in the second transaction, not with the QUEUED state. The
`queue_workload()` docs now say it must only
buffer. No newsfragment added; I can add one if maintainers want it.
- One more `SELECT` and commit per loop. `critical_section_duration` drops,
as it no longer includes the build.
## Tests
New tests cover the commit before the build, the reset on failure (lock
errors, other errors, shutdown, several
executors), task instances changed or deleted between the two transactions,
and not waiting on locked rows
(PostgreSQL, MySQL). 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]