Skip to content

[improve][broker] Skip the size cap estimate when there are no messages to replay - #26954

Merged
lhotari merged 2 commits into
apache:masterfrom
lhotari:lh-improve-broker-replay-size-cap-apache
Oct 17, 2026
Merged

lhotari merged 2 commits into
apache:masterfrom
lhotari:lh-improve-broker-replay-size-cap-apache

Conversation

@lhotari

@lhotari lhotari commented Oct 16, 2026

Copy link
Copy Markdown
Member

Motivation

PersistentDispatcherMultipleConsumers.readMoreEntries asks getMessagesToReplayNow for the messages to replay before each normal read. getMessagesToReplayNow first estimated how many entries fit the read's byte budget (ManagedCursor.applyMaxSizeCap, which walks the ledgers' metadata from the read position), and only then found that there was nothing to replay, which is the common case. The normal read then estimates it again. In a broker profile of an IoT telemetry scenario (2,000 Key_Shared consumers on 3,000 topics), the estimate was 1.25 % of the broker's CPU samples, half of them in getMessagesToReplayNow.

Modifications

  • getMessagesToReplayNow returns an empty set before the estimate when the delayed delivery tracker has no messages available and the redelivery set is empty.
  • hasMessageAvailable is still called once, so it still updates the delayed delivery tracker's timer.
  • Test: the size cap isn't estimated with nothing to replay, and is estimated with a message to replay.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests and can be verified as follows:

  • Added a test in PersistentStickyKeyDispatcherMultipleConsumersTest that checks the size cap isn't estimated when there's nothing to replay, and is estimated when there's a message to replay.

In the broker profile with this change, getMessagesToReplayNow is 0.05 % of the samples.

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

…es to replay

PersistentDispatcherMultipleConsumers.readMoreEntries asks getMessagesToReplayNow for the messages to replay before
each normal read. getMessagesToReplayNow first estimated the number of entries that fit the read's byte budget
(ManagedCursor.applyMaxSizeCap, which walks the ledgers' metadata from the read position), and then found that there
was nothing to replay, the common case; the normal read then estimates it again. In a profile of 2,000 Key_Shared
consumers on 3,000 topics, the estimate was 1.25 % of the broker's CPU samples, half of it in getMessagesToReplayNow.

getMessagesToReplayNow returns an empty set before the estimate when the delayed delivery tracker has no messages
available and the redelivery set is empty. hasMessageAvailable is called once, as before, so it still updates the
tracker's timer.

Assisted-by: Claude Code (claude-opus-5-5)

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed ba4e4ac. No actionable findings identified within the reviewed scope. The new regression test and existing Shared/Key_Shared delayed-delivery tests passed. One optional, non-blocking test-coverage suggestion inline.

…playNow's skip

Assisted-by: Claude Code (claude-opus-5-5)
@lhotari
lhotari merged commit 258b8fe into apache:master Oct 17, 2026
81 of 83 checks passed
@lhotari lhotari added this to the 5.1.0 milestone Oct 17, 2026
lhotari added a commit that referenced this pull request Oct 17, 2026
merlimat added a commit to void-ptr974/pulsar that referenced this pull request Oct 18, 2026
…ated inline

A factory that completes newTrackerAsync() inline, such as the in-memory
one, never paused the dispatch: the read that found the delayed message is
still in progress. Only resume the dispatch with readMoreEntriesAsync()
when the creation completed later, or on a retry.

testGetMessagesToReplayNowChecksDelayedTrackerOnce (added by apache#26954)
stubbed only newTracker(), so its factory mock returned no tracker from
newTrackerAsync(). It now stubs newTrackerAsync().
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.

2 participants