Reduce scheduler spikes for large mapped task expansion - #71967
Draft
hussein-awala wants to merge 1 commit into
Draft
Reduce scheduler spikes for large mapped task expansion#71967hussein-awala wants to merge 1 commit into
hussein-awala wants to merge 1 commit into
Conversation
Large mapped expansions can overload one scheduler pass, increasing scheduling latency and making unrelated Dags wait. This change smooths mapped fan-out handling so high-cardinality expansions do not monopolize scheduler heartbeats, while preserving custom task-instance mutation-hook behavior.
hussein-awala
force-pushed
the
improve-mapped-task-scheduler-performance
branch
from
August 21, 2026 21:42
11762d4 to
f10dce4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Suggested PR title
Improve scheduler performance for large mapped task expansion
Draft PR body
Problem
When a mapped task expands to thousands of task instances in a single shot, scheduler load can spike in one pass.
Current behavior has two expensive patterns:
Bursty same-pass dependency evaluation
DagRun._get_ready_tis, newly expanded mapped task instances are immediately appended toadditional_tisand dependency-checked in the same scheduler pass.Per-row mapped TI creation overhead
TaskMap.expand_mapped_taskandDagRun._revise_map_indexes_if_mappedcreate missing mapped task instances one-by-one via ORM objects, even whentask_instance_mutation_hookis noop.What this PR changes
1) Defer part of expanded mapped TIs to later scheduler passes
airflow-core/src/airflow/models/dagrun.pyDagRun._get_ready_tis[scheduler] max_tis_per_query<= 0:[core] parallelism2) Bulk-insert fast path when mutation hook is noop
File:
airflow-core/src/airflow/models/taskmap.pyMethod:
TaskMap.expand_mapped_taskChange: If
task_instance_mutation_hook.is_noop is True, create missing mapped TIs withbulk_insert_mappingsusingTaskInstance.insert_mapping.File:
airflow-core/src/airflow/models/dagrun.pyMethod:
DagRun._revise_map_indexes_if_mappedChange: Same noop-only bulk insert fast path for map-index growth during revision.
Non-noop hook behavior remains unchanged (falls back to existing per-TI path so custom hook semantics are preserved).
Why this helps
Tests added/updated
airflow-core/tests/unit/models/test_dagrun.pytest_mapped_expansion_defers_some_tis_to_later_scheduler_passairflow-core/tests/unit/models/test_mappedoperator.pytest_expand_mapped_task_uses_bulk_insert_when_mutation_hook_is_noopExisting behavior check still passing:
test_mapped_length_increase_at_runtime_adds_additional_tistest_expand_mapped_task_task_instance_mutation_hook(classic + taskflow)Scope / non-goals
This PR does not yet change:
TaskMap.expand_mapped_task(still row iteration)Those can be follow-up optimizations.
Was generative AI tooling used to co-author this PR?
Generated-by: Codex (GPT-5) following the guidelines