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][ml] Don't trim ledgers while cursors are lazily recovering by lifepuzzlefun · Pull Request #20814 · apache/pulsar

Conversation

@lifepuzzlefun

@lifepuzzlefun lifepuzzlefun commented Jul 15, 2023 •
edited by merlimat
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Problem

With managedLedgerLazyCursorRecovery=true, the managed ledger finishes opening before its cursors are recovered. A cursor only joins the set that trimming looks at once its recovery completes. Trimming takes the slowest position from that set alone, so it can delete ledgers that a recovering subscription still needs. The subscription then comes back with a position inside deleted ledgers, and its backlog is lost. If no cursor has recovered yet, every closed ledger past retention is deleted, and the default retention is 0.

A failed lazy recovery is worse: the cursor is dropped from tracking for good. The next subscribe then creates a new cursor at the end of the topic, overwriting the stored position.

Only brokers that enable lazyCursorRecovery (off by default) are affected.

Example

  1. A topic has a subscription with a backlog, and the broker restarts with lazy recovery on.
  2. While that cursor's recovery is still reading its cursor ledger, a trim runs. It can be triggered by a ledger rollover, the periodic consumed-ledgers check, another subscription's ack crossing a ledger, or an admin trim.
  3. internalTrimLedgers sees !cursors.hasDurableCursors(), sets slowestReaderLedgerId = currentLedgerId + 1, and deletes the backlog.

Change

  • internalTrimLedgers defers the trim through the existing scheduleDeferredTrimming while uninitializedCursors is non-empty. That is where lazily recovering cursors (and cursors still being created) are tracked.
  • A cursor whose lazy recovery fails is remembered in failedLazyRecoveryCursors. While any are listed, trims fail with an exception instead of running.
  • asyncOpenCursor on such a cursor runs the recovery again, rather than creating a new cursor at Latest.
  • The lazy recovery code is moved into recoverCursorLazily(String). It drops the old "remaining" log attribute, which was never updated in the lazy path.

Testing

Two new tests in ManagedLedgerTest control each cursor's recovery through a factory subclass:

  • testLazyCursorRecoveryDefersTrimming checks the trim is held back with no cursor recovered and with only one recovered, then completes once all are recovered and keeps the needed ledgers.
  • testFailedLazyCursorRecoveryBlocksTrimmingUntilRecovered checks a failed recovery blocks trims, and that reopening the cursor recovers its original position.

Both fail without the fix. The full ManagedLedgerTest suite passes.

Originally proposed by @lifepuzzlefun; reworked onto current master.

Sorry, something went wrong.

Uh oh!

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

All reactions
@lifepuzzlefun
1. check if there is lazy recovery before trim ledgers.
46dc189
2. add programmed readEntry delay in `PulsarMockLedgerHandle`
@github-actions github-actions Bot added the doc-label-missing label Jul 15, 2023
@github-actions

github-actions Bot commented Jul 15, 2023

Copy link
Copy Markdown

@lifepuzzlefun Please add the following content to your PR description and select a checkbox:

- [ ] `doc` <!-- Your PR contains doc changes -->
- [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
- [ ] `doc-not-needed` <!-- Your PR changes do not impact docs -->
- [ ] `doc-complete` <!-- Docs have been already added -->
All reactions

Sorry, something went wrong.

Uh oh!

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

@lifepuzzlefun lifepuzzlefun changed the title [improve][ml] ManagedLedger trim operation should always triggered after all cursor has been recovered. [fix][ml] ManagedLedger trim operation should always triggered after all cursor has been recovered. Jul 15, 2023
@github-actions github-actions Bot added doc-not-needed Your PR changes do not impact docs and removed doc-label-missing labels Jul 15, 2023
@lifepuzzlefun
add more comments in unit test.
a1f615e
@lifepuzzlefun lifepuzzlefun changed the title [fix][ml] ManagedLedger trim operation should always triggered after all cursor has been recovered. [fix][ml] ManagedLedger trim operation should be delayed before lazy cursor recovery finished. Jul 15, 2023
@lifepuzzlefun lifepuzzlefun changed the title [fix][ml] ManagedLedger trim operation should be delayed before lazy cursor recovery finished. [fix][ml] fixlazy cursor recovery Jul 16, 2023
@lifepuzzlefun lifepuzzlefun changed the title [fix][ml] fixlazy cursor recovery [fix][ml] fix datalazy cursor recovery Jul 16, 2023
@lifepuzzlefun lifepuzzlefun changed the title [fix][ml] fix datalazy cursor recovery [fix][ml] fix potential data lose when enable lazy cursor recovery Jul 16, 2023
@Technoboy- Technoboy- added this to the 3.2.0 milestone Jul 31, 2023
@lifepuzzlefun

lifepuzzlefun commented Aug 4, 2023

Copy link
Copy Markdown
Contributor Author

@poorbarcode @mattisonchao Could you take a look this pr ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@poorbarcode poorbarcode assigned lifepuzzlefun Aug 4, 2023
@poorbarcode poorbarcode added type/bug The PR fixed a bug or issue reported a bug release/3.0.2 release/2.11.3 release/2.10.6 labels Aug 4, 2023
poorbarcode
poorbarcode reviewed Aug 4, 2023
View reviewed changes

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

Nice catch!

Sorry, something went wrong.

Uh oh!

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

All reactions
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.

@github-actions

github-actions Bot commented Sep 7, 2023

Copy link
Copy Markdown

The pr had no activity for 30 days, mark with Stale label.

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot added the Stale label Sep 7, 2023
@poorbarcode poorbarcode added release/3.0.3 and removed release/3.0.2 labels Oct 24, 2023
@shibd shibd added release/2.11.4 and removed release/2.11.3 labels Oct 26, 2023
@Technoboy- Technoboy- modified the milestones: 3.2.0, 3.3.0 Dec 22, 2023
@coderzc coderzc modified the milestones: 3.3.0, 3.4.0 May 8, 2024
@lhotari lhotari removed the release/3.0.3 label Oct 10, 2024
@lhotari lhotari modified the milestones: 4.0.0, 4.1.0 Oct 11, 2024
@lhotari lhotari added release/3.0.9 and removed release/3.0.8 labels Dec 6, 2024
@lhotari lhotari added release/3.0.10 and removed release/3.0.9 release/2.10.6 release/2.11.4 release/3.0.10 labels Jan 3, 2025
@lhotari lhotari removed this from the 4.1.0 milestone Jan 6, 2025
merlimat added 2 commits October 22, 2026 17:30
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/adopt-20814-pr
3cf8605
# Conflicts:
#	testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockBookKeeper.java
#	testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java
@merlimat
Gate trimming on uninitializedCursors and handle failed lazy recovery
55d6265
Rework the fix on top of current master. Trimming computed the slowest
position from the cursor container alone, which a lazily recovering cursor
joins only once its recovery completes: with no cursor recovered yet, every
closed ledger beyond retention was deleted.

- Defer trimming while any cursor is in uninitializedCursors, instead of
  failing it, with no extra counter.
- Remember cursors whose lazy recovery failed: they block trimming, and
  opening one recovers it again instead of creating a new cursor at the end
  of the managed ledger, which replaced its persisted position.
- Replace the read-delay mock and its test with tests that gate each
  cursor's recovery through a factory subclass, covering no cursor
  recovered, one recovered, and a failed recovery.
@merlimat merlimat changed the title [fix][ml] fix potential data lose when enable lazy cursor recovery [fix][ml] Don't trim ledgers while cursors are lazily recovering Oct 23, 2026
@merlimat merlimat added this to the 5.1.0 milestone Oct 23, 2026
@merlimat

merlimat commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Thanks @lifepuzzlefun — this is a real data-loss risk and it's still present on master: internalTrimLedgers only looks at cursors, so if no cursor has finished lazy recovery yet it trims everything up to the current ledger. Since the PR was idle and conflicting, I've pushed onto your branch: merged master, and reworked the fix along the lines @poorbarcode suggested. Trimming is now deferred while uninitializedCursors is non-empty, with no separate counter. A cursor whose lazy recovery fails now blocks trimming, and reopening it re-runs the recovery instead of creating it at Latest. The tests now gate each cursor's recovery directly, so the mock BookKeeper changes are gone. @poorbarcode, could you take another look?

All reactions

Sorry, something went wrong.

Uh oh!

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

@merlimat merlimat added the release/5.0.2 label Oct 23, 2026
merlimat
merlimat approved these changes Oct 23, 2026
View reviewed changes
@merlimat
merlimat merged commit 4aa2286 into apache:master Oct 23, 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 23, 2026
@lifepuzzlefun @merlimat @lhotari
[fix][ml] Don't trim ledgers while cursors are lazily recovering (#20814
d3bd6b6
)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 4aa2286)
@lhotari lhotari added the cherry-picked/branch-5.0 label Oct 23, 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
@void-ptr974 void-ptr974 mentioned this pull request Nov 24, 2026
[Bug] Topic deletion can leave offloaded objects when lazy cursor recovery finishes late #27736
Open
2 tasks done
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@lifepuzzlefun @merlimat @lhotari
[fix][ml] Don't trim ledgers while cursors are lazily recovering (apa…
ae9d769
…che#20814)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 4aa2286)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@lifepuzzlefun @merlimat @lhotari
[fix][ml] Don't trim ledgers while cursors are lazily recovering (apa…
930e720
…che#20814)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 4aa2286)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@poorbarcode poorbarcode poorbarcode left review comments
@merlimat merlimat merlimat approved these changes

Uh oh!

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

Assignees

@lifepuzzlefun lifepuzzlefun

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 doc-not-needed Your PR changes do not impact docs release/4.0.15 release/4.2.6 release/5.0.2 Stale type/bug The PR fixed a bug or issue reported a bug

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.

7 participants

@lifepuzzlefun @merlimat @poorbarcode @lhotari @Technoboy- @coderzc @shibd
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.