[fix][client] Stabilize scaleReceiverQueueHint against concurrent enqueue/take - #25578
Merged
merlimat merged 1 commit intoApr 24, 2026
Merged
Conversation
…ueue/take ConsumerImpl.updateAutoScaleReceiverQueueHint() is invoked from ConsumerBase.enqueueMessageAndCheckBatchReceive() immediately after incomingMessages.offer(message), under incomingQueueLock. The hint is then derived from getAvailablePermits() + incomingMessages.size(). But incomingMessages.take() (and poll()) do not acquire incomingQueueLock, so a consumer-thread take() that races with the enqueue can drain the just-offered message before the hint is recomputed. The read of incomingMessages.size() then observes zero, and the hint is spuriously cleared to false even though the pipeline was momentarily full. This produces flakiness in ConsumerMemoryLimitTest#testMultiPulsarClientConsumerShareMemoryLimitController: inside its receive() loop the consumer-thread take() frequently overlaps with the client IO thread enqueue, so the hint ends up false even though the queue was at capacity for every message. The subsequent receiveAsync() therefore fails to trigger expectMoreIncomingMessages() (the CAS true->false fails) and the receiver queue never expands to 2. Clamp the buffer component to at least 1 in the enqueue-time hint calculation: we just added a message, so the post-enqueue state of the pipeline is at least 1 regardless of any concurrent drain. No correctness impact outside this race; in the non-racy case incomingMessages.size() is already >= 1 and Math.max is a no-op. Adds ConsumerImplTest#testUpdateAutoScaleReceiverQueueHintRaceWithConcurrentDrain which simulates the offer+drain sequence deterministically and fails without the clamp.
manas-ctds
pushed a commit
to datastax/pulsar
that referenced
this pull request
May 1, 2026
…ueue/take (apache#25578) (cherry picked from commit f6598d8) (cherry picked from commit ac19ba5)
srinath-ctds
pushed a commit
to datastax/pulsar
that referenced
this pull request
May 5, 2026
…ueue/take (apache#25578) (cherry picked from commit f6598d8) (cherry picked from commit ac19ba5)
poorbarcode
pushed a commit
to poorbarcode/pulsar
that referenced
this pull request
May 6, 2026
nodece
pushed a commit
to ascentstream/pulsar
that referenced
this pull request
May 27, 2026
…ueue/take (apache#25578) (cherry picked from commit f6598d8)
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.
Motivation
ConsumerImpl.updateAutoScaleReceiverQueueHint()is invoked fromConsumerBase.enqueueMessageAndCheckBatchReceive()immediately afterincomingMessages.offer(message), underincomingQueueLock. The hint is then derived fromgetAvailablePermits() + incomingMessages.size(). ButincomingMessages.take()(andpoll()) do not acquireincomingQueueLock, so a consumer-threadtake()that races with the enqueue can drain the just-offered message before the hint is recomputed. The read ofincomingMessages.size()then observes zero, and the hint is spuriously cleared tofalseeven though the pipeline was momentarily full.This produces flakiness in
ConsumerMemoryLimitTest#testMultiPulsarClientConsumerShareMemoryLimitController— inside its receive() loop the consumer-threadtake()frequently overlaps with the client IO thread enqueue, so the hint ends upfalseeven though the queue was at capacity for every message. The subsequentreceiveAsync()therefore fails to triggerexpectMoreIncomingMessages()(theCAS true→falsefails) and the receiver queue never expands to 2, causing theAwaitility.await()to time out.Example failing CI run: https://github.com/apache/pulsar/actions/runs/24909862708/job/72949410128
I reproduced it locally with
@Test(invocationCount = 100)— roughly 1 in ~50 runs fails; 100/100 pass after the fix.Modifications
ConsumerImpl.updateAutoScaleReceiverQueueHint(): clampincomingMessages.size()to at least 1 in the enqueue-time hint calculation. The method is only called from the enqueue path, where a message was just added, so the post-enqueue state of the pipeline is at least 1 regardless of any concurrent drain. In the non-racy caseincomingMessages.size() >= 1andMath.max(1, …)is a no-op.Adds
ConsumerImplTest#testUpdateAutoScaleReceiverQueueHintRaceWithConcurrentDrain, a focused unit test that simulates theoffer + concurrent drainsequence deterministically and asserts the hint is retained. It fails without the clamp and passes with it.Verifying this change
This change added tests and can be verified as follows:
ConsumerImplTest#testUpdateAutoScaleReceiverQueueHintRaceWithConcurrentDraincovering the race deterministically.testMultiPulsarClientConsumerShareMemoryLimitControllerpasses 100/100 with the fix (and reliably fails within a handful of invocations without it).Does this pull request potentially affect one of the following parts: