Repository navigation
[fix][broker] Don't let an in-flight read dispatch a Key_Shared entry before an older one added to the replay queue - #26868
Merged
Conversation
… before an older one added to the replay queue When a Key_Shared consumer was removed while a replay read was in flight with a newer entry of one of its hashes, removeConsumer added the consumer's older pending entry of the hash to the replay queue, the hash moved to another consumer, and the completed read could dispatch the newer entry to the new owner before the older one: the existing guard discards a read only when the replay queue's first position became older during the read. - Record each sticky key hash's positions added to the replay queue while a replay read is in flight, and block the read's entries of the hash while an older recorded position is still in the replay queue and wasn't dispatched earlier in the same read. Normal reads are covered by canDispatchEntry already. - Hooks for the start of a read, a dispatch queued on the subscription's thread, and a read that ends without a dispatch keep the record for exactly the replay reads in flight, also with dispatcherDispatchMessagesInSubscriptionThread. Fixes #26867 Assisted-by: Claude Code (claude-opus-5-5)
dao-jun
approved these changes
Oct 12, 2026
2 of 15 tasks
This was referenced Oct 14, 2026
4 of 5 tasks
nodece
pushed a commit
to ascentstream/pulsar
that referenced
this pull request
Nov 16, 2026
… before an older one added to the replay queue (apache#26868) (cherry picked from commit be9207d)
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 #26867
Motivation
Key_Shared subscriptions without out-of-order delivery, the default, delivered a key's message after a newer one when the broker removed consumers whose connections had vanished silently, in the
network/silent-client-disconnectruns of the Pulsar Performance Testing Framework (#26859): in 5 runs out of 5 of master with a 768 MB broker, 1–11 messages per run. The violations fell in the window where the broker's keep-alive removed the old consumers; for each key, the newer message arrived first and the older one, which had been pending on the removed consumer, 1–4 s later.The mechanism, which
PersistentStickyKeyDispatcherMultipleConsumersTestnow reproduces:PersistentDispatcherMultipleConsumers.removeConsumeradds its pending entries to the replay queue, among them an older entry of H, and H moves to another consumer.PersistentStickyKeyDispatcherMultipleConsumers.trySendMessagesToConsumersdiscards a read only when the replay queue's first position became older during the read; when the queue already held an older position of another hash, which a consumer without permits leaves there, the read isn't discarded. Replay reads don't check for the hash's entries in the replay queue, as normal reads do incanDispatchEntry.Modifications
PersistentDispatcherMultipleConsumerscalls new hooks under the dispatcher's monitor:onReadStarted(ReadType)when a normal or a replay read starts,onDispatchQueued(ReadType)when a completed read's dispatch is queued on the subscription's thread, andonReadEnded(ReadType)when a read ends without its entries being dispatched (it failed, was cancelled, read only deleted entries, or completed after every consumer disconnected).cancelPendingReadnow runs under the monitor.PersistentStickyKeyDispatcherMultipleConsumersrecords, inaddMessageToReplay, each sticky key hash's positions added to the replay queue while a replay read is in flight. Normal reads need no record:canDispatchEntryalready blocks their entries of every hash with entries in the replay queue.dispatcherDispatchMessagesInSubscriptionThread=true, a replay read can start while the previous one's dispatch is still queued. The record is kept until no replay read is in flight, so the queued dispatch keeps its protection, and the later read's dispatch gets a superset of its own additions. A replay read that starts with no dispatch queued resets the record, also if the dynamic setting was changed meanwhile.Known limitations:
redeliverUnacknowledgedMessages(Consumer, List<Position>), aren't covered, as before; only the existing guard covers them, partially. Looking their hashes up in the consumer's pending acks would let the per-hash blocking cover them and make the existing guard unnecessary, for a follow-up.dispatcherDispatchMessagesInSubscriptionThread=true, a queued dispatch releases the sharedsendInProgressflag, so another read of the same positions can start before an earlier queued dispatch runs and dispatch the same replayed entries twice. That window is older than this change, a lifecycle problem of the subscription-thread dispatch, for a follow-up.Verifying this change
This change added tests and can be verified as follows:
PersistentStickyKeyDispatcherMultipleConsumersTest.testInFlightReplayReadDoesNotOvertakeRemovedConsumersPendingEntry: a replay read of the newer entry is in flight, the consumer holding the older entry as a pending ack is removed, and the read completes; the hash's new owner gets the older entry first. On master, it gets the newer one first.testOverlappingNormalReadDoesNotConsumeReplayReadsOrderingProtection: the same with a normal read in flight too, which completes first.testQueuedDispatchKeepsItsOrderingProtectionWhenTheNextReadStarts: withdispatcherDispatchMessagesInSubscriptionThread, the completed replay read's dispatch waits behind a busy subscription thread while another replay read starts. The three tests assert the key's messages exactly once each and in order, and fail on master; the first two also check that the record doesn't outlive the reads.PersistentStickyKeyDispatcherMultipleConsumersTest,PersistentDispatcherMultipleConsumersTest,DrainingHashesTrackerTest,KeySharedSubscriptionTestandKeySharedSubscriptionBrokerCacheTestpass.network/silent-client-disconnectof [improve][test] Add network conditions, network faults and geo-replication to the performance tests #26859 with a 768 MB broker, with the performance environment on: master had 5, 11, 1, 3 and 3 ordering violations in 5 runs; this change had none in 5 runs. The runs were of earlier versions of this change, before the record was narrowed to replay reads and the handling of queued dispatches was added; the same tests and the race tests above cover the final version.Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes