From 926537ef3a50a4c22bf4189156f8375239abf160 Mon Sep 17 00:00:00 2001 From: dingo Date: Fri, 16 Oct 2026 10:47:09 -0400 Subject: [PATCH] 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 --- .../region/dev-session-route/listener.yaml | 2 +- tools/ops/retire-region-lives.sql | 87 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tools/ops/retire-region-lives.sql diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 6f40503..31e40d5 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261016T141956-52530:1792171196"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261016T144709-39441:1792172829"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/tools/ops/retire-region-lives.sql b/tools/ops/retire-region-lives.sql new file mode 100644 index 0000000..41d19e7 --- /dev/null +++ b/tools/ops/retire-region-lives.sql @@ -0,0 +1,87 @@ +-- 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