Complete M7.9b upgrade barrier and durability qualification
Schema v8 binds authority to builds (ADR-0068): acquisition names the host build and only retained, unretired builds may hold a lease; no commit may name a retired build; upgrade_barrier retires a build and fences every holder in one transaction, while build-less pre-v8 hosts keep working until the first barrier. Tests cover a writer surviving a failed and then a real online migration, old-writer fencing on every path, whole-history recovery by the new build, and the M7 qualification fixture (19.6 s SIGKILL-to-ready under the production lease, no lost acknowledgements). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
-- M7.9b upgrade barrier (ADR-0068). A build can be retired from publication while
|
||||
-- remaining retained for recovery of the history it wrote: no new commit (published
|
||||
-- link, provisioned genesis or clone genesis) may name a retired build. The owner-only
|
||||
-- barrier retires a build and fences every current lease holder in the database in
|
||||
-- one transaction, so a paused or still-running old writer can never commit again
|
||||
-- and every region is resumed by a host that recovers from the committed head.
|
||||
ALTER TABLE evolution.retained_builds ADD COLUMN retired_at timestamptz;
|
||||
|
||||
CREATE FUNCTION evolution.refuse_retired_build() RETURNS trigger LANGUAGE plpgsql
|
||||
SET search_path = pg_catalog, pg_temp AS $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM evolution.retained_builds b
|
||||
WHERE b.build_identity = NEW.build_identity AND b.retired_at IS NOT NULL) THEN
|
||||
RAISE EXCEPTION 'build retired from publication' USING ERRCODE = 'EVL06';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
CREATE TRIGGER region_commits_current_build BEFORE INSERT ON evolution.region_commits
|
||||
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_retired_build();
|
||||
|
||||
-- Authority now names the build that will use it. The grant itself is the v2
|
||||
-- acquisition body, internal and not executable by the runtime role.
|
||||
ALTER FUNCTION evolution.acquire_region_lease(evolution.u64, evolution.u64, integer)
|
||||
RENAME TO grant_region_lease;
|
||||
REVOKE ALL ON FUNCTION evolution.grant_region_lease(evolution.u64, evolution.u64, integer)
|
||||
FROM PUBLIC, :"runtime_role";
|
||||
ALTER FUNCTION evolution.grant_region_lease(evolution.u64, evolution.u64, integer)
|
||||
SECURITY INVOKER;
|
||||
|
||||
-- Build-aware acquisition (M7.9b hosts): only a retained, unretired build may hold
|
||||
-- authority, so a retired build can neither publish nor sit on an idle lease.
|
||||
CREATE FUNCTION evolution.acquire_region_lease(p_world evolution.u64, p_region evolution.u64,
|
||||
p_duration_ms integer, p_build bytea)
|
||||
RETURNS TABLE (lease_holder uuid, lease_epoch evolution.u64, region_frame evolution.u64,
|
||||
granted_ms integer)
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
BEGIN
|
||||
IF p_build IS NULL OR octet_length(p_build) <> 32 THEN
|
||||
RAISE EXCEPTION 'invalid lease request' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT 1 FROM evolution.retained_builds b
|
||||
WHERE b.build_identity = p_build AND b.retired_at IS NULL) THEN
|
||||
RAISE EXCEPTION 'build not current' USING ERRCODE = 'EVL06';
|
||||
END IF;
|
||||
RETURN QUERY SELECT * FROM evolution.grant_region_lease(p_world, p_region, p_duration_ms);
|
||||
END $$;
|
||||
|
||||
-- Pre-v8 hosts name no build. They keep working until the first barrier, after
|
||||
-- which none of them can take authority again.
|
||||
CREATE FUNCTION evolution.acquire_region_lease(p_world evolution.u64, p_region evolution.u64,
|
||||
p_duration_ms integer)
|
||||
RETURNS TABLE (lease_holder uuid, lease_epoch evolution.u64, region_frame evolution.u64,
|
||||
granted_ms integer)
|
||||
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.retired_at IS NOT NULL) THEN
|
||||
RAISE EXCEPTION 'build-less acquisition ended by an upgrade barrier' USING ERRCODE = 'EVL06';
|
||||
END IF;
|
||||
RETURN QUERY SELECT * FROM evolution.grant_region_lease(p_world, p_region, p_duration_ms);
|
||||
END $$;
|
||||
|
||||
-- Returns the number of leases fenced. Retirement is permanent through this
|
||||
-- interface; at least one current build must remain, or nothing could publish.
|
||||
CREATE FUNCTION evolution.upgrade_barrier(p_retire bytea)
|
||||
RETURNS integer LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE
|
||||
fenced integer;
|
||||
BEGIN
|
||||
IF p_retire IS NULL OR octet_length(p_retire) <> 32 THEN
|
||||
RAISE EXCEPTION 'invalid build identity' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
PERFORM 1 FROM evolution.retained_builds b WHERE b.build_identity = p_retire FOR UPDATE;
|
||||
IF NOT FOUND THEN
|
||||
RAISE EXCEPTION 'build not retained' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT 1 FROM evolution.retained_builds b
|
||||
WHERE b.build_identity <> p_retire AND b.retired_at IS NULL) THEN
|
||||
RAISE EXCEPTION 'no current build would remain' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
UPDATE evolution.retained_builds b SET retired_at = coalesce(b.retired_at, clock_timestamp())
|
||||
WHERE b.build_identity = p_retire;
|
||||
-- Holders keep their epochs; the next acquisition takes a fresh one.
|
||||
UPDATE evolution.region_leases l SET holder = NULL, expires_at = NULL
|
||||
WHERE l.holder IS NOT NULL;
|
||||
GET DIAGNOSTICS fenced = ROW_COUNT;
|
||||
RETURN fenced;
|
||||
END $$;
|
||||
|
||||
REVOKE ALL ON FUNCTION evolution.refuse_retired_build(), evolution.upgrade_barrier(bytea),
|
||||
evolution.acquire_region_lease(evolution.u64, evolution.u64, integer, bytea),
|
||||
evolution.acquire_region_lease(evolution.u64, evolution.u64, integer)
|
||||
FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION
|
||||
evolution.acquire_region_lease(evolution.u64, evolution.u64, integer, bytea),
|
||||
evolution.acquire_region_lease(evolution.u64, evolution.u64, integer)
|
||||
TO :"runtime_role";
|
||||
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (8, :'migration_sha_8');
|
||||
+17
-3
@@ -31,24 +31,34 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
:'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2'
|
||||
|| ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4'
|
||||
|| ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6'
|
||||
|| ',7:' || :'migration_sha_7' AS at_v7 \gset
|
||||
\if :at_v7
|
||||
|| ',7:' || :'migration_sha_7' AS at_v7,
|
||||
:'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2'
|
||||
|| ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4'
|
||||
|| ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6'
|
||||
|| ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' AS at_v8 \gset
|
||||
\if :at_v8
|
||||
\elif :at_v7
|
||||
\ir 0008_upgrade_barrier.sql
|
||||
\elif :at_v6
|
||||
\ir 0007_region_clones.sql
|
||||
\ir 0008_upgrade_barrier.sql
|
||||
\elif :at_v5
|
||||
\ir 0006_commit_lookup.sql
|
||||
\ir 0007_region_clones.sql
|
||||
\ir 0008_upgrade_barrier.sql
|
||||
\elif :at_v4
|
||||
\ir pre_v5_guard.sql
|
||||
\ir 0005_batch_decisions.sql
|
||||
\ir 0006_commit_lookup.sql
|
||||
\ir 0007_region_clones.sql
|
||||
\ir 0008_upgrade_barrier.sql
|
||||
\elif :at_v3
|
||||
\ir 0004_lineage_profile.sql
|
||||
\ir pre_v5_guard.sql
|
||||
\ir 0005_batch_decisions.sql
|
||||
\ir 0006_commit_lookup.sql
|
||||
\ir 0007_region_clones.sql
|
||||
\ir 0008_upgrade_barrier.sql
|
||||
\elif :at_v2
|
||||
\ir 0003_region_checkpoints.sql
|
||||
\ir 0004_lineage_profile.sql
|
||||
@@ -56,6 +66,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
\ir 0005_batch_decisions.sql
|
||||
\ir 0006_commit_lookup.sql
|
||||
\ir 0007_region_clones.sql
|
||||
\ir 0008_upgrade_barrier.sql
|
||||
\elif :at_v1
|
||||
\ir 0002_region_fencing.sql
|
||||
\ir 0003_region_checkpoints.sql
|
||||
@@ -64,6 +75,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
\ir 0005_batch_decisions.sql
|
||||
\ir 0006_commit_lookup.sql
|
||||
\ir 0007_region_clones.sql
|
||||
\ir 0008_upgrade_barrier.sql
|
||||
\else
|
||||
\echo 'Unknown or changed migration history'
|
||||
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
|
||||
@@ -77,6 +89,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
\ir 0005_batch_decisions.sql
|
||||
\ir 0006_commit_lookup.sql
|
||||
\ir 0007_region_clones.sql
|
||||
\ir 0008_upgrade_barrier.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
|
||||
@@ -98,6 +111,7 @@ BEGIN
|
||||
SELECT m.member FROM pg_catalog.pg_auth_members m JOIN closure c ON m.roleid = c.oid
|
||||
), fenced(oid) AS (VALUES
|
||||
('evolution.acquire_region_lease(evolution.u64,evolution.u64,integer)'::regprocedure::oid),
|
||||
('evolution.acquire_region_lease(evolution.u64,evolution.u64,integer,bytea)'::regprocedure::oid),
|
||||
('evolution.renew_region_lease(evolution.u64,evolution.u64,uuid,evolution.u64,integer)'::regprocedure::oid),
|
||||
('evolution.release_region_lease(evolution.u64,evolution.u64,uuid,evolution.u64)'::regprocedure::oid),
|
||||
('evolution.load_region_head(evolution.u64,evolution.u64)'::regprocedure::oid),
|
||||
@@ -131,7 +145,7 @@ BEGIN
|
||||
OR EXISTS (SELECT 1 FROM pg_catalog.pg_namespace n WHERE n.nspname = 'public'
|
||||
AND pg_catalog.has_schema_privilege(c.oid, n.oid, 'CREATE'))
|
||||
UNION ALL
|
||||
-- Only the eleven guarded functions are executable; provisioning/internal are not.
|
||||
-- Only the twelve guarded functions are executable; provisioning/internal are not.
|
||||
SELECT c.oid, 'EXECUTE outside the fenced function set' FROM closure c
|
||||
CROSS JOIN pg_catalog.pg_proc p
|
||||
WHERE p.pronamespace = 'evolution'::regnamespace
|
||||
|
||||
Reference in New Issue
Block a user