Skip to content

[fix][client] Fix cannot retry chunk messages and send to DLQ - #21048

Merged
codelipenghui merged 4 commits into
apache:masterfrom
RobertIndie:dlq-chunk
Aug 30, 2023
Merged

codelipenghui merged 4 commits into
apache:masterfrom
RobertIndie:dlq-chunk

Conversation

@RobertIndie

@RobertIndie RobertIndie commented Aug 22, 2023 •

Copy link
Copy Markdown
Member

Fixes #20934

Motivation

If the consumer receives a chunked message, it currently couldn't retry it or send it to the DLQ. It will throw an exception like this:

java.util.concurrent.CompletionException: org.apache.pulsar.client.api.PulsarClientException$InvalidMessageException: The producer xxx of the topic persistent://a/b/c sends a message with 8085120 bytes that exceeds 5242880 bytes

This is because we don't enable the chunking for the retry producer and the DLQ producer in the consumer.

This PR enables the chunking feature for retry and DLQ producers. However, we cannot enable both chunking and batching simultaneously. Therefore, the trade-off of this change is that we have to turn off the batching for the DLQ producer, while the retry producer has already done so. This is acceptable because the DLQ producer usually has low traffic. It does not have a significant impact on the performance.

This PR also addresses the following two issues:

Issue A: Consumers cannot redeliver chunked messages

When the consumer attempts to redeliver a chunked message, the message ID is reconstructed in discardBatch, which causes this message ID to lose the chunk information. The comparison in the unAckedChunkedMessageIdSequenceMap will fail. The consumer will never be able to redeliver chunked messages.

We should not reconstruct the chunked message ID in discardBatch. The chunk message ID does not contain any batch information.

Issue B: The chunked message cannot be resent

When the producer resends the chunked message, the chunk id in the message metadata of all subsequent chunks will be set to the last chunk id.

The root cause is that the producer shares the same MessageMetadata for all chunks in a chunked message.

final MessageMetadata finalMsgMetadata = msgMetadata;
op.rePopulate = () -> {
op.cmd = sendMessage(producerId, sequenceId, numMessages, messageId, finalMsgMetadata,
encryptedPayload);
};

Therefore, when op.rePopulate is called, the chunk id in finalMsgMetadata is pointed to the last chunk. Suppose we have a large message with 3 chunks. The producer may end up sending all 3 messages with the same chunk id 3. The consumer will treat these messages as corrupted and ignore them, preventing it from receiving any of these messages.

Modification

  • Enable chunking and disable batching for retry and DLQ producers.
  • Don't discard the batch for the chunked message ID to fix Issue A.
  • Don't redeliver the chunk message ID itself when redelivering unacked messages. Just need to redeliver the message ID for all single chunks.
  • Reset the chunkID when resending the chunk message to fix Issue B.
  • Add chunk messages test case for testDeadLetterTopic.

Verifying this change

This change added tests.

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

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Matching PR in forked repository

PR in forked repository:

@RobertIndie RobertIndie self-assigned this Aug 22, 2023
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Aug 22, 2023
@RobertIndie RobertIndie added this to the 3.2.0 milestone Aug 22, 2023
@RobertIndie

Copy link
Copy Markdown
Member Author

Hi, @poorbarcode , I have added the timeout of receive opeartion for the test. PTAL again. Thanks.

@codelipenghui
codelipenghui merged commit 99e3fea into apache:master Aug 30, 2023
@RobertIndie
RobertIndie deleted the dlq-chunk branch August 31, 2023 09:57
RobertIndie added a commit that referenced this pull request Aug 31, 2023
RobertIndie added a commit that referenced this pull request Aug 31, 2023
RobertIndie added a commit to RobertIndie/pulsar that referenced this pull request Sep 5, 2023
RobertIndie added a commit to RobertIndie/pulsar that referenced this pull request Sep 5, 2023
RobertIndie added a commit that referenced this pull request Sep 5, 2023
RobertIndie pushed a commit to apache/pulsar-client-go that referenced this pull request Jun 29, 2026
…tion tests (#1511)

Master Issue: apache/pulsar#21048

Related Issue: #805

### Motivation

In the Java SDK (PR apache/pulsar#21048), the DLQ/RLQ producers are hardcoded with
`enableBatching(false).enableChunking(true)` to ensure large chunked messages can be forwarded to DLQ/RLQ
topics successfully.

However, in the Go SDK, the DLQ/RLQ producers are created using `DLQPolicy.ProducerOptions`, which defaults
to a zero-value `ProducerOptions{}` (i.e., `EnableChunking: false`, `DisableBatching: false`). This means
that if a producer sends a chunked message larger than the broker's `maxMessageSize`, and the consumer is
configured with DLQ/RLQ, the DLQ/RLQ producer will fail to forward the message because it cannot chunk it.

We considered automatically enabling chunking in the DLQ/RLQ producers when the user has not explicitly
configured `ProducerOptions`. However, since `EnableChunking` and `DisableBatching` are both `bool` fields
with a zero value of `false`, there is no way to distinguish between:
- The user intentionally setting them to `false` (meaning "I don't want chunking")
- The user not configuring them at all (meaning "I didn't think about it")

Automatically overriding these values would break backward compatibility for users who explicitly set
`EnableChunking: false`. Therefore, instead of changing the default behavior, we add documentation to
guide users to enable chunking in `DLQPolicy.ProducerOptions` when using chunked messages with DLQ/RLQ.

### Modifications

- Added a note to the `EnableChunking` field documentation in `ProducerOptions` (`producer.go`) explaining
  that when chunking is enabled and the consumer uses DLQ/RLQ, it is recommended to also enable chunking
  in `DLQPolicy.ProducerOptions`.
- Added a note to the `DLQPolicy.ProducerOptions` field documentation (`consumer.go`) explaining that if
  the messages being consumed were produced with chunking enabled, it is recommended to also enable chunking
  here. This helps in decoupled producer/consumer scenarios where the consumer developer may not be aware
  of the producer's chunking configuration.
- Added `TestChunkReconsumeLater` integration test to verify chunked messages (5MB, exceeding broker
  maxMessageSize) can be sent to the retry topic via `ReconsumeLater` and routed to DLQ after exceeding
  max retries, when `DLQPolicy.ProducerOptions` has chunking enabled.
- Added `TestChunkDLQWithNack` integration test to verify chunked messages (5MB) are routed to DLQ
  after exceeding max redelivery count via `Nack`, when `DLQPolicy.ProducerOptions` has chunking enabled.

### Verifying this change

This change added tests and can be verified as follows:

- Added `TestChunkReconsumeLater`: sends a 5MB chunked message, verifies it flows through retry topic
  via `ReconsumeLater` and eventually lands in DLQ after exceeding `MaxDeliveries`.
- Added `TestChunkDLQWithNack`: sends a 5MB chunked message, verifies it is routed to DLQ after
  exceeding max redelivery count via `Nack`, with original properties preserved.

Both tests use payloads larger than broker `maxMessageSize` (~1MB) to ensure that DLQ/RLQ producers
**must** have chunking enabled to successfully forward messages. Without chunking enabled in
`DLQPolicy.ProducerOptions`, these tests would timeout.
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] Dead Letter Policy in Consumer configuration doesn't account for chunking or batching configuration.

4 participants