AIP-104: Task Spreading - #73688
Draft
dabla wants to merge 24 commits into
Draft
AIP-104: Task Spreading#73688dabla wants to merge 24 commits into
dabla wants to merge 24 commits into
Conversation
This was referenced Sep 24, 2026
dabla
force-pushed
the
feature/task-spreading
branch
2 times, most recently
from
September 27, 2026 12:17
2c2a17d to
f3ed32f
Compare
Contributor
Author
|
Rebased on the reworked #62922 (see the comment there: What the rework changed here: |
dabla
force-pushed
the
feature/task-spreading
branch
from
September 27, 2026 12:41
f3ed32f to
5a96cbf
Compare
Contributor
Author
dabla
force-pushed
the
feature/task-spreading
branch
from
September 27, 2026 17:32
5a96cbf to
488139d
Compare
Contributor
Author
dabla
force-pushed
the
feature/task-spreading
branch
13 times, most recently
from
October 1, 2026 10:19
cc06223 to
97a8c3a
Compare
Add Iterable Tasks: `.iterate()` and `.iterate_kwargs()` on operators and `@task`, the counterpart of `.expand()` that processes every item inside one task instance instead of creating one task instance per item. - IterableOperator resolves the input by index as `.expand()` does and runs the items on AsyncAwareExecutor: sync operators in a thread pool, async operators concurrently on one event loop, up to `task_concurrency` at a time. - Each item's return value is pushed as `return_value_<index>`, and the task returns an XComIterable, a lazy read-only Sequence over them that a downstream `.expand()` or `.iterate()` consumes. Skipped items are left out, and downstream tasks with `all_success` are skipped, as with a mapped upstream. - Per-item progress is checkpointed in the task state store (AIP-103), tied to the item's input and the attempt that wrote it, so a retry or a clear after a failure resumes the items that already succeeded and a clear after success runs them all again. Outlet events are replayed from the checkpoint. - XComs and task state written from an item carry its index, and each item runs against its own view of the context. - Deferral, reschedule-mode sensors, TriggerDagRunOperator and downstream skipping from an item are rejected with a clear error. - Documented in task-sdk/docs/dynamic-task-mapping-vs-iteration.rst. Co-Authored-By: Tzu-ping Chung <uranusjr@gmail.com> Co-Authored-By: Copilot <223556219+Copilot@users.noreply.github.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The error an iterated async task gets for a synchronous SDK call already points at Variable.aget/aset, which apache#72329 adds; the class docstring still said Variable had no async equivalent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
partial() already prefixes the wrapped operator's task id with the task group, and BaseOperator.__init__ prefixed it again for the IterableOperator, so inside a TaskGroup the task was registered as "tg.tg.f" while its items pushed their XComs for "tg.f", a task id with no task instance. The IterableOperator now gets the bare id, and an iteration takes the task id of the task instance that runs it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BaseOperator.__deepcopy__ calls copy.copy on every attribute in shallow_copy_attrs, which held the lock guarding the sub-tasks in flight, and a lock cannot be copied: deepcopy of an iterated task and dag.partial_subset() failed on any Dag using .iterate(). A copy is another task with nothing in flight, so it now gets a fresh lock and an empty set through the deepcopy memo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The execution timeout unwinds through the executor, which cancels every item's coroutine, and each one left the register of operators in flight before the runner called on_kill(); sync items too, since they were registered in the coroutine rather than in the thread still running execute. The register was also a set, and the sub-operators of one iterated task compare equal, so it never held more than one of them. on_kill() now runs before the executor cancels, operators are registered where their code runs and keyed by identity, the item the timeout strikes directly is killed as it unwinds, and each is killed once although the runner calls on_kill() again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
create_indexed_task built the indexed task instance without the parent's _ti_context_from_server, so every iteration had no logical date, a template context without dag_run or ds, and get_previous_ti() and get_previous_dagrun() answered for no run at all. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every item failure reached the runner inside a BaseExceptionGroup, and the runner decides by exception type: a retry_policy rule never matched the group, and AirflowSensorTimeout, which the runner fails without a retry, was retried. Fail-fast exceptions are now raised on their own, a single failure unwrapped, and with several failures the retry policy is evaluated on each item's exception and the one whose decision weighs most is raised, the others attached as its cause. IndexedTaskRunner treats the fail-fast exceptions as final for the callbacks too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An iteration is unmapped from the MappedOperator, whose downstream task ids are empty because the edges land on the IterableOperator, so a ShortCircuitOperator took its "no downstream tasks" early return and a branch operator found nothing to skip: every downstream task ran. .iterate() now refuses any SkipMixin operator when the Dag is defined. The check is on the class, since the @task path's can_skip_downstream is False even for @task.short_circuit and @task.branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The SUCCESS checkpoint was written before the result was pushed to XCom, and a failing push fell through to the handler that overwrites it with UP_FOR_RETRY, so the retry ran the operator again for work that had finished. Publishing now fails on its own: the checkpoint stays, the task retries, and the retry replays the result from the checkpoint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The runner deletes every XCom of the task before an attempt, and a retry that skips an item which already succeeded replayed only its return value and outlet events, so any other key it pushed was lost although the task then succeeded. The keys an item pushes are now kept in memory while it runs, written once with its SUCCESS checkpoint and pushed again when a retry skips it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A checkpoint was honoured when the item's iterated kwargs matched, but a .partial() kwarg can come from an upstream task too: after clearing that upstream together with a partly failed task, the items that had succeeded were replayed with the old value while the others ran with the new one. The fingerprint is now taken once the item is rendered and includes the resolved values of partial kwargs that are XComArgs. Other templated partial values stay out of it, since one that changes with every attempt would make every checkpoint look stale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each item decided on its own whether it would be retried, so an item could announce a retry the task never got: after a sibling's AirflowFailException, or for an item _run_tasks rejects. A failure is now only noted when the item exits, and once every item has run each failed one gets the callback matching what happens to the task: on_retry_callback when it is retried, on_failure_callback when not. Success and skip callbacks still fire right away. The docs say which callbacks run when, and that a failure no item owns fires none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l threads InProcessSupervisorComms served a request by removing task_runner.SUPERVISOR_COMMS from the whole process, so that the code serving it acted as the server side, and matched answers to requests by their order only. Items of an iterated task calling it from worker threads failed with ImportError or took each other's answers. The comms are now hidden from the serving thread alone, through a per-thread flag that models.Variable, models.Connection, mask forwarding and the secrets backend choice read via task_runner.supervisor_comms() (in airflow-core through airflow.utils.helpers.in_task_execution_context()), and a lock serves one request at a time from handling it to taking its answer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
.iterate() over an empty input ran nothing, pushed an empty XComIterable and succeeded, so an all_success downstream task ran, where .expand() over the same input is skipped along with it. The IterableOperator now raises AirflowSkipException when no item comes through. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The IterableOperator reported the wrapped operator's task_type while its module stayed its own, so what resolves a class from the two got one that does not run it: OpenLineage picked the wrapped operator's extractor, which failed on the IterableOperator and dropped the declared outlets, and the serialized class reference named a class that does not exist. operator_name is still forwarded, so the task is shown as the wrapped operator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@task is typed as returning Task, which declared partial, expand, expand_kwargs and override only, so .iterate() and .iterate_kwargs() on a decorated callable were attribute errors under mypy and pyright. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The second iteration found the event already set, read its context and left its block before the first resumed, so the reads happened in nesting order and a thread-local stack in place of the ContextVar still passed. Each iteration now reads while the other is inside its block, and neither leaves before both have read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A threading.Thread an iterated task starts, or loop.run_in_executor(), does not inherit the iteration's context, so get_current_context() there returns the task's own, whose keys carry no index. The docs now point at the ti passed to the task, asyncio.to_thread() and contextvars.copy_context().run(), and a test pins each case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…connections Threads overlap blocking I/O up to task_concurrency, async operators scale further, and CPU-bound code speeds up with neither; the page said the reverse. The benchmark rows are loops written by hand in one @task and are now labelled so, a leftover "5 Pokémon" is gone, and iterations do not share a connection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Follows the soft rename of "Dynamic Task Mapping" to mapped tasks: the page, its label and the link to it, and the DTM wording throughout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
wait_for raises asyncio.TimeoutError, which is the built-in TimeoutError only from Python 3.11 on, so the two tests expecting the built-in one failed on 3.10. The runner already catches asyncio's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…uest The in-process API server of dag.test() answers on its own threads, its event loop and a worker thread for sync routes, not on the thread that sent the request. The per-thread flag left the comms visible there, so a route reading models.Variable or models.Connection sent a request of its own and waited for the lock the sender held, which hung every provider test that runs a task under dag_maker. The flag is now process-wide, set and restored while that lock is held. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dabla
force-pushed
the
feature/task-spreading
branch
from
October 1, 2026 10:56
97a8c3a to
276f87b
Compare
…quest The process-wide flag hid the comms from every thread of the task while a request was served, so under dag.test() a sibling item's Variable or Connection lookup took the fallback secrets backends and missed values stored in the metadata database. A per-thread flag on the sender could not work either: a2wsgi starts the request on the in-process API's event loop, which never ran on the sender's thread. The flag is now a ContextVar, set by the sender around _handle_request and by InProcessExecutionAPI around each request it serves. The event loop task and the worker threads of sync routes inherit it from there, and the task's other threads keep their comms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
.batch(size=N).iterate(...) spreads one iteration over N task instances through dynamic task mapping, each iterating its round-robin share with Iterable Tasks. This restores the batching half of the original apache#62922 on top of the iterate-only branch: BatchedOperator and DecoratedBatchedOperator (returned by the new .batch() on OperatorPartial and @task), the MappedIterableOperator and BatchedExpandInput, the runtime batch size resolved by the scheduler from the size task's mapped_length, the docs section and the tests. Unlike before, the batched classes build on the partial's own iterate machinery instead of the partial funnelling through them: they reuse the partial's input validators (_iterate_input / _iterate_kwargs_input) and _expand(), so there is no size=0 sentinel any more. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
dabla
force-pushed
the
feature/task-spreading
branch
from
October 1, 2026 14:59
276f87b to
acec154
Compare
This branch has not been deployed
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.
Was generative AI tooling used to co-author this PR?
Claude Code (Fable 5.1).
Description
This PR adds Task Spreading from AIP-104 on top of Iterable Tasks from #62922:
.batch(size=N).iterate(...)spreads one iteration over exactlyNtask instances through Dynamic Task Mapping, and each task instance iterates over its round-robin share with Iterable Tasks. It is the second half of the original #62922, split out so that PR stays focused on.iterate().What this PR adds
.batch(size=...)onOperatorPartialand on@taskreturns aBatchedOperator/DecoratedBatchedOperator(airflow.sdk.definitions.batchedoperator). Theiriterate()/iterate_kwargs()reuse the partial's own input validation and_expand(), and build aMappedIterableOperatorinstead of anIterableOperator.MappedIterableOperator: aMappedOperatorwhose task instances each run anIterableOperatorover their share of the input.BatchedExpandInputroutes itemito task instancei % size, so exactlysizeinstances are created regardless of how many items the (possibly unbounded or paginated) input yields.size:sizemay be anXComArg, the return value of a plain, non-mapped task. The scheduler never reads the XCom value: the worker pushes the integer as themapped_lengthof that push and the scheduler counts instances from that column (SerializedMappedOperator.resolve_batch_size), raisingNotFullyPopulateduntil the upstream has run. The value must be an integer of at least 2 and at mostcore.max_map_length;.map()/.zip()results, pushed keys and mapped upstreams are rejected at parse time.get_parse_time_mapped_ti_count/get_mapped_ti_count) for spread tasks, the docs section Combining DTM and IT (Batched Task Mapping), and tests for all of the above.Example
Task Iteration with Task Spreading
This example performs the same work as the Task Iteration example in #62922, but spreads the workload over two concurrent task instances. Each task instance processes roughly half of the Pokémon URLs using Task Iteration.
Pokemon.Partitioned.Task.Iteration.mp4
Comparison
get_pokemon.expand(url=urls)get_pokemon.iterate(url=urls)get_pokemon.batch(size=2).iterate(url=urls)Task Spreading keeps the reduced TaskInstance overhead of Task Iteration while allowing controlled parallelism across workers.
Open points
.batch(size=N)in the code. The devlist vote leans towards.spread(), which describes it better: exactlyNtask instances with the items dealt round-robin, notitertools.batched-style chunks. The rename lands in this PR.XComArgnext to.map()and.zip(), e.g..expand(keys=files.batch(10)). It needs no iteration and is not part of this PR.🤖 Generated with Claude Code