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

   ## Why
   
   Every scheduler loop drains each executor's event buffer. Since #73916 
(first released in 3.4.0), each drain copies
   the executor's table of tracked task attempts, builds a reverse map of all 
of it, then walks it again to forget the
   attempts no longer referenced. That is a Python pass over every tracked 
task, on every loop and for every executor,
   even when no task finished. In a laptop profile with 70,000 running tasks, 
it was 10% of the scheduler's time. So
   this is effectively a fix for a performance regression in 3.4.0; whether to 
backport it is the maintainers' call.
   
   ## What changes
   
   - `_register_task` keeps a reverse index, from coordinates (the 
`TaskInstanceKey`) to attempt ids, so an
     event keyed by coordinates resolves with one lookup.
   - The sweep that forgets unreferenced attempts (now 
`_forget_unreferenced_tasks`) only checks the attempts
     registered since the last sweep, and those whose keys left `running`, the 
task queue or the event buffer. It finds
     those keys with a set difference of key copies, which runs in C.
   - Which attempts are known, and which attempt each event is attributed to, 
are the same as before. A randomized
     test checks this against the old full-scan logic after every operation.
   
   ## Results
   
   Micro-benchmark on a laptop: one drain with 50 events, median of 50 drains, 
old logic (`main` at `bada0686ef`)
   against this PR.
   
   | | main | this PR | change |
   |---|---:|---:|---:|
   | 10,000 running tasks, `TaskInstanceKey` keys | 8.8 ms | 0.52 ms | **17x 
faster** |
   | 70,000 running tasks, `TaskInstanceUuid` keys | 47.2 ms | 2.8 ms | **17x 
faster** |
   | 70,000 running tasks, `TaskInstanceKey` keys | 66.5 ms | 2.8 ms | **24x 
faster** |
   
   Not visible end to end in the benchmarks run. On AWS with the 
`AwsEcsExecutor` (2 schedulers, 10,000 tasks), each
   scheduler tracks about 1,000 tasks, where a drain costs under 1 ms either 
way. On a laptop with the
   `CeleryExecutor` (4 schedulers, 200,000 tasks), each tracks 12,000 to 
17,000, and the scan removed is about 1% of a
   1.2 s loop. The gain is for executors tracking tens of thousands of tasks 
each.
   
   ## Trade-offs
   
   - About 10 MB more memory at 70,000 running tasks (6.2 MB reverse index, 4.0 
MB key snapshot), and about 14 MB at
     the peak of a sweep.
   - Executor authors: `get_event_buffer()` and `_register_task` overrides must 
call `super()`, or the tables grow
     without bound, and `running`, the task queue and `event_buffer` must only 
change on the thread that calls
     `get_event_buffer()`. Every executor in the repository already complies; 
the executor docs now state the
     `get_event_buffer()` rules.
   
   ## Tests
   
   New tests check that a drain no longer scans every tracked task, the 
randomized equivalence with the old logic, and
   an attempt missing from the reverse index. The executor and scheduler tests 
pass on SQLite, PostgreSQL and MySQL,
   and so do the Celery, Kubernetes, Amazon and Edge executor tests.
   
   ---
   
   ##### 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]

Reply via email to