diff --git a/pm-stack/STORE-MIGRATION-PLAN-6816.md b/pm-stack/STORE-MIGRATION-PLAN-6816.md index 8ff2dc2254..f8594ad88b 100644 --- a/pm-stack/STORE-MIGRATION-PLAN-6816.md +++ b/pm-stack/STORE-MIGRATION-PLAN-6816.md @@ -113,6 +113,11 @@ credential enters argv, logs, or the receipt. Suites: writes made on Supabase during the window are exported (`pg_dump` of the target) and replayed into the local DB before reopening, so rollback restores the prior writer **without losing writes** (#6816 acceptance). + Driver: `pm-stack/scripts/kaneo-cutover.sh --mode rollback` — refuses + unless the promoted writer is fenced, replays the target delta into the + source, demands exact table/row parity, and writes a receipt with no + DSN. The DSN flip and restarts stay window operator steps; the first + window records a run. - **Grist / Pad:** stop the promoted instance, point the data directory back at the untouched laptop volume, restart. Writes made in the Supabase-backed home sync back before the restart — the sync is @@ -124,8 +129,9 @@ credential enters argv, logs, or the receipt. Suites: - Step 2 (windowed): the cutover itself, per §4 — run `presync`, then the fenced writers, then `promote` (the gate in §4 names the driver and what - it refuses). Rollback (§5) is not yet driven by a script: it is operator - steps until the first window records a run. + it refuses). Rollback (§5) is driven by `kaneo-cutover.sh --mode + rollback` (fence-gated replay with parity receipt, stub-tested); the + DSN flip, restarts, and the first window run remain operator steps. - Step 3 (windowed): the first isolated laptop-off drill, recorded per `doc/pm-stack/COLD-RESTORE-RUNBOOK.md` with measured RTO/RPO against the #1258 targets (4 h core RTO; 1 h Kaneo RPO; 24 h Grist/Pad RPO). diff --git a/pm-stack/scripts/kaneo-cutover.sh b/pm-stack/scripts/kaneo-cutover.sh index 7a221646d6..685c7ef214 100755 --- a/pm-stack/scripts/kaneo-cutover.sh +++ b/pm-stack/scripts/kaneo-cutover.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash # kaneo-cutover — the gated Kaneo → Supabase cutover driver. # -# Implements the Kaneo half of pm-stack/STORE-MIGRATION-PLAN-6816.md §3-§4 -# for issue #6816. Two modes: +# Implements the Kaneo half of pm-stack/STORE-MIGRATION-PLAN-6816.md §3-§5 +# for issue #6816. Three modes: # # presync while live: pg_dump the source `kaneo` database, pg_restore it # into the target (muse-dev2), then verify schema parity (same @@ -20,10 +20,23 @@ # The script never rewrites the DSN itself: it gates the step # and records the evidence, so no configuration can promote # while a second writer is alive. +# rollback the gate before reopening the local writer (§5). Requires the +# promoted writer FENCED — the DSN flip renames no container, +# so the existing writer set proves no writer runs against +# either store — then exports the window's writes (pg_dump of +# the target) and replays them into the source, demanding exact +# table/row parity afterwards. Only then is a rollback receipt +# written and ROLLBACK OK printed. --unreachable is refused +# here: the writer being fenced is local, not gone. +# The driver replays the delta but never flips a DSN or +# restarts a service: the window operator does that per the +# runbook, attaching this receipt to the window record. # -# Acceptance language this implements (#6816 criterion 2): "the old writer -# is fenced before any failover promotion; no configuration permits two -# active writers." +# Acceptance language this implements (#6816 criteria 2 and 4): "the old +# writer is fenced before any failover promotion; no configuration permits +# two active writers" and "rollback restores the prior writer without +# losing writes" (the replay half; the DSN flip and restart stay window +# operator steps per the runbook). # # Secrets: DSNs are read from files (--source-dsn-file / --target-dsn-file) # and never echoed, logged, or written to the receipt. Credentials resolve @@ -32,20 +45,22 @@ # Argument contract (#3352): flags only, no positionals. `--opt value` and # `--opt=value` behave identically. Anything else is a usage error (exit 2) # before any subprocess runs: -# --mode presync|promote required +# --mode presync|promote|rollback required # --source-dsn-file FILE required; file holding the source DSN # --target-dsn-file FILE required; file holding the target DSN -# --dump-file FILE required for presync; the dump to write +# --dump-file FILE required for presync and rollback; the dump to write # --receipt FILE write the receipt here (default: stdout only) # --pg-bin-dir DIR find pg_dump/pg_restore/psql in DIR (tests) # --fence-check PATH fence checker (default: sibling fence-check.sh) # --units-dir DIR passed to fence-check (fixture mode) # --containers-file FILE passed to fence-check (fixture mode) -# --unreachable passed to fence-check: old host unreachable +# --unreachable passed to fence-check: old host unreachable. +# Refused in rollback mode (exit 2): the writer +# being fenced there is local. # -h, --help usage, exit 0 # -# Exit codes: 0 pass, 2 usage, 3 not fenced (promotion refused), 4 parity -# failure, 5 command failure. +# Exit codes: 0 pass, 2 usage, 3 not fenced (promotion/rollback refused), +# 4 parity failure, 5 command failure. set -uo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -57,6 +72,7 @@ DUMP_FILE="" RECEIPT="" PG_BIN_DIR="" FENCE_CHECK="$SCRIPT_DIR/fence-check.sh" +UNREACHABLE_FLAG=0 FENCE_ARGS=() # Exact counts for every public table, in one query: table count, then the @@ -66,7 +82,7 @@ COUNT_SQL="select count(*) || ' ' || coalesce(sum(c), 0) from (select (xpath('/r usage() { cat <<'USAGE' -usage: kaneo-cutover.sh --mode presync|promote --source-dsn-file FILE +usage: kaneo-cutover.sh --mode presync|promote|rollback --source-dsn-file FILE --target-dsn-file FILE [--dump-file FILE] [--receipt FILE] [--pg-bin-dir DIR] [--fence-check PATH] [--units-dir DIR] @@ -76,6 +92,9 @@ usage: kaneo-cutover.sh --mode presync|promote --source-dsn-file FILE promote require the old writer FENCED and exact table/row parity, then write the cutover receipt. Exit: 0 pass, 2 usage, 3 not fenced, 4 parity failure, 5 command failure. + rollback fence the promoted writer, replay the target delta into the + source, require exact parity, write the rollback receipt. + --unreachable is refused. Exit codes as promote. USAGE } @@ -93,6 +112,7 @@ while [ "$#" -gt 0 ]; do ;; --unreachable) FENCE_ARGS+=("--unreachable") + UNREACHABLE_FLAG=1 shift ;; --mode | --source-dsn-file | --target-dsn-file | --dump-file | --receipt | --pg-bin-dir | --fence-check | --units-dir | --containers-file) @@ -140,9 +160,9 @@ while [ "$#" -gt 0 ]; do done case "$MODE" in -presync | promote) ;; -"") fail_usage "--mode is required (presync or promote)" ;; -*) fail_usage "--mode must be presync or promote (got '$MODE')" ;; +presync | promote | rollback) ;; +"") fail_usage "--mode is required (presync, promote or rollback)" ;; +*) fail_usage "--mode must be presync, promote or rollback (got '$MODE')" ;; esac [ -n "$SOURCE_DSN_FILE" ] || fail_usage "--source-dsn-file is required" @@ -152,6 +172,10 @@ esac if [ "$MODE" = "presync" ]; then [ -n "$DUMP_FILE" ] || fail_usage "--dump-file is required for presync" fi +if [ "$MODE" = "rollback" ]; then + [ -n "$DUMP_FILE" ] || fail_usage "--dump-file is required for rollback" + [ "$UNREACHABLE_FLAG" -eq 0 ] || fail_usage "--unreachable is for failover promote only; rollback fences the local promoted writer" +fi if [ -n "$PG_BIN_DIR" ]; then [ -d "$PG_BIN_DIR" ] || fail_usage "--pg-bin-dir '$PG_BIN_DIR' is not a directory" @@ -253,6 +277,47 @@ if [ "$MODE" = "presync" ]; then exit 0 fi +# --- rollback: fence the promoted writer first, then replay the window's +# writes back into the source. The DSN flip renames no container, so the +# existing writer set proves no writer runs against either store. The +# driver replays the delta but never flips a DSN or restarts a service: +# the window operator does that per the runbook, attaching the receipt. +if [ "$MODE" = "rollback" ]; then + if ! FENCE_OUT="$("$FENCE_CHECK" ${FENCE_ARGS[@]+"${FENCE_ARGS[@]}"} 2>&1)" || [ "${FENCE_OUT%%:*}" != "FENCED" ]; then + FENCE_LINE="UNFENCED — ${FENCE_OUT:-fence-check produced no output}" + write_receipt "REFUSED — rollback requires the promoted writer fenced" + echo "kaneo-cutover: REFUSED — promoted writer not fenced; no rollback, no replay. $FENCE_OUT" >&2 + exit 3 + fi + FENCE_LINE="$FENCE_OUT" + + echo "kaneo-cutover: rollback — exporting the target delta to '$DUMP_FILE'" + "$PG_DUMP" -Fc -f "$DUMP_FILE" -d "$target_dsn" || { + echo "kaneo-cutover: pg_dump of the target failed" >&2 + exit 5 + } + echo "kaneo-cutover: rollback — replaying into the source" + "$PG_RESTORE" --no-owner --no-privileges -d "$source_dsn" "$DUMP_FILE" || { + echo "kaneo-cutover: pg_restore into the source failed" >&2 + exit 5 + } + + SRC_COUNTS="$(counts_of source "$source_dsn")" || exit $? + TGT_COUNTS="$(counts_of target "$target_dsn")" || exit $? + if [ "$SRC_COUNTS" != "$TGT_COUNTS" ]; then + write_receipt "FAIL — replay parity: source $SRC_COUNTS, target $TGT_COUNTS" + echo "kaneo-cutover: FAIL — replay parity: source $SRC_COUNTS, target $TGT_COUNTS (re-export and replay before reopening the local writer)" >&2 + exit 4 + fi + + write_receipt "ROLLBACK OK — fenced, delta replayed, source and target both $SRC_COUNTS" + echo "kaneo-cutover: ROLLBACK OK — $FENCE_OUT; source and target both $SRC_COUNTS" + if [ -z "$RECEIPT" ]; then + printf '\n%s\n' "$RECEIPT_BODY" + fi + exit 0 +fi + # --- promote: fence first, fail closed, before anything touches the stores --- if ! FENCE_OUT="$("$FENCE_CHECK" ${FENCE_ARGS[@]+"${FENCE_ARGS[@]}"} 2>&1)" || [ "${FENCE_OUT%%:*}" != "FENCED" ]; then FENCE_LINE="UNFENCED — ${FENCE_OUT:-fence-check produced no output}" diff --git a/pm-stack/scripts/kaneo-cutover.test.sh b/pm-stack/scripts/kaneo-cutover.test.sh index 341dc0d835..c672404314 100755 --- a/pm-stack/scripts/kaneo-cutover.test.sh +++ b/pm-stack/scripts/kaneo-cutover.test.sh @@ -51,20 +51,38 @@ cat "$f" STUB cat >"$TMP/bin/pg_dump" <<'STUB' #!/usr/bin/env bash -printf 'pg_dump\n' >>"$CUTOVER_FIXTURE_DIR/log" -out="" +dsn=""; out="" while [ "$#" -gt 0 ]; do case "$1" in -f) out="$2"; shift 2 ;; + -d) dsn="$2"; shift 2 ;; *) shift ;; esac done [ -n "$out" ] || { echo "stub pg_dump: no -f" >&2; exit 1; } +case "$dsn" in + *source*) role="source" ;; + *target*) role="target" ;; + *) echo "stub pg_dump: unknown dsn" >&2; exit 1 ;; +esac +printf 'pg_dump:%s\n' "$role" >>"$CUTOVER_FIXTURE_DIR/log" printf 'dump\n' >"$out" STUB cat >"$TMP/bin/pg_restore" <<'STUB' #!/usr/bin/env bash -printf 'pg_restore\n' >>"$CUTOVER_FIXTURE_DIR/log" +dsn="" +while [ "$#" -gt 0 ]; do + case "$1" in + -d) dsn="$2"; shift 2 ;; + *) shift ;; + esac +done +case "$dsn" in + *source*) role="source" ;; + *target*) role="target" ;; + *) echo "stub pg_restore: unknown dsn" >&2; exit 1 ;; +esac +printf 'pg_restore:%s\n' "$role" >>"$CUTOVER_FIXTURE_DIR/log" STUB chmod +x "$TMP/bin/psql" "$TMP/bin/pg_dump" "$TMP/bin/pg_restore" @@ -94,10 +112,10 @@ common=(--pg-bin-dir "$TMP/bin" --source-dsn-file "$TMP/source.dsn" --target-dsn set_counts '45 90139' '45 0' reset_log if out="$(run --mode presync --dump-file "$TMP/k.dump" --receipt "$TMP/r1.md" "${common[@]}")"; then - if [ "$(cat "$TMP/fx/log" | tr '\n' ' ')" = "pg_dump pg_restore " ]; then - pass 'presync dumps then restores' + if [ "$(cat "$TMP/fx/log" | tr '\n' ' ')" = "pg_dump:source pg_restore:target " ]; then + pass 'presync dumps the source then restores into the target' else - fail "presync dumps then restores (log: $(cat "$TMP/fx/log" | tr '\n' ' '))" + fail "presync dumps the source then restores into the target (log: $(cat "$TMP/fx/log" | tr '\n' ' '))" fi if grep -q 'PASS (schema)' "$TMP/r1.md" && ! grep -q 'target-db\|source-db' "$TMP/r1.md"; then pass 'presync receipt records schema parity and no DSN' @@ -174,6 +192,62 @@ else fi printf '45 90139\n' >"$TMP/fx/counts-target.txt" +# 8. rollback refuses while a writer is running — and touches nothing. +set_counts '45 90139' '45 90500' +reset_log +if out="$(run --mode rollback --dump-file "$TMP/k8.dump" --receipt "$TMP/r8.md" --units-dir "$TMP/units" --containers-file "$TMP/containers-kaneo-running.txt" "${common[@]}")"; then + fail "rollback refuses while unfenced (exit 0, got: $out)" +else + if [ "$?" -eq 3 ] && log_is_empty && grep -q 'REFUSED' "$TMP/r8.md"; then + pass 'rollback refuses unfenced, writes REFUSED, touches no store' + else + fail "rollback refuses unfenced, writes REFUSED, touches no store (got: $out)" + fi +fi + +# 9. rollback replays target→source and passes on exact parity. +set_counts '45 90500' '45 90500' +reset_log +if out="$(run --mode rollback --dump-file "$TMP/k9.dump" --receipt "$TMP/r9.md" --units-dir "$TMP/units" --containers-file "$TMP/containers-clean.txt" "${common[@]}")"; then + if [ "$(cat "$TMP/fx/log" | tr '\n' ' ')" = "pg_dump:target pg_restore:source " ]; then + pass 'rollback dumps the target then replays into the source' + else + fail "rollback dumps the target then replays into the source (log: $(cat "$TMP/fx/log" | tr '\n' ' '))" + fi + case "$out" in + *'ROLLBACK OK'*FENCED*) pass 'rollback passes when fenced and parity holds' ;; + *) fail "rollback passes when fenced and parity holds (got: $out)" ;; + esac + if grep -q 'ROLLBACK OK' "$TMP/r9.md" && grep -q '45 90500' "$TMP/r9.md" && ! grep -q 'target-db\|source-db' "$TMP/r9.md"; then + pass 'rollback receipt records the fence, the counts, and no DSN' + else + fail 'rollback receipt records the fence, the counts, and no DSN' + fi +else + fail "rollback passes when fenced and parity holds (exit $?, got: $out)" +fi + +# 10. rollback fails when the replayed source still disagrees with the target. +set_counts '45 90400' '45 90500' +reset_log +if out="$(run --mode rollback --dump-file "$TMP/k10.dump" --receipt "$TMP/r10.md" --units-dir "$TMP/units" --containers-file "$TMP/containers-clean.txt" "${common[@]}")"; then + fail "rollback fails on replay mismatch (exit 0, got: $out)" +else + if [ "$?" -eq 4 ] && grep -q 'FAIL — replay parity' "$TMP/r10.md"; then + pass 'rollback fails on replay mismatch with exit 4' + else + fail "rollback fails on replay mismatch with exit 4 (got: $out)" + fi +fi + +# 11. rollback rejects --unreachable: the writer being fenced is local. +reset_log +if run --mode rollback --unreachable --dump-file "$TMP/k11.dump" "${common[@]}" >/dev/null 2>&1; then + fail 'rollback --unreachable exits 2' +else + [ "$?" -eq 2 ] && log_is_empty && pass 'rollback --unreachable exits 2 with no subprocess' || fail 'rollback --unreachable exits 2 with no subprocess' +fi + # 7. Contract (#3352): usage errors before any subprocess (exit 2). reset_log if run --bogus >/dev/null 2>&1; then fail 'unknown flag exits 2'; else @@ -187,6 +261,11 @@ if run --mode presync --source-dsn-file "$TMP/source.dsn" --target-dsn-file "$TM else [ "$?" -eq 2 ] && pass 'missing --dump-file for presync exits 2' || fail 'missing --dump-file for presync exits 2' fi +if run --mode rollback --source-dsn-file "$TMP/source.dsn" --target-dsn-file "$TMP/target.dsn" >/dev/null 2>&1; then + fail 'missing --dump-file for rollback exits 2' +else + [ "$?" -eq 2 ] && pass 'missing --dump-file for rollback exits 2' || fail 'missing --dump-file for rollback exits 2' +fi if run --mode promote --source-dsn-file "$TMP/nope.dsn" --target-dsn-file "$TMP/target.dsn" >/dev/null 2>&1; then fail 'unreadable source DSN exits 2' else