GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — [fix][broker] Fix Shared and Key_Shared consumer stalls during FLOW and unblock races by void-ptr974 · Pull Request #26797 · apache/pulsar Conversation
|
| Step | Connection event-loop thread processing the flow-control command | Acknowledgement-completion or redelivery thread | Broker state after the step |
|---|---|---|---|
| Initial | — | — | blockedConsumerOnUnackedMsgs = truepermitsReceivedWhileConsumerBlocked = 0messagePermits = 0pendingDispatcherFlowPermits = 0 |
| 1 | Reads blockedConsumerOnUnackedMsgs = true, enters the blocked branch, and pauses before adding the 100 permits. |
— | State is unchanged. The destination counter has already been chosen from the old blocked state. |
| 2 | — | Makes the consumer eligible to resume, writes blockedConsumerOnUnackedMsgs = false, and drains permitsReceivedWhileConsumerBlocked. The drain returns 0, so no active permit is added. |
blockedConsumerOnUnackedMsgs = falsepermitsReceivedWhileConsumerBlocked = 0messagePermits = 0pendingDispatcherFlowPermits = 0 |
| 3 | Resumes in the blocked branch selected in step 1 and adds 100 to permitsReceivedWhileConsumerBlocked. That branch does not notify the dispatcher. |
— | blockedConsumerOnUnackedMsgs = falsepermitsReceivedWhileConsumerBlocked = 100messagePermits = 0pendingDispatcherFlowPermits = 0 |
| Failure | Later flow-control commands see an unblocked consumer and add their permits to messagePermits; they do not revisit the deferred counter. |
— | The accepted 100 permits remain stranded. Neither messagePermits nor pendingDispatcherFlowPermits includes them, and the dispatcher was never notified about them. |
The race window is between deciding to block after increasing unackedMessages and publishing blockedConsumerOnUnackedMsgs = true.
| Step | Dispatch thread increasing the unacknowledged-message count | Acknowledgement-completion or policy-update thread | Broker state after the step |
|---|---|---|---|
| Initial | — | — | unackedMessages is below the configured maximum.blockedConsumerOnUnackedMsgs = false |
| 1 | Atomically increases unackedMessages to or above the maximum. The blocking condition evaluates to true, then the thread pauses before publishing the blocked state. |
— | unackedMessages >= maximumblockedConsumerOnUnackedMsgs = false |
| 2a: acknowledgement variant | — | Atomically reduces unackedMessages to or below maximum / 2. The old reconciliation code reads blockedConsumerOnUnackedMsgs = false, concludes that there is nothing to unblock, and returns. |
The count now allows delivery.blockedConsumerOnUnackedMsgs = false |
| 2b: policy variant | — | Instead of step 2a, a policy update disables the maximum-unacknowledged-message limit. Before this change, no consumer-state reconciliation is performed before dispatch resumes. | The policy now allows delivery.blockedConsumerOnUnackedMsgs = false |
| 3 | Resumes and publishes blockedConsumerOnUnackedMsgs = true using the decision completed in step 1. |
— | The consumer is marked blocked even though the current count or policy allows delivery. |
| Failure | Delivery stops. If no later acknowledgement or redelivery occurs, nothing corrects the stale blocked state. | — | The consumer can remain blocked indefinitely. |
This change uses the existing per-consumer flowPermitAccountingLock to close both windows:
messagePermits and pendingDispatcherFlowPermits before publishing the unblocked state.flowPermitAccountingLock order without introducing the reverse order.An acknowledgement that remains above the resume threshold does not acquire flowPermitAccountingLock. Exclusive, Failover, and non-persistent flow-control paths are unchanged. There are no client-facing API, wire-protocol, configuration, dependency, or default-value changes.
The tests use deterministic pause points at both race boundaries instead of relying on probabilistic stress loops. They cover:
Shared and Key_Shared.The following checks passed:
./gradlew --no-parallel :pulsar-broker:test -PtestRetryCount=0 \ --tests ConsumerBlockedPermitAccountingTest \ --tests SharedDispatcherPermitAccountingTest \ --tests ConsumerTest \ --tests AdminApiMaxUnackedMessagesTest.testNamespacePolicy ./gradlew --no-parallel quickCheck
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…FLOW/unblock race Serialize blocked credit routing and unblock transfer with the existing per-consumer accounting lock, and revalidate stale block transitions before publication. Assisted-by: Codex (GPT-5)
e018a58 to
5ad35c6
Compare
Synchronize the ACK that crosses the consumer resume threshold with blocked-state publication, while keeping ACKs above the threshold on the lock-free path. Assisted-by: Codex (GPT-5)
Clarify that low-level pending-ack and unacked counter operations do not reconcile blocked consumer state, so future ACK paths keep using the unified reconciliation helper. Assisted-by: Codex (GPT-5)
|
Thanks! Looks great at first sight! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the detailed write-up and for turning both races into deterministic tests; the pause-point approach makes the failure scenarios easy to follow.
I traced every acquisition of flowPermitAccountingLock (Consumer.java:493, 1000, 1077, 1115, 1137, 1398) and found no reverse order: the dispatcher-side callers (internalConsumerFlow, removeConsumer, sendMessages) take the dispatcher monitor first, nothing inside the lock takes another monitor or calls back into the dispatcher or subscription (only plain policy-field reads), and consumerFlow is always invoked after release. An ack that stays above the resume threshold only does lock-free reads. Race 1 and Race 2 both look closed to me, the permit transfer adds equally to messagePermits and pendingDispatcherFlowPermits so the removal invariant and the signed wrap are preserved, and the Exclusive/Failover/non-persistent paths keep their lock-free behavior.
I also ran ConsumerBlockedPermitAccountingTest (13 tests, a few seconds) and mutated the fix away piece by piece: removing the Flow-side lock, the lock around block publication, the recheck under the lock, the threshold-crossing detection, and the PersistentTopic policy reconcile each makes a test fail. The one gap is the non-transactional partial-ack branch, which survives reverting (see the inline comment). The remaining comments are small cleanups.
One scoping note for the description: this does not touch the separate negative per-consumer messagePermits wedge tracked in #24926, so that issue should stay open.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Cover non-transactional partial ACK completion racing with block publication, remove the unused unblock wrapper, and avoid redundant blocked-state publication and zero-permit ACK notifications. Preserve dispatcher wakeup on real unblock and verify deferred permit accounting during removal. Assisted-by: Codex
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM. All four earlier points are addressed in ae0790d, with tests that pin the behavior.
I re-ran ConsumerBlockedPermitAccountingTest at this head and it passes. I then reverted the non-transactional partial-ack branch in applyPendingAckCompletions to an unsynchronized decrement plus reconcileBlockedConsumerState(false), and testNonTransactionalPartialAckWaitsForBlockStatePublication fails, so that path is now pinned. The remaining delta (test setup now going through real pending-ack state, the extra SharedDispatcherPermitAccountingTest removal case) reads as sound and I found no new problems.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…nd unblock races (apache#26797) (cherry picked from commit 006431a)
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.