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] Retry a failed read on the bookies a cached read handle's ledger was re-replicated to by KannarFr · Pull Request #27147 · apache/pulsar

Conversation

@KannarFr

@KannarFr KannarFr commented Oct 28, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Motivation

ManagedLedgerImpl.getLedgerHandle caches the read handles of the ledgers it opens, possibly for a long time. A cached handle keeps the ensembles the ledger had when it was opened, and doesn't see the bookies auto-recovery re-replicates the ledger to.

While a bookie of the original ensemble is still up, the reads succeed: with bookkeeperClientReorderReadSequenceEnabled (enabled by default), the bookies that left the cluster are read last. But once no bookie of the original ensemble is left, e.g. when the bookies of a cluster are decommissioned one after the other, a read on the cached handle fails. The handle is then invalidated and reopened with the current metadata by the next read, but the dispatcher waits dispatcherReadFailureBackoffInitialTimeInMs (15 seconds by default) before reading again, for every subscription reading such a ledger. #22559 reports a related symptom.

Opening these handles with withKeepUpdateMetadata(true), as #25199 did on other paths, would avoid the failed read, but at the cost of one ledger metadata watch per cached handle. With the ZooKeeper driver of BookKeeper, that is one ZooKeeper watch per ledger read and still retained, as the cache has no size limit or TTL. The same concern led apache/bookkeeper#4613 to keep this behavior opt-in. So this change retries the failed read instead, which costs nothing until a read fails.

While looking at this, some paths turned out not to release the cached handles, see below.

Modifications

  • When a read from storage fails in RangeEntryCacheImpl, except with TooManyRequestsException, the new ManagedLedgerImpl.reopenLedgerHandleIfReplicated releases the handle as before and opens the ledger again with its current metadata. If the ensembles changed, the read is retried once on the new handle; otherwise it fails as before. Reads failing on the same handle share the new open through the handle cache, which the next read would have opened anyway.
  • Release the cached read handles on every exit of asyncClose: when the managed ledger is fenced, when it has no current ledger, and when closing the current ledger fails. They were only released when the current ledger closed successfully.
  • Once the close has released the handles, getLedgerHandle fails with ManagedLedgerAlreadyClosedException instead of opening and caching a handle that nothing would release. An open that was already running when the handles were released closes its handle when it completes.
  • In invalidateLedgerHandle, only remove the cached handle if it is the one that failed. When several reads failed on the same handle and another read had already opened a new one, the later calls removed the new handle from the cache without closing it. The handle is now also removed when the managed ledger has no current ledger, e.g. a terminated managed ledger whose last ledger was offloaded and deleted from BookKeeper.

The retry doesn't avoid the first failed read, nor its timeout when the bookies don't answer. It isn't added to EntryCacheDisabled, used when the entry cache is disabled, where reads still fail and reopen the handle on the next read as before.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests and can be verified as follows:

  • ManagedLedgerReadHandleMetadataTest runs on real bookies. It caches a read handle, stops the bookies of its ledger and reads through the managed ledger:
    • testReadRetriedOnReReplicatedBookies first re-replicates the ledger with BookKeeperAdmin.recoverBookieData through another client: the read succeeds, through a handle opened again with the new ensemble.
    • testReadNotRetriedWithoutReReplication doesn't: the read fails.
  • ManagedLedgerReadHandlePulsarMetadataDriverTest runs the same tests with the managed ledger reading through PulsarMetadataClientDriver, which caches the ledger metadata, as apache/pulsar-helm-chart can enable it; both check which driver they use.
  • ManagedLedgerTest.testReadHandlesReleasedWhenCloseFails closes a fenced managed ledger, and one whose current ledger fails to close (failNow), and checks that no read handle is left in the cache.
  • ManagedLedgerTest.testReadHandleOpenedAfterCloseIsNotCached asks for a read handle after the close. The release of a handle whose open started before the close but was cached after it is not covered: that race cannot be reproduced deterministically.
  • ManagedLedgerTest.testFailedReadHandleDoesNotEvictItsReplacement reports the same failed handle twice, with a new handle opened in between.
  • OffloadPrefixReadTest.testReadTerminatedFinalLedgerDeletedFromBookkeeper now also checks a read error and the close of a managed ledger without a current ledger.

All these tests, except testReadNotRetriedWithoutReReplication, fail without the change in the main code.

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

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

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

All reactions
@KannarFr @claude
[fix][ml] Keep cached read handles' ledger metadata updated after re-…
b2c1a68
…replication

ManagedLedgerImpl.getLedgerHandle caches read handles opened without
withKeepUpdateMetadata, so they keep the ensembles the ledger had when
opened. Once no bookie of that ensemble is left, a read on the cached
handle fails and the dispatcher backs off before reading again.

Open these handles with withKeepUpdateMetadata(true), and release them on
every path where the managed ledger stops using them, since each one now
holds a ledger metadata listener: on every exit of asyncClose, for an open
that completes after the close, and in invalidateLedgerHandle only when the
cached handle is the one that failed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@void-ptr974

void-ptr974 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Thanks for investigating this and adding the cleanup fixes. I’d prefer keeping metadata watching disabled for this backlog path.

For example, 1,000 topic partitions × 100 cached historical ledgers could mean roughly 100,000 additional watches with the native ZooKeeper driver when a slow subscription prevents cleanup. The handle cache has no explicit size limit or TTL. Watches add memory and session-recovery load. With PulsarMetadataClientDriver, the watch is shared, so the additional cost is local listeners instead.

This concern was raised in BookKeeper #4613, which subsequently retained opt-in behavior.

This path already recovers through invalidate/reopen. Could we instead evaluate one asynchronous reopen with fresh metadata and retry before dispatcher backoff, sharing the refresh across concurrent failures? That could avoid the additional ~15-second delay, though not the initial failed read or timeout.

The cleanup fixes remain useful independently. I wouldn’t replace all existing watches together: compaction and delayed-snapshot paths need safe handle/context replacement first; schema already opens and closes per read; current-ledger and recovery paths need separate lifecycle checks.

All reactions

Sorry, something went wrong.

Uh oh!

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

@KannarFr @claude
[fix][ml] Retry a failed read on the re-replicated bookies instead of…
88864ff
… watching metadata

Keeping the metadata of every cached read handle updated costs one ledger
metadata watch per handle, and the cache has no size limit or TTL. Drop
withKeepUpdateMetadata from getLedgerHandle, and instead, when a read from
storage fails, open the ledger again with its current metadata and retry the
read once if its ensembles changed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KannarFr KannarFr changed the title [fix][ml] Keep cached read handles' ledger metadata updated after re-replication [fix][ml] Retry a failed read on the bookies a cached read handle's ledger was re-replicated to Oct 28, 2026
@KannarFr

KannarFr commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, done: I dropped withKeepUpdateMetadata and retry the failed read once, sharing the reopen, if the ensembles changed. I kept the cleanup fixes.

All reactions

Sorry, something went wrong.

Uh oh!

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

merlimat
merlimat approved these changes Oct 30, 2026
View reviewed changes

@merlimat merlimat left a comment

Copy link
Copy Markdown
Contributor

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, this looks good. I traced the retry: it's bounded to one (allowRetry=false), nothing blocks, and with the compare-and-remove in invalidateLedgerHandle the reopen is really shared across reads that failed on the same handle. Reopening is also the right hook here: a BK ReadHandle can't refresh its metadata without withKeepUpdateMetadata. The close-path fixes look correct, including the open that races the close.

One heads-up: #26816 adds the same kind of closing flag and cache drain to ReadOnlyManagedLedgerImpl. With this PR, the base asyncClose already releases a read-only ledger's handles through the lh == null branch, so it would be good to align the two so we don't end up with two flags.

A few small suggestions inline.

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.

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.

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.

Comment thread managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerTest.java Resolved

Uh oh!

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

Comment thread ...er/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerReadHandleMetadataTest.java Resolved

Uh oh!

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

codelipenghui
codelipenghui approved these changes Oct 30, 2026
View reviewed changes

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

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.

Reviewed commit 88864ff. No actionable findings identified within the reviewed scope. All 28 focused test cases passed across two validation runs.

The bounded retry approach looks sound. Additional deterministic coverage for close/open races and concurrent failures sharing a reopen would be useful but is non-blocking.

Sorry, something went wrong.

Uh oh!

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

All reactions
Denovo1998
Denovo1998 approved these changes Oct 31, 2026
View reviewed changes

@Denovo1998 Denovo1998 left a comment

Copy link
Copy Markdown
Contributor

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.

The bounded retry and read-handle cache cleanup look sound to me. I didn't find any additional blocking issues in the current diff. The existing inline suggestions are worth addressing, particularly the offloaded-handle and metadata-cache cases.

Sorry, something went wrong.

Uh oh!

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

All reactions
@KannarFr @claude
[fix][ml] Address review: skip the reopen for offloaded or trimmed le…
c2ad2f2
…dgers, await metadata in test

- Don't reopen an OffloadedLedgerHandle, whose data re-replication never moves, nor a ledger that is no longer in
  the managed ledger.
- Read currentLedger once, and check the cached handle with getNow.
- Wait for the reader's metadata to show the new ensemble before stopping the bookies, so that the test can't flake
  with a metadata driver that caches it.
- Fix a test comment left over from withKeepUpdateMetadata.

Assisted-by: Claude Code (Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KannarFr

KannarFr commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I pushed the suggestions. About aligning with #26816: I couldn't find that PR in apache/pulsar. Could you point me to the one adding the closing flag to ReadOnlyManagedLedgerImpl? I didn't change anything for it here; whichever lands second can drop its own flag and rely on the base asyncClose, which already releases a read-only ledger's handles through the lh == null branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

@KannarFr

KannarFr commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

@merlimat could you approve the CI workflows? They have been waiting for approval since 2026-10-28, and none of the commits on this PR has run CI yet.

All reactions

Sorry, something went wrong.

Uh oh!

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

@codelipenghui
codelipenghui merged commit 0065fdc into apache:master Nov 3, 2026
43 checks passed

Uh oh!

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

lhotari pushed a commit that referenced this pull request Nov 4, 2026
@KannarFr @claude @lhotari
[fix][ml] Retry a failed read on the bookies a cached read handle's l…
8af7709
…edger was re-replicated to (#27147)

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 0065fdc)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 labels Nov 4, 2026
@codelipenghui codelipenghui mentioned this pull request Nov 18, 2026
[fix][ml] Fix cached read handle leaks when closing read-only managed ledgers #26816
Open
3 of 11 tasks
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@KannarFr @claude @lhotari
[fix][ml] Retry a failed read on the bookies a cached read handle's l…
2e6b30b
…edger was re-replicated to (apache#27147)

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 0065fdc)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@KannarFr @claude @lhotari
[fix][ml] Retry a failed read on the bookies a cached read handle's l…
551c854
…edger was re-replicated to (apache#27147)

Co-authored-by: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 0065fdc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@merlimat merlimat merlimat approved these changes
@codelipenghui codelipenghui codelipenghui approved these changes
@Denovo1998 Denovo1998 Denovo1998 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 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

6 participants

@KannarFr @void-ptr974 @merlimat @codelipenghui @Denovo1998 @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.