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
+369
View File
@@ -746,6 +746,373 @@ void boundary_upgrades(Fixture& f) {
require(f.raw->format_version() == cellular_lives_migration_format,
"reverse transition downgraded accounting owner");
}
void migration_publication(Fixture& f, const RegionKey destination_key,
FilesystemObjectStore& destination_objects,
LeaseGrant& destination_lease, CommittedCheckpoint& destination_head) {
const std::array<Sha256Digest, 1> builds{f.build};
const auto wildlife = std::ranges::find_if(
f.raw->lives(), [](const auto& life) { return life.account == LifeAccount{}; });
require(wildlife != f.raw->lives().end(), "paired publication needs real wildlife");
const auto genome = wildlife->identity.genome;
const auto make_host = [&](RegionKey key) {
auto objects = take(
FilesystemObjectStore::open(environment("PGHOST"), key, max_cellular_population_bytes),
"paired host objects");
CellularHostConfig config{key, 1, {}, f.build, {f.build}};
config.lease.commit_timeout = 300ms;
return take(
CellularRegionHost::create(
config,
[] { return std::expected<std::unique_ptr<Postgres>, PgFailure>{connection()}; },
std::move(objects)),
"paired host");
};
for (unsigned representation = 0; representation < 2; ++representation) {
if (representation == 0) {
f.head = take(materialize_population_profile(*f.db, *f.objects, f.lease, f.head, f.id(),
f.build, 5s),
"paired raw source");
destination_head =
take(materialize_population_profile(*f.db, destination_objects, destination_lease,
destination_head, f.id(), f.build, 5s),
"paired raw destination");
} else {
f.head = take(migrate_cellular_to_population(*f.db, *f.objects, f.lease, f.head, f.id(),
f.build, {true, 1200, 1800}, 5s),
"paired population source");
destination_head = take(migrate_cellular_to_population(
*f.db, destination_objects, destination_lease,
destination_head, f.id(), f.build, {true, 1200, 1800}, 5s),
"paired population destination");
}
for (unsigned direction = 0; direction < 2; ++direction) {
auto& sh = direction == 0 ? f.head : destination_head;
auto& dh = direction == 0 ? destination_head : f.head;
auto& so = direction == 0 ? *f.objects : destination_objects;
auto& dst = direction == 0 ? destination_objects : *f.objects;
auto& sl = direction == 0 ? f.lease : destination_lease;
auto& dl = direction == 0 ? destination_lease : f.lease;
const auto sk = direction == 0 ? f.key : destination_key;
const auto dk = direction == 0 ? destination_key : f.key;
take(release_region_lease(*f.db, sk, sl, 5s), "release paired source to host");
take(release_region_lease(*f.db, dk, dl, 5s), "release paired destination to host");
auto source_host = make_host(sk), destination_host = make_host(dk);
require(source_host->step().ready && destination_host->step().ready,
"paired hosts before preparation");
sl = take(source_host->authority(), "current source authority before pause");
dl = take(destination_host->authority(), "current destination authority before pause");
const auto source_before = sh, destination_before = dh;
const auto s = take(decode_manifest(sh.manifest), "paired predecessor source");
const auto d = take(decode_manifest(dh.manifest), "paired predecessor destination");
const auto sb =
take(so.get({s.payload_sha256, s.payload_bytes}), "paired source bytes");
const auto dbb =
take(dst.get({d.payload_sha256, d.payload_bytes}), "paired destination bytes");
const CellularMigrationRequest request{
f.id(),
{sk.region, sh.head.commit, f.id(), sh.head.digest},
{dk.region, dh.head.commit, f.id(), dh.head.digest},
genome};
take(source_host->prepare_migration_freeze(dh, dst, request),
"owning paired source freeze");
const auto frozen = take(take(load_cellular_migration_freeze(*f.db, so, sk, sl, 5s),
"paired source evidence"),
"missing paired freeze");
// Preparation alone cannot authorize either result.
require(!publish_cellular_migration(*f.db, so, dst, sl, dl, sh, dh, frozen.trace,
f.build, 5s),
"unstaged migration published");
take(destination_host->stage_migration(sh, so, frozen.trace), "owning paired stage");
std::vector<std::uint8_t> expected_source, expected_destination;
if (representation == 0) {
auto a = take(restore_cellular_payload(sb, s), "paired raw source oracle");
auto b = take(restore_cellular_payload(dbb, d), "paired raw destination oracle");
auto pair = take(replay_migration_trace(*a, *b, request, frozen.trace),
"paired raw oracle");
expected_source = pair.source->snapshot();
expected_destination = pair.destination->snapshot();
} else {
auto a = take(restore_population_payload(sb, s), "paired population source oracle");
auto b = take(restore_population_payload(dbb, d),
"paired population destination oracle");
auto pair = take(replay_migration_trace(*a, *b, request, frozen.trace),
"paired population oracle");
expected_source = pair.source->snapshot();
expected_destination = pair.destination->snapshot();
}
// Fail specifically on the SECOND result insert, after the first and receipt.
take(f.owner->query(
"CREATE FUNCTION evolution.test_refuse_second_migration() RETURNS trigger "
"LANGUAGE plpgsql AS $$ BEGIN IF NEW.commit_id='" +
hexadecimal(request.destination.commit, false) +
"'::uuid THEN RAISE EXCEPTION 'injected second insert' USING "
"ERRCODE='EVL06'; END IF; RETURN NEW; END $$",
{}, 5s),
"second insert fault function");
take(f.owner->query(
"CREATE TRIGGER test_refuse_second_migration BEFORE INSERT ON "
"evolution.region_commits "
"FOR EACH ROW EXECUTE FUNCTION evolution.test_refuse_second_migration()",
{}, 5s),
"second insert fault trigger");
const auto rejected = publish_cellular_migration(*f.db, so, dst, sl, dl, sh, dh,
frozen.trace, f.build, 5s);
take(f.owner->query(
"DROP TRIGGER test_refuse_second_migration ON evolution.region_commits", {},
5s),
"remove second insert fault");
take(f.owner->query("DROP FUNCTION evolution.test_refuse_second_migration()", {}, 5s),
"remove fault function");
require(
!rejected && rejected.error().store.kind == LeaseStoreError::unsupported &&
take(load_region_checkpoint(*f.db, sk, 5s), "rollback source head").head ==
source_before.head &&
take(load_region_checkpoint(*f.db, dk, 5s), "rollback destination head").head ==
destination_before.head &&
take(load_cellular_migration_freeze(*f.db, so, sk, sl, 5s),
"rollback source pause")
.has_value() &&
take(load_cellular_migration_stage(*f.db, dst, dk, dl, 5s),
"rollback destination pause")
.has_value(),
"failed second publication left half a migration or released a pause");
const auto prepared = rejected.error().prepared;
take(f.db->query("BEGIN", {}, 5s), "false paired biology transaction");
auto false_pair = prepared;
auto false_source =
take(decode_manifest(false_pair.source_manifest), "false source manifest");
false_source.payload_sha256 = s.payload_sha256;
false_source.payload_bytes = s.payload_bytes;
false_pair.source_manifest =
take(encode_manifest(false_source), "false source canonical bytes");
take(retry_cellular_migration_commit(*f.db, sl, dl, false_pair, 5s),
"metadata-only false pair");
const auto false_recovery = verify_cellular_migration_checkpoint(
*f.db, so, sl, take(load_region_checkpoint(*f.db, sk, 5s), "false paired head"), sb,
5s);
take(f.db->query("ROLLBACK", {}, 5s), "rollback false paired biology fixture");
require(!false_recovery && false_recovery.error().kind == RecoveryError::history,
"paired recovery accepted a valid old object as the migrated biology");
const std::array<std::string, 1> generic{hexadecimal(prepared.source_manifest)};
require(!f.db->query("SELECT * FROM evolution.publish_region_checkpoint($1::bytea)",
generic, 5s),
"generic publisher admitted a migration half");
const auto child = ::fork();
require(child >= 0, "pre-transaction paired fork");
if (child == 0) {
try {
auto db = connection();
PublishHooks hooks{[] { ::kill(::getpid(), SIGKILL); }};
if (!publish_cellular_migration(*db, so, dst, sl, dl, sh, dh, frozen.trace,
f.build, 5s, hooks))
::_exit(52);
} catch (...) {
::_exit(51);
}
::_exit(52);
}
int status{};
require(
::waitpid(child, &status, 0) == child && WIFSIGNALED(status) &&
WTERMSIG(status) == SIGKILL &&
take(load_region_checkpoint(*f.db, sk, 5s), "killed pair source").head ==
source_before.head &&
take(load_region_checkpoint(*f.db, dk, 5s), "killed pair destination").head ==
destination_before.head,
"pre-transaction paired death changed a head");
if (direction == 0) {
auto locker = connection(true), timed = connection();
take(locker->query("BEGIN", {}, 5s), "pair ambiguity transaction");
take(locker->query("SELECT 1 FROM evolution.region_leases WHERE world_id=30201 AND "
"region_id=" +
std::to_string(sk.region) + " FOR UPDATE",
{}, 5s),
"hold paired reply");
const auto unknown =
retry_cellular_migration_commit(*timed, sl, dl, prepared, 300ms);
require(!unknown && unknown.error().kind == PublicationError::unknown &&
unknown.error().prepared.source_manifest == prepared.source_manifest &&
unknown.error().prepared.destination_manifest ==
prepared.destination_manifest,
"ambiguous pair lost its exact manifests");
take(locker->query("ROLLBACK", {}, 5s), "release paired ambiguity");
} else {
const auto committed_child = ::fork();
require(committed_child >= 0, "post-commit paired fork");
if (committed_child == 0) {
try {
auto db = connection();
if (!retry_cellular_migration_commit(*db, sl, dl, prepared, 5s))
::_exit(53);
::kill(::getpid(), SIGKILL);
} catch (...) {
::_exit(54);
}
::_exit(55);
}
require(::waitpid(committed_child, &status, 0) == committed_child &&
WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL,
"paired publisher failed before post-commit kill");
}
const auto committed =
take(retry_cellular_migration_commit(*f.db, sl, dl, prepared, 5s),
"resolve exact paired commit");
sh = committed.source;
dh = committed.destination;
require(!abort_cellular_migration_freeze(*f.db, sk, sl, request.request, 5s),
"committed migration was aborted");
auto changed = prepared;
auto altered =
take(decode_manifest(changed.destination_manifest), "conflicting committed pair");
altered.payload_sha256.back() ^= 1;
changed.destination_manifest = take(encode_manifest(altered), "conflicting pair bytes");
const auto conflict = retry_cellular_migration_commit(*f.db, sl, dl, changed, 5s);
auto stale = sl;
--stale.epoch;
require(!conflict && conflict.error().store.kind == LeaseStoreError::commit_conflict &&
!retry_cellular_migration_commit(*f.db, stale, dl, prepared, 5s),
"changed pair or stale source authority was accepted");
for (unsigned attempt = 0;
attempt < 200 && (!source_host->step().ready || !destination_host->step().ready);
++attempt)
std::this_thread::sleep_for(25ms);
require(source_host->status().ready && destination_host->status().ready &&
take(source_host->committed(), "adopted source").head == sh.head &&
take(destination_host->committed(), "adopted destination").head == dh.head,
"pointer release resumed a stale owner instead of recovering the pair");
const auto check_bytes = [&](const CommittedCheckpoint& h, RegionKey key,
const FilesystemObjectStore& objects,
const LeaseGrant& lease) {
require(take(load_region_checkpoint(*f.db, key, 5s), "current paired recovery head")
.head == h.head,
"paired recovery selected a different head");
if (representation == 0)
return take(recover_cellular_checkpoint(*f.db, objects, key, lease, builds, 5s),
"recover raw paired result")
.state->snapshot();
return take(recover_population_checkpoint(*f.db, objects, key, lease, builds, 5s),
"recover cohort paired result")
.state->snapshot();
};
require(check_bytes(sh, sk, so, sl) == expected_source &&
check_bytes(dh, dk, dst, dl) == expected_destination,
"committed biology/member/material differs from the exact pair");
const auto capsule_path = so.directory() / hexadecimal(d.payload_sha256, false);
const auto backup = capsule_path.string() + ".paired-test";
std::filesystem::rename(capsule_path, backup);
const auto missing =
verify_cellular_migration_checkpoint(*f.db, so, sl, sh, expected_source, 5s);
std::filesystem::rename(backup, capsule_path);
require(!missing && missing.error().kind == RecoveryError::tail &&
!verify_cellular_migration_checkpoint(*f.db, so, sl, sh, sb, 5s),
"pair recovery ignored missing counterpart evidence or false local biology");
source_host->drain();
destination_host->drain();
source_host = make_host(sk);
destination_host = make_host(dk);
require(source_host->step().ready && destination_host->step().ready,
"replacement paired hosts failed complete recovery");
sl = take(source_host->authority(), "replacement source fence");
dl = take(destination_host->authority(), "replacement destination fence");
require(take(retry_cellular_migration_commit(*f.db, sl, dl, prepared, 5s),
"new epoch exact paired retry")
.source.head == sh.head,
"fresh authority changed a committed retry");
source_host->drain();
destination_host->drain();
sl = take(acquire_region_lease(*f.db, sk, f.build, 60000ms, 5s),
"return paired source authority");
dl = take(acquire_region_lease(*f.db, dk, f.build, 60000ms, 5s),
"return paired destination authority");
}
}
// Race the actual two-head transaction against abort on independent connections.
const auto prepare_pair = [&](bool reverse) {
auto& sh = reverse ? destination_head : f.head;
auto& dh = reverse ? f.head : destination_head;
auto& so = reverse ? destination_objects : *f.objects;
auto& dst = reverse ? *f.objects : destination_objects;
auto& sl = reverse ? destination_lease : f.lease;
auto& dl = reverse ? f.lease : destination_lease;
const auto sk = reverse ? destination_key : f.key;
const auto dk = reverse ? f.key : destination_key;
CellularMigrationRequest request{f.id(),
{sk.region, sh.head.commit, f.id(), sh.head.digest},
{dk.region, dh.head.commit, f.id(), dh.head.digest},
genome};
const auto freeze =
take(prepare_cellular_migration_freeze(*f.db, so, dst, sl, sh, dh, request, 5s),
"commit/abort source preparation");
take(stage_cellular_migration(*f.db, so, dst, dl, sh, dh, freeze.trace, 5s),
"commit/abort destination stage");
return freeze.trace;
};
const auto aborted_trace = prepare_pair(false);
take(abort_cellular_migration_freeze(*f.db, f.key, f.lease, aborted_trace.request.request, 5s),
"abort wins before paired commit");
const auto after_abort = publish_cellular_migration(
*f.db, *f.objects, destination_objects, f.lease, destination_lease, f.head,
destination_head, aborted_trace, f.build, 5s);
require(!after_abort && after_abort.error().store.kind == LeaseStoreError::commit_conflict,
"paired publication reopened an aborted workflow");
for (unsigned race = 0; race < 4; ++race) {
const auto trace = prepare_pair(false);
auto committing = connection(), aborting = connection();
std::barrier start(2);
std::optional<std::expected<CellularMigrationCommitted, CellularMigrationCommitFailure>>
published;
std::optional<std::expected<CellularMigrationFreeze, LeaseStoreFailure>> aborted;
std::thread commit_thread([&] {
start.arrive_and_wait();
published = publish_cellular_migration(*committing, *f.objects, destination_objects,
f.lease, destination_lease, f.head,
destination_head, trace, f.build, 5s);
});
std::thread abort_thread([&] {
start.arrive_and_wait();
aborted = abort_cellular_migration_freeze(*aborting, f.key, f.lease,
trace.request.request, 5s);
});
commit_thread.join();
abort_thread.join();
require(published.has_value() && aborted.has_value() &&
(published->has_value() != aborted->has_value()),
"commit/abort race had two outcomes or no definitive outcome");
if (published->has_value()) {
require(aborted->error().kind == LeaseStoreError::commit_conflict,
"losing abort was not refused");
f.head = (*published)->source;
destination_head = (*published)->destination;
const auto returning = prepare_pair(true);
const auto returned =
take(publish_cellular_migration(*f.db, destination_objects, *f.objects,
destination_lease, f.lease, destination_head,
f.head, returning, f.build, 5s),
"return race winner's stock");
destination_head = returned.source;
f.head = returned.destination;
} else {
require(published->error().store.kind == LeaseStoreError::commit_conflict,
"losing paired publication was not refused");
}
require(
take(load_region_checkpoint(*f.db, f.key, 5s), "race source head").head ==
f.head.head &&
take(load_region_checkpoint(*f.db, destination_key, 5s), "race destination head")
.head == destination_head.head &&
!take(load_cellular_migration_freeze(*f.db, *f.objects, f.key, f.lease, 5s),
"race source settled")
.has_value() &&
!take(load_cellular_migration_stage(*f.db, destination_objects, destination_key,
destination_lease, 5s),
"race destination settled")
.has_value(),
"commit/abort race left half a head or a pending pause");
}
recover(f);
f.raw = restore(f.population->state());
}
void migration_freezes(Fixture& f) {
const RegionKey destination_key{f.key.world, 2};
auto destination_objects =
@@ -1426,6 +1793,8 @@ void migration_freezes(Fixture& f) {
destination_host->drain();
destination_lease = take(acquire_region_lease(*f.db, destination_key, f.build, 60000ms, 5s),
"return staged destination fixture authority");
migration_publication(f, destination_key, *destination_objects, destination_lease,
destination_head);
f.head = take(
materialize_population_profile(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s),
"resume frozen population as raw owner");
+2 -1
View File
@@ -214,6 +214,7 @@ void upgrade_across_migration_and_barrier() {
" -v migration_sha_46=$(s 0046_boundary_accounting_upgrade.sql)"
" -v migration_sha_47=$(s 0047_cellular_migration_freeze.sql)"
" -v migration_sha_48=$(s 0048_cellular_migration_staging.sql)"
" -v migration_sha_49=$(s 0049_cellular_migration_publication.sql)"
" -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");
@@ -231,7 +232,7 @@ void upgrade_across_migration_and_barrier() {
"tools/migrate-postgres.sh\" > /dev/null") == 0);
CHECK(history(*owner_db) ==
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,"
"25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48");
"25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49");
CHECK(scalar(*owner_db, "SELECT (c.condeferrable AND c.condeferred AND "
"c.confrelid = 'evolution.region_commits'::regclass)::text "
"FROM pg_catalog.pg_constraint c WHERE c.conrelid = "