GeezerVison is showing github.com — [fix][ml] Add ReadOnlyManagedLedger asyncClose interface + production call path by Asthenia0412 · Pull Request #26934 · apache/pulsar
Conversation
Motivation
void-ptr974's PR #26816 fixed a BookKeeper read handle memory leak in ReadOnlyManagedLedgerImpl by overriding asyncClose and getLedgerHandle. However, lhotari's review identified several gaps that this PR addresses.
Modifications
1. Public close path (ReadOnlyManagedLedger interface + Wrapper)
- Added
default CompletableFuture<Void> asyncClose() to ReadOnlyManagedLedger interface — backward-compatible no-op default
ReadOnlyManagedLedgerImplWrapper now delegates asyncClose() to its wrapped implementation
2. Production caller — SnapshotSegmentAbortedTxnProcessorImpl
readSegmentEntries now wraps its work in try/finally and calls managedLedger.asyncClose() in the finally block
- This is the critical fix: without a close path on the interface, every invocation of
readSegmentEntries leaked a ReadOnlyManagedLedger with its BookKeeper read-handle cache
3. ReadOnlyManagedLedgerImpl (cherry-picked from void-ptr974#26816, plus bridge to the new interface)
- Override of the internal
asyncClose(CloseCallback, Object) now:
- Sets
closingOrClosed = true before draining ledgerCache
- Iterates the cache and calls
invalidateReadHandle for each entry, then falls through to super.asyncClose
- Override of
getLedgerHandle(long): guards both before and after the super call against closingOrClosed. If the flag flipped during the cache-insert window, we invalidate the just-inserted future and fail it with ManagedLedgerAlreadyClosedException
- Added a bridging
asyncClose() returning CompletableFuture<Void> so that ReadOnlyManagedLedgerImpl satisfies the new interface while keeping the callback-based path for existing ManagedLedgerImpl users
4. Tests (ReadOnlyManagedLedgerHandleCloseTest)
- Cherry-picked void-ptr974's new test file and adapted it:
testCloseReleasesCachedHandles — verifies cached handles close on normal + fenced close; also exercises double-close
testCloseRejectsSubsequentReadsWithoutOpeningHandles — after close, asyncReadEntry and reopenReadHandle both fail with ManagedLedgerAlreadyClosedException and no new BookKeeper ops are created
testCloseFailureDoesNotSkipOtherCachedHandles — one handle's closeAsync failure does not prevent the cache from being drained
testCloseReleasesHandleWhoseOpenIsPending — the fragile openedHandles.size() == 2 assertion replaced by a relative pre/post count, and the newly-opened handle is captured from the tail of the list; this directly exercises the post-super-call recheck branch of getLedgerHandle
- New
testAsyncCloseOnWrapperDelegatesAndReleasesHandles — creates a wrapper via the factory API (the production path used by SnapshotSegmentAbortedTxnProcessorImpl) and verifies that calling asyncClose() on the interface releases the underlying handles
Fixes
Review
Credit to @lhotari for the review comments on void-ptr974#26816; this PR directly addresses all four raised issues.
Assisted-by: OpenAI Codex
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This PR supersedes void-ptr974#26816 by addressing lhotari's review feedback:
1. Add asyncClose() to ReadOnlyManagedLedger interface and Wrapper
2. Add close call in SnapshotSegmentAbortedTxnProcessorImpl.readSegmentEntries
3. Fix brittle test assertion on openedHandles.size()
4. Add test coverage through factory API
Fixes apache#26815
Assisted-by: OpenAI Codex
codelipenghui reacted with laugh emoji
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.
Assignees
No one assigned
Milestone
No milestone
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.