Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][broker]Optimize InMemoryDelayedDeliveryTracker by maintaining state #23918

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yunmaoQu
Copy link

@yunmaoQu yunmaoQu commented Feb 2, 2025

Fixes #23912

Main Issue: #23912

PIP: #xyz

Motivation

In the current delayed message delivery, there's an opportunity to reduce unnecessary reads to storage.

Modifications

It would be useful to keep state also in the InMemoryDelayedDeliveryTracker and skip reading delayed messages when the information is already available for the delivery time of a specific entry.

Verifying this change

  • Make sure that the change passes the CI checks.

This change is already covered by existing tests.

Does this pull request potentially affect one of the following parts:

  • 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:
yunmaoQu#2

@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Feb 2, 2025
@yunmaoQu yunmaoQu changed the title [improve]Optimize InMemoryDelayedDeliveryTracker by maintaining state [improve][broker]Optimize InMemoryDelayedDeliveryTracker by maintaining state Feb 2, 2025
@Override
public CompletableFuture<Void> clear() {
this.delayedMessageMap.clear();
long cutoffTime = getCutoffTime();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only clear expired index?

Copy link
Author

@yunmaoQu yunmaoQu Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dao-jun The decision to clear only expired indices in the clear() method of the InMemoryDelayedDeliveryTracker is aimed at optimizing performance and maintaining the logical state of the message delivery system.

By focusing on expired messages, we reduce the overhead associated with clearing and re-adding valid messages, which enhances performance, especially in scenarios with a high volume of delayed messages. This approach also allows us to retain the state of valid messages, enabling more efficient message delivery without needing to re-read them from storage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that it will introduce memory leak problem. For example, we need to clear all delayed messages at method:
org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers#clearDelayedMessages

IMO, the payoff of re-adding valid messages for once is acceptable, while the risk of such change is great.

@@ -218,9 +218,26 @@ public NavigableSet<Position> getScheduledMessages(int maxMessages) {
return positions;
}

public boolean shouldSkipMessage(long ledgerId, long entryId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it better to keep the method name with BucketDelayedDeliveryTracker, change shouldSkipMessage to contains.

Copy link
Author

@yunmaoQu yunmaoQu Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yunmaoQu
Copy link
Author

yunmaoQu commented Feb 4, 2025

Hi @dao-jun , I'm in need of your review on this PR to move forward with the feature. Could you please check it out as soon as you can? Thanks a lot!

Copy link
Member

@thetumbled thetumbled left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you help to add unit test to cover the logic changed?
We need to ensure that such kind of skipping reading delayed messages won't impact the normal read when we want to dispatch them.
And i wonder that how much improvement this pr can offer? Do unnecessary reads occur frequently or not? In what cases the unnecessary reads will occur? If the answer is positive, i am glad to accept this improvement and adopt it in my production.
@yunmaoQu @lhotari

@Override
public CompletableFuture<Void> clear() {
this.delayedMessageMap.clear();
long cutoffTime = getCutoffTime();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that it will introduce memory leak problem. For example, we need to clear all delayed messages at method:
org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers#clearDelayedMessages

IMO, the payoff of re-adding valid messages for once is acceptable, while the risk of such change is great.

Comment on lines -451 to 450
if (deliveryTracker instanceof BucketDelayedDeliveryTracker) {
if (deliveryTracker instanceof InMemoryDelayedDeliveryTracker) {
skipCondition = position -> ((InMemoryDelayedDeliveryTracker) deliveryTracker)
.shouldSkipMessage(position.getLedgerId(), position.getEntryId());
} else if (deliveryTracker instanceof BucketDelayedDeliveryTracker) {
skipCondition = position -> ((BucketDelayedDeliveryTracker) deliveryTracker)
.containsMessage(position.getLedgerId(), position.getEntryId());
Copy link
Member

@thetumbled thetumbled Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change shouldSkipMessage to contains to avoid duplicate code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-not-needed Your PR changes do not impact docs
Projects
None yet
3 participants