pulsar icon indicating copy to clipboard operation
pulsar copied to clipboard

[improve][meta] Recover ambiguous ledger metadata creation using cToken in PulsarLedgerManager

Open void-ptr974 opened this issue 2 months ago • 1 comments

Search before reporting

  • [x] I searched the Pulsar issues and did not find an existing issue that tracks this behavior.

Motivation

When BookKeeper uses Pulsar's metadata driver, PulsarLedgerManager#createLedgerMetadata creates ledger metadata with a create-if-absent write:

store.put(path, data, Optional.of(-1L))

A conflict returned by this operation does not always mean that another create owns the ledger ID. For ZooKeeper, the original create can reach the server and then return CONNECTIONLOSS. If ZKMetadataStore retries the request, the retry observes NODEEXISTS, exposed through Pulsar as a BadVersionException. The caller cannot distinguish:

  1. a genuine ledger-ID collision; and
  2. the current create succeeded, but its response was lost.

BookKeeper's native AbstractZkLedgerManager resolves this ambiguity with the creator token (cToken) added for version-3 ledger metadata by apache/bookkeeper#1967 and apache/bookkeeper#2006. After NODEEXISTS, it reads the metadata back: a matching token means the original create succeeded, while a different token means the ID was already owned by another create.

PulsarLedgerManager currently does not implement this recovery behavior.

Before #26669, an ambiguous result failed ledger creation with BKMetadataVersionException even when the metadata had been persisted, leaving that metadata behind. With #26669, a create-if-absent conflict becomes BKLedgerExistException, allowing LedgerCreateOp to generate another ID. That is correct for a genuine collision, but in the ambiguous-success case the original metadata can remain unused while creation continues with a new ID. No handle is returned for the abandoned ID, so this does not introduce an entry-write path for that ID.

The ambiguity and residual metadata are pre-existing robustness gaps, not regressions introduced by #26669. That PR restores the expected collision error contract without implementing full cToken recovery semantics. See the review discussion.

Solution

Align PulsarLedgerManager#createLedgerMetadata with BookKeeper's native cToken semantics while accounting for Pulsar's generic MetadataStore and metadata cache.

For metadata format version 3 and newer:

  1. Generate a fresh, non-blank creator token for each logical createLedgerMetadata call. Keep it stable across internal retries of that call.
  2. Add the ledger ID and creator token before serializing the metadata.
  3. If create-if-absent succeeds, return token-bearing metadata with the version reported by the store.
  4. If it reports BadVersionException or AlreadyExistsException, including wrapped forms, perform an authoritative store-level read-back. Use MetadataStore.sync(path) where needed, followed by a direct MetadataStore.get(path), or an equivalent mechanism; do not decide from a stale or negatively cached value.
  5. If the persisted token matches, treat the original create as successful for the same ledger ID and return the persisted metadata and version.
  6. If the token differs, return BKLedgerExistException without changing the existing metadata.
  7. If the path is missing, the read-back fails, or deserialization fails, fail conservatively. Do not report success and do not overwrite or delete uncertain metadata.
  8. Invalidate or refresh the ledger metadata cache after recovering an ambiguous success so that subsequent reads observe the recovered metadata and its version.

Metadata format versions 1 and 2 cannot persist cToken; they should keep the current conservative collision behavior. Update and delete version-conflict behavior must remain unchanged. The token is already part of BookKeeper's version-3 metadata, so this should not require a new metadata format, wire protocol, public API, or configuration.

Acceptance criteria

  • A normal version-3 create persists and returns the same non-blank cToken.
  • A genuine collision with a different token returns BKLedgerExistException and preserves the existing metadata.
  • A deterministic ambiguous-success test persists the attempted metadata and then reports BadVersionException or AlreadyExistsException; the create succeeds with the original ledger ID after matching the stored token.
  • Direct and wrapped conflict exceptions are covered.
  • An end-to-end or equivalent LedgerCreateOp test verifies that ambiguous success does not allocate a replacement ledger ID.
  • A stale or negative cache entry cannot cause an incorrect result, and subsequent reads return the recovered metadata and correct version.
  • Missing metadata, read-back failure, and malformed metadata never produce false success or modify uncertain metadata.
  • Version-1 and version-2 metadata retain their existing collision behavior and serialized formats.
  • Existing duplicate-create, update, and delete version-conflict tests continue to pass across the configured MetadataStore providers.
  • ZooKeeper coverage reproduces or deterministically simulates a create committed before CONNECTIONLOSS, followed by a retry that observes NODEEXISTS.

Alternatives

Keep mapping every create conflict to BKLedgerExistException and let BookKeeper generate a new ID. This is safe for a genuine collision and is the scoped behavior in #26669, but it cannot recover the original ID after ambiguous success and can leave unused ledger metadata behind.

Non-goals

  • Changing the ledger-ID regeneration contract exercised by #26669.
  • Cleaning up historical unused ledger metadata.
  • Making ledger deletion idempotent, which was a separate part of the original BookKeeper change.
  • Adding cToken to metadata format versions 1 or 2.
  • Recovering across a process restart or a new create invocation that cannot retain the original token.
  • Treating arbitrary timeouts or transport failures as successful without a conflict followed by an authoritative token validation.

Related links

  • Pulsar PR #26669: https://github.com/apache/pulsar/pull/26669
  • Review discussion: https://github.com/apache/pulsar/pull/26669#discussion_r4123109407
  • BookKeeper issue #1967: https://github.com/apache/bookkeeper/issues/1967
  • BookKeeper PR #2006: https://github.com/apache/bookkeeper/pull/2006
  • PulsarLedgerManager: https://github.com/apache/pulsar/blob/master/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/bookkeeper/PulsarLedgerManager.java
  • ZKMetadataStore: https://github.com/apache/pulsar/blob/master/pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/ZKMetadataStore.java

Are you willing to submit a PR?

  • [ ] I'm willing to submit a PR!

void-ptr974 avatar Oct 01 '26 02:10 void-ptr974

Contributions are welcome! If anyone is interested in working on this, please feel free to pick it up. The proposed scope and acceptance criteria above should provide a starting point, and we can use this issue to coordinate implementation details.

void-ptr974 avatar Oct 01 '26 02:10 void-ptr974