fix: M8 review findings (restore ownership, barrier tool, World0 claimRef, CI selection, drill verdicts)
P1: restore-world.sh claims its objects path (mkdir) and the database (a held session with a restore try-lock and the exclusive upgrade lock), and removes only what it created, so a losing restore never drops a winner's schema. P2: barrier Ready-pod selection via shared jsonpath helper with fixtures; barrier pod fsGroup; script-generated pods inspected by the policy check; World0 PV claim references protected; ci-paths.sh uses the event's real base and runs on uncertainty; fault-drill asserts RTO and needs positive quarantine evidence. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
# (ADR-0074).
|
||||
#
|
||||
# tools/backup/cluster-backup.sh <dev|stage> --to DIR [--verify WORLD] [--context NAME]
|
||||
# tools/backup/cluster-backup.sh <dev|stage> --print-manifest
|
||||
#
|
||||
# Runs one short-lived Pod in evolution-<env> (image: the postgres-tools digest
|
||||
# pinned in deploy/kubernetes/stores/<env>; migrator login; the aevolution-objects
|
||||
@@ -14,11 +15,12 @@
|
||||
# hypervisor host. --verify WORLD then runs verify-backup.sh on this machine
|
||||
# (restore on a private cluster, rebuilt and served as isolated world WORLD); it
|
||||
# needs the PostgreSQL 18.6 binaries and evo-server/evo-world-clone.
|
||||
# World0 is refused before anything touches the cluster. The Pod is an operator
|
||||
# --print-manifest prints the Pod and exits without touching the cluster (the deploy
|
||||
# policy check inspects it). World0 is refused before anything touches the cluster. The Pod is an operator
|
||||
# action, like fault-drill.sh; it is not part of any Argo application. A scheduled
|
||||
# backup (CronJob in Git, synced by Argo) follows once the destination exists.
|
||||
set -euo pipefail
|
||||
usage() { echo "usage: $0 <dev|stage> --to DIR [--verify WORLD] [--context NAME]" >&2; exit 2; }
|
||||
usage() { echo "usage: $0 <dev|stage> --to DIR [--verify WORLD] [--context NAME] | --print-manifest" >&2; exit 2; }
|
||||
[ $# -ge 1 ] || usage
|
||||
env="$1"; shift
|
||||
case "$env" in
|
||||
@@ -26,16 +28,19 @@ case "$env" in
|
||||
*) echo "refused: '$env' is not a disposable environment (only dev or stage; never world0)" >&2
|
||||
exit 2 ;;
|
||||
esac
|
||||
to="" verify="" k=(kubectl --request-timeout=30s)
|
||||
to="" verify="" print=0 k=(kubectl --request-timeout=30s)
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--to) to="${2:-}"; shift 2 ;;
|
||||
--verify) verify="${2:-}"; shift 2 ;;
|
||||
--print-manifest) print=1; shift ;;
|
||||
--context) k+=(--context "${2:?}"); shift 2 ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
done
|
||||
[ -n "$to" ] && [ -d "$to" ] || { echo "--to must be an existing directory" >&2; exit 2; }
|
||||
if [ "$print" != 1 ]; then
|
||||
[ -n "$to" ] && [ -d "$to" ] || { echo "--to must be an existing directory" >&2; exit 2; }
|
||||
fi
|
||||
root="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
ns="evolution-$env"
|
||||
image="$(awk '/name: aevolution-postgres-tools/ {f = 1} f && /newName:/ {n = $2} f && /digest:/ {print n "@" $2; exit}' \
|
||||
@@ -45,15 +50,14 @@ stamp="$(date -u +%Y%m%dT%H%M%SZ)"
|
||||
pod="aevolution-backup-$(date -u +%Y%m%d%H%M%S)"
|
||||
dest="$to/$env-$stamp"
|
||||
say() { echo "$(date -u +%H:%M:%S) $*"; }
|
||||
cleanup() { "${k[@]}" delete pod -n "$ns" "$pod" --wait=false > /dev/null 2>&1 || true; }
|
||||
trap cleanup EXIT
|
||||
|
||||
say "starting $pod in $ns ($image)"
|
||||
"${k[@]}" apply -n "$ns" -f - > /dev/null <<YAML
|
||||
manifest() {
|
||||
cat <<YAML
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: $pod
|
||||
namespace: $ns
|
||||
labels:
|
||||
app.kubernetes.io/part-of: aevolution
|
||||
app.kubernetes.io/component: backup
|
||||
@@ -112,6 +116,13 @@ spec:
|
||||
- name: scratch
|
||||
emptyDir: {medium: Memory, sizeLimit: 8Mi}
|
||||
YAML
|
||||
}
|
||||
if [ "$print" = 1 ]; then manifest; exit 0; fi
|
||||
|
||||
cleanup() { "${k[@]}" delete pod -n "$ns" "$pod" --wait=false > /dev/null 2>&1 || true; }
|
||||
trap cleanup EXIT
|
||||
say "starting $pod in $ns ($image)"
|
||||
manifest | "${k[@]}" apply -f - > /dev/null
|
||||
|
||||
for _ in $(seq 1 1800); do
|
||||
phase="$("${k[@]}" get pod -n "$ns" "$pod" -o jsonpath='{.status.phase}' 2> /dev/null || true)"
|
||||
|
||||
@@ -14,13 +14,23 @@
|
||||
# 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 target has no `evolution` schema and runs the manifest's server version;
|
||||
# 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;
|
||||
# 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).
|
||||
# On a failure after step 2 the restored schema and the copied objects are removed,
|
||||
# so an unverified restore can never be mistaken for a world. The result is a
|
||||
# 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).
|
||||
@@ -89,32 +99,69 @@ if [ "$in_place" = 1 ]; then
|
||||
privileges=()
|
||||
fi
|
||||
|
||||
# 2. Target: empty, same server version.
|
||||
[ ! -e "$objects" ] || refuse "objects target already exists: $objects"
|
||||
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=""
|
||||
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
|
||||
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"
|
||||
}
|
||||
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')")"
|
||||
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" = 0 ] || refuse "target database already has an evolution schema"
|
||||
|
||||
restored=0
|
||||
cleanup() {
|
||||
[ "$restored" = 1 ] && return
|
||||
psql -X --no-password -q -v ON_ERROR_STOP=1 -c 'DROP SCHEMA IF EXISTS evolution CASCADE' > /dev/null 2>&1 || true
|
||||
rm -rf -- "$objects"
|
||||
echo "RESTORE FAILED: the restored schema and objects were removed" >&2
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# 3. Database in one transaction, then objects.
|
||||
target="${PGDATABASE:-}"
|
||||
[ -n "${PGSERVICE:-}" ] && target="service=$PGSERVICE"
|
||||
[ -n "$target" ] || { echo "set PGDATABASE or PGSERVICE for the target" >&2; exit 2; }
|
||||
# 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"
|
||||
mkdir -m 700 -- "$objects"
|
||||
--dbname="$target" "$bundle/database.dump" || refuse "pg_restore failed; the database was not changed"
|
||||
installed=1
|
||||
(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
|
||||
|
||||
# 4. Heads exactly as recorded.
|
||||
actual_heads="$(psql -X --no-password -A -t -F ' ' -v ON_ERROR_STOP=1 -c "
|
||||
|
||||
Reference in New Issue
Block a user