Repository navigation
perf(asyncio): gate pipeline packing offload on batch size - #4561
petyaslavova merged 4 commits into
Conversation
Move async pipeline command packing to a worker thread so large transactions and pipelines do not monopolize the event loop.\n\nRefs redis#3416 (cherry picked from commit b2996ac)
Only move pipeline command packing onto a worker thread when the batch
has at least _PIPELINE_PACK_OFFLOAD_MIN_COMMANDS (1000) commands.
Small pipelines keep the synchronous path, so they avoid the
asyncio.to_thread dispatch and the loop's shared default executor;
large batches still pack off the event loop.
The 1000-command cut-off comes from measuring pack_commands against the
to_thread dispatch on this machine (median of 15 runs, tiny PING
commands):
commands pack ms to_thread ms
10 0.015 0.119
100 0.099 0.209
500 0.469 0.577
1000 0.952 1.066
10000 9.831 9.987
At ~1000 commands the packing alone is about a millisecond, which
outgrows the ~0.1 ms dispatch. The threshold is a module-level
constant, not a new public option, and can be tuned if maintainers
prefer a different cut-off.
HIMPORT PREPARE preflight commands stay inside the packed batch, and
the MULTI/EXEC framing and reply handling are unchanged on the
offloaded path.
Tests: small pipelines do not call asyncio.to_thread; large pipelines
do (both transaction modes); the event-loop-not-blocked regression uses
a large batch; HIMPORT preflight remains in the offloaded batch.
Follows the original offload in redis#4210.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2df4f2dc95
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| timer = threading.Timer(0.05, release_packing.set) | ||
| timer.start() |
There was a problem hiding this comment.
Release the packer only after the worker has started
If worker dispatch takes over 50 ms, such as on a loaded CI runner, this timer sets release_packing before pack_commands starts. The worker then sets both packing_started and packing_finished without waiting, leaving event_loop_ticks_during_packing at zero even though offloading is correct. I reproduced this false failure in both transaction modes by delaying the default-executor worker for 150 ms. Have the observer release the worker after recording a tick during packing, retaining a timeout only as a deadlock safeguard.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 79697c5. The observer now sets release_packing only after it has actually recorded a tick while packing_started is set and packing_finished is clear; the fixed 50 ms timer is gone, and the worker's wait(timeout=1) plus the outer wait_for remain as the deadlock guard. Reproduced the race by delaying asyncio.to_thread dispatch by 150 ms: the previous design recorded 0 ticks in both transaction modes, while the new design passes in both. ruff check and ruff format --check pass. (The branch head was force-updated once to normalize line endings; the diff content is unchanged.)
3f73e52 to
79697c5
Compare
petyaslavova
left a comment
There was a problem hiding this comment.
Hey @BlueX888, thank you for your contribution!
This covers everything from the takeover note: the offload only kicks in at the batch-size threshold, small pipelines keep the existing inline path, and async cluster pipelines are left as a follow-up. The event-loop regression test with the observed-tick release is a nice touch. LGTM.
Takes over #4210 per @petyaslavova's open-for-takeover note. Builds on @lh0156's commit (
72c9a1d9is acherry-pick -xofb2996ac; authorship preserved) and applies the review checklist. Follow-up to #3416 (large async pipelines block the event loop while packing).Change
asyncio.to_threadonly when the batch reaches a threshold —_PIPELINE_PACK_OFFLOAD_MIN_COMMANDS = 1000(private module constant; measured: the dispatch costs ~0.07–0.10 ms while packing takes ~0.95 ms at 1000 commands, so below the threshold the inline path is cheaper). Small pipelines keep master's exact synchronous behavior._execute_pipelineconflict with current master was resolved keeping master's HIMPORT preflight inside the packed batch.Checklist mapping (per the takeover note)
to_threadoffload behind a batch-size threshold — small pipelines keep the current synchronous path.redis/asyncio/cluster.py, same packing) are out of scope — follow-up.executein async blocks as it packs the commands #3416 as the tracking issue.Tests
tests/test_asyncio/test_pipeline.py(7 selected, both transaction modes): small pipelines never callasyncio.to_thread; large pipelines offload with MULTI/EXEC ordering intact; the event-loop-not-blocked regression now runs at threshold size; HIMPORT PREPARE stays first in the offloaded batch.assert 0 > 0.ruff check/ruff format --checkclean. The 79 live-server tests were not run locally (no Docker daemon) — CI runs them.Note
Medium Risk
Changes the async pipeline execute hot path and thread-pool usage for large batches; behavior is threshold-gated and tests assert wire ordering, but mis-gating could add latency or reintroduce event-loop blocking.
Overview
Fixes large async pipelines blocking the event loop during RESP packing (#3416) by routing packing through a new
_pack_commandshelper onPipeline.Behavior: batches with fewer than 1000 commands still call
connection.pack_commandson the event loop (unchanged). At 1000+ commands, packing runs viaasyncio.to_threadso CPU-heavy work does not stall other coroutines._PIPELINE_PACK_OFFLOAD_MIN_COMMANDSdocuments the threshold; async cluster pipelines are explicitly out of scope.Call sites:
_execute_transactionand_execute_pipelinenowawait _pack_commandsinstead of packing inline; the transactional path passes a materialized command list (not a generator). HIMPORT preflight commands remain in the same packed batch before send.Tests: New asyncio pipeline tests cover non-blocking packing at threshold size, no
to_threadfor small batches, offload + MULTI/EXEC ordering for large batches, and HIMPORT PREPARE still first when offloaded.Reviewed by Cursor Bugbot for commit bb08761. Bugbot is set up for automated code reviews on this repo. Configure here.