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] Discard a ledger created after its creation was given up on by lordcheng10 · Pull Request #16647 · apache/pulsar

Conversation

@lordcheng10

@lordcheng10 lordcheng10 commented Jul 18, 2022 •
edited by merlimat
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

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:

  1. createLedgerAfterClosed sets CreatingLedger and queues asyncCreateLedger on the executor. The creation's timeout is only set up once that queued task runs.
  2. 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
  3. After readyToCreateNewLedger(), the next add starts a second creation.
  4. 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.

All reactions
@lordcheng10
update currentLedger when lh.getId() > currentLedger.getId()
10939c1
@lordcheng10

lordcheng10 commented Jul 18, 2022

Copy link
Copy Markdown
Contributor Author

/pulsarbot run-failure-checks

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 doc-not-needed Your PR changes do not impact docs label Jul 18, 2022
@lordcheng10

lordcheng10 commented Jul 18, 2022

Copy link
Copy Markdown
Contributor Author

/pulsarbot run-failure-checks

All reactions

Sorry, something went wrong.

Uh oh!

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

@mattisonchao

mattisonchao commented Jul 18, 2022

Copy link
Copy Markdown
Member

@lordcheng10
Looks like we have the lock to ensure concurrency problem, could you describe the detail?

All reactions

Sorry, something went wrong.

Uh oh!

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

@lordcheng10

lordcheng10 commented Jul 29, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

@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:

pulsar/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java

Lines 1559 to 1566 in 4d64e2e

private void updateLedgersListAfterRollover(MetaStoreCallback<Void> callback, LedgerInfo newLedger) {
if (!metadataMutex.tryLock()) {
// Defer update for later
scheduledExecutor.schedule(() -> updateLedgersListAfterRollover(callback, newLedger),
100, TimeUnit.MILLISECONDS);
return;
}

All reactions

Sorry, something went wrong.

Uh oh!

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

@lordcheng10

lordcheng10 commented Jul 29, 2022

Copy link
Copy Markdown
Contributor Author

@codelipenghui @hangc0276 PTAL,thanks!

All reactions

Sorry, something went wrong.

Uh oh!

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

@Technoboy-
Technoboy- requested review from BewareMyPower, codelipenghui, gaoran10, hangc0276, merlimat, nodece and zymap August 3, 2022 04:27
@github-actions

github-actions Bot commented Sep 3, 2022

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 3, 2022
@Technoboy- Technoboy- added this to the 3.2.0 milestone Jul 31, 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 modified the milestones: 4.0.0, 4.1.0 Oct 14, 2024
@coderzc coderzc modified the milestones: 4.1.0, 4.2.0 Sep 1, 2025
merlimat added 2 commits October 23, 2026 09:09
@merlimat
[fix][ml] Discard a ledger created after its creation was given up on
74b3d53
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
Merge the PR branch: the generation guard supersedes the ledger ID check
fcfdc3d
@merlimat merlimat modified the milestones: 4.2.0, 5.1.0 Oct 23, 2026
@merlimat 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
@merlimat

merlimat commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

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.

All reactions

Sorry, something went wrong.

Uh oh!

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

merlimat
merlimat approved these changes Oct 23, 2026
View reviewed changes
@merlimat
Merge branch 'master' into update_current_ledger
22f8799
@merlimat
merlimat merged commit 5b53713 into apache:master Oct 24, 2026
81 of 83 checks passed

Uh oh!

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

lhotari pushed a commit that referenced this pull request Oct 26, 2026
@lordcheng10 @merlimat @lhotari
[fix][ml] Discard a ledger created after its creation was given up on (…
3560dc9
…#16647)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 5b53713)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Oct 26, 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
@lordcheng10 @merlimat @lhotari
[fix][ml] Discard a ledger created after its creation was given up on (…
319f0d1
…apache#16647)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 5b53713)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@lordcheng10 @merlimat @lhotari
[fix][ml] Discard a ledger created after its creation was given up on (…
4f31575
…apache#16647)

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

Reviewers

@merlimat merlimat merlimat approved these changes
@BewareMyPower BewareMyPower Awaiting requested review from BewareMyPower
@codelipenghui codelipenghui Awaiting requested review from codelipenghui
@gaoran10 gaoran10 Awaiting requested review from gaoran10
@hangc0276 hangc0276 Awaiting requested review from hangc0276
@zymap zymap Awaiting requested review from zymap
@nodece nodece Awaiting requested review from nodece

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 doc-not-needed Your PR changes do not impact docs release/4.0.15 release/4.2.6 release/5.0.2 Stale

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.

6 participants

@lordcheng10 @mattisonchao @merlimat @lhotari @Technoboy- @coderzc
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.