fix(backup): durable restore gate and resolved install outcomes (review 3a34ae3)

The restore no longer relies on an auxiliary lock session. One install
transaction holds the dump, a nonce claim row, fenced leases and retired builds,
so the world is unservable until a nonce-checked publish after verification,
regardless of connection loss or restarts. A failed install is resolved from the
claim (committed, rolled back, or unknown with the target kept unservable and
--abandon NONCE to resolve); cleanup only removes what the nonce owns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-01 06:14:34 -04:00
co-authored by Claude Opus 5.5
parent 3a34ae39bc
commit 68da5654ff
5 changed files with 386 additions and 145 deletions
+79 -33
View File
@@ -60,7 +60,7 @@ expect_refusal() { # expect_refusal <exit code> <bundle> <what>
--bundle "$2" --objects "$work/restored-$case_n" > "$work/case-$case_n.log" 2>&1 || code=$?
[ "$code" = "$1" ] || { cat "$work/case-$case_n.log" >&2; fail "$3: exit $code, expected $1"; }
[ ! -e "$work/restored-$case_n" ] || fail "$3: restored objects were left behind"
[ "$(PGDATABASE="evo_restore_case_$case_n" psql -XAtc "SELECT count(*) FROM pg_namespace WHERE nspname = 'evolution'")" = 0 ] ||
[ "$(PGDATABASE="evo_restore_case_$case_n" psql -XAtc "SELECT count(*) FROM pg_namespace WHERE nspname IN ('evolution', 'evolution_restore')")" = 0 ] ||
fail "$3: a restored schema was left behind"
}
damaged() { rm -rf "$work/damaged"; cp -a "$work/bundle" "$work/damaged"; }
@@ -90,46 +90,92 @@ PGDATABASE=evo_restore_twice bash "$root/tools/backup/restore-world.sh" --bundle
[ "$code" = 4 ] && grep -q 'already has an evolution schema' "$work/twice.log" || fail "a second restore into one database"
[ "$(PGDATABASE=evo_restore_twice psql -XAtc 'SELECT end_tick FROM evolution.region_heads WHERE world_id = 300')" = "$backup_tick" ] ||
fail "the refused second restore changed the first"
# Ownership races (review M8 P1). A competing actor creates the schema between this
# restore's checks and its pg_restore: the restore fails and leaves that schema alone.
# Ownership and failure paths (M8 review P1; follow-up P1/P2). Each case names the
# boundary it interrupts; only this invocation's state may ever be removed.
restore() { # restore <database> <objects> [env...]: runs restore-world.sh, sets rc.
local db="$1" dir="$2"; shift 2
rc=0
env PGDATABASE="$db" "$@" bash "$root/tools/backup/restore-world.sh" --bundle "$work/bundle" \
--objects "$dir" > "$work/$(basename "$dir").log" 2>&1 || rc=$?
}
schemas() { PGDATABASE="$1" psql -XAtc "SELECT count(*) FROM pg_namespace WHERE nspname IN ('evolution', 'evolution_restore')"; }
acquire_refused() { # A lease acquisition in <database> is refused (the world is unservable).
! PGDATABASE="$1" psql -XAt -v ON_ERROR_STOP=1 \
-c "SELECT * FROM evolution.acquire_region_lease(300, 1, 20000, decode(repeat('b1', 32), 'hex'))" \
> "$work/acquire-$1.log" 2>&1 && grep -q 'build not current' "$work/acquire-$1.log"
}
log_of() { cat "$work/$(basename "$1").log" >&2; }
# A competing actor creates the schema after this restore's checks: its install
# transaction rolls back (confirmed), and the competitor's schema survives.
createdb evo_restore_race
code=0
PGDATABASE=evo_restore_race EVO_RESTORE_TEST_HOOK='[ "$1" = before-restore ] && psql -XAtq -c "CREATE SCHEMA evolution" -c "CREATE TABLE evolution.winner(x int)" || true' \
bash "$root/tools/backup/restore-world.sh" --bundle "$work/bundle" --objects "$work/race-a" \
> "$work/race-a.log" 2>&1 || code=$?
[ "$code" = 4 ] && grep -q 'pg_restore failed; the database was not changed' "$work/race-a.log" ||
{ cat "$work/race-a.log" >&2; fail "a pre-empted restore: exit $code"; }
restore evo_restore_race "$work/race-a" EVO_RESTORE_TEST_HOOK='[ "$1" = before-install ] && psql -XAtq -c "CREATE SCHEMA evolution" -c "CREATE TABLE evolution.winner(x int)" || true'
[ "$rc" = 4 ] && grep -q 'rolled back; the database was not changed (confirmed)' "$work/race-a.log" ||
{ log_of race-a; fail "a pre-empted restore: exit $rc"; }
[ "$(PGDATABASE=evo_restore_race psql -XAtc "SELECT count(*) FROM pg_tables WHERE schemaname = 'evolution' AND tablename = 'winner'")" = 1 ] ||
fail "a pre-empted restore deleted the competing schema"
[ ! -e "$work/race-a" ] || fail "a pre-empted restore left its own objects directory"
# Two restores of one database: the second refuses at once and touches nothing of
# the first, which completes. While the first holds the database (after its install),
# a lease acquisition there waits instead of running against unverified state.
# Two restores of one database: whichever installs first wins; the other rolls back
# and leaves the winner published and intact.
createdb evo_restore_pair
PGDATABASE=evo_restore_pair EVO_RESTORE_TEST_HOOK='
if [ "$1" = before-restore ]; then touch "'"$work"'/pair-claimed"; sleep 3; fi
if [ "$1" = after-restore ]; then
psql -XAt -c "SET lock_timeout = '"'"'1s'"'"'" -c "SELECT * FROM evolution.acquire_region_lease(300, 1, 20000, decode(repeat('"'"'b1'"'"', 32), '"'"'hex'"'"'))" > "'"$work"'/pair-acquire.log" 2>&1 || true
fi' bash "$root/tools/backup/restore-world.sh" --bundle "$work/bundle" --objects "$work/pair-a" \
> "$work/pair-a.log" 2>&1 &
restore evo_restore_pair "$work/pair-a" EVO_RESTORE_TEST_HOOK='[ "$1" = before-install ] && sleep 4 || true' &
pair_pid=$!
for _ in $(seq 1 100); do [ -e "$work/pair-claimed" ] && break; sleep 0.1; done
code=0
PGDATABASE=evo_restore_pair bash "$root/tools/backup/restore-world.sh" --bundle "$work/bundle" \
--objects "$work/pair-b" > "$work/pair-b.log" 2>&1 || code=$?
[ "$code" = 4 ] && grep -q 'another restore holds this database' "$work/pair-b.log" ||
{ cat "$work/pair-b.log" >&2; fail "a concurrent restore: exit $code"; }
[ ! -e "$work/pair-b" ] || fail "the refused concurrent restore left its objects directory"
sleep 1
restore evo_restore_pair "$work/pair-b"
[ "$rc" = 0 ] && grep -q '^RESTORE VERIFIED regions=1 ' "$work/pair-b.log" || { log_of pair-b; fail "the first installer: exit $rc"; }
code=0; wait "$pair_pid" || code=$?
[ "$code" = 0 ] && grep -q '^RESTORE VERIFIED regions=1 ' "$work/pair-a.log" ||
{ cat "$work/pair-a.log" >&2; fail "the first of two restores: exit $code"; }
grep -q 'lock timeout' "$work/pair-acquire.log" ||
{ cat "$work/pair-acquire.log" >&2; fail "a lease acquisition ran during the restore"; }
grep -q 'rolled back; the database was not changed (confirmed)' "$work/pair-a.log" && [ ! -e "$work/pair-a" ] ||
{ log_of pair-a; fail "the second installer: exit $code"; }
[ "$(PGDATABASE=evo_restore_pair psql -XAtc "SELECT state FROM evolution_restore.claims")" = published ] &&
[ "$(PGDATABASE=evo_restore_pair psql -XAtc 'SELECT end_tick FROM evolution.region_heads WHERE world_id = 300')" = "$backup_tick" ] ||
fail "the losing restore disturbed the winner"
# After install and before publication the world is unservable, durably: across a
# database restart (every connection and lock lost) acquisition is still refused, and
# the restore then verifies and publishes; afterwards the build is current again.
createdb evo_restore_gate
restore evo_restore_gate "$work/gate" EVO_RESTORE_TEST_HOOK='
if [ "$1" = after-install ]; then
psql -XAt -c "SELECT * FROM evolution.acquire_region_lease(300, 1, 20000, decode(repeat('"'"'b1'"'"', 32), '"'"'hex'"'"'))" > "'"$work"'/gate-before.log" 2>&1 || true
pg_ctl -D "$PGHOST/data" -m fast -w restart > /dev/null
psql -XAt -c "SELECT * FROM evolution.acquire_region_lease(300, 1, 20000, decode(repeat('"'"'b1'"'"', 32), '"'"'hex'"'"'))" > "'"$work"'/gate-after.log" 2>&1 || true
fi'
[ "$rc" = 0 ] && grep -q '^RESTORE VERIFIED regions=1 ' "$work/gate.log" || { log_of gate; fail "a restore across a restart: exit $rc"; }
grep -q 'build not current' "$work/gate-before.log" && grep -q 'build not current' "$work/gate-after.log" ||
{ cat "$work/gate-before.log" "$work/gate-after.log" >&2; fail "authority was available before publication"; }
[ "$(PGDATABASE=evo_restore_gate psql -XAtc 'SELECT count(*) FROM evolution.retained_builds WHERE retired_at IS NULL')" = 1 ] ||
fail "publication did not restore the current build"
# A committed install whose completion is lost is resolved from the claim and
# verified, not reported as unchanged.
createdb evo_restore_lost
restore evo_restore_lost "$work/lost" EVO_RESTORE_TEST_INSTALL_ACK=lost
[ "$rc" = 0 ] && grep -q '^install outcome resolved: committed' "$work/lost.log" &&
grep -q '^RESTORE VERIFIED regions=1 ' "$work/lost.log" || { log_of lost; fail "a lost install acknowledgement: exit $rc"; }
# An install whose outcome cannot be resolved keeps everything, stays unservable,
# and is removed only by --abandon with its claim.
createdb evo_restore_unknown
restore evo_restore_unknown "$work/unknown" EVO_RESTORE_TEST_INSTALL_ACK=unreachable
nonce="$(sed -n 's/^restore claim \([0-9a-f]\{32\}\)$/\1/p' "$work/unknown.log")"
[ "$rc" = 5 ] && grep -q '^RESTORE OUTCOME UNKNOWN' "$work/unknown.log" && [ -d "$work/unknown" ] && [ -n "$nonce" ] ||
{ log_of unknown; fail "an unresolvable install: exit $rc"; }
[ "$(schemas evo_restore_unknown)" = 2 ] && acquire_refused evo_restore_unknown || fail "an unresolved restore is servable"
code=0
PGDATABASE=evo_restore_unknown bash "$root/tools/backup/restore-world.sh" --abandon 00000000000000000000000000000000 \
--objects "$work/unknown" > "$work/abandon-wrong.log" 2>&1 || code=$?
[ "$(schemas evo_restore_unknown)" = 2 ] && [ -d "$work/unknown" ] || fail "--abandon with another claim removed state"
PGDATABASE=evo_restore_unknown bash "$root/tools/backup/restore-world.sh" --abandon "$nonce" \
--objects "$work/unknown" > "$work/abandon.log" 2>&1 || { cat "$work/abandon.log" >&2; fail "--abandon"; }
[ "$(schemas evo_restore_unknown)" = 0 ] && [ ! -e "$work/unknown" ] || fail "--abandon left state behind"
restore evo_restore_unknown "$work/unknown-retry"
[ "$rc" = 0 ] || { log_of unknown-retry; fail "a restore after --abandon: exit $rc"; }
# An existing objects path is never claimed or removed.
mkdir "$work/occupied"; echo keep > "$work/occupied/mine"
createdb evo_restore_occupied
code=0
PGDATABASE=evo_restore_occupied bash "$root/tools/backup/restore-world.sh" --bundle "$work/bundle" \
--objects "$work/occupied" > "$work/occupied.log" 2>&1 || code=$?
[ "$code" = 4 ] && [ "$(cat "$work/occupied/mine")" = keep ] || fail "an occupied objects path: exit $code"
restore evo_restore_occupied "$work/occupied"
[ "$rc" = 4 ] && [ "$(cat "$work/occupied/mine")" = keep ] && [ "$(schemas evo_restore_occupied)" = 0 ] ||
fail "an occupied objects path: exit $rc"
echo "backup and restore qualification passed (backup tick $backup_tick, acknowledged $acked_before, source $source_tick)"