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
commented
Jul 15, 2023
•
edited by merlimat
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
- A topic has a subscription with a backlog, and the broker restarts with lazy recovery on.
- 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.
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.
2. add programmed readEntry delay in `PulsarMockLedgerHandle`
|
@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 -->
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
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
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
changed the title
[fix][ml] fix datalazy cursor recovery
[fix][ml] fix potential data lose when enable lazy cursor recovery
Jul 16, 2023
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.
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.
|
The pr had no activity for 30 days, mark with Stale label.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
# Conflicts:
# testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockBookKeeper.java
# testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java
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
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
|
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?
|
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
merlimat
merlimat approved these changes
Uh oh!
There was an error while loading. Please reload this page.
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.
|