Skip to content

[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
merlimat merged 1 commit into
masterfrom
lh-fix-key-shared-replay-ordering
Oct 12, 2026
Merged

merlimat merged 1 commit into
masterfrom
lh-fix-key-shared-replay-ordering

Conversation

@lhotari

@lhotari lhotari commented Oct 12, 2026

Copy link
Copy Markdown
Member

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-disconnect runs 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 PersistentStickyKeyDispatcherMultipleConsumersTest now reproduces:

  1. A replay read is in flight with a newer entry of a sticky key hash H, such as one that a consumer without permits couldn't take earlier.
  2. The consumer that owns H is removed. PersistentDispatcherMultipleConsumers.removeConsumer adds its pending entries to the replay queue, among them an older entry of H, and H moves to another consumer.
  3. The read completes. The guard in PersistentStickyKeyDispatcherMultipleConsumers.trySendMessagesToConsumers discards 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 in canDispatchEntry.
  4. The newer entry of H goes to H's new owner, and the older one only with the next replay read.

Modifications

  • PersistentDispatcherMultipleConsumers calls 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, and onReadEnded(ReadType) when a read ends without its entries being dispatched (it failed, was cancelled, read only deleted entries, or completed after every consumer disconnected). cancelPendingRead now runs under the monitor.
  • PersistentStickyKeyDispatcherMultipleConsumers records, in addMessageToReplay, each sticky key hash's positions added to the replay queue while a replay read is in flight. Normal reads need no record: canDispatchEntry already blocks their entries of every hash with entries in the replay queue.
  • When a replay read is dispatched, its entries of a recorded hash are blocked, as entries of a blocked hash are, while an older recorded position of the hash is still in the replay queue and wasn't dispatched earlier in the same read: they go to the replay queue and are dispatched after the older entry. Recorded positions that left the replay queue, because they were dispatched, acknowledged or expired, no longer block and are pruned, so a record can't stall a hash. When an entry is blocked this way, the dispatcher reads again without a backoff, and the hash's owner isn't counted as unable to make progress for the look-ahead.
  • With 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.
  • This applies only when out-of-order delivery isn't allowed. The existing guard, which discards a read when a position older than the replay queue's first one was added during it, is unchanged: it still covers the positions added without a hash.

Known limitations:

  • Entries added to the replay queue without a hash, by 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.
  • With dispatcherDispatchMessagesInSubscriptionThread=true, a queued dispatch releases the shared sendInProgress flag, 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

  • Make sure that the change passes the CI checks.

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: with dispatcherDispatchMessagesInSubscriptionThread, 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.
  • The existing PersistentStickyKeyDispatcherMultipleConsumersTest, PersistentDispatcherMultipleConsumersTest, DrainingHashesTrackerTest, KeySharedSubscriptionTest and KeySharedSubscriptionBrokerCacheTest pass.
  • network/silent-client-disconnect of [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

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

… 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)
@merlimat
merlimat merged commit be9207d into master Oct 12, 2026
44 checks passed
lhotari added a commit that referenced this pull request Oct 12, 2026
… before an older one added to the replay queue (#26868)

(cherry picked from commit be9207d)
lhotari added a commit that referenced this pull request Oct 13, 2026
… before an older one added to the replay queue (#26868)

(cherry picked from commit be9207d)
lhotari added a commit that referenced this pull request Oct 13, 2026
… before an older one added to the replay queue (#26868)

(cherry picked from commit be9207d)
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][broker] Key_Shared delivers a key's message after a newer one when consumers reconnect while their old consumers are still registered

3 participants