Files
aevolution/tools/ops/retire-region-lives.sql
T
dingoandClaude Opus 5.5 926537ef3a
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m1s
Foundation / postgres-tools (push) Successful in 6s
Foundation / server-image (push) Successful in 5s
Foundation / image (push) Successful in 5s
Add an operator script that retires a region's player lives
DEV runs one region at a time and moves to a new one before test cells die. A life
left in the old region keeps its account's home there: the grant broker routes the
account to a region nobody serves (503 region_unavailable) and any other region
refuses its birth (existing life, one active life per world).

tools/ops/retire-region-lives.sql, run as the database owner, ends each published
life of the region at the region's latest commit (as end_cellular_lives does) and
cancels unpublished reservations. It refuses while a server holds the region's lease,
is a dry run unless confirm=yes, is idempotent, and leaves checkpoints and history
unchanged. Tested on a disposable migrated cluster.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 10:47:09 -04:00

88 lines
3.9 KiB
PL/PgSQL

-- Retire a region's player lives so their accounts can be born again elsewhere.
--
-- DEV operator tool, run as the database owner, for a region that no longer has a
-- server: we run one region at a time and move to a new one before test cells die.
-- An account may hold one active life per world (ADR-0127). A life left in a retired
-- region keeps its account's home there, so the grant broker routes the account to a
-- region nobody serves ("temporarily unavailable") and every other region refuses its
-- birth ("existing life").
--
-- - A published life (a birth binding exists) is ended at the region's latest commit,
-- exactly as the region itself records a death (end_cellular_lives). The ending is
-- final.
-- - A reservation that was never published is cancelled (rejection 3).
--
-- The region's checkpoints, organisms, corpses and history are left unchanged. Never
-- serve the region again afterwards: its checkpoint still holds these cells alive.
--
-- The script refuses while the region's lease is unexpired, which means a server still
-- holds it. It is a dry run unless confirm=yes; either way it prints what it ends.
--
-- psql -X -v world=100 -v region=5 -f retire-region-lives.sql # dry run
-- psql -X -v world=100 -v region=5 -v confirm=yes -f retire-region-lives.sql
\set ON_ERROR_STOP on
\if :{?world}
\else
\echo 'usage: psql -v world=W -v region=R [-v confirm=yes] -f retire-region-lives.sql'
\quit
\endif
\if :{?region}
\else
\echo 'usage: psql -v world=W -v region=R [-v confirm=yes] -f retire-region-lives.sql'
\quit
\endif
\if :{?confirm}
\else
\set confirm no
\endif
BEGIN;
SELECT set_config('retire.world', :'world', true), set_config('retire.region', :'region', true);
DO $$
DECLARE
w numeric := current_setting('retire.world')::numeric;
r numeric := current_setting('retire.region')::numeric;
latest uuid;
ended integer;
cancelled integer;
BEGIN
IF w <= 0 OR r <= 0 THEN
RAISE EXCEPTION 'world and region must be positive';
END IF;
PERFORM 1 FROM evolution.region_leases l WHERE l.world_id = w AND l.region_id = r FOR UPDATE;
IF NOT FOUND THEN
RAISE EXCEPTION 'region % of world % does not exist', r, w;
END IF;
IF EXISTS (SELECT 1 FROM evolution.region_leases l WHERE l.world_id = w AND l.region_id = r
AND l.expires_at > now()) THEN
RAISE EXCEPTION 'region % is still leased by a server; stop it first', r;
END IF;
SELECT c.commit_id INTO latest FROM evolution.region_commits c
WHERE c.world_id = w AND c.region_id = r ORDER BY c.commit_sequence DESC LIMIT 1;
UPDATE evolution.life_reservations x SET ended_commit = latest
WHERE x.world_id = w AND x.region_id = r AND x.rejection = 0 AND x.ended_commit IS NULL
AND latest IS NOT NULL
AND EXISTS (SELECT 1 FROM evolution.primitive_life_bindings b WHERE b.world_id = x.world_id
AND b.account_id = x.account_id AND b.request_id = x.request_id);
GET DIAGNOSTICS ended = ROW_COUNT;
UPDATE evolution.life_reservations x SET rejection = 3
WHERE x.world_id = w AND x.region_id = r AND x.rejection = 0 AND x.ended_commit IS NULL
AND NOT EXISTS (SELECT 1 FROM evolution.primitive_life_bindings b
WHERE b.world_id = x.world_id AND b.account_id = x.account_id
AND b.request_id = x.request_id);
GET DIAGNOSTICS cancelled = ROW_COUNT;
IF EXISTS (SELECT 1 FROM evolution.life_reservations x WHERE x.world_id = w
AND x.region_id = r AND x.rejection = 0 AND x.ended_commit IS NULL) THEN
RAISE EXCEPTION 'region % still has active lives (a published life without a commit)', r;
END IF;
RAISE NOTICE 'world % region %: % lives ended at commit %, % unpublished reservations cancelled',
w, r, ended, coalesce(latest::text, '(none)'), cancelled;
END $$;
\if :confirm
COMMIT;
\echo 'committed'
\else
ROLLBACK;
\echo 'dry run: nothing changed (add -v confirm=yes to apply)'
\endif