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 — [improve][broker] Add a random value upto 5% to avoid rollover multiple cursor ledgers by TakaHiR07 · Pull Request #26542 · apache/pulsar

Conversation

@TakaHiR07

@TakaHiR07 TakaHiR07 commented Sep 11, 2026 •
edited by merlimat
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

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:

metadata store latency during simultaneous cursor ledger rollovers

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.

All reactions
fanjianye
add a random value upto 5% to avoid rollover multiple cursor ledgers …
907ae39
…at the same time
@TakaHiR07
TakaHiR07 force-pushed the improve_avoid_rollover_multiple_cursor_ledger branch from beebec8 to 907ae39 Compare September 11, 2026 08:34
lhotari
lhotari reviewed Sep 11, 2026
View reviewed changes
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java Resolved

Uh oh!

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

Denovo1998
Denovo1998 requested changes Sep 18, 2026
View reviewed changes
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedCursor.java Resolved

Uh oh!

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

Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java Resolved

Uh oh!

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

@TakaHiR07
TakaHiR07 requested review from Denovo1998 and lhotari September 21, 2026 07:29
fanjianye
only cache jitter value in ManagedCursorImpl
1fa0059
@TakaHiR07
TakaHiR07 force-pushed the improve_avoid_rollover_multiple_cursor_ledger branch from 69b6e0d to 1fa0059 Compare September 22, 2026 03:07
Denovo1998
Denovo1998 reviewed Sep 22, 2026
View reviewed changes
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java Resolved

Uh oh!

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

Denovo1998
Denovo1998 reviewed Sep 22, 2026
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.

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
All reactions
  • 👍 1 reaction
fanjianye
add test
070c186
Denovo1998
Denovo1998 approved these changes Sep 23, 2026
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.

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
lhotari
lhotari reviewed Oct 12, 2026
View reviewed changes

@lhotari lhotari left a comment

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.

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.

All reactions
merlimat added 2 commits October 19, 2026 11:49
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/adopt-26542-…
49f95b4
…cursor-rollover-jitter
@merlimat
Keep the cursor ledger entries limit a maximum when jittering rollover
b82d82a
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.
@merlimat

merlimat commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@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
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

merlimat
merlimat approved these changes Oct 21, 2026
View reviewed changes
@merlimat merlimat added this to the 5.1.0 milestone Oct 21, 2026
@merlimat
merlimat merged commit 586a0ba into apache:master Oct 21, 2026
44 checks passed

Uh oh!

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

lhotari pushed a commit that referenced this pull request Oct 22, 2026
@TakaHiR07 fanjianye
@merlimat @lhotari
[improve][broker] Add a random value upto 5% to avoid rollover multip…
da4a1c5
…le cursor ledgers (#26542)

Co-authored-by: fanjianye <[email protected]>
Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 586a0ba)
@lhotari lhotari added release/5.0.1 cherry-picked/branch-5.0 labels Oct 22, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@TakaHiR07 fanjianye
@merlimat @lhotari
[improve][broker] Add a random value upto 5% to avoid rollover multip…
1f07bac
…le cursor ledgers (apache#26542)

Co-authored-by: fanjianye <[email protected]>
Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 586a0ba)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@TakaHiR07 fanjianye
@merlimat @lhotari
[improve][broker] Add a random value upto 5% to avoid rollover multip…
263dcaf
…le cursor ledgers (apache#26542)

Co-authored-by: fanjianye <[email protected]>
Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 586a0ba)
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
@merlimat merlimat merlimat approved these changes
@Denovo1998 Denovo1998 Denovo1998 approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.1

Projects

None yet

Milestone

5.1.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

4 participants

@TakaHiR07 @merlimat @lhotari @Denovo1998
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.