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][broker] Revalidate ownership before retrying a fenced topic load by codelipenghui · Pull Request #27753 · apache/pulsar

Conversation

@codelipenghui

@codelipenghui codelipenghui commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Motivation

#27285 makes a broker validate its namespace lock authoritatively before it reloads a topic whose managed ledger was fenced, but only once the topic has finished loading. Two partial loads still leave no gate, so the retry trusts the cached ownership and can recover the new owner's ledger. Both were found in the review of #27285:

  • The managed-ledger open fails because another broker updated the ledger's metadata first (BadVersionException on the metadata update). The load fails and its future is removed without a gate.
  • An initialization step, such as opening the compaction or deduplication cursor, hits a fence. The topic's fence handler skipped the gate because the load future was still pending, and the topic's own close() then removes that pending future.

Modifications

  • When a load fails with a fence or a managed-ledger metadata version conflict, the managed-ledger open and initialization failure handlers install the readmission gate while the failed load is still cached. Other load failures keep using the cached ownership.
  • requireFreshOwnershipBeforeTopicReadmission also gates a topic whose own load future is still cached, so a fence seen during initialization installs the gate before close() removes the load.
  • The gate is tied to the ownership the broker holds when the failure is seen, as on the loaded-topic path.

Verifying this change

  • Make sure that the change passes the CI checks.

New PersistentTopicTest cases cover a version conflict on the managed-ledger open, a fence on the compaction cursor and on the deduplication cursor during the load, and an unrelated open failure that must not install the gate. The first three failed before the change. Removing the check for a topic that is still loading makes the compaction case fail again.

PersistentTopicTest (54), BrokerServiceTest (73), OwnershipCacheTest (33) and BrokerServiceTopicDeletionTest (17) pass locally, as does quickCheck.

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

Documentation

  • doc-not-needed

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

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

All reactions
@codelipenghui
[fix][broker] Revalidate ownership before retrying a fenced topic load
46c671a
A topic load that failed partway because another broker opened the
topic's managed ledger left no readmission gate, so the retry trusted the
cached namespace ownership and could recover the new owner's ledger:

- the managed-ledger open lost its metadata update to another writer
  (BadVersionException), or
- an initialization step, such as opening the compaction or
  deduplication cursor, observed a fence.

Install the gate while the failed load is still cached: in the
managed-ledger open and initialization failure handlers when the failure
is a fence or a managed-ledger metadata version conflict, and in the
fence handlers of a topic that is still loading, whose close() removes
the pending load. Other load failures keep using the cached ownership.

Assisted-by: Claude Code (Opus 5.5)
@codelipenghui codelipenghui self-assigned this Nov 26, 2026
@codelipenghui codelipenghui added this to the 5.1.0 milestone Nov 26, 2026
@codelipenghui codelipenghui added type/bug The PR fixed a bug or issue reported a bug release/4.2.6 release/4.0.15 release/5.0.2 labels Nov 26, 2026
@codelipenghui
codelipenghui marked this pull request as ready for review November 26, 2026 02:12
@codelipenghui codelipenghui added the ready-to-test label Nov 26, 2026
nodece
nodece approved these changes Nov 26, 2026
View reviewed changes

@nodece nodece left a comment

Copy link
Copy Markdown
Member

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.

Verified independently, with each new test pinned to the mechanism that covers it:

  • Dedup-cursor fence during load — red without the requireFreshOwnershipAfterFailedLoad call in the initialization-failure handler; green with it. The gate installs before the submitted close removes the cached load.
  • Managed-ledger version conflict on open — red without the call in openLedgerFailed; green with it. failTopicFuture removes the entry on the executor, so installing the gate first closes the retried-on-cached-ownership window.
  • Compaction-cursor fence during load — stays green with both new calls removed, and goes red when the requireFreshOwnershipBeforeTopicReadmission predicate is reverted to the loaded-only check: this one is covered by the still-loading identity arm (future == failedTopic.getCreateFuture()), exactly as the description states. Each test is load-bearing for exactly one mechanism.
  • isOwnershipConflict walking the cause chain for ManagedLedgerFencedException/BadVersionException matches how these failures actually arrive (wrapped), and the unrelated-failure test confirms bookies-unavailable style errors keep the cached ownership.
  • Full PersistentTopicTest (54) green on the PR head.

The direction of the guard is also safe against false positives: a gate only forces a fresh ownership check on the next readmission, which is cheap when the ownership is in fact still valid.

Sorry, something went wrong.

Uh oh!

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

All reactions
lhotari
lhotari reviewed Nov 26, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

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 for closing these two remaining gaps from #27285. The gate now covers both partial-load paths.

The change looks correct. A BadVersionException from the managed-ledger open now installs the gate in openLedgerFailed. For a fence seen while a cursor opens during initialization, the new getCreateFuture() clause gates the still-pending load before the topic's own close() removes it. I traced the compaction test: the mocked cursor open fails inline, before getDurableSubscription returns its future. close() then removes the cache entry synchronously, so the load chain's handler finds nothing cached. That means the test really pins the createFuture clause, as the description says. Unrelated load failures keep the cached ownership.

I have two small suggestions inline. One is to harden the replicator cursor path. The other is about log and comment wording that no longer matches the new callers.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
.attr("topic", topic)
.exceptionMessage(ex)
.log("Topic initialization failed. Removing topic from cache");
requireFreshOwnershipAfterFailedLoad(topic, topicFuture, ex);

@lhotari lhotari Nov 26, 2026

Copy link
Copy Markdown
Member

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.

[QUALITY] A fence on a replicator cursor during the load can be hidden by another replicator's failure

This handler only sees the aggregated initialization error. The replication step builds that error from FutureUtil.waitForAll (CompletableFuture.allOf, see FutureUtil.java:60 and PersistentTopic.java:2425). allOf reports one cause only, the earliest failing future in list order. startReplicator's openCursorFailed (PersistentTopic.java:2850) just completes its own future with PersistenceException(fenced). It does not call requireFreshOwnershipBeforeTopicReadmission the way getDurableSubscription does.

Scenario: a topic replicates to two remote clusters. The first replicator's cursor opens, but addReplicationCluster then fails (for example, the cluster metadata lookup fails). The second replicator's cursor open observes the fence. allOf surfaces the lookup error, isOwnershipConflict returns false, and the failed load is removed without the gate.

The window is narrow, because once the ledger is fenced every later cursor open fails right away. Still, the fix is cheap, and the new getCreateFuture() clause already makes it work while the topic is loading:

@Override
public void openCursorFailed(ManagedLedgerException exception, Object ctx) {
    if (exception instanceof ManagedLedgerFencedException) {
        brokerService.requireFreshOwnershipBeforeTopicReadmission(PersistentTopic.this);
    }
    future.completeExceptionally(new PersistenceException(exception));
}

This can also be a follow-up.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java
} catch (RuntimeException e) {
// The caller must still close the fenced topic. Without the gate, a reload uses the cached ownership.
log.warn().attr("topic", failedTopic.getName()).exception(e)
log.warn().attr("topic", topic).exception(e)

@lhotari lhotari Nov 26, 2026

Copy link
Copy Markdown
Member

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.

[QUALITY] The log and comments still describe only a fenced, loaded topic

Nit: requireFreshOwnershipBeforeReload is now also called from openLedgerFailed. On that path no topic instance exists, and the cause may be a BadVersionException rather than a fence. The comment above ("The caller must still close the fenced topic") and the log message ("...before reloading the fenced topic") would mislead someone reading the broker log after a version conflict on open. Wording such as "Failed to require fresh namespace ownership before reloading the topic after an ownership conflict" would fit every caller.

The isOwnershipConflict comment says "a version conflict on the managed-ledger metadata". indexOfType matches a BadVersionException anywhere in the cause chain, though, including a cursor-metadata conflict raised during initialization (ManagedCursorImpl lets it escape only when its own ownership check fails). Gating that case seems right. It is worth saying so in the comment.

Sorry, something went wrong.

Uh oh!

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

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari left review comments
@nodece nodece nodece approved these changes

Uh oh!

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

Assignees

@codelipenghui codelipenghui

Labels

ready-to-test release/4.0.15 release/4.2.6 release/5.0.2 type/bug The PR fixed a bug or issue reported a bug

Projects

None yet

Milestone

5.1.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

3 participants

@codelipenghui @lhotari @nodece
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.