Repository navigation
[fix][broker] Prevent shared subscription stall on orphan chunked message - #26942
Closed
Asthenia0412 wants to merge 1 commit into
Closed
Asthenia0412 wants to merge 1 commit into
Asthenia0412 wants to merge 1 commit into
Conversation
Asthenia0412
force-pushed
the
fix/shared-chunk-stall
branch
from
October 17, 2026 01:08
424ad23 to
8d3d445
Compare
…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.
Asthenia0412
force-pushed
the
fix/shared-chunk-stall
branch
from
October 18, 2026 04:20
8d3d445 to
44753ba
Compare
Contributor
|
Thanks @Asthenia0412. This is the same change as #26396, which is still active, so it should land there under its original author. I'll close this one as a duplicate. A review on #26396 would be very welcome. |
10 tasks
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
Supersedes #26396
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 in-memory
uuidToConsumercache. This cache is only populated when chunk 0 is assigned, so the mapping is gone after a topic unload, broker restart, or consumer disconnect.SharedConsumerAssignor.getConsumerForUuid()returnsnullfor any non-zero chunk with no cached mapping:A
nullreturn puts the entry on the replay queue. The dispatcher drains the replay queue before every normal read, so it replays the same orphan forever, the mark-delete position never moves, and the subscription stalls.The same stall happens when a consumer disconnects 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
SharedConsumerAssignorgetConsumerForUuid(): Orphan chunks (chunkId != 0with no cached mapping) are now dispatched to the default consumer instead of returningnull. The reasoning: chunk 0 is what seeds the mapping, and the cursor always re-reads chunk 0 ahead of later chunks while it is still unacked. A missing mapping therefore 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 atWARN.removeConsumer(Consumer): drops alluuidToConsumerentries pointing to a removed consumer.clear(): drops alluuidToConsumerentries when all consumers are gone, preventing leaks from messages whose last chunk is never published.PersistentDispatcherMultipleConsumersassignor.removeConsumer(consumer)inremoveConsumer().assignor.clear()inclearComponentsAfterRemovedAllConsumers().PersistentStickyKeyDispatcherMultipleConsumersextendsPersistentDispatcherMultipleConsumersand callssuper.removeConsumer()/super.clearComponentsAfterRemovedAllConsumers(), so Key_Shared subscriptions get the fix as well.Why this supersedes PR #26396
PR #26396 (by
0asys) has the correct core fix but modifiesPersistentDispatcherMultipleConsumersClassic.java— a file that was removed by PIP-379 (#26687) on 2026-09-22, one month after PR #26396 was opened. This leaves PR #26396 in a permanent merge-conflict state (merge state: DIRTY). This PR applies the same logic cleanly against the file set that exists on master today.Verification
./gradlew :pulsar-broker:compileJava— BUILD SUCCESSFUL./gradlew :pulsar-broker:compileTestJava— BUILD SUCCESSFUL./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.SharedConsumerAssignorTest"— BUILD SUCCESSFUL./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumersTest"— BUILD SUCCESSFULAdded three regression tests to
SharedConsumerAssignorTest:testOrphanChunkIsDispatchedInsteadOfReplayedForever: the actual bug. Verifies that an orphan chunk (last chunk and middle chunk) is dispatched to a consumer and does not go back to the replay queue. Also verifies nouuidToConsumermapping is created. Fails on original code (the entry is replayed, not dispatched).testUuidMappingIsRemovedWhenConsumerIsRemoved: verifies that removing a consumer from the dispatcher drops its uuid mappings, so remaining chunks can be dispatched to another consumer instead of being replayed forever.testClearRemovesAllUuidMappings: verifiesassignor.clear()bounds the map when no more chunks are expected.Broker does not auto-ack the orphan chunk
The broker deliberately does not acknowledge the orphan chunk. Dropping an incomplete chunked message is a client-side decision (
autoAckOldestChunkedMessageOnQueueFull,expireTimeOfIncompleteChunkedMessage,AutoAckIncompleteChunkin the Go client). The broker's job here is to stop refusing to deliver it. With auto-ack disabled the entry stays unacked and gets redelivered and discarded on every reconnect, but the subscription keeps making progress.Does this PR affect anything else?
SharedConsumerAssignor, which was already a public class)