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

   In each scheduling pass of a Dag run, `_get_ready_tis` checks every 
schedulable task instance against one `DepContext`. That context stores the 
finished task instances of the run. `TriggerRuleDep` and 
`NotPreviouslySkippedDep` need the finished task instances of only a few task 
ids, such as the direct upstreams, the setups in scope or the `SkipMixin` 
parents. To find them, they iterated over the whole finished list for each task 
instance.
   
   So the CPU time of a scheduling pass grows with the number of schedulable 
task instances times the number of finished ones. In a large Dag run this scan 
takes most of the pass, even though it does not query the database. The 
scheduler loop examines Dag runs one after another, so every other Dag run in 
the loop waits for it.
   
   ## How it works
   
   `DepContext` now indexes the finished task instances by task id once per 
pass, next to the per-pass memos from #67672. `TriggerRuleDep` (direct 
upstreams, setup constraint, teardown scope), `NotPreviouslySkippedDep` and 
`_mapped_group_skip_decisions` look up only the ids they need and apply the 
same filters as before.
   
   - **The index follows the list it was built from.** `DepContext` rebuilds it 
when the current `finished_tis` is a different list. A context created without 
`finished_tis` fetches the list lazily, and each `UP_FOR_RESCHEDULE` copy that 
`are_dependencies_met` makes with `attrs.evolve` fetches its own. So each copy 
reads the index of its own list. The field is `init=True` for the reason given 
on `upstream_task_id_counts`.
   - **Invalidation.** `_get_ready_tis` replaces finished entries with 
`REMOVED` copies in place (#71737) and drops the index right after. This is a 
separate method because `invalidate_upstream_task_id_counts` is not called when 
a mapped task only shrinks. 
`test_mapped_task_length_reduction_marks_removed_for_downstream_in_same_pass` 
covers that path.
   - **Same decisions.** For each task id, the index gives the same objects as 
the full scan, in the same order. The readers count states or key by `(task_id, 
map_index)`, so the result does not depend on the order of the ids.
   
   ## Known limitation
   
   A task instance downstream of a mapped task still reads every finished 
instance of that task id and filters them by map index. So a mapped task that 
feeds a mapped task of the same size still costs N×N per stage. Only the scans 
of the other task ids go away. A follow-up can memoize the upstream state 
counts per set of upstream ids, as #67672 does for the count query.
   
   ## Tests
   
   `test_dep_context.py` counts how many times a pass iterates over the 
finished list. With this change the count is the same for 2 and 20 downstream 
task instances, with and without `UP_FOR_RESCHEDULE`. On main it grows with the 
number of task instances. A second test reuses a lazy context for two 
`UP_FOR_RESCHEDULE` task instances. It checks that the second one sees an 
upstream that finished between the two evaluations.
   
   The `ti_deps` tests, `test_dagrun.py`, `test_taskinstance.py`, 
`test_mappedoperator.py`, `test_dynamic_region.py` and `test_scheduler_job.py` 
pass unchanged in Breeze on SQLite.
   
   ## Benchmark
   
   The benchmark times one `task_instance_scheduling_decisions` call on 
generated Dags. Each Dag is a set of task groups, and each group is a chain of 
`EmptyOperator` tasks. One more Dag chains mapped tasks.
   
   The runs used Breeze on SQLite and alternated main and this PR over 5 
rounds. Each value is the median over the rounds of the median of 5 passes, 
with GC disabled while timing. Both sides made the same scheduling decisions 
for every Dag. A control query, `get_task_instances` of the run, moved by at 
most 2%.
   
   | Dag | Unfinished / finished | main | This PR | Speed-up |
   |---|---|---:|---:|---:|
   | 10 groups × 10 tasks, 5 finished per chain | 50 / 50 | 7.7 ms | 7.5 ms | 
1.0× |
   | 30 × 34, 17 finished | 510 / 510 | 88 ms | 62 ms | 1.4× |
   | 100 × 80, 2 finished | 7,800 / 200 | 1.19 s | 1.03 s | 1.2× |
   | 100 × 80, 40 finished | 4,000 / 4,000 | 2.17 s | 0.56 s | 3.9× |
   | 100 × 80, 78 finished | 200 / 7,800 | 257 ms | 85 ms | 3.0× |
   | 200 × 100, 50 finished | 10,000 / 10,000 | 12.65 s | 1.49 s | 8.5× |
   | 9 mapped stages × 300, 5 finished | 1,200 / 1,500 | 399 ms | 247 ms | 1.6× 
|
   
   The benchmark script is not part of this PR. I can share it if that helps.
   
   related: #67672, #71737
   
   ## Backport
   
   Can a maintainer add the `backport-to-v3-4-test` label and the 3.4.1 
milestone? `v3-4-test` does not have #71737 yet. If #71737 goes to `v3-4-test` 
first, this PR cherry-picks with the invalidation. Otherwise I will open a 
manual backport without it.
   
   ---
   
   ##### 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]

  • [PR] Speed up dependency check... amdubovoi (via GitHub) via commits

Reply via email to