Repository navigation
[fix][ml] Fix BookKeeper metadata listener leaks when unloading fenced managed ledgers - #27692
Conversation
Assisted-by: Codex (GPT-6.2)
Assisted-by: Codex (GPT-6.2)
Assisted-by: Codex (GPT-6.2)
Assisted-by: Codex (GPT-6.2)
lhotari
left a comment
There was a problem hiding this comment.
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.
Assisted-by: Codex (GPT-6.2)
|
@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
left a comment
There was a problem hiding this comment.
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.
|
Thanks for confirming it on both revisions and opening #27737. Keeping it separate from this PR makes sense. |
…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
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.
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.Closed. Unload can leave the old instance inFencedorFencedForDeletion, and late rollover failures can overwrite the lifecycle state. A lazy recovery can therefore register a cursor after unload and return success to anopenCursorcall waiting on the old broker. Its read handle also remains registered.Modifications
Fencedfor a later explicit delete of the same instance. Give cursors recovered inFencedForDeletionto the existing deletion flow in an inactive state before clearing the pending recovery.FencedandClosedacross late BookKeeper creation failures and ledger-list metadata update failures.Verifying this change
quickCheckreports 89 existingSummaryJavadocviolations in unchanged source lines.Does this pull request potentially affect any of the following areas?