GeezerVison is showing github.com — [fix][ml] Discard a ledger created after its creation was given up on by lordcheng10 · Pull Request #16647 · apache/pulsar
Conversation
lordcheng10
commented
Jul 18, 2022
•
edited by merlimat
Loading
Uh oh!
There was an error while loading. Please reload this page.
Motivation
A ledger rollover can start two ledger creations at once, and a late creation can then replace the ledger that writes are going to. This can hit any topic whose broker stalls during a rollover. The ledger that gets replaced stays in the ledgers list, open, with no entries recorded, so entries already acked on it are lost. If the older ledger wins, new positions go backwards.
How it happens on master:
createLedgerAfterClosed sets CreatingLedger and queues asyncCreateLedger on the executor. The creation's timeout is only set up once that queued task runs.
- If the executor stalls past 2 ×
metadataOperationsTimeoutSeconds, internalAsyncAddEntry fails the creation by force:
this.createComplete(Code.TimeoutException, null, null); // ctx == null: nothing marks the creation as abandoned
- After
readyToCreateNewLedger(), the next add starts a second creation.
- When the stalled task finally runs, its creation succeeds as well. Both completions update the ledgers list one after the other, and each sets
currentLedger = lh unconditionally, so the last one wins.
This PR originally guarded the update with lh.getId() > currentLedger.getId(). That avoids moving backwards, but the other ledger is still added to the list, left open, and loses the entries written to it, and a lower ledger that was written to first is still replaced.
Modifications
- Each ledger creation now gets a generation number (
ledgerCreationGeneration), captured in the creation's callback through newLedgerCreationCallback().
- A failed completion ends its generation. That covers the forced failure, which still goes through the public
createComplete(rc, lh, ctx).
- A completion from an earlier generation closes and deletes its ledger (
discardStaleLedger) and leaves the state and pending adds alone. This check runs:
- in
createComplete;
- before the ledgers list update, including after a retry that was postponed because the metadata lock was busy;
- when that update returns, whether it succeeded or failed.
- If a stale ledger has already been written to the ledgers list, the next metadata update drops it, as the terminate path from [fix][ml] Fail queued adds when a managed ledger is terminated during a ledger rollover #26680 does. Recovery already skips a missing last ledger.
Verifying this change
- New test
ManagedLedgerErrorsTest.ledgerCreatedAfterItsCreationWasGivenUpOnIsDiscarded. It holds back the first creation the way a stalled executor would, triggers the forced timeout, lets the second creation complete and get written to, then releases the first. It checks that the late ledger is deleted, that currentLedger and the ledgers list are unchanged, and that the next add goes to the current ledger. On master the test fails: the late ledger is never deleted.
ManagedLedgerTest.testCloseManagedLedgerAfterRollover now stubs the 4-argument createComplete, because rollovers no longer pass this as the callback.
- The new test sleeps for about 2 seconds to reach the forced-timeout path.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
/pulsarbot run-failure-checks
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
/pulsarbot run-failure-checks
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@lordcheng10
Looks like we have the lock to ensure concurrency problem, could you describe the detail?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
lordcheng10
commented
Jul 29, 2022
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
@lordcheng10 Looks like we have the lock to ensure concurrency problem, could you describe the detail?
@mattisonchao When there is a serious Full GC, there may be multiple ledgers to update the metadata information on zookeeper at the same time. Suppose we create ledger in the order of ledger1, ledger2, ledger3, ledger4, ledger5, and assume that a comparison occurs. In severe Full GC, ledger5 may grab the metadataMutex lock before ledger4, so ledger5 may trigger the zookeeper callback method operationComplete before ledger4, so that ledger4 may cover ledger5:
|
private void updateLedgersListAfterRollover(MetaStoreCallback<Void> callback, LedgerInfo newLedger) { |
|
if (!metadataMutex.tryLock()) { |
|
// Defer update for later |
|
scheduledExecutor.schedule(() -> updateLedgersListAfterRollover(callback, newLedger), |
|
100, TimeUnit.MILLISECONDS); |
|
return; |
|
} |
|
|
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
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.
When a ledger creation does not complete within twice the metadata
operations timeout, the next add fails it by force. If that creation was
still queued on the executor, its creation timeout was never armed, so
nothing stopped it from completing later. A new creation can be started
in the meantime, and whichever of the two updated the ledgers list last
became the current ledger: the other one was left open in the ledgers
list with no entries recorded, losing the entries already written to it,
and positions could go backwards.
Each ledger creation now gets a generation. A completion that is not
from the creation in progress, at creation, before the ledgers list
update or after it, closes and deletes its ledger and leaves the state
and the pending adds alone.
Assisted-by: Claude Code (claude-opus-5-5)
merlimat
changed the title
[fix][broker]Ensure that the ledgerId updated by currentLedger is monotonically increasing
[fix][ml] Discard a ledger created after its creation was given up on
Oct 23, 2026
|
Thanks @lordcheng10 for finding this, and sorry it sat for so long. The race is real: two ledger creations can be in flight when the forced 2x-timeout failure fires before the first one runs.
Skipping the currentLedger update alone doesn't fully fix it, though: the other ledger stays in the list, open, and the entries written to it are lost. I've pushed a reworked fix to this branch. Each creation now gets a generation, and a completion from a creation that was given up on closes and deletes its ledger, with a test that reproduces the scenario. I merged your branch rather than rewriting its history, and updated the title and description to match.
|
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
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.
|