[improve][broker] Support consumer side delayed messages #24372
+450
−1
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 #xyz
Main Issue: #xyz
PIP: #xyz
Motivation
When consumers encounter transient failures while processing messages, a common requirement is to retry processing after a certain delay. Before this feature, developers typically resorted to:
These existing solutions introduced varying degrees of overhead, complexity, or inflexibility. This change aims to provide a native, efficient mechanism for consumer-side delayed message redelivery.
Modifications
This change introduces the capability for Pulsar consumers to negatively acknowledge (Nack) messages with a specified custom delay. The key modifications include:
Client API Extension:
negativeAcknowledge(MessageId messageId, long delay, TimeUnit unit)
andnegativeAcknowledge(Message<?> message, long delay, TimeUnit unit)
have been added to theorg.apache.pulsar.client.api.Consumer
interface.Protocol Enhancement:
CommandRedeliverUnacknowledgedMessages
protobuf command has been augmented with an optionaldelay_at_time
(uint64) field. This field carries the absolute timestamp at which the message is expected to be redelivered.Broker-Side Core Logic Adjustments:
ServerCnx
: Updated to recognize and process thedelay_at_time
field inCommandRedeliverUnacknowledgedMessages
.Consumer
/Subscription
/Dispatcher
:delayAtTime
.PersistentDispatcherMultipleConsumers
(and its classic variant) now leverage the topic-levelDelayedDeliveryTracker
to manage Nacked messages with a specified delay. The message's position and the targetdelayAtTime
are added to the tracker, which triggers redelivery when the specified time is reached.Shared
andKey_Shared
subscription types to provide precise delay semantics.With these modifications, consumers can directly request the broker to redeliver a message after a specific delay, leading to:
Verifying this change
(Please pick either of the following options)
This change is a trivial rework / code cleanup without any test coverage.
(or)
This change is already covered by existing tests, such as (please describe tests).
(or)
This change added tests and can be verified as follows:
(example:)
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
doc
doc-required
doc-not-needed
doc-complete
Matching PR in forked repository
PR in forked repository: