Skip to content

[fix][broker] Prevent shared subscription stall on orphan chunked message - #26942

Closed
Asthenia0412 wants to merge 1 commit into
apache:masterfrom
Asthenia0412:fix/shared-chunk-stall
Closed

Asthenia0412 wants to merge 1 commit into
apache:masterfrom
Asthenia0412:fix/shared-chunk-stall

Conversation

@Asthenia0412

Copy link
Copy Markdown
Contributor

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 uuidToConsumer cache. 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() returns null for any non-zero chunk with no cached mapping:

if (metadata.getChunkId() != 0) {
    // Not the first chunk, skip it
    return null;
}

A null return 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

SharedConsumerAssignor

  • getConsumerForUuid(): Orphan chunks (chunkId != 0 with no cached mapping) are now dispatched to the default consumer instead of returning null. 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 at WARN.
  • New removeConsumer(Consumer): drops all uuidToConsumer entries pointing to a removed consumer.
  • New clear(): drops all uuidToConsumer entries when all consumers are gone, preventing leaks from messages whose last chunk is never published.

PersistentDispatcherMultipleConsumers

  • Call assignor.removeConsumer(consumer) in removeConsumer().
  • Call assignor.clear() in clearComponentsAfterRemovedAllConsumers().

PersistentStickyKeyDispatcherMultipleConsumers extends PersistentDispatcherMultipleConsumers and calls super.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 modifies PersistentDispatcherMultipleConsumersClassic.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 SUCCESSFUL

Added three regression tests to SharedConsumerAssignorTest:

  1. 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 no uuidToConsumer mapping is created. Fails on original code (the entry is replayed, not dispatched).
  2. 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.
  3. testClearRemovesAllUuidMappings: verifies assignor.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, AutoAckIncompleteChunk in 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?

  • Dependencies (add or upgrade a dependency)
  • The public API (added two public methods on SharedConsumerAssignor, which was already a public class)
  • 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

@Asthenia0412
Asthenia0412 force-pushed the fix/shared-chunk-stall branch from 424ad23 to 8d3d445 Compare October 17, 2026 01:08
…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
Asthenia0412 force-pushed the fix/shared-chunk-stall branch from 8d3d445 to 44753ba Compare October 18, 2026 04:20
@merlimat

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Shared subscription stalls forever on a chunked message whose first chunk is already acked

2 participants