GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][broker] fix delayedMessagesCount error in InMemoryDelayedDeliveryTracker by TakaHiR07 · Pull Request #25076 · apache/pulsar

Conversation

@TakaHiR07

@TakaHiR07 TakaHiR07 commented Dec 16, 2025

Copy link
Copy Markdown
Contributor

Motivation

Occur a NPE issue in delay message. And then find the reason is in delayedMessagesCount. When InMemoryDelayedDeliveryTracker#addMessage(), it don't judge whether the entryId is exist in roaringbitmap, that result in the delayedMessagesCount of the map size is not correct.

企业微信截图_bb4f30f2-8829-413d-a7e8-e8746dc07adc

Modifications

  1. add test to test the duplicate entry case
  2. check whether roaring64Bitmap contains entryId
  3. log error for the case of "n < 0" in getScheduledMessages(), since this case should not occur

Verifying this change

  • Make sure that the change passes the CI checks.

Documentation

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

thetumbled reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Dec 16, 2025
@Technoboy- Technoboy- added the ready-to-test label Dec 16, 2025
@Technoboy- Technoboy- assigned TakaHiR07 Dec 16, 2025
@Technoboy- Technoboy- added this to the 4.2.0 milestone Dec 16, 2025
@Technoboy- Technoboy- added release/3.0.16 release/4.0.9 release/4.1.3 labels Dec 16, 2025
lhotari
lhotari reviewed Dec 16, 2025
View reviewed changes
Comment thread ...ar-broker/src/main/java/org/apache/pulsar/broker/delayed/InMemoryDelayedDeliveryTracker.java Outdated
.computeIfAbsent(timestamp, k -> new Long2ObjectRBTreeMap<>())
.computeIfAbsent(ledgerId, k -> new Roaring64Bitmap());
if (!roaring64Bitmap.contains(entryId)) {
roaring64Bitmap.add(entryId);

@lhotari lhotari Dec 16, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It looks like .addLong should be used in the Roaring64Bitmap API.

Suggested change
roaring64Bitmap.add(entryId);
roaring64Bitmap.addLong(entryId);

The .add method works too, but the method signature takes a long array (long...). Perhaps the compiler is able to optimize that, so it might not make a difference.

It's unfortunate that Roaring64Bitmap doesn't have the checkedAdd method as there is in RoaringBitmap. That would eliminate the need for the .contains check.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TakaHiR07 TakaHiR07 Dec 16, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yes, unfortunately.

"add" or "addLong" I guess is the same after compile, sure it may be better to use "addLong" directly.

Besides, I think there is no need to consider concurrent situation in InMemoryDelayedDeliveryTracker. I don't see any code point out that concurrent situation would occur.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Denovo1998 Denovo1998 Dec 16, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Threading question: addMessage() and getScheduledMessages() are invoked under synchronized (this) in the dispatcher (e.g. PersistentDispatcherMultipleConsumers#trackDelayedDelivery), but clearDelayedMessages() doesn’t seem synchronized and InMemoryDelayedDeliveryTracker#clear() isn’t synchronized either.

Is clear() guaranteed to be called under the same lock, or should we align with BucketDelayedDeliveryTracker#clear() (synchronized) to avoid concurrent access to delayedMessageMap/bitmaps?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@lhotari lhotari Jan 2, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@TakaHiR07 Please check the previous comment by @Denovo1998.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lhotari

lhotari commented Dec 16, 2025

Copy link
Copy Markdown
Member

btw. this code location was discussed in the review: https://github.com/apache/pulsar/pull/24430/changes#r2156278377

thetumbled reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Denovo1998
Denovo1998 requested changes Dec 16, 2025
View reviewed changes

@Denovo1998 Denovo1998 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Left some comments.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...ar-broker/src/main/java/org/apache/pulsar/broker/delayed/InMemoryDelayedDeliveryTracker.java
Comment on lines 137 to 139
updateTimer();

checkAndUpdateHighest(deliverAt);

@Denovo1998 Denovo1998 Dec 16, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

One thought: should updateTimer() and checkAndUpdateHighest(deliverAt) run only when we actually insert a new entryId?

With the current structure, duplicate addMessage() calls still update highestDeliveryTimeTracked / messagesHaveFixedDelay, which could disable the fixed-delay optimization even though the tracker state didn’t change.”

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TakaHiR07 TakaHiR07 Dec 19, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Great catch. I have checked the earliest fixed delay implementation in #16609. In the earliest implementation, the issue is already exist. When duplicate addMessage(), highestDeliveryTimeTracked is ok, but messagesHaveFixedDelay would be set to false incorrectly.

I would check why exist duplicate addMessage() later. And I prefer that we open another pr to fix the additional issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...ar-broker/src/main/java/org/apache/pulsar/broker/delayed/InMemoryDelayedDeliveryTracker.java
Comment on lines +210 to +211
log.error("[{}] Delayed message tracker getScheduledMessages should not < 0, number is: {}",
dispatcher.getName(), n);

@Denovo1998 Denovo1998 Dec 16, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

About the new n < 0 branch: this should be unreachable in normal flow. One potential way to hit it is int overflow from int cardinality = (int) entryIds.getLongCardinality().

Would it be better to keep cardinality as long (and compare cardinality <= (long) n) to eliminate overflow, instead of only logging when n < 0?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TakaHiR07 TakaHiR07 Dec 17, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You are right. I think it is another issue and both use long value is better. Do you think we fix it in this pr or you push another pr to fix?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pulsar-broker/src/test/java/org/apache/pulsar/broker/delayed/InMemoryDeliveryTrackerTest.java



// case2: addMessage() with duplicate entryId,

@Denovo1998 Denovo1998 Dec 16, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

In case2 the comment says it enters cardinality > n, but with getScheduledMessages(10) and 4 unique entryIds it should hit the cardinality <= n branch. Could we adjust the comment to match the scenario (case3 seems to be the one exercising cardinality > n)?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@TakaHiR07 TakaHiR07 Dec 17, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yes. have changed the comment

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Denovo1998

Denovo1998 commented Dec 16, 2025

Copy link
Copy Markdown
Contributor

btw. this code location was discussed in the review: https://github.com/apache/pulsar/pull/24430/changes#r2156278377

@thetumbled Do you have a chance to review the current PR?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@thetumbled

thetumbled commented Dec 17, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

btw. this code location was discussed in the review: https://github.com/apache/pulsar/pull/24430/changes#r2156278377

Maybe we should figure out the reason why duplicate entry IDs are added multiple times if this class does not intentionally allow that behavior.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari
lhotari requested a review from Copilot December 17, 2025 10:39
Copilot started reviewing on behalf of lhotari December 17, 2025 10:40
View session
Copilot AI reviewed Dec 17, 2025
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a bug in the InMemoryDelayedDeliveryTracker where duplicate message entries could cause incorrect delayed message counts, potentially leading to NPE issues. The fix adds a duplicate check before incrementing the counter and improves error handling for edge cases.

Key changes:

  • Add duplicate entry check in addMessage() using Roaring64Bitmap.contains() before adding entries
  • Improve error handling in getScheduledMessages() to explicitly handle and log the n < 0 case
  • Add comprehensive test coverage for duplicate entry scenarios across multiple test cases

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pulsar-broker/src/main/java/org/apache/pulsar/broker/delayed/InMemoryDelayedDeliveryTracker.java Implements duplicate entry check in addMessage() and enhances error handling in getScheduledMessages()
pulsar-broker/src/test/java/org/apache/pulsar/broker/delayed/InMemoryDeliveryTrackerTest.java Adds comprehensive test method testDelayedMessagesCountWithDuplicateEntryId() covering three scenarios: multiple timestamps with duplicates, single timestamp with duplicates, and partial retrieval with duplicates

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
fanjianye added 3 commits December 18, 2025 11:58
fanjianye
fix error in delayedMessagesCount
712e2f6
fanjianye
use addLong
ca48bb3
fanjianye
change comment
9b562dc
@TakaHiR07
TakaHiR07 force-pushed the fix_delayedMessagesCount_error branch 2 times, most recently from d12846f to 9b562dc Compare December 18, 2025 03:59
@lhotari lhotari added release/3.0.17 release/4.1.4 release/4.0.10 and removed release/3.0.16 release/4.1.3 release/4.0.9 labels Feb 9, 2026
@lhotari lhotari added release/3.0.18 release/4.0.11 and removed release/3.0.17 release/4.0.10 labels Apr 27, 2026
@lhotari lhotari modified the milestones: 4.2.0, 4.3.0 Apr 27, 2026
@lhotari lhotari added the release/4.2.2 label Apr 27, 2026
@lhotari lhotari added release/4.2.3 release/4.0.12 and removed release/4.2.2 release/4.0.11 labels Jun 8, 2026
@lhotari

lhotari commented Jun 10, 2026

Copy link
Copy Markdown
Member

#25681 addresses the same issue

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari

lhotari commented Jun 12, 2026

Copy link
Copy Markdown
Member

btw. this code location was discussed in the review: https://github.com/apache/pulsar/pull/24430/changes#r2156278377

Maybe we should figure out the reason why duplicate entry IDs are added multiple times if this class does not intentionally allow that behavior.

@thetumbled One reason finally came up to the surface with PR #26012 (reported issue is #25996)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari added release/4.2.4 release/4.0.13 and removed release/4.2.3 release/4.0.12 labels Jun 30, 2026
@lhotari lhotari added release/4.2.5 release/4.0.14 and removed release/4.2.4 release/4.0.13 labels Jul 27, 2026
@lhotari

lhotari commented Aug 28, 2026

Copy link
Copy Markdown
Member

closing this one since #25681 is merged and addressed the same problem.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari closed this Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari left review comments
@Denovo1998 Denovo1998 Denovo1998 requested changes
Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@TakaHiR07 TakaHiR07

Labels

doc-not-needed Your PR changes do not impact docs ready-to-test release/3.0.18 release/4.0.14 release/4.1.4 release/4.2.5

Projects

None yet

Milestone

5.0.0-M1

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@TakaHiR07 @lhotari @Denovo1998 @thetumbled @Technoboy-
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.