Skip to content

3.x.x: retry a failed candidate attempt's claim release until the database answers, finish an offer in flight at shutdown, and record a recovered reservation's outcome as unknown (#529, #578) - #585

Merged
djh58 merged 3 commits into
3.x.xfrom
djh58/prism-b529-lease-takeover
Sep 29, 2026
Merged

djh58 merged 3 commits into
3.x.xfrom
djh58/prism-b529-lease-takeover

Conversation

@djh58

@djh58 djh58 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Closes #529 (items a and b; item c was fixed by #570). Closes #578.

Why

In the #531 async failover drill, a found block that was mid-landing when the primary failed landed about 120 s after the promotion: the CANDIDATE_LEASE. The offering frontend's attempt failed on the old primary's dead connections, and so did its claim release (retry_candidate), so no frontend could take the offer_reserved row until the lease expired (item b). The recovered row then landed submitted with offer_outcome NULL, while the reconciliation and orphan paths record unknown (item a).

What

(b) The holder retries its own claim release. Design options and the safety argument were agreed before implementation (holder-side deferred release, no schema change):

  • When an attempt fails and its bounded release fails too, the submit loop keeps it and retries the same token- and lease-fenced release that retry_candidate runs (split out as Ledger::release_candidate_claim, which reports false instead of failing when the claim no longer holds the row), about once a second, until the database answers or the lease's local end passes. At shutdown it tries each once more through Ledger::release_recovery_claim, the path the submit loop's own shutdown release uses.
  • Structural guarantee. claim_release::Attempt owns the claim and the future of Attempt::run borrows it; the only ways to release the claim consume the attempt: Attempt::failed (the FailedAttempt a release takes) and, at shutdown, Attempt::hand_back_at_shutdown. The borrow checker therefore drops the work first, and nothing this process started under the claim, submitblock included, can still run once the row is released.
  • Fenced. The release matches only this claim's token on a live lease, so it changes nothing once another frontend has taken the row or the lease expired. A recovered offer_reserved row is never offered again, as before, so no new submitblock path exists.
  • Bounded, no starvation. Retries keep their schedule while the loop processes other rows (a select branch next to the attempt). Before each claim the loop waits for at most one retry, bounded by the lease's 5 s and never past the lease's local end. The local end comes from the heartbeat (the start of the last successful renewal plus the lease), which is never later than the database's expiry.
  • Visible. WARN logs candidate claim release deferred when it starts, and deferred candidate claim release succeeded / ... expired with its lease when it stops.
  • Not covered: a holder that died with the old primary still leaves its claim to the 120 s lease. A takeover keyed on the promotion (claims stamped with the primary's timeline) would cover it, but needs a migration and preempts every healthy holder after each promotion; it stays a documented option on 3.x.x: PostgreSQL failover findings — async gap can lose found blocks; lease delays mid-landing recovery; recovered outcome unrecorded #529.

#578: a graceful shutdown finishes an offer in flight. Once a reservation commits, no frontend offers the block again, so a shutdown that dropped the attempt between that commit and submitblock lost the block on an ordinary restart; #570's standby wait sits inside that window.

  • The offer path holds an offer section (coordinator/offer_shutdown.rs) from just before the reservation until the node's answer is recorded, or the unsent reservation is returned to pending (RpcNotSentError / release_unsent_offer, unchanged). It closes on every path when dropped, so a lost lease still ends it.
  • A shutdown that finds a section open keeps driving the attempt until the section closes, then drops it; the landing is left to the next claim, which lands the offered row without another offer. The wait is bounded by the send's deadline + the standby wait's bound + the lease's bound for each of the reservation, the renewal before the send and the recording (about 16 s with the defaults), logs shutdown waits for a found block's offer in flight, and on the bound logs an ALERT and stops. Everything the loop does after the shutdown signal (this wait, the 3.x.x: fanout claim/attempt bookkeeping edge cases (follow-up to #569) #573 hand-back, the deferred releases) shares one 20 s budget inside the server's 30 s join, and a signal that arrives during the pre-claim retry claims no new row.
  • A crash in the same window is unchanged: recovery reconciles the reservation as delivery unknown.

(a) The recovered outcome. finish_candidate records offer_outcome = unknown for an offer-state row that has none, matching reconciliation and the orphan disposition. A pending row keeps none (the 015 offer rule). Rows that already landed with NULL are not rewritten.

Docs. The ledger operations guide describes the deferred release, the shutdown mid-offer and the recorded outcome; the HA reference's promotion step 5 says when a mid-landing block lands.

Tests

All gated and listed, sorted, in test/prism-gated-tests.txt.

Test What it proves
coordinator::candidate_lease_tests::claim_release_tests::a_failed_release_is_retried_after_the_attempt_stops_and_frees_the_row_before_its_lease A third frontend reaches PostgreSQL through a TCP relay the test cuts and heals, with a 30 s lease and its attempt parked after the reservation. The cut fails the attempt and its release; after the heal the claim is released in about 0.3 s, and only after the attempt's work was dropped. Another frontend recovers the reservation and lands it submitted with outcome unknown, no call time, and no submitblock
...::a_deferred_release_never_frees_a_row_another_frontend_took After the lease expired and another frontend claimed the row, the old holder's retried release leaves that claim untouched for 3 s of retries
...::shutdown_attempts_each_deferred_release_once A shutdown before the next scheduled retry releases the deferred claim
coordinator::candidate_lease_tests::offer_shutdown_tests::shutdown_after_the_offer_reservation_sends_and_records_the_block_once #578, with the standby wait configured: the submit loop's attempt is parked right after the reservation commits (before the standby wait), or after the wait right before the send; a shutdown then waits for the offer, the block is sent exactly once and recorded accepted before the loop exits, and it lands once with no second offer
live_regtest::pg_failover_tests::real_async_failover_mid_landing_loses_only_the_replication_gap (extended) Now asserts landing within 30 s of the promotion (2.3 s here, was about 120 s) and outcome unknown. The fenced drill is unchanged (0.3 s, accepted)

Mutants (each fails at least one test)

Mutant Caught by
No deferral (today's behaviour: give up after the first failed release) first test (the row waits for the 30 s lease), shutdown test, and the live async drill (landed 119.8 s after the promotion, beyond 30 s)
Release not fenced on the token or lease second test (changed another frontend's claim)
Release started from the heartbeat failure while the attempt's work keeps running first test (released while the attempt's work was still running), and the other two
No release at shutdown shutdown test
(a) outcome left NULL first test (landed as submitted with outcome None)
#578: shutdown drops the attempt at once (the cancellable path) offer_shutdown test (shutdown dropped the offer after its reservation committed)
#578: section opened only after the standby wait offer_shutdown test (same, at the stage after the reservation)

Verification

On the final tree (rebased onto #576; the fmt, clippy, full --lib and pg_failover_tests runs were repeated on the latest 3.x.x, 2f907447, with the same results):

Coordination

🤖 Generated with Claude Code

…abase answers, finish an offer in flight at shutdown, and record a recovered reservation's outcome as unknown (#529, #578)

A PostgreSQL failover while a found block is mid-landing delayed the landing
by up to the 120 s candidate lease (#529 item b). The offering frontend's
attempt failed on the old primary's dead connections, its claim release
failed on the same connections, and no frontend could take the row until the
lease expired.

The submit loop now keeps a failed attempt whose release did not reach the
database, and retries the same token- and lease-fenced release (the one
retry_candidate runs, split out as release_candidate_claim) about once a
second until the database answers or the lease's local end passes. At
shutdown it tries each once more through release_recovery_claim.

- Structural, not by convention: claim_release::Attempt owns the claim,
  the future of Attempt::run borrows it, and the only ways to release it
  (Attempt::failed, and at shutdown Attempt::hand_back_at_shutdown, which
  is #573's hand-back moved there unchanged: same release, reason, bound
  and logs) consume it, so the borrow checker drops the work first.
  Nothing this process started under the claim, submitblock included, can
  still run once the row is released.
- The release is fenced on the token and the live lease, so it changes
  nothing once another frontend has taken the row or the lease expired.
  Offer-state rows are never offered again by the recovery, as before.
- Retries keep their schedule while the loop processes other rows; before
  each claim the loop waits for at most one bounded retry (the lease's 5 s
  bound, never past the lease's local end).
- The lease's local end comes from the heartbeat itself (the start of the
  last successful renewal plus the lease), never later than the database's
  own expiry.
- WARN logs when a release is deferred and when it succeeds or expires.
- A holder that died with the old primary still leaves its claim to the
  lease. A takeover keyed on the promotion (a timeline-stamped claim) would
  cover it, but needs a migration and preempts every healthy holder after a
  promotion; it stays a documented option in #529.

A graceful shutdown between the offer reservation's commit and submitblock
dropped the attempt, leaving an offer_reserved row that was never sent and
that recovery never offers again, so a restart could lose a found block;
#570's standby wait sits inside that window (#578). The offer path now
holds an offer section from just before the reservation until the node's
answer is recorded or the unsent reservation is returned to pending. A
shutdown that finds one open keeps driving the attempt until it closes,
within the send's deadline, the standby wait's bound and the lease's bound
for the reservation and the recording, logs that it waits, and only then
stops; the landing after it is left to the next claim, which lands the
offered row without another offer. Pre-dispatch failures still return the
reservation through release_unsent_offer. A crash in the window is
unchanged: recovery reconciles the reservation as delivery unknown.

A recovered offer_reserved row whose call's answer was never recorded landed
submitted with offer_outcome NULL (#529 item a). finish_candidate now
records unknown for an offer-state row without an outcome, as the
reconciliation and orphan dispositions already do; pending rows keep none.

Tests (gated, listed in test/prism-gated-tests.txt):
- claim_release_tests, a third frontend reaching PostgreSQL through a TCP
  relay the test cuts and heals, with a 30 s lease: the claim is released
  0.3 s after the database answers and only after the attempt's work was
  dropped, then another frontend recovers the reservation and lands it with
  outcome unknown and no submitblock; a deferred release leaves a claim
  another frontend took after expiry untouched; shutdown releases a
  deferred claim.
- offer_shutdown_tests: a shutdown with the attempt parked right after the
  reservation (before the standby wait) or right before the send waits for
  the offer; the block is sent once, recorded accepted, and landed once.
- The live async failover drill now asserts landing within 30 s of the
  promotion (2.3 s here; 119.8 s with the retry removed) and the unknown
  outcome.

Docs: the ledger operations guide describes the deferred release, the
shutdown mid-offer and the recorded outcome, and the HA reference's
promotion step 5 says when a mid-landing block lands.

Closes #529. Closes #578.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@djh58
djh58 requested a review from kiwidream September 29, 2026 17:38
@blacksmith-sh

This comment has been minimized.

…offer section the renewal's allowance, and classify the moved pool checkouts (#529, #578)

Review of #585 found two ways the shutdown path could run out of time.

- The offer-section bound allowed the lease's bound twice, for the
  reservation and the recording, but a section has three database steps:
  the strictly-live renewal before submitblock is the third. A slow but
  live database could end the wait during the send and drop a reserved
  block, the loss #578 prevents. It now allows the lease's bound three
  times.
- After the shutdown signal the loop could spend the section wait, the
  claim hand-back and one bounded attempt per deferred release in series,
  past the server's 30 s join of its tasks, which then aborts the loop and
  leaves its claims to the lease. Everything after the signal now shares
  one 20 s budget (SHUTDOWN_BUDGET), leaving room for one bounded retry
  already running when the signal came, and a signal that arrives during
  the pre-claim retry claims no new row.

scripts/prism_pool_acquires.json follows the moves: the heartbeat's
checkout is now in with_tracked_heartbeat, retry_candidate's BEGIN in
release_candidate_claim, and Attempt::run is classified non-database (a
name collision, like the server's runtime run), which the Python shard's
pool-acquire check requires.

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

djh58 commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

Review ledger (deep-review)

Stakes: high. The change is on the found-block offer, claim and landing path (submitblock, candidate claims, the landing's recorded outcome).

Battery. /code-review at medium effort; an adversarial pass (correctness, partial failure and retries, money path, test strength) and a structural code-quality pass, both on Claude. The cross-provider adversarial lane was not run: this lane is Claude-only. No review bots comment on this repository.

# Source Finding Disposition
1 code-review The shutdown wait for an offer in flight allowed the lease's bound twice for three database steps (reservation, the renewal immediately before submitblock, recording), so a slow but live database could end the wait mid-send and drop a reserved block Fixed in 91070c5: three allowances
2 code-review After a shutdown signal the loop's waits (offer in flight, claim hand-back, deferred releases, a pre-claim retry) could add up past the server's 30 s task join, which then aborts the loop Fixed in 91070c5: one shared budget, below
3 CI, Python shard 2 scripts/prism_pool_acquires.json still named the checkouts at their old sites Fixed in 91070c5
4 CI, Python shard 1 test_the_checked_in_manifest_passes fails: two earlier live tests are not named in the e2e manifest Deferred to #584: pre-existing on 3.x.x; this PR adds no live test
5 adversarial A deferred release whose COMMIT reply was lost, for a row this frontend then re-claims inside the lease, takes FOR UPDATE and can queue behind that landing's transaction Rejected: bounded by the 5 s release budget, the landing's renewal already handles lock contention (it re-reads its live token), and the release is token-fenced, so it reports "no longer held" rather than releasing anything

Shutdown budget. Everything the submit loop does after a shutdown signal (waiting for an offer in flight, #578; handing back its claim, #573; the deferred releases, #529) shares one 20 s budget, inside the server's 30 s join of its tasks. That leaves room for one bounded (5 s) retry already running when the signal came, and no new row is claimed once the signal is seen. The offer wait is the smaller of that budget and the send's deadline + the standby wait's bound + three lease bounds (about 16 s with the defaults).

A block still in its offer section when the budget runs out. The loop logs ALERT: shutdown stopped waiting for a found block's offer in flight and drops the attempt. The row is left offer_reserved (the call may or may not have reached the node), and the claim is handed back when time remains, else it expires with its lease. Recovery then treats it exactly as a crash in that window: delivery unknown, never offered again, reconciled against the chain and landed if the block is active. Only a database or node that holds the section past the budget reaches this; with a responsive database the section closes well inside it.

Also verified. The recovered outcome change (a) is exact against the 015 offer rule (only offer-state rows get unknown; pending rows keep none). The deferred release can only run after the attempt's future is gone (the future borrows the attempt; every release consumes it). All seven mutants listed in the description are caught. #584's own CI run just saw the unchanged 3.x.x async drill record unknown where its old assertion expected NULL, so that outcome was already racy on the base; with (a) it is deterministic, and this PR asserts it.

CI on 91070c5. Everything is green except Python shard 1 (item 4, #584) and the aggregate job that depends on it.

@djh58 djh58 closed this Sep 29, 2026
@djh58 djh58 reopened this Sep 29, 2026
@djh58
djh58 merged commit 1bf77e9 into 3.x.x Sep 29, 2026
21 checks passed
djh58 added a commit that referenced this pull request Sep 29, 2026
Resolves test/e2e-scenarios.toml: keeps #547's real-node scenarios and #586's #575 scenarios; check_e2e_scenarios and its unit tests pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
djh58 added a commit that referenced this pull request Oct 1, 2026
… candidate backlog under load, nightly and weekly on the real node (#554) (#635)

* 3.x.x: fail the database over, fill its WAL volume and restart over a candidate backlog under load, nightly and weekly on the real node (#554)

The second half of #554's faults, in qbit-prism-load's `faults` phase. Each
is injected while the sessions keep mining and held to its criteria on top
of the run's own reconciliation. No server code changes.

- primary-kill: a barrier shows the standby flushed the primary's position,
  replication is cut, the primary keeps acknowledging for `cut` s and is
  stopped immediately, and the async standby is promoted. Every acknowledged
  share the new primary lacks is listed; none may predate the barrier, and
  each must be absent from what the standby had received.
- primary-switch: writers fenced, the standby replays through the old
  primary's flush position, then the switch. Nothing may be lost.
- block-failover: the relay holds a found block's submitblock while
  replication is cut and the primary stopped. With #529's standby wait on,
  the reservation must be on the promoted primary, and the block must reach
  the node once and land within 30 s of the promotion (#585).
- wal-disk-full: #575's DiskFullInjector fills the primary's pg_wal volume
  until PostgreSQL stops, then frees it. Nothing acknowledged may be lost or
  acknowledged while it is down. The frontends must serve again without a
  restart, and every /metrics must show #575's paging condition.
- candidate-backlog: the relay answers submitblock with qbitd's warmup error
  (#526) while found blocks pile up, every frontend is restarted, the relay
  heals. Every backlog row must reach a terminal state, and none may reach
  the node twice.

For these, the managed cluster streams its standby through a replication
link the harness can cut. Every writer (the frontends and the harness's side
pool) reaches the primary through a writer endpoint, and the public reader
reaches the standby through a read endpoint. A promotion moves the endpoints
and rebuilds the standby; nothing is restarted. The run excuses, from its
durability finding, only the acknowledged shares a failover's verdict proves
lie in the replication gap.

faults-short-real-node (nightly) gains every new fault but the full WAL
volume. faults-long-real-node (weekly, #588's Saturday selection, 16 vCPU)
runs all twelve with 2,000 sessions. faults-failover-fake-node runs the five
on dispatch. No per-PR test is added.

Refs #554

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

* 3.x.x: install fuse2fs in the load-run job for a preset that fills the WAL volume (#554)

faults-long-real-node, on the Saturday `weekly` selection beside the soak,
and faults-failover-fake-node, on dispatch, run #554's wal-disk-full fault.
That fault mounts the primary's pg_wal on a small ext4 image with fuse2fs,
without root, as #575's disk-full scenario does. The run job now installs
fuse2fs and fuse3 when the preset's --faults lists wal-disk-full, and the
header and the dispatch input name the new weekly preset and its cost.

Refs #554

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

* 3.x.x: close the #554 fault checks review found could pass or flake, and finish an abandoned failover (#554)

Review of #635 (/code-review medium, a quality review and an adversarial
review) found checks that could pass without proving their claim, a flake,
and a cleanup gap. No server code changes.

- candidate-backlog passed even if every backlog row was abandoned at the
  relaunch. It now requires one backlog block to land, offered once.
- block-failover took its barrier before waiting up to 30 s for the found
  block, so that whole window counted as the replication gap. The barrier
  is now taken after the held call arrives, just before the cut. The held
  call goes to the node within 800 ms of arriving, inside the frontend's
  1 s submitblock deadline, however long the stop takes.
- wal-disk-full now starts its down intervals and its outcome-unknown
  window at the fill's start, so a PANIC during the fill is counted. An
  acknowledgement inside a down interval fails the check only when its
  share is absent from PostgreSQL, since a late answer to an earlier
  commit is not a lie.
- An abandoned failover waits for its in-flight stop or promotion, promotes
  once, heals a cut link and moves the writer endpoint, so the run's end
  still finds a primary.
- The gap key's constant is renamed to match its value; the frozen set's
  lookback is named and leaves re-offers out; a unit test pins the backlog's
  unfinished states to the server's CandidateState::UNFINISHED_SQL.

Refs #554

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

---------

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant