GeezerVison is showing github.com — [fix][ml] Close the backing managed ledger when a factory ReadOnlyCursor is closed by Drifter-yh · Pull Request #27782 · apache/pulsar
Conversation
Drifter-yh
commented
Nov 28, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes #27759
Motivation
ManagedLedgerFactory#openReadOnlyCursor / asyncOpenReadOnlyCursor create a ReadOnlyManagedLedgerImpl internally and return only a ReadOnlyCursor built on top of it. ReadOnlyCursorImpl#asyncClose closes the cursor, but not the backing read-only managed ledger. The read handles cached for the entries read through the cursor therefore stay open for the lifetime of the factory. The same applies to offloaded read handles.
The managed-ledger close in #26816 covers a caller-owned read-only ledger. The factory cursor API still does not own the ledger it opens.
Modifications
- Add
OwnedReadOnlyCursorImpl, a ReadOnlyCursorImpl that closes the read-only managed ledger from asyncClose after closing the cursor.
- Make
ReadOnlyManagedLedgerImplWrapper / ReadOnlyManagedLedgerImpl create that cursor for the factory path only. Cursors created through ReadOnlyManagedLedger#createReadOnlyCursor(Position) stay on ReadOnlyCursorImpl because the caller owns the ledger.
- Document the ownership of the cursor returned by the factory API, including that in-flight reads may fail when the cursor is closed.
- Add
ReadOnlyCursorCloseTest:
- opens a factory cursor over two ledgers, reads entries so both read handles are cached, closes the cursor and verifies both handles are closed and the read-only ledger is
Closed;
- covers
asyncClose;
- covers close without reads;
- verifies a cursor created on a caller-owned ledger leaves the ledger and its read handle open;
- verifies a writable ledger can be reopened and read after the factory cursor is closed.
Without the fix, verify(handle).closeAsync() fails with WantedButNotInvoked.
Scope
Closing a factory cursor closes the read-only managed ledger and releases its cached read handles. The factory's name-keyed entry cache is unchanged; #26816 changes independently opened read-only ledgers to use a private cache, which this change is compatible with.
Verifying this change
-
./gradlew :managed-ledger:test --tests org.apache.bookkeeper.mledger.impl.ReadOnlyCursorCloseTest (5 tests pass)
-
./gradlew :managed-ledger:test --tests org.apache.bookkeeper.mledger.impl.ReadOnlyCursorTest --tests org.apache.bookkeeper.mledger.impl.ReadOnlyManagedLedgerImplTest --tests org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryShutdownTest (21 tests pass)
-
./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.transaction.TransactionProduceTest.produceAndCommitTest (1 test passes)
-
./gradlew :managed-ledger:spotlessJavaCheck :managed-ledger:checkstyleMain :managed-ledger:checkstyleTest
- Full
:managed-ledger:test: 1492 tests, 1427 passed, 60 skipped, 5 failed. The 5 failures are BookKeeper cluster tests that fail in setUp with UnknownHostException: Unable to resolve hostname for ip address: 10.255.255.254 in this environment, before running any test code.
Does this pull request potentially affect one of the following parts:
- 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.
…sor closes
ManagedLedgerFactory#openReadOnlyCursor opens a read-only managed ledger
internally but returns only a ReadOnlyCursor. ReadOnlyCursorImpl#asyncClose
only closes the cursor, so the ledger's cached read handles stay open for the
lifetime of the factory.
Return an ownership-aware cursor from the factory cursor API. It closes the
backing read-only managed ledger when the cursor is closed, while cursors
created from a caller-owned ledger through
ReadOnlyManagedLedger#createReadOnlyCursor keep the ledger open.
Add a regression test that reads entries through the factory cursor, closes the
cursor and verifies that every cached read handle is closed. The test fails
without the fix.
Signed-off-by: Drifter-yh <[email protected]>
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.