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
+216 -95
View File
@@ -3,53 +3,67 @@
#
# tools/backup/restore-world.sh --bundle DIR --objects DIR
# [--in-place --runtime-role ROLE --accept-rewind]
# tools/backup/restore-world.sh --abandon NONCE --objects DIR
#
# Target connection: PG* environment naming an EMPTY database that the login owns
# (PGHOST/PGPORT/PGDATABASE/PGUSER, or PGSERVICE with PGSERVICEFILE/PGPASSFILE).
# The restore needs no source role, credential or key: owners and grants are not
# restored, so the target login owns everything restored. --objects must not
# exist yet (it is created 0700). evo-world-clone comes from EVO_WORLD_CLONE_BIN or
# PATH.
# The restore needs no source role, credential or key: owners are not restored, and
# grants only with --in-place, so the target login owns everything restored.
# evo-world-clone comes from EVO_WORLD_CLONE_BIN or PATH.
#
# Order, failing closed at each step:
# 1. every bundle file matches SHA256SUMS, no file is unlisted, and every object's
# name is its digest; nothing is touched before this passes;
# 2. the targets are claimed for this invocation alone (below), then the target
# has no `evolution` schema and runs the manifest's server version;
# 3. pg_restore in one transaction, then objects are copied;
# 2. --objects is claimed by creating it (mkdir, never -p: an existing path is
# refused and never touched) and marked with this invocation's random NONCE; the
# target must have neither `evolution` nor `evolution_restore` and must run the
# manifest's server version;
# 3. INSTALL, one transaction: the dump, a claim row evolution_restore.claims(NONCE,
# unpublished), every lease fenced and every current build retired. From that
# commit on the world is unservable: acquisition, renewal and commits refuse
# until publication, whatever happens to any connection or the server (review
# M8 follow-up P1). Then the objects are copied;
# 4. the restored heads equal the MANIFEST heads exactly;
# 5. every region's full history is audited (evo-world-clone audit: each link's
# manifest, payload, trace replay and outcomes, from genesis to the head).
# Ownership (review M8 P1): --objects is claimed by creating it (mkdir, never -p: an
# existing path is refused and never touched), and the database by one psql session
# held open for the whole restore. That session takes the restore claim (advisory
# 71420004, try-lock: a concurrent restore of the same database refuses at once) and
# the upgrade lock exclusively (71420002: provisioning, acquisition and lease checks
# in that database wait until this restore is verified or removed). On failure only
# what this invocation created is removed: the schema only if its own pg_restore
# committed, the objects directory only if its own mkdir created it. So an
# unverified restore can never be mistaken for a world, and a losing restore never
# deletes a winner's state. EVO_RESTORE_TEST_HOOK (tests only) is run by bash with
# $1 = before-restore (claimed, checked) and after-restore (installed, objects copied). The result is a
# verbatim, owner-only copy with the source's world identities: it has no runtime
# grants and is never served. To run it, clone it into an isolated world
# (tools/backup/verify-backup.sh --isolate).
# 5. every region's full history is audited (evo-world-clone audit), and with
# --in-place migrate-postgres.sh audits the runtime role's effective rights;
# 6. PUBLISH, one transaction that first locks this NONCE's unpublished claim:
# exactly the builds the install retired become current again and the claim
# becomes published. A restore that lost its claim cannot publish.
# A failed or interrupted install is resolved, not assumed (review M8 follow-up P2):
# the claim row says whether this invocation's transaction committed. Committed ->
# verification continues; absent -> rolled back (the database was not changed);
# unreachable -> OUTCOME UNKNOWN (exit 5): the target stays unservable and both the
# schema and the objects are kept; resolve it later with --abandon NONCE.
# Cleanup only ever removes what this NONCE owns: the schemas only through a
# transaction that locks this NONCE's unpublished claim, the objects directory only
# while it carries this NONCE's marker. A competing restore's or provisioner's state
# is never touched. The default result is a verbatim, owner-only copy with the
# source's world identities, no runtime grants and never served; to run it, clone it
# into an isolated world (tools/backup/verify-backup.sh --isolate).
#
# --in-place (M8.8 host-loss recovery for dev/stage, ADR-0075) restores the world
# under its own identity so its hosts can serve it again: the dump's grants are
# restored (the environment's roles must already exist in the target cluster), every
# restored lease is fenced so a new owner acquires under a newer epoch, and
# migrate-postgres.sh audits the runtime role's effective rights. Every acknowledgement
# after the bundle's cut is undone: deaths may reverse and resources may reappear.
# --accept-rewind states that this is acceptable, which it is for dev and stage only;
# World0 launch stays blocked until acknowledgements survive host loss (ADR-0075).
# Fence the old database with fence-database.sh before any of its hosts can return.
# under its own identity so its hosts can serve it again after publication: the
# dump's grants are restored (the environment's roles must already exist in the
# target cluster). Every acknowledgement after the bundle's cut is undone: deaths may
# reverse and resources may reappear. --accept-rewind states that this is
# acceptable, which it is for dev and stage only; World0 launch stays blocked until
# acknowledgements survive host loss (ADR-0075). Fence the old database with
# fence-database.sh before any of its hosts can return.
#
# Exit: 0 verified and published, 2 usage, 3 verification failed (this restore's
# state removed), 4 refused or rolled back (nothing of this restore left),
# 5 outcome unknown (target unservable; evidence kept).
# Tests only: EVO_RESTORE_TEST_HOOK is run by bash with $1 = before-install,
# after-install or before-publish; EVO_RESTORE_TEST_INSTALL_ACK=lost treats a committed
# install as failed (an ambiguous completion), =unreachable also makes its resolution
# unreachable.
set -euo pipefail
usage() {
echo "usage: $0 --bundle DIR --objects DIR [--in-place --runtime-role ROLE --accept-rewind]" >&2
echo " $0 --abandon NONCE --objects DIR" >&2
exit 2
}
bundle="" objects="" in_place=0 runtime_role="" accept_rewind=0
bundle="" objects="" in_place=0 runtime_role="" accept_rewind=0 abandon=""
while [ $# -gt 0 ]; do
case "$1" in
--bundle) bundle="${2:-}"; shift 2 ;;
@@ -57,10 +71,16 @@ while [ $# -gt 0 ]; do
--in-place) in_place=1; shift ;;
--runtime-role) runtime_role="${2:-}"; shift 2 ;;
--accept-rewind) accept_rewind=1; shift ;;
--abandon) abandon="${2:-}"; shift 2 ;;
*) usage ;;
esac
done
[ -n "$bundle" ] && [ -n "$objects" ] || usage
[ -n "$objects" ] || usage
if [ -n "$abandon" ]; then
[[ "$abandon" =~ ^[0-9a-f]{32}$ ]] && [ -z "$bundle" ] && [ "$in_place" = 0 ] || usage
else
[ -n "$bundle" ] || usage
fi
if [ "$in_place" = 1 ]; then
[ -n "$runtime_role" ] || { echo "--in-place needs --runtime-role" >&2; exit 2; }
[ "$accept_rewind" = 1 ] ||
@@ -69,10 +89,55 @@ elif [ -n "$runtime_role" ] || [ "$accept_rewind" = 1 ]; then
usage
fi
root="$(cd "$(dirname "$0")/../.." && pwd)"
bundle="$(realpath -- "$bundle")" objects="$(realpath -m -- "$objects")"
objects="$(realpath -m -- "$objects")"
clone_bin="${EVO_WORLD_CLONE_BIN:-evo-world-clone}"
umask 077
begin_ms="$(date +%s%3N)"
q() { psql -X --no-password -q -A -t -v ON_ERROR_STOP=1 "$@"; }
# The claim row of NONCE: none, unpublished, published, or unknown (unreachable).
claim_state() {
local nonce="$1" exists state
exists="$(q -c "SELECT to_regclass('evolution_restore.claims') IS NOT NULL" 2> /dev/null)" || { echo unknown; return; }
[ "$exists" = t ] || { echo none; return; }
state="$(q -c "SELECT state FROM evolution_restore.claims WHERE nonce = '$nonce'" 2> /dev/null)" ||
{ echo unknown; return; }
echo "${state:-none}"
}
# Removes NONCE's unpublished restore: both schemas, in one transaction that first
# locks that claim. Anything else (another claim, a published one) is refused.
drop_claimed() {
q -c "DO \$\$ BEGIN
IF to_regclass('evolution_restore.claims') IS NULL THEN
RAISE EXCEPTION 'no restore claim';
END IF;
PERFORM 1 FROM evolution_restore.claims WHERE nonce = '$1' AND state = 'unpublished' FOR UPDATE;
IF NOT FOUND THEN
RAISE EXCEPTION 'restore claim % is not unpublished here', '$1';
END IF;
DROP SCHEMA evolution CASCADE;
DROP SCHEMA evolution_restore CASCADE;
END \$\$" > /dev/null 2>&1
}
# Removes the objects directory only while it carries NONCE's marker.
drop_objects() {
[ -f "$objects/.restore-claim" ] && [ "$(cat "$objects/.restore-claim")" = "$1" ] && rm -rf -- "$objects"
}
if [ -n "$abandon" ]; then
state="$(claim_state "$abandon")"
case "$state" in
unpublished) drop_claimed "$abandon" || { echo "ABANDON FAILED: could not remove claim $abandon" >&2; exit 3; } ;;
none) ;;
published) echo "ABANDON REFUSED: claim $abandon is published (a verified world)" >&2; exit 4 ;;
*) echo "ABANDON FAILED: the target database is unreachable" >&2; exit 5 ;;
esac
drop_objects "$abandon" || true
echo "ABANDONED claim $abandon (database state was $state; objects $([ -e "$objects" ] && echo kept || echo removed))"
exit 0
fi
bundle="$(realpath -- "$bundle")"
refuse() { echo "RESTORE REFUSED: $*" >&2; exit 4; }
# 1. Bundle integrity, before anything else is touched.
@@ -93,78 +158,112 @@ done <<< "$present"
grep -qx 'format aevolution-backup-v1' "$bundle/MANIFEST" || refuse "unknown bundle format"
expected_heads="$(grep '^head ' "$bundle/MANIFEST" | cut -d' ' -f2- | sort)"
[ -n "$expected_heads" ] || refuse "manifest names no region"
privileges=(--no-privileges)
privileges=(--no-privileges) mode=isolated
if [ "$in_place" = 1 ]; then
grep -qx 'dump privileges' "$bundle/MANIFEST" || refuse "bundle predates in-place restore (no grants in its dump)"
privileges=()
privileges=() mode=in-place
fi
manifest_sha="$(sha256sum "$bundle/MANIFEST")"; manifest_sha="${manifest_sha%% *}"
label="$(sed -n 's/^label \([A-Za-z0-9._:-]*\)$/\1/p' "$bundle/MANIFEST" | head -1)"
hook() { [ -z "${EVO_RESTORE_TEST_HOOK:-}" ] || bash -c "$EVO_RESTORE_TEST_HOOK" hook "$1"; }
target="${PGDATABASE:-}"
[ -n "${PGSERVICE:-}" ] && target="service=$PGSERVICE"
[ -n "$target" ] || { echo "set PGDATABASE or PGSERVICE for the target" >&2; exit 2; }
# 2. Claim the targets, then check them under the claim.
created_objects=0 installed=0 restored=0 lock_pid="" claim=""
# 2. Claim the objects path, then check the target.
nonce="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"
mkdir -m 700 -- "$objects" 2> /dev/null || refuse "objects target already exists or cannot be created: $objects"
echo "$nonce" > "$objects/.restore-claim"
work="$(mktemp -d /tmp/evo-restore.XXXXXXXX)"
installed=unknown_before published=0
cleanup() {
local code=$?
if [ "$restored" != 1 ]; then
if [ "$installed" = 1 ]; then
psql -X --no-password -q -v ON_ERROR_STOP=1 -c 'DROP SCHEMA evolution CASCADE' > /dev/null 2>&1 || true
fi
[ "$created_objects" = 1 ] && rm -rf -- "$objects"
if [ "$installed" = 1 ] || [ "$created_objects" = 1 ]; then
echo "RESTORE FAILED: removed only what this restore created" \
"(schema=$([ "$installed" = 1 ] && echo yes || echo no), objects=$([ "$created_objects" = 1 ] && echo yes || echo no))" >&2
fi
if [ "$published" != 1 ]; then
case "$installed" in
no) drop_objects "$nonce" || true ;;
yes)
if drop_claimed "$nonce"; then
drop_objects "$nonce" || true
echo "RESTORE FAILED: removed this restore's schema and objects (claim $nonce)" >&2
else
echo "RESTORE FAILED: claim $nonce could not be removed; the target stays unservable." \
"Resolve with: $0 --abandon $nonce --objects $objects" >&2
code=5
fi
;;
unknown_before) drop_objects "$nonce" || true ;;
*)
echo "RESTORE OUTCOME UNKNOWN: claim $nonce. If installed, the target is unservable" \
"(every build retired). Resolve with: $0 --abandon $nonce --objects $objects" >&2
code=5
;;
esac
fi
[ -n "$claim" ] && exec 7>&- # Ends the claim session: its locks are released.
[ -n "$lock_pid" ] && { wait "$lock_pid" 2> /dev/null || true; }
[ -n "$claim" ] && rm -rf -- "$claim"
return "$code"
rm -rf -- "$work"
exit "$code"
}
trap cleanup EXIT
mkdir -m 700 -- "$objects" 2> /dev/null || refuse "objects target already exists or cannot be created: $objects"
created_objects=1
claim="$(mktemp -d /tmp/evo-restore-claim.XXXXXXXX)"
mkfifo "$claim/in"
psql -X --no-password -q -A -t -v ON_ERROR_STOP=1 < "$claim/in" > "$claim/out" 2>&1 &
lock_pid=$!
exec 7> "$claim/in"
cat >&7 <<SQL
SELECT pg_catalog.pg_try_advisory_lock(71420004) AS claimed \gset
\if :claimed
SELECT pg_catalog.pg_advisory_lock(71420002);
\! touch '$claim/held'
\else
\! touch '$claim/busy'
\endif
SQL
for _ in $(seq 1 600); do
[ -e "$claim/held" ] || [ -e "$claim/busy" ] && break
kill -0 "$lock_pid" 2> /dev/null || break
sleep 0.1
done
[ -e "$claim/busy" ] && refuse "another restore holds this database"
[ -e "$claim/held" ] || refuse "could not claim the target database ($(head -c 200 "$claim/out"))"
version="$(psql -X --no-password -A -t -v ON_ERROR_STOP=1 -c "SELECT current_setting('server_version')")"
echo "restore claim $nonce"
version="$(q -c "SELECT current_setting('server_version')")"
grep -qx "database $version" "$bundle/MANIFEST" || refuse "target server $version differs from the manifest"
existing="$(psql -X --no-password -A -t -v ON_ERROR_STOP=1 -c "SELECT count(*) FROM pg_namespace WHERE nspname = 'evolution'")"
existing="$(q -c "SELECT count(*) FROM pg_namespace WHERE nspname IN ('evolution', 'evolution_restore')")"
[ "$existing" = 0 ] || refuse "target database already has an evolution schema"
# 3. Database in one transaction, then objects. A failed pg_restore installed
# nothing (one transaction), so nothing of the database is removed after it.
hook() { [ -z "${EVO_RESTORE_TEST_HOOK:-}" ] || bash -c "$EVO_RESTORE_TEST_HOOK" hook "$1"; }
hook before-restore
pg_restore --no-password --exit-on-error --single-transaction --no-owner "${privileges[@]}" \
--dbname="$target" "$bundle/database.dump" || refuse "pg_restore failed; the database was not changed"
installed=1
# 3. INSTALL: dump, claim and the serving gate commit together or not at all. The
# COMMIT is only sent after the complete script: a truncated stream rolls back.
pg_restore --no-owner "${privileges[@]}" --file="$work/restore.sql" "$bundle/database.dump" ||
refuse "the dump could not be read"
{
cat <<SQL
\\set ON_ERROR_STOP on
BEGIN;
CREATE SCHEMA evolution_restore;
REVOKE ALL ON SCHEMA evolution_restore FROM PUBLIC;
CREATE TABLE evolution_restore.claims (
nonce text PRIMARY KEY CHECK (nonce ~ '^[0-9a-f]{32}\$'),
state text NOT NULL CHECK (state IN ('unpublished', 'published')),
mode text NOT NULL, manifest_sha256 text NOT NULL, label text,
held_builds bytea[] NOT NULL,
installed_at timestamptz NOT NULL DEFAULT clock_timestamp(),
published_at timestamptz);
REVOKE ALL ON evolution_restore.claims FROM PUBLIC;
SQL
cat "$work/restore.sql"
cat <<SQL
INSERT INTO evolution_restore.claims(nonce, state, mode, manifest_sha256, label, held_builds)
SELECT '$nonce', 'unpublished', '$mode', '$manifest_sha', '$label',
coalesce(pg_catalog.array_agg(b.build_identity ORDER BY b.build_identity), '{}')
FROM evolution.retained_builds b WHERE b.retired_at IS NULL;
UPDATE evolution.retained_builds SET retired_at = pg_catalog.clock_timestamp() WHERE retired_at IS NULL;
UPDATE evolution.region_leases SET holder = NULL, expires_at = NULL WHERE holder IS NOT NULL;
COMMIT;
SQL
} > "$work/install.sql"
hook before-install
status=0
psql -X --no-password -q -f "$work/install.sql" > "$work/install.log" 2>&1 || status=$?
case "${EVO_RESTORE_TEST_INSTALL_ACK:-}" in
lost) status=1 ;;
unreachable) status=1; export PGPORT=1 PGHOST=/nonexistent ;;
esac
if [ "$status" = 0 ]; then
installed=yes
else
state="$(claim_state "$nonce")"
case "$state" in
unpublished) installed=yes; echo "install outcome resolved: committed (claim $nonce)" ;;
none)
installed=no
sed -n 's/^psql:[^:]*:[0-9]*: //p' "$work/install.log" | head -3 >&2
refuse "the restore transaction rolled back; the database was not changed (confirmed)"
;;
*) installed=unknown; exit 5 ;;
esac
fi
(cd "$bundle/objects" && find . -mindepth 2 -maxdepth 2 -type d -printf '%P\0' |
xargs -0 -r -I{} mkdir -p -m 700 -- "$objects/{}")
(cd "$bundle/objects" && find . -type f -printf '%P\0' | xargs -0 -r -I{} cp -- {} "$objects/{}")
hook after-restore
hook after-install
# 4. Heads exactly as recorded.
actual_heads="$(psql -X --no-password -A -t -F ' ' -v ON_ERROR_STOP=1 -c "
actual_heads="$(q -F ' ' -c "
SELECT h.world_id, h.region_id, l.frame_id, h.commit_sequence, h.end_tick, h.commit_id,
encode(h.digest, 'hex'), encode(c.build_identity, 'hex')
FROM evolution.region_heads h
@@ -173,7 +272,8 @@ actual_heads="$(psql -X --no-password -A -t -F ' ' -v ON_ERROR_STOP=1 -c "
AND c.commit_sequence = h.commit_sequence" | sort)"
[ "$actual_heads" = "$expected_heads" ] || { echo "restored heads differ from the manifest" >&2; exit 3; }
# 5. Full history audit of every region.
# 5. Full history audit of every region (the CLI's retained list, not the database's
# temporarily retired state), then the runtime rights audit for --in-place.
pg=()
if [ -n "${PGSERVICEFILE:-}" ]; then
pg=(--pg-service "$PGSERVICEFILE")
@@ -195,17 +295,38 @@ while read -r world region _ _ tick _; do
echo "region $world/$region $line"
done <<< "$expected_heads"
if [ "$in_place" = 1 ]; then
# No lease from before the loss survives: the next owner acquires a newer epoch.
psql -X --no-password -q -v ON_ERROR_STOP=1 -c \
'UPDATE evolution.region_leases SET holder = NULL, expires_at = NULL WHERE holder IS NOT NULL' > /dev/null
EVO_PG_RUNTIME_ROLE="$runtime_role" bash "$root/tools/migrate-postgres.sh" > /dev/null ||
{ echo "runtime role $runtime_role failed the rights audit" >&2; exit 3; }
fi
# 6. PUBLISH under this NONCE's claim; an ambiguous completion is resolved likewise.
hook before-publish
status=0
q -c "DO \$\$ DECLARE held bytea[]; BEGIN
SELECT c.held_builds INTO held FROM evolution_restore.claims c
WHERE c.nonce = '$nonce' AND c.state = 'unpublished' FOR UPDATE;
IF NOT FOUND THEN
RAISE EXCEPTION 'restore claim % lost before publication', '$nonce';
END IF;
UPDATE evolution.retained_builds SET retired_at = NULL WHERE build_identity = ANY (held);
UPDATE evolution_restore.claims SET state = 'published', published_at = clock_timestamp()
WHERE nonce = '$nonce';
END \$\$" > /dev/null 2>&1 || status=$?
if [ "$status" != 0 ]; then
case "$(claim_state "$nonce")" in
published) ;;
unpublished) echo "publication failed" >&2; exit 3 ;;
*) installed=unknown; exit 5 ;;
esac
fi
published=1
rm -f -- "$objects/.restore-claim"
if [ "$in_place" = 1 ]; then
created="$(sed -n 's/^created //p' "$bundle/MANIFEST")"
while read -r world region _ _ tick _; do
echo "REWIND region $world/$region restored at tick $tick (backup $created): every acknowledgement after this tick is undone"
done <<< "$expected_heads"
fi
restored=1
what="RESTORE VERIFIED"
[ "$in_place" = 1 ] && what="RESTORE IN PLACE VERIFIED"
echo "$what regions=$(wc -l <<< "$expected_heads") commits=$commits duration_ms=$(( $(date +%s%3N) - begin_ms ))"
echo "$what regions=$(wc -l <<< "$expected_heads") commits=$commits claim=$nonce duration_ms=$(( $(date +%s%3N) - begin_ms ))"