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] Fail queued adds when a managed ledger is terminated during a ledger rollover by merlimat · Pull Request #26680 · apache/pulsar

Conversation

@merlimat

@merlimat merlimat commented Sep 21, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Motivation

ManagedLedgerImpl.asyncTerminate() sets the Terminated state and closes the current ledger regardless of a ledger rollover that may be in progress (CreatingLedger, or the update of the ledgers list that follows the creation). The rollover callbacks did not check for the terminated state either: createComplete() only special-cased the Closed state, and then either reset the state to ClosedLedger / WriteFailed when the creation failed, or went on to updateLedgersIdsComplete(), which unconditionally set LedgerOpened and re-submitted every queued OpAddEntry to the new ledger.

So a managed ledger terminated while a new ledger was being created became writable again, and the adds queued during the rollover were persisted after the terminated position stored in the metadata. The reproducer sees entries written at 4:0 after the ledger was terminated at 3:0, and the ledger keeps rolling over from there. Readers stop at the terminated position, so those entries are acknowledged to the producer but never delivered.

Scalable topics seal a segment with PersistentTopic.terminate() under producer load, where a rollover in progress is a common state to hit. This is the companion of #26678, which covers the adds that were already in flight on the ledger closed by the terminate (ledgerClosed()): this PR covers the adds that were queued waiting for the next ledger. The two changes touch different code paths.

Modifications

  • ManagedLedgerImpl.createComplete(): if the managed ledger was terminated while the ledger was being created (whether the creation succeeded, failed or timed out), abort the rollover instead of reopening the managed ledger for writes.
  • The ledgers-list update that follows a successful creation checks for the terminated state as well: before each attempt (the update is deferred while metadataMutex is held by another operation, which leaves room for the terminate to complete in between) and in both its success and failure callbacks. Without the check before the attempt, the new ledger would be persisted next to the terminated position, and a terminated managed ledger could later be recovered with a last ledger that no longer exists.
  • Aborting the rollover (abortRolloverAfterTerminate()) keeps the Terminated state, fails the queued adds with ManagedLedgerTerminatedException (as internalAsyncAddEntry() does for adds arriving after the terminate), and closes and deletes the ledger that was just created so that it is not leaked.
  • asyncTerminate() stored the terminated position without taking metadataMutex, so it could race the ledgers-list update of the rollover it had overtaken, both with the same expected version: whichever write lost failed with BadVersionException and fenced the managed ledger, either failing the terminate without storing its position, or flipping the in-memory state from Terminated to Fenced after the terminate had already succeeded. The terminated position is now stored under metadataMutex, deferring the write while another update of the ledgers list is in flight, as all the other updates of the ledgers list do.

Verifying this change

This change added tests and can be verified as follows.

Eight new tests in ManagedLedgerTerminationTest terminate the managed ledger at each point of a rollover in progress, driven by gates on the mock BookKeeper client and on the metadata store rather than by timing:

  • while the creation of the new ledger is pending, and the creation then succeeds, fails, or times out;
  • while the update of the ledgers list is deferred because metadataMutex is held;
  • while the update of the ledgers list is in flight and completes, successfully or not, before the terminate gets to its own metadata update;
  • while the update of the ledgers list is in flight and the terminate gets to its own metadata update before that one completes, in either order the two writes would otherwise be applied.

Each of them asserts that the queued adds fail with ManagedLedgerTerminatedException, that the state stays Terminated, that nothing was written past the terminated position (in memory, in the stored ManagedLedgerInfo, and in BookKeeper, where the created ledger is deleted), that a later add is still rejected, and, for the first one, that the terminated state is what gets recovered on reopen. All of them fail without the fix, and each of the added checks is covered by a test that fails when only that check is removed.

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • 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
@merlimat
[fix][ml] Fail queued adds when a managed ledger is terminated during…
1ff1c14
… a ledger rollover

asyncTerminate() sets the Terminated state and closes the current ledger
regardless of a rollover in progress, and the rollover callbacks did not
check for it: createComplete() only special-cased the Closed state, then
either reset the state on a failed creation or went on to
updateLedgersIdsComplete(), which set LedgerOpened and re-submitted the
queued adds to the new ledger. A managed ledger terminated while a new
ledger was being created became writable again, and the queued adds were
persisted after the terminated position stored in the metadata, where no
reader ever goes.

Check for the Terminated state in createComplete() and around the
ledgers-list update that follows the creation (before each attempt, and in
its success and failure callbacks): keep the state, fail the queued adds
with ManagedLedgerTerminatedException and discard the new ledger.
lhotari
lhotari reviewed Sep 22, 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.

Thanks for working on this rollover termination race. The queued-add cleanup paths look good, but the in-flight metadata update still has a version-conflict ordering that can fence the ledger or leave the termination unstored.

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.

@merlimat
[fix][ml] Serialize the terminate metadata update with the ledgers li…
c1c7715
…st updates

The terminate stored its position without taking the metadata mutex, so
it could race the ledgers list update of a rollover it had overtaken,
both with the same expected version. Whichever write lost failed with
BadVersionException and fenced the managed ledger: either the terminate
failed without storing its position, or the in-memory state flipped from
Terminated to Fenced after the terminate had already succeeded.

Store the terminated position under the metadata mutex, deferring the
update while another one is in flight, as the other updates of the
ledgers list do.
lhotari
lhotari approved these changes Sep 25, 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 termination write now goes through the same metadataMutex as the ledger rollover, so the version race raised in the previous round is closed on both orderings.

Sorry, something went wrong.

Uh oh!

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

All reactions
@lhotari
lhotari merged commit 5eca6a9 into apache:master Sep 25, 2026
44 checks passed

Uh oh!

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

@lhotari lhotari added this to the 5.0.0 milestone Sep 25, 2026
@lhotari lhotari added release/4.2.5 release/4.0.14 labels Sep 25, 2026
@lhotari lhotari mentioned this pull request Sep 25, 2026
[fix][ml] Prevent terminated managed ledger from transitioning back to writable state #25795
Closed
1 of 11 tasks
This was referenced Sep 26, 2026
[Bug][ML] ManagedLedger.terminate() can race with ledger rollover and make a terminated ledger writable again #25858
Closed
[fix][ml] Make managed ledger properties updates atomic #26569
Merged
ascentstream-bot pushed a commit to ascentstream/pulsar that referenced this pull request Oct 1, 2026
@merlimat @lhotari
[fix][ml] Fail queued adds when a managed ledger is terminated during…
4bd67ee
… a ledger rollover (apache#26680)

(cherry picked from commit 5eca6a9)
ascentstream-bot pushed a commit to ascentstream/pulsar that referenced this pull request Oct 2, 2026
@merlimat @lhotari
[fix][ml] Fail queued adds when a managed ledger is terminated during…
4014b3a
… a ledger rollover (apache#26680)

(cherry picked from commit 5eca6a9)
Radiancebobo pushed a commit to Radiancebobo/pulsar that referenced this pull request Oct 8, 2026
@merlimat hechengbo
[fix][ml] Fail queued adds when a managed ledger is terminated during…
c80d55c
… a ledger rollover (apache#26680)
@lhotari lhotari added cherry-picked/branch-4.0 cherry-picked/branch-4.2 labels Oct 13, 2026
@merlimat merlimat mentioned this pull request Oct 18, 2026
[fix][offload] Fix final ledger not being offloaded for terminated topics #25955
Merged
merlimat added a commit to void-ptr974/pulsar that referenced this pull request Oct 18, 2026
@merlimat
Merge remote-tracking branch 'apache/master' into fix/terminated-topi…
7e7ed67
…c-final-ledger-offload

Keep master's storeTerminatedPosition() (apache#26680), which already serializes the
terminate metadata update with the metadata mutex, and refresh the final ledger
info and trigger the offload on top of it. Drop the test that expected the
termination to be stored after a concurrent close, which master now fails on
purpose.

Assisted-by: Claude Code (Opus 5.5)
@merlimat merlimat mentioned this pull request Oct 23, 2026
[fix][ml] Discard a ledger created after its creation was given up on #16647
Merged
@merlimat
merlimat deleted the mmerli/ml-terminate-during-rollover branch October 24, 2026 00:00
@nodece nodece mentioned this pull request Nov 5, 2026
[fix][ml][broker][client] Sync branch-as-4.0 with apache branch-4.0 (2026-09 to 2026-11, 159 commits) ascentstream/pulsar#109
Merged
4 of 5 tasks
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Nov 16, 2026
@merlimat @nodece
[fix][ml] Fail queued adds when a managed ledger is terminated during…
8f95105
… a ledger rollover (apache#26680)

(cherry picked from commit 5eca6a9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari 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 release/4.0.14 release/4.2.5

Projects

None yet

Milestone

5.0.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

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