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:
dingo
2026-09-29 07:31:04 -04:00
co-authored by Claude Opus 5.5
parent 703f67834f
commit f59cca2c3b
24 changed files with 893 additions and 52 deletions
+403
View File
@@ -0,0 +1,403 @@
// M7.9b schema migration, upgrade barrier and durability qualification (ADR-0068)
// against real PostgreSQL, real evo-server processes and the checked-in migrations
// (EVO_SOURCE_ROOT). Run by tools/ci/postgres-check.sh; prints measured latencies.
#include "evo/persistence/region_clone.hpp"
#include "evo/persistence/sha256.hpp"
#include "fixtures/server_process.hpp"
#include <algorithm>
#include <source_location>
#include <thread>
void run_region_migration_tests();
namespace {
using namespace evo::test::process;
using namespace evo::persistence;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("region migration test line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
constexpr std::size_t budget = 8000000;
const std::string b1_text = [] {
std::string s;
for (int i = 0; i < 32; ++i)
s += "b1";
return s;
}();
const std::string b2_text = [] {
std::string s;
for (int i = 0; i < 32; ++i)
s += "b2";
return s;
}();
Sha256Digest digest_of(std::uint8_t b) {
Sha256Digest d{};
d.fill(b);
return d;
}
const Sha256Digest b1 = digest_of(0xB1), b2 = digest_of(0xB2);
template <std::size_t N> std::array<std::uint8_t, N> filled(std::uint8_t value) {
std::array<std::uint8_t, N> out{};
out.fill(value);
return out;
}
std::unique_ptr<Postgres> connect_to(const std::string& database, const std::string& user) {
auto made = Postgres::connect({env("PGHOST"), {}, env("PGPORT"), database, user, {}, {}}, 5s);
CHECK(made);
return std::move(*made);
}
std::string sqlstate(const PgFailure& f) {
return f.sqlstate.data();
}
std::string scalar(Postgres& db, const std::string& sql) {
auto rows = db.query(sql, {}, 5s);
CHECK(rows && rows->size() == 1 && (*rows)[0].size() == 1);
return (*rows)[0][0].value_or("");
}
int shell(const std::string& command) {
Child c{{"/bin/bash", "-c", command}};
return c.wait_exit(120s);
}
std::vector<std::string> with(std::vector<std::string> args, const std::string& flag,
const std::string& value) {
const auto at = std::ranges::find(args, flag);
CHECK(at != args.end() && at + 1 != args.end());
*(at + 1) = value;
return args;
}
std::unique_ptr<FilesystemObjectStore> store(const ServerRegion& r) {
auto made = FilesystemObjectStore::open(r.root, r.key, max_lineage_payload_bytes);
CHECK(made);
return std::move(*made);
}
std::optional<Uuid> parse_uuid(const std::string& text) {
std::string compact;
for (const char c : text)
if (c != '-')
compact.push_back(c);
if (compact.size() != 32)
return std::nullopt;
Uuid out{};
for (std::size_t i = 0; i < 16; ++i)
out[i] = static_cast<std::uint8_t>(std::stoul(compact.substr(2 * i, 2), nullptr, 16));
return out;
}
// A pre-v8 writer: the build-less acquisition every earlier host used.
std::expected<LeaseGrant, std::string> legacy_acquire(Postgres& db, RegionKey key) {
const std::array<std::string, 2> p{std::to_string(key.world), std::to_string(key.region)};
auto rows = db.query("SELECT * FROM evolution.acquire_region_lease($1::evolution.u64, "
"$2::evolution.u64, 20000)",
p, 5s);
if (!rows)
return std::unexpected{sqlstate(rows.error())};
CHECK(rows->size() == 1 && (*rows)[0].size() == 4);
const auto& r = (*rows)[0];
const auto holder = parse_uuid(r[0].value_or(""));
CHECK(holder.has_value());
return LeaseGrant{holder.value_or(Uuid{}), std::stoull(r[1].value_or("0")),
std::stoull(r[2].value_or("0")), 20000ms};
}
const char* const sha_function =
"s(){ sha256sum \"$EVO_SOURCE_ROOT/migrations/$1\" | cut -d' ' -f1; }; ";
std::string history(Postgres& owner) {
return scalar(owner, "SELECT string_agg(version::text, ',' ORDER BY version) FROM "
"evolution.schema_migrations");
}
// An existing v7 database is upgraded while an older writer holds a lease. A
// migration that fails leaves v7 exactly and the writer unaffected; the real one
// commits under it. The barrier then retires the old build: the writer is fenced
// and can never regain authority, while the new build recovers the whole history.
void upgrade_across_migration_and_barrier() {
const std::string database = "evo_online";
const std::string runtime_user = env("EVO_PG_RUNTIME_USER");
CHECK(shell(std::string(sha_function) + "cd \"$EVO_SOURCE_ROOT/migrations\" && createdb " +
database + " && PGDATABASE=" + database +
" psql -X -q -v ON_ERROR_STOP=1 -1 -v migration_sha=$(s 0001_region_bootstrap.sql)"
" -v migration_sha_2=$(s 0002_region_fencing.sql)"
" -v migration_sha_3=$(s 0003_region_checkpoints.sql)"
" -v migration_sha_4=$(s 0004_lineage_profile.sql)"
" -v migration_sha_5=$(s 0005_batch_decisions.sql)"
" -v migration_sha_6=$(s 0006_commit_lookup.sql)"
" -v migration_sha_7=$(s 0007_region_clones.sql)"
" -v runtime_role=evo_runtime_test -f 0001_region_bootstrap.sql"
" -f 0002_region_fencing.sql -f 0003_region_checkpoints.sql"
" -f 0004_lineage_profile.sql -f 0005_batch_decisions.sql"
" -f 0006_commit_lookup.sql -f 0007_region_clones.sql > /dev/null") == 0);
auto owner_db = connect_to(database, env("PGUSER"));
CHECK(history(*owner_db) == "1,2,3,4,5,6,7");
CHECK(owner_db->query("INSERT INTO evolution.retained_builds(build_identity) VALUES "
"(decode(repeat('b1', 32), 'hex')), (decode(repeat('b2', 32), 'hex'))",
{}, 5s));
const ServerRegion r{{7, 91}, fs::path(env("PGHOST")) / "online-91"};
fs::remove_all(r.root);
fs::create_directory(r.root);
fs::permissions(r.root, fs::perms::owner_all, fs::perm_options::replace);
auto objects = store(r);
auto genesis =
provision_lineage_region(*owner_db, *objects, r.key, evo::test::region_identity.frame,
*evo::test::lineage_region(), filled<16>(0xDA), b1, budget, 5s);
CHECK(genesis);
// The old writer: build-less lease, publishing with build b1.
auto runtime = connect_to(database, runtime_user);
auto lease = legacy_acquire(*runtime, r.key);
CHECK(lease);
auto state = evo::test::lineage_region();
RegionBatch<evo::processes::LineageRegion> batch{*state};
CommittedCheckpoint head = *genesis;
std::uint8_t commit_seed = 0xDB;
const auto publish_next = [&](std::uint64_t ticks) {
for (std::uint64_t i = 0; i < ticks && !batch.sealed(); ++i)
CHECK(batch.advance(evo::test::region_input(batch.end_tick() + 1, true)));
batch.seal();
return publish_lineage_batch(*runtime, *objects, *lease, head, batch,
filled<16>(commit_seed++), b1, budget, 5s);
};
const auto confirm = [&](const std::expected<PublishedBatch, PublicationFailure>& done) {
CHECK(done && batch.confirm(done->committed.head.end_tick));
head = done->committed;
};
confirm(publish_next(64)); // Ticks 1..20: the consumer's death seals it.
CHECK(head.head.end_tick == 20);
// A v8 migration that fails at its last statement changes nothing.
CHECK(shell(std::string(sha_function) + "cd \"$EVO_SOURCE_ROOT\" && PGDATABASE=" + database +
" psql -X -q -v ON_ERROR_STOP=1 -v migration_sha=$(s 0001_region_bootstrap.sql)"
" -v migration_sha_2=$(s 0002_region_fencing.sql)"
" -v migration_sha_3=$(s 0003_region_checkpoints.sql)"
" -v migration_sha_4=$(s 0004_lineage_profile.sql)"
" -v migration_sha_5=$(s 0005_batch_decisions.sql)"
" -v migration_sha_6=$(s 0006_commit_lookup.sql)"
" -v migration_sha_7=$(s 0007_region_clones.sql) -v migration_sha_8=invalid"
" -v runtime_role=evo_runtime_test -f migrations/apply.sql"
" > /dev/null 2>&1") != 0);
CHECK(history(*owner_db) == "1,2,3,4,5,6,7");
CHECK(scalar(*owner_db, "SELECT count(*) FROM information_schema.columns WHERE "
"table_schema = 'evolution' AND column_name = 'retired_at'") == "0");
CHECK(scalar(*owner_db,
"SELECT (to_regprocedure('evolution.acquire_region_lease("
"evolution.u64, evolution.u64, integer, bytea)') IS NULL)::text") == "true");
CHECK(renew_region_lease(*runtime, r.key, *lease, 20000ms, 5s));
confirm(publish_next(20)); // 21..40, still v7.
// The real migration commits while the writer holds its lease; it keeps going.
CHECK(shell("PGDATABASE=" + database +
" EVO_PG_RUNTIME_ROLE=evo_runtime_test bash \"$EVO_SOURCE_ROOT/"
"tools/migrate-postgres.sh\" > /dev/null") == 0);
CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8");
CHECK(renew_region_lease(*runtime, r.key, *lease, 20000ms, 5s));
confirm(publish_next(20)); // 41..60, now v8.
CHECK(head.head.end_tick == 60);
// Barrier guards: the last current build cannot be retired; runtime cannot call it.
const std::string retire_b1 = "SELECT evolution.upgrade_barrier(decode(repeat('b1', 32), "
"'hex'))::text";
auto denied = runtime->query(retire_b1, {}, 5s);
CHECK(!denied && sqlstate(denied.error()) == "42501");
// The barrier: b1 retired and every holder fenced, in one transaction.
CHECK(scalar(*owner_db, retire_b1) == "1");
auto last_b2 = owner_db->query(
"SELECT evolution.upgrade_barrier(decode(repeat('b2', 32), 'hex'))", {}, 5s);
CHECK(!last_b2 && sqlstate(last_b2.error()) == "22023");
// The old writer is fenced and cannot regain authority by any path.
auto after = publish_next(10);
CHECK(!after && after.error().kind == PublicationError::rejected &&
after.error().store.kind == LeaseStoreError::fenced);
auto renewed = renew_region_lease(*runtime, r.key, *lease, 20000ms, 5s);
CHECK(!renewed && renewed.error().kind == LeaseStoreError::fenced);
auto legacy = legacy_acquire(*runtime, r.key);
CHECK(!legacy && legacy.error() == "EVL06");
auto retired = acquire_region_lease(*runtime, r.key, b1, 20000ms, 5s);
CHECK(!retired && retired.error().kind == LeaseStoreError::unsupported);
// Authority for b2 still cannot publish a link naming the retired build.
auto current = acquire_region_lease(*runtime, r.key, b2, 20000ms, 5s);
CHECK(current);
lease = *current;
auto stale_build = publish_lineage_batch(*runtime, *objects, *lease, head, batch,
filled<16>(commit_seed++), b1, budget, 5s);
CHECK(!stale_build && stale_build.error().kind == PublicationError::rejected &&
stale_build.error().store.kind == LeaseStoreError::unsupported);
CHECK(release_region_lease(*runtime, r.key, *lease, 5s));
const auto before_new = head.head;
// The new build resumes from the committed head, and b1 history stays readable.
auto args = with(with(server_args(r), "--pg-database", database), "--build", b2_text);
args.insert(args.end(), {"--retained", b1_text});
{
Child server{args};
CHECK(server.wait_for([](const std::string& l) { return l == "ready 1"; }, 20s));
CHECK(server.printed("phase running epoch=3 committed=60 "));
server.send(input_lines(61, 90, true));
CHECK(server.wait_line("ack " + request_hex(90) + " 90 ", 30s));
server.signal(SIGTERM);
CHECK(server.wait_exit(20s) == 0);
}
auto final_head = load_region_checkpoint(*owner_db, r.key, 5s);
CHECK(final_head && final_head->head.end_tick == 90 &&
final_head->head.sequence > before_new.sequence);
const auto final_manifest = decode_manifest(final_head->manifest);
CHECK(final_manifest && final_manifest->build == b2);
const std::array<Sha256Digest, 2> both{b1, b2};
const std::array<Sha256Digest, 1> only_new{b2};
auto audited = audit_region_history(*runtime, *objects, r.key, both, budget, 100, 5s);
CHECK(audited && audited->commits == final_head->head.sequence + 1 && audited->outcomes == 1);
auto without_old = audit_region_history(*runtime, *objects, r.key, only_new, budget, 100, 5s);
CHECK(!without_old && without_old.error().kind == AuditError::unsupported &&
without_old.error().sequence == 0);
// A host of the retired build never serves, not even once the region is free.
{
Child old_host{with(with(server_args(r), "--pg-database", database), "--build", b1_text)};
CHECK(old_host.wait_exit(20s) == 4);
CHECK(old_host.printed("phase quarantined") && !old_host.printed("ready 1"));
}
CHECK(load_region_checkpoint(*owner_db, r.key, 5s)->head == final_head->head);
}
std::uint64_t percentile(std::vector<std::uint64_t> v, unsigned p) {
CHECK(!v.empty());
std::ranges::sort(v);
return v[std::min(v.size() - 1, (v.size() * p) / 100)];
}
std::uint64_t ms_between(Clock::time_point a, Clock::time_point b) {
return static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(b - a).count());
}
// Sends inputs first..last paced at the 50 ms tick; returns send times.
std::vector<Clock::time_point> paced(Child& server, std::uint64_t first, std::uint64_t last) {
std::vector<Clock::time_point> sent;
const auto start = Clock::now();
for (auto tick = first; tick <= last; ++tick) {
const auto due = start + std::chrono::milliseconds{50 * (tick - first)};
if (const auto now = Clock::now(); due > now)
server.pump(std::chrono::duration_cast<std::chrono::milliseconds>(due - now));
server.send(input_line(tick, true));
sent.push_back(Clock::now());
}
return sent;
}
std::optional<Clock::time_point> ack_time(const Child& server, std::uint64_t tick) {
const auto prefix = "ack " + request_hex(tick) + " " + std::to_string(tick) + " ";
for (std::size_t i = 0; i < server.lines().size(); ++i)
if (starts(server.lines()[i], prefix))
return server.times()[i];
return std::nullopt;
}
Clock::time_point ready_time(const Child& server) {
for (std::size_t i = 0; i < server.lines().size(); ++i)
if (server.lines()[i] == "ready 1")
return server.times()[i];
CHECK(false);
return {};
}
// The M7 exit fixture: production lease policy (20 s lease, 5 s renewal) and
// flush interval (1 s), one lineage region on a local socket and filesystem.
// Measures acknowledgement latency, worker-kill-to-ready recovery against the
// proposed 60 s target, and the cost of verification, audit and clone.
void durability_qualification() {
const auto r = provision(93, 0xDC);
const auto args =
with(with(server_args(r), "--lease-profile", "default"), "--flush-ms", "1000");
std::vector<std::uint64_t> latency, kill_to_ready;
std::uint64_t death_latency = 0;
std::uint64_t next = 1;
std::uint64_t epoch = 1;
for (int round = 0; round < 3; ++round) {
Child server{args};
CHECK(server.wait_for([](const std::string& l) { return l == "ready 1"; }, 60s));
CHECK(server.printed("phase running epoch=" + std::to_string(epoch) + " "));
const auto first = next;
const auto last = next + 99;
const auto sent = paced(server, first, last);
CHECK(server.wait_line("ack " + request_hex(last) + " " + std::to_string(last) + " ", 30s));
for (auto tick = first; tick <= last; ++tick) {
const auto at = ack_time(server, tick);
CHECK(at.has_value());
const auto ms = ms_between(sent[tick - first], at.value_or(Clock::time_point{}));
if (tick == 20)
death_latency = ms;
else
latency.push_back(ms);
}
// Mid-batch force kill: more inputs are in flight, none acknowledged yet.
server.send(input_lines(last + 1, last + 10, true));
server.pump(200ms);
const auto acked = max_acked(server);
server.signal(SIGKILL);
const auto killed = Clock::now();
CHECK(server.wait_exit(20s) == 128 + SIGKILL);
const auto committed = head_of(r.key);
CHECK(acked <= committed.tick && acked >= last); // No acknowledged input lost.
next = committed.tick + 1;
++epoch;
if (round == 2)
break;
Child successor{args};
CHECK(successor.wait_for([](const std::string& l) { return l == "ready 1"; }, 60s));
kill_to_ready.push_back(ms_between(killed, ready_time(successor)));
CHECK(successor.printed("phase running epoch=" + std::to_string(epoch) +
" committed=" + std::to_string(committed.tick) + " "));
successor.signal(SIGTERM);
CHECK(successor.wait_exit(20s) == 0);
++epoch;
}
CHECK(decisions_of(r.key) == 1);
for (const auto ms : kill_to_ready)
CHECK(ms < 60000); // The declared target for this fixture.
std::printf("qualification ack_latency_ms flush=1000 n=%zu p50=%llu p99=%llu max=%llu "
"death=%llu\n",
latency.size(), static_cast<unsigned long long>(percentile(latency, 50)),
static_cast<unsigned long long>(percentile(latency, 99)),
static_cast<unsigned long long>(percentile(latency, 100)),
static_cast<unsigned long long>(death_latency));
std::printf("qualification kill_to_ready_ms lease=20000 renew=5000 runs=%zu first=%llu "
"second=%llu target=60000\n",
kill_to_ready.size(), static_cast<unsigned long long>(kill_to_ready[0]),
static_cast<unsigned long long>(kill_to_ready[1]));
// Verification cost on the final head (no lease needed to read).
auto reader = connect_to(env("PGDATABASE"), env("EVO_PG_RUNTIME_USER"));
auto objects = store(r);
const std::array<Sha256Digest, 1> retained{b1};
std::vector<std::uint64_t> verify_us;
std::uint64_t payload_bytes = 0;
for (int i = 0; i < 10; ++i) {
const auto start = Clock::now();
auto recovered = recover_lineage_checkpoint(*reader, *objects, r.key, retained, budget, 5s);
CHECK(recovered);
payload_bytes = recovered->manifest.payload_bytes;
verify_us.push_back(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - start).count()));
}
const auto audit_start = Clock::now();
auto audited = audit_region_history(*reader, *objects, r.key, retained, budget, 1000, 5s);
const auto audit_ms = ms_between(audit_start, Clock::now());
CHECK(audited);
auto clone_owner = connect_to(env("EVO_PG_CLONE_DATABASE"), env("PGUSER"));
const ServerRegion target{{8, 93}, fs::path(env("PGHOST")) / "qualify-clone-93"};
fs::remove_all(target.root);
fs::create_directory(target.root);
fs::permissions(target.root, fs::perms::owner_all, fs::perm_options::replace);
const auto clone_start = Clock::now();
auto cloned = clone_region(*reader, *objects, r.key, *clone_owner, *store(target), target.key,
filled<16>(0xDD), retained, budget, 5s);
const auto clone_ms = ms_between(clone_start, Clock::now());
CHECK(cloned);
std::printf("qualification verify_head_us payload_bytes=%llu n=10 p50=%llu max=%llu\n",
static_cast<unsigned long long>(payload_bytes),
static_cast<unsigned long long>(percentile(verify_us, 50)),
static_cast<unsigned long long>(percentile(verify_us, 100)));
std::printf("qualification audit_ms commits=%llu ticks=%llu ms=%llu clone_ms=%llu\n",
static_cast<unsigned long long>(audited->commits),
static_cast<unsigned long long>(audited->end_tick),
static_cast<unsigned long long>(audit_ms),
static_cast<unsigned long long>(clone_ms));
}
} // namespace
void run_region_migration_tests() {
durability_qualification();
upgrade_across_migration_and_barrier();
}