M12.4: atomically publish and recover biological migration pairs
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m21s
Foundation / checks (network) (push) Successful in 20m51s
Foundation / checks (client) (push) Successful in 22m2s
Foundation / checks (tsan) (push) Successful in 22m59s
Foundation / checks (postgres) (push) Successful in 53m40s
Foundation / checks (checks) (push) Successful in 1h7m19s
Foundation / postgres-tools (push) Successful in 29s
Foundation / server-image (push) Successful in 38s
Foundation / image (push) Successful in 4m31s

This commit is contained in:
dingo
2026-10-18 10:02:55 -04:00
parent 0515702964
commit 0f071f3f8b
21 changed files with 1116 additions and 31 deletions
@@ -0,0 +1,163 @@
-- ADR-0149: one shared receipt and two heads in one fenced transaction.
CREATE TABLE evolution.cellular_migration_commits (
request_id uuid PRIMARY KEY REFERENCES evolution.cellular_migration_stages,
source_result uuid UNIQUE NOT NULL REFERENCES evolution.region_commits(commit_id) DEFERRABLE INITIALLY DEFERRED,
destination_result uuid UNIQUE NOT NULL REFERENCES evolution.region_commits(commit_id) DEFERRABLE INITIALLY DEFERRED,
CHECK(source_result<>destination_result)
);
REVOKE ALL ON evolution.cellular_migration_commits FROM PUBLIC;
CREATE TRIGGER cellular_migration_commits_immutable BEFORE UPDATE OR DELETE ON evolution.cellular_migration_commits
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
CREATE TRIGGER cellular_migration_commits_no_truncate BEFORE TRUNCATE ON evolution.cellular_migration_commits
FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite();
CREATE FUNCTION evolution.cellular_migration_commit_record(id uuid)
RETURNS TABLE(trace bytea,source_predecessor bytea,destination_predecessor bytea,source_result bytea,destination_result bytea,ghost_sha256 bytea,ghost_bytes evolution.u64)
LANGUAGE sql STABLE SET search_path=pg_catalog,pg_temp AS $$
SELECT p.trace,s.manifest,d.manifest,sr.manifest,dr.manifest,g.ghost_sha256,g.ghost_bytes
FROM evolution.cellular_migration_commits x JOIN evolution.cellular_migration_preparations p USING(request_id)
JOIN evolution.cellular_migration_stages g USING(request_id)
JOIN evolution.region_commits s ON s.commit_id=p.source_commit
JOIN evolution.region_commits d ON d.commit_id=p.destination_commit
JOIN evolution.region_commits sr ON sr.commit_id=x.source_result
JOIN evolution.region_commits dr ON dr.commit_id=x.destination_result WHERE x.request_id=id
$$;
REVOKE ALL ON FUNCTION evolution.cellular_migration_commit_record(uuid) FROM PUBLIC;
CREATE FUNCTION evolution.publish_cellular_migration(id uuid,sm bytea,dm bytea,sh uuid,se evolution.u64,dh uuid,de evolution.u64)
RETURNS TABLE(trace bytea,source_predecessor bytea,destination_predecessor bytea,source_result bytea,destination_result bytea,ghost_sha256 bytea,ghost_bytes evolution.u64)
LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog,pg_temp AS $$
DECLARE p evolution.cellular_migration_preparations%ROWTYPE; g evolution.cellular_migration_stages%ROWTYPE;
old evolution.cellular_migration_commits%ROWTYPE;
s evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(sm);
d evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(dm);
predecessor evolution.checkpoint_manifest; h evolution.region_heads%ROWTYPE;
m bytea; c evolution.checkpoint_manifest; result_digest bytea;
bundle constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000';
zero32 constant bytea := decode(repeat('00',32),'hex');
BEGIN
SELECT * INTO p FROM evolution.cellular_migration_preparations x WHERE x.request_id=id;
IF NOT FOUND THEN RAISE EXCEPTION 'unknown migration preparation' USING ERRCODE='EVL04'; END IF;
IF s.world_id<>p.world_id OR d.world_id<>p.world_id OR s.region_id<>p.source_region OR d.region_id<>p.destination_region
OR s.commit_id<>encode(substring(p.trace FROM 121 FOR 16),'hex')::uuid
OR d.commit_id<>encode(substring(p.trace FROM 193 FOR 16),'hex')::uuid THEN
RAISE EXCEPTION 'foreign migration results' USING ERRCODE='22023';
END IF;
PERFORM 1 FROM evolution.region_leases x WHERE x.world_id=p.world_id AND x.region_id IN (p.source_region,p.destination_region)
ORDER BY x.region_id FOR UPDATE;
PERFORM evolution.check_region_lease(p.world_id,p.source_region,sh,se);
PERFORM evolution.check_region_lease(p.world_id,p.destination_region,dh,de);
SELECT * INTO old FROM evolution.cellular_migration_commits x WHERE x.request_id=id;
IF FOUND THEN
IF NOT EXISTS(SELECT 1 FROM evolution.region_commits x WHERE x.commit_id=old.source_result AND x.manifest=sm)
OR NOT EXISTS(SELECT 1 FROM evolution.region_commits x WHERE x.commit_id=old.destination_result AND x.manifest=dm) THEN
RAISE EXCEPTION 'migration commit request reused' USING ERRCODE='EVL07';
END IF;
RETURN QUERY SELECT * FROM evolution.cellular_migration_commit_record(id);
RETURN;
END IF;
SELECT * INTO g FROM evolution.cellular_migration_stages x WHERE x.request_id=id;
IF NOT FOUND OR EXISTS(SELECT 1 FROM evolution.cellular_migration_aborts x WHERE x.request_id=id)
OR NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_pending x WHERE x.world_id=p.world_id AND x.region_id=p.source_region AND x.request_id=id)
OR NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_destination_pending x WHERE x.world_id=p.world_id AND x.region_id=p.destination_region AND x.request_id=id) THEN
RAISE EXCEPTION 'migration not staged or was aborted' USING ERRCODE='EVL18';
END IF;
IF s.holder<>sh OR s.epoch<>se OR d.holder<>dh OR d.epoch<>de
OR d.payload_sha256<>g.ghost_sha256 OR d.payload_bytes<>g.ghost_bytes THEN
RAISE EXCEPTION 'migration writer or staged ghost differs' USING ERRCODE='EVL07';
END IF;
FOREACH m IN ARRAY ARRAY[sm,dm] LOOP
c := evolution.parse_checkpoint_manifest(m);
SELECT * INTO h FROM evolution.region_heads x WHERE x.world_id=c.world_id AND x.region_id=c.region_id FOR UPDATE;
IF NOT FOUND OR h.commit_sequence=18446744073709551615 OR c.commit_sequence<>h.commit_sequence+1
OR c.predecessor_commit<>h.commit_id OR c.predecessor_digest<>h.digest OR c.begin_tick<>h.end_tick
OR c.end_tick<>h.end_tick OR c.end_tick<>evolution.manifest_uint(p.trace,32,8)
OR h.commit_id<>(CASE WHEN c.region_id=p.source_region THEN p.source_commit ELSE p.destination_commit END) THEN
RAISE EXCEPTION 'migration endpoint head moved' USING ERRCODE='EVL03';
END IF;
predecessor := evolution.parse_checkpoint_manifest((SELECT x.manifest FROM evolution.region_commits x WHERE x.commit_id=h.commit_id));
IF c.profile NOT IN (4,5) OR c.profile<>predecessor.profile OR c.profile<>s.profile OR c.versions<>bundle
OR c.versions<>predecessor.versions OR c.frame_id<>predecessor.frame_id
OR c.tick_microseconds<>predecessor.tick_microseconds OR c.frame_id<>evolution.manifest_uint(p.trace,24,8)
OR c.frame_id<>(SELECT x.frame_id FROM evolution.region_leases x WHERE x.world_id=c.world_id AND x.region_id=c.region_id)
OR evolution.manifest_uint(m,228,4)<>0
OR evolution.manifest_uint(m,232,4)<>(CASE c.profile WHEN 4 THEN 4 ELSE 5 END)
OR evolution.manifest_uint(m,236,4)<>1 OR c.trace_bytes<>0 OR c.trace_sha256<>zero32
OR c.decision_count<>0 OR c.decisions_sha256<>zero32
OR NOT EXISTS(SELECT 1 FROM evolution.retained_builds x WHERE x.build_identity=c.build_identity)
OR EXISTS(SELECT 1 FROM evolution.population_interval_pending x WHERE x.world_id=c.world_id AND x.region_id=c.region_id) THEN
RAISE EXCEPTION 'unsupported paired migration result' USING ERRCODE='EVL06';
END IF;
END LOOP;
INSERT INTO evolution.cellular_migration_commits VALUES(id,s.commit_id,d.commit_id);
DELETE FROM evolution.cellular_migration_pending x WHERE x.request_id=id;
DELETE FROM evolution.cellular_migration_destination_pending x WHERE x.request_id=id;
FOREACH m IN ARRAY ARRAY[sm,dm] LOOP
c := evolution.parse_checkpoint_manifest(m); result_digest := sha256(m);
INSERT INTO evolution.region_commits(world_id,region_id,commit_sequence,commit_id,commit_digest,manifest,
begin_tick,end_tick,published_epoch,tick_microseconds,versions,build_identity,payload_bytes,payload_sha256)
VALUES(c.world_id,c.region_id,c.commit_sequence,c.commit_id,result_digest,m,c.begin_tick,c.end_tick,c.epoch,
c.tick_microseconds,c.versions,c.build_identity,c.payload_bytes,c.payload_sha256);
UPDATE evolution.region_heads x SET commit_sequence=c.commit_sequence,commit_id=c.commit_id,digest=result_digest,
end_tick=c.end_tick,published_epoch=c.epoch WHERE x.world_id=c.world_id AND x.region_id=c.region_id;
END LOOP;
RETURN QUERY SELECT * FROM evolution.cellular_migration_commit_record(id);
END $$;
REVOKE ALL ON FUNCTION evolution.publish_cellular_migration(uuid,bytea,bytea,uuid,evolution.u64,uuid,evolution.u64) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.publish_cellular_migration(uuid,bytea,bytea,uuid,evolution.u64,uuid,evolution.u64) TO :"runtime_role";
CREATE FUNCTION evolution.load_cellular_migration_commit(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64,target uuid)
RETURNS TABLE(trace bytea,source_predecessor bytea,destination_predecessor bytea,source_result bytea,destination_result bytea,ghost_sha256 bytea,ghost_bytes evolution.u64)
LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog,pg_temp AS $$
DECLARE id uuid;
BEGIN
PERFORM evolution.check_region_lease(w,r,holder,epoch);
SELECT x.request_id INTO id FROM evolution.cellular_migration_commits x
JOIN evolution.cellular_migration_preparations p USING(request_id)
WHERE p.world_id=w AND ((p.source_region=r AND x.source_result=target) OR (p.destination_region=r AND x.destination_result=target));
IF FOUND THEN RETURN QUERY SELECT * FROM evolution.cellular_migration_commit_record(id); END IF;
END $$;
REVOKE ALL ON FUNCTION evolution.load_cellular_migration_commit(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.load_cellular_migration_commit(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role";
ALTER FUNCTION evolution.abort_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) RENAME TO abort_cellular_migration_freeze_v48;
REVOKE ALL ON FUNCTION evolution.abort_cellular_migration_freeze_v48(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM :"runtime_role",PUBLIC;
CREATE FUNCTION evolution.abort_cellular_migration_freeze(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64,id uuid)
RETURNS TABLE(trace bytea,source_manifest bytea,destination_manifest bytea,aborted boolean)
LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog,pg_temp AS $$
BEGIN
PERFORM evolution.check_region_lease(w,r,holder,epoch);
IF EXISTS(SELECT 1 FROM evolution.cellular_migration_commits x JOIN evolution.cellular_migration_preparations p USING(request_id)
WHERE x.request_id=id AND p.world_id=w AND p.source_region=r) THEN
RAISE EXCEPTION 'committed migration cannot abort' USING ERRCODE='EVL07';
END IF;
RETURN QUERY SELECT * FROM evolution.abort_cellular_migration_freeze_v48(w,r,holder,epoch,id);
END $$;
REVOKE ALL ON FUNCTION evolution.abort_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.abort_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role";
CREATE OR REPLACE FUNCTION evolution.refuse_unpublished_cellular() RETURNS trigger
LANGUAGE plpgsql SET search_path=pg_catalog,pg_temp AS $$
BEGIN
IF evolution.manifest_uint(NEW.manifest,12,4)=4 AND NEW.commit_sequence>0
AND NOT EXISTS(SELECT 1 FROM evolution.cellular_publications x WHERE x.commit_id=NEW.commit_id)
AND NOT EXISTS(SELECT 1 FROM evolution.cellular_profile_migrations x WHERE x.target_commit=NEW.commit_id)
AND NOT EXISTS(SELECT 1 FROM evolution.population_profile_transitions x WHERE x.target_commit=NEW.commit_id AND x.target_profile=4)
AND NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_commits x WHERE x.source_result=NEW.commit_id OR x.destination_result=NEW.commit_id) THEN
RAISE EXCEPTION 'cellular commit requires fenced publisher' USING ERRCODE='EVL06';
END IF;
RETURN NEW;
END $$;
CREATE OR REPLACE FUNCTION evolution.refuse_unpublished_population() RETURNS trigger
LANGUAGE plpgsql SET search_path=pg_catalog,pg_temp AS $$
BEGIN
IF evolution.manifest_uint(NEW.manifest,12,4)=5 AND (NEW.commit_sequence=0 OR
(NOT EXISTS(SELECT 1 FROM evolution.population_publications x WHERE x.commit_id=NEW.commit_id)
AND NOT EXISTS(SELECT 1 FROM evolution.population_profile_transitions x WHERE x.target_commit=NEW.commit_id AND x.target_profile=5)
AND NOT EXISTS(SELECT 1 FROM evolution.cellular_profile_migrations x WHERE x.target_commit=NEW.commit_id)
AND NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_commits x WHERE x.source_result=NEW.commit_id OR x.destination_result=NEW.commit_id))) THEN
RAISE EXCEPTION 'population commit requires fenced publisher' USING ERRCODE='EVL06';
END IF;
RETURN NEW;
END $$;
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(49,:'migration_sha_49');
+14 -3
View File
@@ -124,6 +124,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
SELECT :'history_v45' || ',46:' || :'migration_sha_46' AS history_v46 \gset
SELECT :'history_v46' || ',47:' || :'migration_sha_47' AS history_v47 \gset
SELECT :'history_v47' || ',48:' || :'migration_sha_48' AS history_v48 \gset
SELECT :'history_v48' || ',49:' || :'migration_sha_49' AS history_v49 \gset
SELECT :'history' = :'history_v22' AS at_v22,
:'history' = :'history_v23' AS at_v23,
:'history' = :'history_v24' AS at_v24,
@@ -150,8 +151,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
:'history' = :'history_v45' AS at_v45,
:'history' = :'history_v46' AS at_v46,
:'history' = :'history_v47' AS at_v47,
:'history' = :'history_v48' AS at_v48 \gset
\if :at_v48
:'history' = :'history_v48' AS at_v48,
:'history' = :'history_v49' AS at_v49 \gset
\if :at_v49
\elif :at_v48
\elif :at_v47
\elif :at_v46
\elif :at_v45
@@ -541,7 +544,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
coalesce(max(version), 0) = 45 AS at_v45,
coalesce(max(version), 0) = 46 AS at_v46,
coalesce(max(version), 0) = 47 AS at_v47,
coalesce(max(version), 0) = 48 AS at_v48
coalesce(max(version), 0) = 48 AS at_v48,
coalesce(max(version), 0) = 49 AS at_v49
FROM evolution.schema_migrations \gset
\if :at_v24
\ir 0025_cellular_v2_bundle.sql
@@ -758,6 +762,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\elif :at_v46
\elif :at_v47
\elif :at_v48
\elif :at_v49
\else
\echo 'Unknown or changed migration history after v43'
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
@@ -782,6 +787,10 @@ SELECT coalesce(max(version), 0) = 47 AS needs_v48 FROM evolution.schema_migrati
\if :needs_v48
\ir 0048_cellular_migration_staging.sql
\endif
SELECT coalesce(max(version), 0) = 48 AS needs_v49 FROM evolution.schema_migrations \gset
\if :needs_v49
\ir 0049_cellular_migration_publication.sql
\endif
-- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes
-- alone are insufficient: predefined roles such as pg_write_all_data, PUBLIC
-- grants or SET-able memberships would bypass the fenced functions. Checks the
@@ -801,6 +810,8 @@ BEGIN
UNION
SELECT m.member FROM pg_catalog.pg_auth_members m JOIN closure c ON m.roleid = c.oid
), fenced(oid) AS (VALUES
('evolution.publish_cellular_migration(uuid,bytea,bytea,uuid,evolution.u64,uuid,evolution.u64)'::regprocedure::oid),
('evolution.load_cellular_migration_commit(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
('evolution.stage_cellular_migration(evolution.u64,evolution.u64,uuid,evolution.u64,bytea,bytea,evolution.u64)'::regprocedure::oid),
('evolution.load_cellular_migration_stage(evolution.u64,evolution.u64,uuid,evolution.u64)'::regprocedure::oid),
('evolution.prepare_cellular_migration_freeze(evolution.u64,evolution.u64,uuid,evolution.u64,bytea)'::regprocedure::oid),