Repository navigation
[fix][broker] Shared subscription stalls forever on a chunked message - #26396
Merged
Merged
Conversation
5 of 14 tasks
Asthenia0412
pushed a commit
to Asthenia0412/pulsar
that referenced
this pull request
Oct 17, 2026
…sage Issue: apache#26395 Motivation: A Shared subscription stops dispatching forever when it runs into a chunk of a chunked message whose chunk 0 is no longer in the uuidToConsumer cache. This happens after topic unload, broker restart, or consumer disconnect. SharedConsumerAssignor pins all chunks of a message to one consumer through the in-memory uuidToConsumer map, which is only written when chunk 0 is assigned. The original code returned null for any non-zero chunk with no cached mapping: if (metadata.getChunkId() != 0) { // Not the first chunk, skip it return null; } The null return causes the entry to be handed to the replay queue. The dispatcher drains the replay queue before every normal read, so it keeps replaying the same orphan chunk forever and the mark-delete position never moves. The same stall occurs when a consumer is removed while chunks are still pinned to it — the mapping survives, the disconnected consumer has no permits, so every remaining chunk also gets replayed forever. Modifications: - SharedConsumerAssignor.getConsumerForUuid(): orphan chunks (chunkId != 0 with no cached mapping) are now dispatched to the default consumer instead of returning null. The chunk 0 is what seeds the mapping, and the cursor always re-reads chunk 0 ahead of later chunks while it is still unacked, so a missing mapping means chunk 0 is already acked and the message can never be reassembled. Dispatching it lets the client discard the incomplete chunked message and move on. This path is logged at WARN so operators can detect silently-acked chunks. - SharedConsumerAssignor.removeConsumer(Consumer): new public method that drops all uuid mappings pointing to a removed consumer. Called from the dispatcher when a consumer disconnects. - SharedConsumerAssignor.clear(): new public method to drop all uuid mappings. Called when all consumers are removed from the dispatcher to prevent leaks from messages whose last chunk is never published. - PersistentDispatcherMultipleConsumers: call assignor.removeConsumer() in removeConsumer() and assignor.clear() in clearComponentsAfterRemovedAllConsumers(). PersistentStickyKeyDispatcherMultipleConsumers inherits both via super(), so the fix covers Key_Shared subscriptions too. Note on PR apache#26396: this supersedes PR apache#26396, which has a merge conflict because it modifies PersistentDispatcherMultipleConsumersClassic.java — a file that was removed by PIP-379 (apache#26687) on 2026-09-22. The core fix in apache#26396 is correct; this PR applies the same logic to the file set that exists on master today. Testing: - SharedConsumerAssignorTest.testOrphanChunkIsDispatchedInsteadOfReplayedForever: verifies orphan chunks are dispatched, not replayed, and do not create a uuid mapping. Fails on original code. - SharedConsumerAssignorTest.testUuidMappingIsRemovedWhenConsumerIsRemoved: verifies the uuid mapping is cleaned up when its consumer is removed, and the remaining chunk can be dispatched to another consumer. - SharedConsumerAssignorTest.testClearRemovesAllUuidMappings: verifies the map is cleared properly to prevent leaks. - All existing SharedConsumerAssignorTest tests pass. - All existing PersistentDispatcherMultipleConsumersTest tests pass.
1 of 4 tasks
Asthenia0412
pushed a commit
to Asthenia0412/pulsar
that referenced
this pull request
Oct 18, 2026
…sage Issue: apache#26395 Motivation: A Shared subscription stops dispatching forever when it runs into a chunk of a chunked message whose chunk 0 is no longer in the uuidToConsumer cache. This happens after topic unload, broker restart, or consumer disconnect. SharedConsumerAssignor pins all chunks of a message to one consumer through the in-memory uuidToConsumer map, which is only written when chunk 0 is assigned. The original code returned null for any non-zero chunk with no cached mapping: if (metadata.getChunkId() != 0) { // Not the first chunk, skip it return null; } The null return causes the entry to be handed to the replay queue. The dispatcher drains the replay queue before every normal read, so it keeps replaying the same orphan chunk forever and the mark-delete position never moves. The same stall occurs when a consumer is removed while chunks are still pinned to it — the mapping survives, the disconnected consumer has no permits, so every remaining chunk also gets replayed forever. Modifications: - SharedConsumerAssignor.getConsumerForUuid(): orphan chunks (chunkId != 0 with no cached mapping) are now dispatched to the default consumer instead of returning null. The chunk 0 is what seeds the mapping, and the cursor always re-reads chunk 0 ahead of later chunks while it is still unacked, so a missing mapping means chunk 0 is already acked and the message can never be reassembled. Dispatching it lets the client discard the incomplete chunked message and move on. This path is logged at WARN so operators can detect silently-acked chunks. - SharedConsumerAssignor.removeConsumer(Consumer): new public method that drops all uuid mappings pointing to a removed consumer. Called from the dispatcher when a consumer disconnects. - SharedConsumerAssignor.clear(): new public method to drop all uuid mappings. Called when all consumers are removed from the dispatcher to prevent leaks from messages whose last chunk is never published. - PersistentDispatcherMultipleConsumers: call assignor.removeConsumer() in removeConsumer() and assignor.clear() in clearComponentsAfterRemovedAllConsumers(). PersistentStickyKeyDispatcherMultipleConsumers inherits both via super(), so the fix covers Key_Shared subscriptions too. Note on PR apache#26396: this supersedes PR apache#26396, which has a merge conflict because it modifies PersistentDispatcherMultipleConsumersClassic.java — a file that was removed by PIP-379 (apache#26687) on 2026-09-22. The core fix in apache#26396 is correct; this PR applies the same logic to the file set that exists on master today. Testing: - SharedConsumerAssignorTest.testOrphanChunkIsDispatchedInsteadOfReplayedForever: verifies orphan chunks are dispatched, not replayed, and do not create a uuid mapping. Fails on original code. - SharedConsumerAssignorTest.testUuidMappingIsRemovedWhenConsumerIsRemoved: verifies the uuid mapping is cleaned up when its consumer is removed, and the remaining chunk can be dispatched to another consumer. - SharedConsumerAssignorTest.testClearRemovesAllUuidMappings: verifies the map is cleared properly to prevent leaks. - All existing SharedConsumerAssignorTest tests pass. - All existing PersistentDispatcherMultipleConsumersTest tests pass.
…d of dispatching it A normal read in flight when a consumer is removed can return a later chunk after that consumer's chunk 0 was moved to the replay queue. With the uuid mapping dropped, the chunk was dispatched as an orphan ahead of its chunk 0. On a normal read, send an unmapped non-first chunk after the first replay position back to replay, so chunk 0 recreates the mapping first. Assisted-by: Claude Code (claude-opus-5-5)
Contributor
|
Thanks @0asys, this is the right fix, and sorry it sat for so long. I pushed two commits to your branch to get it in:
I also updated the description; the Key_Shared note is gone, since that dispatcher doesn't use the assignor. #26942 and #25120 are closed in favor of this one. |
merlimat
approved these changes
Oct 21, 2026
lhotari
pushed a commit
that referenced
this pull request
Oct 22, 2026
…#26396) Co-authored-by: Matteo Merli <[email protected]> (cherry picked from commit 91aac70)
poorbarcode
pushed a commit
to poorbarcode/pulsar
that referenced
this pull request
Nov 26, 2026
…apache#26396) Co-authored-by: Matteo Merli <[email protected]> (cherry picked from commit 91aac70)
poorbarcode
pushed a commit
to poorbarcode/pulsar
that referenced
this pull request
Nov 26, 2026
…apache#26396) Co-authored-by: Matteo Merli <[email protected]> (cherry picked from commit 91aac70)
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 #26395
Motivation
A Shared subscription stops dispatching for good when it reaches a chunk of a chunked message whose chunk 0 is already gone. This happens, for example, when the client's incomplete-chunk expiry or queue-full eviction acked the chunks it had, when a seek, TTL expiry or skip landed in the middle of the message, or after a reconnect, unload or restart lost the in-memory mapping.
SharedConsumerAssignorpins all chunks of a message to one consumer throughuuidToConsumer, and only chunk 0 writes that mapping. A later chunk with no mapping hit:nullsends the entry toaddEntryToReplay. Replay reads always run before normal reads, so the dispatcher replays that same entry forever, the mark-delete position never moves, and nothing is logged. The only way out today ispulsar-admin topics skip.A mapping that points at a removed consumer causes the same stall.
removeConsumernever cleaned the mapping, so the remaining chunks stayed pinned to a consumer that no longer exists.Modifications
SharedConsumerAssignor:chunkId != 0and no mapping (an orphan) is now dispatched to the default consumer and logged at WARN. The client then discards it under its incomplete-chunk policy. The broker never acks it, so with client auto-ack disabled it stays in the backlog, but the subscription keeps moving.removeConsumer(Consumer)andclear()methods drop stale mappings.PersistentDispatcherMultipleConsumers:assignor.removeConsumer()inremoveConsumer()andassignor.clear()inclearComponentsAfterRemovedAllConsumers().assign().Key_Shared doesn't use
SharedConsumerAssignor, so its behavior is unchanged.Verifying this change
New tests in
SharedConsumerAssignorTest:testOrphanChunkIsDispatchedInsteadOfReplayedForever: an orphan last chunk and an orphan middle chunk are dispatched, not replayed, and create no mapping. This fails on the old code.testUuidMappingIsRemovedWhenConsumerIsRemoved: a removed consumer's mapping is dropped, and the remaining chunk goes to another consumer.testChunkAfterPendingReplayIsReplayedInOrder: an unmapped chunk read behind a pending replay goes back to replay and later reaches the same consumer as its chunk 0. An orphan before the first replay position is still dispatched.testClearRemovesAllUuidMappings:clear()empties the map.SharedConsumerAssignorTest(7),PersistentDispatcherMultipleConsumersTest(17) andMessageChunkingSharedTest(4) pass locally, and checkstyle is clean.Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes