GeezerVison is showing github.com — [improve][broker] Add a random value upto 5% to avoid rollover multiple cursor ledgers by TakaHiR07 · Pull Request #26542 · apache/pulsar
Conversation
TakaHiR07
commented
Sep 11, 2026
•
edited by merlimat
Loading
Uh oh!
There was an error while loading. Please reload this page.
Motivation
A cursor rolls over its metadata ledger when ManagedCursorImpl.shouldCloseLedger() finds that the ledger
reached managedLedgerCursorMaxEntriesPerLedger entries, or that managedLedgerCursorRolloverTimeInSeconds
passed since the last switch. Both limits are checked exactly. The check runs after every cursor-ledger
append, and from rolloverCursorsInBackground(), which the broker runs for every topic in the same pass
(every retentionCheckIntervalInSeconds).
The rollover clock starts when the cursor is created. So cursors loaded together, such as the subscriptions
of one topic or all topics after a broker restart or a bundle move, reach the timeout in the same pass and
create and switch their ledgers at the same time. That causes a spike of BookKeeper ledger creations and
metadata-store writes:
Topic ledgers already avoid this with a +5% random jitter on the rollover time
(ManagedLedgerImpl.getMaximumRolloverTimeMs). Cursor ledgers had none.
Modifications
- Each
ManagedCursorImpl draws two jitter factors once, in its constructor:
- time:
[1.0, 1.05), the same as the topic ledger rollover;
- entries:
(0.95, 1.0], so that managedLedgerCursorMaxEntriesPerLedger stays a maximum, as documented.
The entries threshold is rounded up, so limits below 20 entries are not changed by the jitter.
shouldCloseLedger() applies the factors to the live config on every check, so config updates are still
picked up. Only the factors are cached, not the thresholds.
- The time threshold is now computed as a
long (* 1000L), so a timeout above ~24 days no longer
overflows int.
With the default 4 h timeout, the rollovers of a cohort spread over up to 12 minutes, about 6 background
checks at the default 120 s interval. Each cursor keeps its jitter, so cohorts keep drifting apart after the
first rollover.
Verifying this change
ManagedCursorTest:
testShouldCloseLedgerObservesUpdatedMetadataMaxEntriesPerLedger and
testShouldCloseLedgerObservesUpdatedLedgerRolloverTimeout check that a config update after openCursor()
is picked up.
testShouldCloseLedgerEntriesJitterStaysWithinConfiguredMax checks, over 50 cursors, that no cursor
rolls over before 95% of the entries limit or goes past it.
- None of them depend on the random draw.
- Existing tests that assert exact rollovers use limits ≤ 10 entries, which the jitter does not change.
ManagedLedgerBkTest.testPeriodicRollover waits 6 s for a 5 s timeout, and the jittered timeout is at most
5.25 s.
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
This approach looks better. Caching only the per-cursor jitter when reading the current config in shouldCloseLedger() avoids the stale-threshold issue from the earlier version.
Could we add a small regression test for the config update case? For example, we could create a cursor with a large metadataMaxEntriesPerLedger, update the managed-ledger config to 1, and verify that shouldCloseLedger() observes the new value.
Using 1 also makes the assertion deterministic because (long) (1 * [1.0, 1.05)) is always 1, meaning the test doesn’t depend on a specific random jitter value.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
TakaHiR07 reacted with thumbs up emoji
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
LGTM. The change is now small and self-contained: each cursor draws its jitter once in its constructor, and shouldCloseLedger derives both thresholds from the live config on every check. That removes the need for a config-update handler and the stale-threshold window during cursor initialization. The two new tests exercise a config swap after openCursor(); they cannot flake on the random draw. One open question remains about the jitter direction (see that thread). It is not a blocker for me.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
The entries jitter is now in (0.95, 1.0], rounded up, so
managedLedgerCursorMaxEntriesPerLedger is never exceeded and limits
below 20 entries keep their exact value. The time jitter stays in
[1.0, 1.05), like the managed ledger rollover.
|
@TakaHiR07 thanks for the change and for working through the review rounds. To help it land, I pushed two commits to the branch:
- a merge of the current
master;
- a fix for the jitter-direction question @Denovo1998 raised. The entries jitter is now
(0.95, 1.0], rounded up, so managedLedgerCursorMaxEntriesPerLedger is never exceeded and limits below 20 entries keep their exact value. The time jitter stays [1.0, 1.05), like the topic ledger rollover. There is a new test that checks the entries bound over 50 cursors.
I also updated the description. Let me know if anything there doesn't match what you intended.
|
TakaHiR07 reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 21, 2026
Reviewers
lhotari
lhotari left review comments
merlimat
merlimat approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|