Skip to content

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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

for (Long2ObjectMap<Roaring64Bitmap> ledgerMap : delayedMessageMap.values()) {
Roaring64Bitmap entryIds = ledgerMap.get(ledgerId);
if (entryIds != null && entryIds.contains(entryId)) {
return true;
}
}
return false;
}

@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.

delayedMessageMap.headMap(cutoffTime).clear();

if (log.isDebugEnabled()) {
log.debug("[{}] Cleared expired delayed messages before {}, remaining messages: {}",
dispatcher.getName(), cutoffTime, getNumberOfDelayedMessages());
}

return CompletableFuture.completedFuture(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,6 @@ public synchronized CompletableFuture<Void> addConsumer(Consumer consumer) {
shouldRewindBeforeReadingOrReplaying = false;
}
redeliveryMessages.clear();
delayedDeliveryTracker.ifPresent(tracker -> {
// Don't clean up BucketDelayedDeliveryTracker, otherwise we will lose the bucket snapshot
if (tracker instanceof InMemoryDelayedDeliveryTracker) {
tracker.clear();
}
});
}

if (isConsumersExceededOnSubscription()) {
Expand Down Expand Up @@ -448,7 +442,10 @@ protected Predicate<Position> createReadEntriesSkipConditionForNormalRead() {
// Filter out and skip read delayed messages exist in DelayedDeliveryTracker
if (delayedDeliveryTracker.isPresent()) {
final DelayedDeliveryTracker deliveryTracker = delayedDeliveryTracker.get();
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());
Comment on lines -451 to 450
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.

}
Expand Down