[fix][broker] Fix persistent throughput degradation caused by permit loss during frequent reconnects on Shared subscriptions - #26289
Open
void-ptr974 wants to merge 3 commits into
Conversation
void-ptr974
marked this pull request as ready for review
August 8, 2026 01:25
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.
Fixes #26288
Motivation
For Shared subscriptions, the consumer permit counter is updated immediately on the connection EventLoop, while the dispatcher permit counter is updated asynchronously by a task submitted to the broker executor. Consumer removal can run before that queued task.
The following diagram shows one possible failing execution order. Time flows downward.
sequenceDiagram participant A as Connection EventLoop (Thread A) participant C as Consumer participant B as Broker Executor (Thread B) participant D as Shared Dispatcher Note over A,D: Initial state: removed consumer permits = 10, dispatcher total = 20 A->>C: t1: handleFlow(+1000) C->>C: messagePermits: 10 → 1010 C-->>B: Queue internalConsumerFlow(+1000) Note over B: Flow task has not run yet A->>D: t2: handleCloseConsumer() → removeConsumer() D->>D: totalAvailablePermits: 20 - 1010 = -990 D->>D: Remove consumer from consumerSet B->>D: t3: Run internalConsumerFlow(+1000) D->>D: Consumer is already removed, ignore Flow Note over D: Dispatcher total remains -990, but the correct value is 10At
t2, consumer removal subtracts all 1,010 permits even though the queued Flow task has not added its 1,000 permits to the dispatcher total. Att3, that task cannot restore the count because the consumer has already been removed.Frequent consumer reconnects can accumulate this negative permit drift and cause persistent consumption throughput degradation.
Modifications
In the example above, removal now subtracts only
1,010 - 1,000 = 10permits, leaving the dispatcher total at the correct value of 10.Verifying this change
This change added tests and can be verified as follows:
./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.service.persistent.SharedDispatcherPermitAccountingTest./gradlew :pulsar-broker:checkstyleMain :pulsar-broker:checkstyleTest./gradlew quickCheckThe regression test fails on the unpatched code with the dispatcher permit total at
-990instead of10, and passes with this change.Does this pull request potentially affect one of the following parts:
The change adds a per-consumer monitor around the two permit counters used by Flow processing and consumer removal. No callbacks or I/O are executed while holding this monitor.