Skip to content

[fix][ml] Make managed ledger properties updates atomic - #26569

Merged
lhotari merged 20 commits into
apache:masterfrom
void-ptr974:fix/ml-properties-api-safety
Nov 26, 2026
Merged

lhotari merged 20 commits into
apache:masterfrom
void-ptr974:fix/ml-properties-api-safety

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

ManagedLedgerImpl initializes propertiesMap as a ConcurrentHashMap, but replaces it with a plain
HashMap when recovering persisted properties. In addition, getProperties() exposes the live internal
map, and property updates mutate the in-memory map before the metadata-store write succeeds.

As a result, callers can mutate managed-ledger state without using the property update APIs, readers can
observe a partially applied multi-property update, and a failed metadata-store update can leave memory
inconsistent with persisted metadata.

Modifications

  • Keep recovered properties in a concurrent map and publish replacements through a volatile reference.
  • Return detached snapshots from getProperties() and property-update callbacks.
  • Defensively copy maps supplied to synchronous and asynchronous property update APIs.
  • Build property updates on a private candidate map, including interceptor changes, and publish the new
    snapshot only after the metadata-store write succeeds.
  • Release the metadata mutex before invoking completion callbacks and isolate callback failures on every
    property-update completion path, including failures while building metadata locally.
  • Persist the internal migration marker through the property update path before starting termination, so
    it is serialized with other property snapshot updates.
  • Coalesce overlapping migration requests into one underlying operation while returning an independent
    future to each caller.
  • Publish a terminated position only after the BookKeeper close has finalized lastConfirmedEntry, so a
    concurrent property update cannot persist a stale termination position.
  • Allow migration to retry after either the marker update or the final termination metadata update fails,
    without treating a BookKeeper close failure as a completed migration.

After merging the latest master, the final termination metadata write is also serialized through
metadataMutex by storeTerminatedPosition() from #26680. Other managed-ledger metadata update flows are
unchanged by this PR.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests covering:

  • detached property snapshots and callback maps;
  • rollback of in-memory state after a metadata-store failure;
  • atomic visibility while a multi-property metadata update is pending;
  • metadata-mutex release and callback isolation when a callback throws;
  • preservation of the migration marker during a pending property update;
  • coalescing concurrent migration requests without an additional marker write;
  • omission of the termination position from metadata until the BookKeeper close finalizes it;
  • retry after migration-marker and termination-metadata failures;
  • serialization of a property update started after the migration marker but before the BookKeeper close
    callback, including the persisted migration, property, and termination state after reopening;
  • existing concurrent property updates and read-only managed-ledger compatibility.

Commands run locally:

  • ./gradlew quickCheck
  • targeted :managed-ledger:test properties, concurrency, migration, and termination tests
  • the complete ManagedLedgerTerminationTest
  • ./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.intercept.ManagedLedgerInterceptorImplTest.testRecoveryIndex

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

ManagedLedger.getProperties() now explicitly returns a detached snapshot rather than the live backing
map. The threading change is limited to atomic publication and serialization of managed-ledger property
and migration updates; no executors or thread pools are changed.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on making managed-ledger property updates atomic. One concurrent migration path can still lose the durable migration marker when it overlaps a pending property update.

Serialize the migration marker through the managed-ledger property update path before terminating the ledger, and add deterministic coverage for the overlapping update race.

Assisted-by: Codex
Coalesce concurrent migration attempts, avoid persisting an unfinished termination position, and isolate property failure callbacks. Cover marker and termination metadata retries.

Assisted-by: Codex

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix. The lost-marker race is closed. The two follow-up commits add retry handling for migration, and I found two cases there that still need a look (inline).

Share termination completion across migrations and independent terminate
calls. Keep marker-write failures retryable and fail queued property
updates when the ledger is fenced or closed.

Release the metadata mutex when property writes fail synchronously and
isolate completion callbacks without publishing failed property updates.
Add deterministic coverage for concurrent termination, rollover fencing,
retry behavior, and synchronous storage failures.

Validation: quickCheck and 45 targeted managed-ledger tests passed.

Assisted-by: Codex
Share cached read-handle cleanup between both close paths. Cover migration after the final ledger was offloaded and deleted from BookKeeper, durable migration recovery, and idempotent retries.

Assisted-by: Codex
@void-ptr974

Copy link
Copy Markdown
Contributor Author

Updated this PR to f8b22d47a57 with three commits:

  • a95523a98bc: Merged master (9cbb6d9b893) and resolved the ManagedLedgerImpl conflicts. The merge preserves the abandoned-initialization guard, final closed-ledger metadata and automatic offload scheduling, and termination completion on the managed-ledger executor after confirmed add callbacks, alongside this PR's property/migration changes.
  • e6edd4154df: Added interceptor regression coverage for staging user and interceptor properties together, retaining the previous snapshot on metadata failure, publishing only after successful metadata completion, detached callback maps, and recovery after reopening. Also removed an unintended dependency on map iteration order in an existing assertion.
  • f8b22d47a57: Fixed cached read-handle cleanup when closing a recovered terminated ledger with no current writing ledger. Both close paths now invalidate cached read handles before closing cursors. Added coverage for migration after the final ledger has been offloaded and deleted from BookKeeper, including durable migration/termination state, idempotent repeated migration, reads after reopening, and read-handle cleanup.

Targeted local verification passed: 55 managed-ledger test executions and 14 broker test executions (TopicTerminationTest and ManagedLedgerInterceptorImplTest.testRecoveryIndex), with no failures, errors, or skips. ./gradlew quickCheck also passed.

The local jcstress tests, dependencies, and build configuration are not included in these pushed commits.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the quick follow-up: both retry-handling points are fixed in the code, and the new regression tests exercise exactly those scenarios. The marker-write failure now clears the cached migration independently of termination state, and queued property updates fail once the ledger is fenced or closed instead of retrying forever. I read the termination rewrite (shared terminationFuture), the merge resolution and the read-handle cleanup in the later commits and found nothing blocking.

Keep upstream termination recovery and read-handle cleanup. Publish the terminated position in ordinary metadata writes after the dedicated write succeeds, and cover migration retry, shadow ledger behavior, and broker migration readiness.

Assisted-by: OpenAI Codex
Store a live migration marker with the terminated position. If ordinary termination submitted first, publish the marker independently after its success. Keep the final BookKeeper ledger recoverable until termination is durable and test ownership changes, offload, and queued property inputs.

Assisted-by: OpenAI Codex
@void-ptr974

Copy link
Copy Markdown
Contributor Author

Updated this branch to 716387a6e48 with master at 4c493c05b3e. The merge preserves recovery and retry after a BookKeeper close failure from #27282.

  • Persist the migrated marker with the terminated position. If an ordinary termination write is already in flight, wait for it to succeed before writing the marker.
  • Keep the final BookKeeper ledger available until the terminated position is durable. Manual offload requests that cover the final ledger wait for a pending termination metadata write.
  • Add regression coverage for reopening after a failed migration, reading the complete ledger after a failed termination and offload, and modifying an input map while its property update is queued.

Local validation passed 306 managed-ledger tests, five broker test executions including the previously failing replication backlog case, and two transaction sequence ID recovery cases. Scoped Checkstyle reported the same Javadoc findings as the clean master comparison.

…ination

Bind the migration marker to the termination attempt it joins so an ordinary retry cannot persist a failed migration. Reject fenced and closed migration handles, and repair a deleted marker when migration is requested again.

Queue automatic offload after termination completion so a manual offload already waiting on its final ledger can proceed. Add coverage for each ordering and the existing disabled-threshold case.

Assisted-by: OpenAI Codex
…d ledger

An empty rollover ledger can disappear from the persisted list after a failed termination and a subsequent property update. Recover the retained offloaded ledger from its stored stats and read its last entry from tiered storage when an interceptor needs it.

Assisted-by: OpenAI Codex

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the follow-up commits. Tying each migration attempt to its own termination future (ManagedLedgerImpl.java:1872 and the check in storeTerminatedPosition at ManagedLedgerImpl.java:2007) means a retried ordinary terminate() can no longer commit the marker of a migration that already failed, and testOrdinaryTerminationRetryDoesNotCommitFailedMigration reproduces that interleaving. Rejecting asyncMigrate() on a fenced or closed ledger is safe for the broker, which only calls it after isMigrated() returns false. testRepeatedMigrationRestoresDeletedMarker and testAlreadyMigratedLedgerRejectsMigrationAfterFencing cover the new isMigrated() semantics. Reopening a writable ledger whose last ledger was offloaded and deleted from BookKeeper now keeps its stored stats and reads the last entry through the offloaded handle for the interceptor (ManagedLedgerImpl.java:568-577), and testRecoverOffloadedTailAfterEmptyLedgerRolloverAndFailedTermination covers both the interceptor and the no-interceptor case.

Close the temporary BookKeeper handle after interceptor initialization and keep the offloaded tail handle managed by the ledger cache.

Assisted-by: OpenAI Codex

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The only change since my approval is the merge of master, and the conflict in ManagedLedgerImpl.java was resolved correctly: the last-ledger recovery path keeps master's handling of temporary read handles (the handle is closed after the interceptor completes when it is not retained, ManagedLedgerImpl.java:601-611), and both the BookKeeper and offloaded-tail paths still go through the shared initializeAfterInterceptor helper (ManagedLedgerImpl.java:663-671) with supplySafely and the unwrapped interceptor exception.

@lhotari
lhotari dismissed codelipenghui’s stale review November 26, 2026 12:55

Change requests have been addressed.

@lhotari
lhotari merged commit 67052c8 into apache:master Nov 26, 2026
43 checks passed
lhotari pushed a commit that referenced this pull request Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants