vincbeck opened a new pull request, #77550:
URL: https://github.com/apache/airflow/pull/77550
## Why
When a scheduler dies, another one adopts or resets its task instances in
one transaction
(`adopt_or_reset_orphaned_tasks`). That transaction sent about 32 statements
per reset task instance, mostly lazy
loads of columns the orphan query did not load, and one UPDATE per adopted
one. With thousands of orphans it lasts
minutes: the scheduler running it schedules nothing, and other schedulers
whose orphan check fires meanwhile wait
on the dead job's row. On AWS,
resetting 32,768 orphans took one call of 795 s.
## What changes
- The orphan query loads every task instance column except
`executor_config`, `next_method` and `next_kwargs`, and
their Dag runs from the join it already has. The scheduler's orphan code
no longer lazy-loads anything per task
instance.
- A new private `TaskInstance._reset_orphaned_attempts` resets the task
instances the executors declined in
batches of 250: one locked `SELECT`, one archive `UPDATE`, and multi-row
INSERTs for the new attempts and their
notes. It writes the same rows as `prepare_db_for_next_try`.
- Adoption is recorded with one UPDATE per batch of 250.
- The orphan query keeps the same joins, filters and row locks, so
schedulers with and without this change can run
side by side during a rolling upgrade.
## Results
`main` is `bada0686ef`. On a laptop: PostgreSQL 17, 2 schedulers, one killed
with 16,000 task instances queued. On
AWS: RDS PostgreSQL 14, 2 schedulers with the real `AwsEcsExecutor` on
Fargate, one killed with about 940 task
instances.
| | main | this PR | change |
|---|---:|---:|---:|
| Reset 16,000 orphans, executor that adopts nothing (laptop) | 615 s | 5.5
s | **110x faster** |
| Survivor's longest stretch without a finished loop, same run | 617 s | 6 s
| **-99%** |
| Adopt 16,000 orphans, `CeleryExecutor` (laptop) | 338 s | 288 s | **-15%**
|
| Adopt or reset about 940 orphans, `AwsEcsExecutor` (AWS) | 11.3 s | 5.3 s
| **-53%** |
Most of the remaining 288 s on Celery is
`CeleryExecutor.try_adopt_task_instances` reading each task's result one
at a time, which a provider follow-up could fix.
## Trade-offs
- The reset is batched but stays in one transaction, unlike the
batch-and-commit rule in `AGENTS.md`: the row
locks of the orphan query are what keep another scheduler from handling
the same task instances. The PR makes
that transaction short instead.
- The bulk reset duplicates the SQL that `prepare_db_for_next_try` uses to
write the rows. A test compares both
paths column by column.
- A backport to `v3-4-test` must add `from airflow.utils.helpers import
chunks` to `taskinstance.py` (`main` has it
since #76806), and has one context conflict in `scheduler_job_runner.py`.
## Tests
New tests compare the bulk reset with `prepare_db_for_next_try` column by
column, and cover several batches, an
attempt archived meanwhile, the one UPDATE per adoption batch, and a
statement count that no longer grows with the
number of orphans (on `main`, 35 statements for 1 orphan and 483 for 15).
The scheduler and task instance 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)
--
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]