GeezerVison is showing github.com — [fix][broker] Shared subscription stalls forever on a chunked message by 0asys · Pull Request #26396 · apache/pulsar
Conversation
0asys
commented
Aug 20, 2026
•
edited by merlimat
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
SharedConsumerAssignor pins all chunks of a message to one consumer through uuidToConsumer, and only chunk 0 writes that mapping. A later chunk with no mapping hit:
if (metadata.getChunkId() != 0) {
// Not the first chunk, skip it
return null;
}
null sends the entry to addEntryToReplay. 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 is pulsar-admin topics skip.
A mapping that points at a removed consumer causes the same stall. removeConsumer never cleaned the mapping, so the remaining chunks stayed pinned to a consumer that no longer exists.
Modifications
SharedConsumerAssignor:
- A chunk with
chunkId != 0 and 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.
- Exception: when the entries come from a normal read and the replay queue holds an earlier position, the orphan goes back to replay instead. A normal read that was in flight when the owning consumer was removed can return chunk 1 after that consumer's chunk 0 was moved to replay. Replay reads come first and run in position order, so chunk 0 recreates the mapping and both chunks reach the same consumer. A real orphan is dispatched on its replay read, so it can't stall.
- New
removeConsumer(Consumer) and clear() methods drop stale mappings.
PersistentDispatcherMultipleConsumers:
- Calls
assignor.removeConsumer() in removeConsumer() and assignor.clear() in clearComponentsAfterRemovedAllConsumers().
- On a normal read, passes the first position in the replay queue to
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) and MessageChunkingSharedTest (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
- 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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
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)
|
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:
- A merge of master.
PersistentDispatcherMultipleConsumersClassic was removed by [improve][broker] PIP-379: Remove the classic Shared and Key_Shared dispatcher implementations #26687, so that hunk is gone.
- A guard for one race. After
removeConsumer drops A's mappings, a normal read already in flight can still return chunk 1 after A's chunk 0 was moved to the replay queue. Before the guard, chunk 1 went to B as an orphan ahead of its chunk 0. The client would then discard it, and if the message was older than expireTimeOfIncompleteChunkedMessage it would also ack it. Now, on a normal read, an unmapped chunk after the first replay position goes back to replay, and both chunks reach the same consumer in order. A real orphan is still dispatched on its replay read. The new test is testChunkAfterPendingReplayIsReplayedInOrder.
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 21, 2026
Reviewers
merlimat
merlimat approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
|