fix: add backpressure and cancellation to the topic writer pipeline - #591
Open
sshaplygin wants to merge 1 commit into
Open
fix: add backpressure and cancellation to the topic writer pipeline#591sshaplygin wants to merge 1 commit into
sshaplygin wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #591 +/- ##
==========================================
+ Coverage 86.91% 86.94% +0.02%
==========================================
Files 198 198
Lines 19492 19635 +143
==========================================
+ Hits 16941 17071 +130
- Misses 2551 2564 +13
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Problem
The topic writer pipeline had no backpressure anywhere along its length:
Queue::add_messageaccepted messages unconditionally.mpsc::unbounded_channel.A producer writing faster than the network drains grows all three without limit, so the failure mode is memory exhaustion rather than a slowed-down caller.
Separately, the compression worker's two loops only exited when their channels closed. Nothing observed the writer's
CancellationToken, so a worker parked onqueue.submitor ontx.sendcould outlive a cancelled writer.Change
Bound the queue.
Queuegains amax_buffered_messagescap (16 000 by default, counting queued plus in-flight-to-server messages via a newMessageQueue::len). At capacity,add_messagewaits on amessage_removed_or_closednotifier instead of accepting. Acknowledgements release capacity;close_for_new_messagesandnotify_reception_ticketswake every waiter vianotify_waitersso nobody is stranded when the queue shuts down. The wait re-checksis_open_for_new_messages, so a caller blocked on a full queue that then closes gets an error rather than a hang.Bound the channels. Both the batch and compressed-request channels become
mpsc::channel, sizedavailable_parallelism() * OUTPUT_BACKLOG_PER_TASK. Because a boundedsendcan now block,write_messages_loopselects it against the cancellation token.Make compression cancellable. Every await in both worker loops — receive,
codec_selector.step,queue.submit, and the outputsend— is selected against the cancellation token, so cancelling the writer stops the worker instead of leaving it parked.Tests
add_message_waits_for_capacity_until_an_ack_releases_a_slotfills a queue of capacity 1, asserts a secondadd_messagedoes not complete, then acknowledges the first and asserts the second is admitted.cancellation_stops_scheduler_waiting_for_a_worker_slotuses aHoldingExecutorthat never runs the work it is handed, so the scheduler blocks on the single worker slot; cancelling must still unwind the task within 50 ms.Note for reviewers
16 000 is a default with no configuration knob attached. If it should be reachable through
WriterOptions, or aligned with an existing limit, that is a straightforward follow-up. Related: #565 (TopicWriterwriteMaxInFlight) and #563 (cancellation safety) look adjacent to this work.Verification
🤖 Generated with Claude Code