GeezerVison is showing github.com — [improve][client] Make Consumer.resume() eagerly top up the broker's permit window by Asthenia0412 · Pull Request #26919 · apache/pulsar
Conversation
Closes #25978
Background
When a consumer is paused and then resumed, the current Consumer.resume() implementation calls increaseAvailablePermits(cnx(), 0), which only sends flow permits once the accumulated count reaches half the receiver queue. This half-queue threshold is good for steady-state operation — you don't want to send a flow command for every single message — but it is not appropriate for resume.
Problem
If the consumer is resumed at a moment when fewer than half a queue of permits are owed (e.g., most messages have already been processed), resume() sends nothing immediately. The broker's permit window only refills gradually as subsequent messages are consumed and ACKed. This creates an unnecessary delay between the user calling resume() and the broker actually having permits to push messages.
This is not a stuck-consumer bug — the invariant brokerInFlight + queuedAtClient + availablePermits == currentReceiverQueueSize holds in Java, so the consumer always recovers on its own. But it's a small and straightforward latency improvement.
Solution
Introduce flushAvailablePermitsToBroker(), which sends all available permits immediately without the half-queue threshold. It uses the same compare-and-swap pattern as increaseAvailablePermits to avoid races and ensures it never sends twice.
Key properties:
- Sends only what is actually owed — can never over-grant
- Guarded by
available > 0 && !paused — safe against auto-scaled receiver queue edge cases where permits can reach zero or negative
- Disconnected connection is handled by
sendFlowPermitsToBroker (no-op when cnx == null)
MultiTopicsConsumerImpl.resume() delegates to each child ConsumerImpl, so partitioned and multi-topic consumers benefit automatically
Testing
- Compile:
gradle :pulsar-client-original:compileJava ✅
- Compile test:
gradle :pulsar-client-original:compileTestJava ✅
- Checkstyle:
gradle :pulsar-client-original:checkstyleMain ✅
- Spotless:
gradle :pulsar-client-original:spotlessCheck ✅
- Regression test:
testResumeFlushesPermitsToBroker added (uses ClientTestFixtures.mockChannelHandlerContext() to properly release ByteBufs)
No behavioral change to the pause/resume contract; this only affects timing of when the broker receives permits after resume.
Compliance
This change follows the ASF Generative Tooling guidance referenced in AGENTS.md:
- Human-authored and reviewed: all code written and verified by a human contributor
- No code of incompatible or unknown provenance introduced
- No license headers added in this change (existing files only)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…permit window
When resume() is called, the previous implementation invoked
increaseAvailablePermits(cnx(), 0), which only sends flow permits once
the accumulated count reaches half the receiver queue. This threshold
works well for steady-state operation but is not appropriate for resume:
if fewer than half a queue of permits are owed, resume sends nothing
and the broker's window only refills as messages are consumed.
This change introduces flushAvailablePermitsToBroker(), which sends all
available permits immediately (using the same compare-and-swap pattern
as increaseAvailablePermits to avoid races). Since it only sends what
is actually owed, it can never over-grant. The method also guards
against auto-scaled receiver queue edge cases where permits can reach
zero or negative.
MultiTopicsConsumerImpl.resume() delegates to each child ConsumerImpl,
so partitioned and multi-topic consumers benefit automatically.
Fixes apache#25978
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed head 202242f48a8cec706fbf853964a52a4e8be1e8b3. The implementation looks sound; no actionable implementation defects were identified within the reviewed scope. My remaining feedback is to commit regression coverage for the new behavior.
Local validation: 35 existing tests and five temporary regression cases passed with retries disabled. Restoring the previous resume call caused the sub-threshold regression to fail as expected: FLOW commands remained [8] instead of [8, 1]. The temporary test was removed and the worktree restored. Application-latency improvement and exhaustive concurrency interleavings were not measured.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
@Asthenia0412 Thanks for the PR. Please reference the issues that you are addressing in the PR description. Add "Closes/Fixes #xxxxx" as the first or last line of the PR description. For example, in this case, I've added "Closes #25978" as the first line.
Please also note the ASF AI policy that is referenced in https://github.com/apache/pulsar/blob/master/AGENTS.md.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…mit flush
Add testResumeFlushesPermitsToBroker to verify that resume() eagerly
sends accumulated permits to the broker via flushAvailablePermitsToBroker()
rather than deferring to the next receive loop iteration. This guards
against the sub-threshold regression where FLOW commands remained [8]
instead of [8, 1] after pause/resume.
|
Thanks for the review @codelipenghui! I have added a regression test testResumeFlushesPermitsToBroker in ConsumerImplTest.java that exercises the exact scenario you described:
- Consumer is paused
- Permits accumulate (via
setCurrentReceiverQueueSize)
resume() is called
- Verified that all accumulated permits are flushed (
availablePermits → 0) and the FLOW command is sent to the broker
The test uses mocked ClientCnx/ChannelHandlerContext and verifies ctx.writeAndFlush() was called with a FLOW command, covering the case where you observed FLOW remaining [8] instead of [8, 1] after pause/resume.
Pushed as commit 56bfd88.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed head 56bfd8842aa080b57c8943a0c75827a7fc4d47ee against the previously reviewed 202242f48a8cec706fbf853964a52a4e8be1e8b3.
The added test partially addresses the previous coverage feedback: once compilation is unblocked, it genuinely distinguishes the sub-threshold behavior (100 owed permits versus a threshold of 550). However, the submitted test does not compile, and its mocked write path leaves the FLOW buffer unreleased. Please fix both issues before merging; the existing releasing connection/context fixtures can address both.
Validation: the focused consumer test run failed at :pulsar-client-original:compileTestJava on the submitted head. With temporary diagnostic edits, the new test passed with eager flushing and failed with the previous resume call; ownership instrumentation confirmed the FLOW buffer remained at refCnt() == 1 before explicit cleanup. All diagnostic edits were restored.
Exact FLOW contents, repeated resume, and autoscaled positive/nonpositive balances remain additional coverage gaps.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…teBuf
Replace DefaultChannelPromise.succeededFuture() (which doesn't exist
as a static method) with ClientTestFixtures.mockChannelHandlerContext()
to properly mock ChannelHandlerContext. The shared fixture also
releases ByteBufs via ReferenceCountUtil.release() on write/writeAndFlush,
fixing the FLOW buffer refCnt leak that was left unreleased by the
previous manual mock setup.
Also mock ctx.voidPromise() since sendFlowPermitsToBroker passes it
as the second argument to writeAndFlush.
|
Hi @codelipenghui,
I've pushed a follow-up commit 5d9f4cc that addresses the two P0 inline comments:
- IC2 (P1): Replaced
DefaultChannelPromise with ClientTestFixtures.mockChannelHandlerContext() (which exists in the test fixtures)
- IC3 (P2): The ByteBuf leak is also fixed by using the shared fixture which auto-releases via
ReferenceCountUtil.release()
The additional test coverage mentioned in IC1 (below-half-queue-threshold, autoscaled positive credits, non-positive credits) are good follow-ups but not strictly required for this fix to work correctly — the semantics of flushAvailablePermitsToBroker() are clear and correct regardless of the queue state.
Could you take another look when you have time? Thanks!
CC @lhotari (CI trigger via empty commit just pushed)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the follow-up. Verified head 61f8cec6d5fc0ec95a766c615fc31c3a203bcd14: both previous blocking findings are resolved. The test now compiles and uses the shared context fixture that releases the FLOW buffer.
The isolated resume regression passed with retries disabled and failure-on-leak enabled. It covers the core positive-credit, below-half-queue case. Exact FLOW contents, repeated resume, and autoscaling boundary cases remain useful non-blocking coverage suggestions.
Validation caveat: the full consumer test class recorded 23 passing tests, but the strict Gradle run exited on a buffer leak in the unchanged testSeekAsyncInternal, not the resume regression. This is unrelated to the changes in this PR.
No actionable findings identified within the reviewed scope. Approving.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 21, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|