Skip to content

[fix][ml] Fix BookKeeper metadata listener leaks when unloading fenced managed ledgers - #27692

Merged
lhotari merged 6 commits into
apache:masterfrom
void-ptr974:fix/fenced-cursor-read-handles
Nov 26, 2026
Merged

lhotari merged 6 commits into
apache:masterfrom
void-ptr974:fix/fenced-cursor-read-handles

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #27670

Motivation

When a broker unloads a fenced writable managed ledger, its BookKeeper client can retain read handles for recovered durable cursors through the metadata listener registry.

  • Recovery completes before fencing. The fenced asyncClose() path invalidates cached data-ledger read handles and skips cursor close. The recovered cursor holds a separate read handle to its cursor ledger, leaving that handle's metadata listener registered after unload.
  • Recovery completes after fencing. Cursor registration checks for Closed. Unload can leave the old instance in Fenced or FencedForDeletion, and late rollover failures can overwrite the lifecycle state. A lazy recovery can therefore register a cursor after unload and return success to an openCursor call waiting on the old broker. Its read handle also remains registered.

Modifications

  • Close locally held cursor read handles during fenced unload using the same cleanup as the writer-close failure path. The close unregisters the listeners and preserves the cursor metadata and BookKeeper ledgers for the next owner.
  • Reject recovered-cursor opens that finish after fencing or close. Release the local recovery handle and fail the pending open.
  • Reject new termination attempts on a closed managed ledger. Duplicate requests share an in-flight termination or reuse its successful result without changing the ledger state.
  • Retain recovered cursors in Fenced for a later explicit delete of the same instance. Give cursors recovered in FencedForDeletion to the existing deletion flow in an inactive state before clearing the pending recovery.
  • Preserve Fenced and Closed across late BookKeeper creation failures and ledger-list metadata update failures.

Verifying this change

  • Added deterministic coverage for recovery completing before fencing and after fenced unload. Both cases fail on the unpatched base and pass with this change. The tests verify listener release, metadata and ledger preservation, and recovery by the next owner.
  • Covered both orderings of deletion and lazy cursor recovery, plus a failed delete followed by unload and a same-instance retry. The tests verify failed opens, listener release on unload, and deletion of cursor metadata and its BookKeeper ledger.
  • Covered late recovery while the writer-close callback is pending, and late rollover failures from BookKeeper creation and ledger-list metadata updates.
  • Verified that a duplicate terminate after close waits for an in-flight metadata write, returns the same final position, and leaves the ledger closed. A later duplicate reuses the result without another metadata write.
  • Passed 72 targeted managed-ledger tests, including the complete cursor handle lifecycle, managed ledger error, and managed ledger termination test classes. Spotless and test Checkstyle passed. The module quickCheck reports 89 existing SummaryJavadoc violations in unchanged source lines.

Does this pull request potentially affect any of the following areas?

  • 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

@void-ptr974 void-ptr974 changed the title [fix][ml] Release recovered cursor read handles on fenced unload [fix][ml] Fix BookKeeper metadata listener leaks when unloading fenced managed ledgers Nov 22, 2026
@void-ptr974
void-ptr974 marked this pull request as draft November 22, 2026 07:31
@void-ptr974
void-ptr974 marked this pull request as ready for review November 22, 2026 08:48

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for tracking down these unload races and for the deterministic lifecycle tests. They make the fix easy to follow.

I traced the state handling in ManagedLedgerImpl. Lazy recovery that completes in Fenced, FencedForDeletion or Closed now releases its local read handle and fails the pending open. Cursors parked in cursorsRecoveredWhileFenced are only touched under the instance lock, and setFencedForDeletion hands them over before the truncate snapshot. The fenced asyncClose path now releases the recovered cursor handles. The new early returns in createComplete and the ledger-list operationFailed callback leave no pending adds behind, and they leave the mutex unlocked. Closed is already handled at the top of createComplete.

A few small points inline: the description is out of date with the final commit, there is one misleading comment, and one test assertion is loose.

One related gap that predates this PR, possibly for a follow-up: the new-cursor path in asyncOpenCursor (initializationCallback.operationComplete, ManagedLedgerImpl.java:1326) still calls setActive()/addCursor() and reports success when fencing or close happens while the cursor's metadata write is in flight. No read handle leaks there, since a new cursor starts in NoLedger, but the open can still succeed on the old owner, the same symptom this PR fixes for recovered cursors.

@void-ptr974

Copy link
Copy Markdown
Contributor Author

@lhotari, following up on the new-cursor race from your review. I reproduced it on both the base and the PR revision. A pending durable cursor open can still succeed on the old owner after close or fencing. I opened #27737 to track the fix.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the quick follow-up, for updating the description and for opening the follow-up issues.

The reordered guard in asyncTerminate (ManagedLedgerImpl.java:1841-1851) reads well. A terminate that arrives after close now shares an in-flight attempt or reuses its successful result. Only a closed ledger with no attempt, or a failed one, is rejected, so the terminated-then-closed case keeps returning the cached position and the state stays Closed. duplicateTerminateAfterCloseSharesExistingAttempt covers both the pending and the completed duplicate and checks there is only one metadata write. The reworded recovery comment and the added Closed state assertion address my other inline points.

@lhotari

lhotari commented Nov 25, 2026

Copy link
Copy Markdown
Member

Thanks for confirming it on both revisions and opening #27737. Keeping it separate from this PR makes sense.

@lhotari
lhotari merged commit 6fe7b61 into apache:master Nov 26, 2026
43 checks passed
Jeremiah-D added a commit to Jeremiah-D/pulsar that referenced this pull request Nov 27, 2026
…eletion

A cursor whose lazy recovery finishes after asyncDelete starts registers
with its stale mark-delete position (by design since apache#27692, so the delete
can see it), but that position then pins the truncate inside the delete
and the offloaded ledgers are never selected for cleanup. The final
delete only removes BookKeeper ledgers and metadata, so the offloaded
copies are orphaned (see apache#27736).

When the ledger is fenced for deletion no cursor will ever read again,
so internalTrimLedgers treats it as having no durable cursors and the
delete's truncate selects every ledger but current, letting
asyncDeleteLedger clean up each offloaded copy.

Fixes apache#27736
lhotari pushed a commit that referenced this pull request Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Fenced managed ledger unload can leak recovered cursor read handles

3 participants