#include "evo/persistence/cellular_migration_store.hpp" #include "cellular_migration_observation_detail.hpp" #include "evo/persistence/sha256.hpp" #include "evo/server/lifecycle/cellular_census_set.hpp" #include #include #include #include namespace evo::persistence { namespace { using std::chrono::milliseconds; LeaseStoreFailure failure(const PgFailure& cause) { const std::string_view state(cause.sqlstate.data(), 5); auto kind = LeaseStoreError::store; if (cause.kind == PgError::unknown) kind = LeaseStoreError::unknown; else if (state == "22023" || cause.kind == PgError::bounds || cause.kind == PgError::config) kind = LeaseStoreError::invalid; else if (state == "EVL02") kind = LeaseStoreError::fenced; else if (state == "EVL03") kind = LeaseStoreError::head_mismatch; else if (state == "EVL04") kind = LeaseStoreError::not_provisioned; else if (state == "EVL06") kind = LeaseStoreError::unsupported; else if (state == "EVL07" || state == "EVL18") kind = LeaseStoreError::commit_conflict; return {kind, cause}; } std::string hex(std::span bytes) { constexpr std::string_view digits = "0123456789abcdef"; std::string out; for (const auto byte : bytes) { out += digits[byte >> 4U]; out += digits[byte & 15U]; } return out; } std::string uuid(const Uuid& id) { const auto h = hex(id); return h.substr(0, 8) + '-' + h.substr(8, 4) + '-' + h.substr(12, 4) + '-' + h.substr(16, 4) + '-' + h.substr(20); } std::optional> unhex(std::string_view value, std::size_t cap) { if (value.size() % 2 != 0 || value.size() / 2 > cap) return std::nullopt; std::vector out; out.reserve(value.size() / 2); for (std::size_t at = 0; at < value.size(); at += 2) { unsigned byte{}; const auto [end, error] = std::from_chars(value.data() + at, value.data() + at + 2, byte, 16); if (error != std::errc{} || end != value.data() + at + 2 || byte > 255) return std::nullopt; out.push_back(static_cast(byte)); } return out; } struct Record { std::vector trace, source, destination; bool aborted{}; }; std::optional parse(const PgRows::value_type& row) { if (row.size() != 4 || (row[3].value_or("") != "true" && row[3].value_or("") != "false")) return std::nullopt; auto trace = unhex(row[0].value_or(""), server::max_migration_trace_bytes); auto source = unhex(row[1].value_or(""), checkpoint_manifest_bytes); auto destination = unhex(row[2].value_or(""), checkpoint_manifest_bytes); if (!trace || !source || !destination) return std::nullopt; return Record{std::move(*trace), std::move(*source), std::move(*destination), row[3].value_or("") == "true"}; } bool bound(const CommittedCheckpoint& c, const CheckpointManifest& m) { const auto digest = sha256(c.manifest); return digest && *digest == c.head.digest && m.commit == c.head.commit && m.frame == c.head.frame && m.sequence == c.head.sequence && m.end_tick == c.head.end_tick; } bool cuts(const CheckpointManifest& s, const CheckpointManifest& d, const Sha256Digest& source_digest, const Sha256Digest& destination_digest, const server::CellularMigrationTrace& t) { const auto profile = t.representation == server::CellularMigrationRepresentation::individual ? CheckpointProfile::cellular_lives_v1 : CheckpointProfile::cellular_population_v1; const bool version = (t.format_version == 1 && s.cellular_versions == cellular_v10_versions) || (t.format_version == 2 && s.cellular_versions == cellular_v11_versions); return version && supported(s) && supported(d) && encode_manifest(s) && encode_manifest(d) && d.cellular_versions == s.cellular_versions && s.profile == profile && d.profile == profile && s.sequence > 0 && d.sequence > 0 && s.key == RegionKey{t.world, t.request.source.region} && d.key == RegionKey{t.world, t.request.destination.region} && s.frame == t.frame && d.frame == t.frame && s.end_tick == t.tick && d.end_tick == t.tick && s.commit == t.request.source.predecessor && d.commit == t.request.destination.predecessor && source_digest == t.request.source.predecessor_digest && destination_digest == t.request.destination.predecessor_digest; } std::expected derive(const CheckpointManifest& s, const CheckpointManifest& d, std::span source, std::span destination, const server::CellularMigrationRequest& request) { if (s.profile == CheckpointProfile::cellular_lives_v1) { auto src = restore_cellular_payload(source, s), dst = restore_cellular_payload(destination, d); if (!src || !dst) return std::unexpected(CheckpointError::payload); auto pair = server::prepare_migration_trace(**src, **dst, request); if (!pair) return std::unexpected(CheckpointError::payload); return std::move(pair->trace); } auto src = restore_population_payload(source, s), dst = restore_population_payload(destination, d); if (!src || !dst) return std::unexpected(CheckpointError::payload); auto pair = server::prepare_migration_trace(**src, **dst, request); if (!pair) return std::unexpected(CheckpointError::payload); return std::move(pair->trace); } std::optional checked_record(const Record& record, RegionKey key, const LeaseGrant& lease) { auto trace = server::decode_migration_trace(record.trace); const auto source = decode_manifest(record.source), destination = decode_manifest(record.destination); const auto s = sha256(record.source), d = sha256(record.destination); if (!trace || !source || !destination || !s || !d || key != RegionKey{trace->world, trace->request.source.region} || lease.frame != trace->frame || !cuts(*source, *destination, *s, *d, *trace)) return std::nullopt; return CellularMigrationFreeze{std::move(*trace), record.aborted}; } constexpr auto columns = "encode(trace,'hex'),encode(source_manifest,'hex')," "encode(destination_manifest,'hex'),aborted::text"; CellularMigrationFailure failed(PublicationError kind, LeaseStoreFailure store = {}, ObjectError object = {}, CellularMigrationPrepared prepared = {}) { return {kind, store, object, std::move(prepared)}; } } // namespace std::expected retry_cellular_migration_freeze(Postgres& db, const LeaseGrant& lease, const CellularMigrationPrepared& prepared, milliseconds timeout) { const auto trace = server::decode_migration_trace(prepared.trace); if (!trace || prepared.key != RegionKey{trace->world, trace->request.source.region} || lease.frame != trace->frame) return std::unexpected(failed(PublicationError::candidate)); const std::array args{std::to_string(prepared.key.world), std::to_string(prepared.key.region), uuid(lease.holder), std::to_string(lease.epoch), "\\x" + hex(prepared.trace)}; auto rows = db.query(std::string("SELECT ") + columns + " FROM evolution.prepare_cellular_migration_freeze(" "$1::evolution.u64,$2::evolution.u64,$3::uuid,$4::evolution.u64,$5::bytea)", args, timeout); if (!rows) { const auto store = failure(rows.error()); return std::unexpected(failed(store.kind == LeaseStoreError::unknown ? PublicationError::unknown : PublicationError::rejected, store, {}, prepared)); } const auto record = rows->size() == 1 ? parse(rows->front()) : std::nullopt; const auto result = record ? checked_record(*record, prepared.key, lease) : std::nullopt; if (!result || record->trace != prepared.trace) return std::unexpected(failed(PublicationError::unknown, {}, {}, prepared)); return *result; } std::expected prepare_cellular_migration_freeze(Postgres& db, const FilesystemObjectStore& source_objects, const FilesystemObjectStore& destination_objects, const LeaseGrant& lease, const CommittedCheckpoint& source, const CommittedCheckpoint& destination, const server::CellularMigrationRequest& request, milliseconds timeout, const PublishHooks& hooks) { const auto s = decode_manifest(source.manifest), d = decode_manifest(destination.manifest); if (!s || !d || !bound(source, *s) || !bound(destination, *d)) return std::unexpected(failed(PublicationError::predecessor)); auto src = source_objects.get({s->payload_sha256, s->payload_bytes}); auto dst = destination_objects.get({d->payload_sha256, d->payload_bytes}); if (!src || !dst) return std::unexpected( failed(PublicationError::predecessor, {}, !src ? src.error() : dst.error())); auto trace = derive(*s, *d, *src, *dst, request); if (!trace || !cuts(*s, *d, source.head.digest, destination.head.digest, *trace) || lease.frame != s->frame) return std::unexpected(failed(PublicationError::candidate)); auto encoded = server::encode_migration_trace(*trace); if (!encoded) return std::unexpected(failed(PublicationError::candidate)); auto capsule = source_objects.put(*dst); if (!capsule) return std::unexpected(failed(PublicationError::upload, {}, capsule.error())); auto readback = source_objects.get(*capsule); if (!readback || *readback != *dst || *capsule != VerifiedObject{d->payload_sha256, d->payload_bytes}) return std::unexpected(failed(PublicationError::verify, {}, readback ? ObjectError::corrupt : readback.error())); if (hooks.before_transaction) hooks.before_transaction(); return retry_cellular_migration_freeze(db, lease, {s->key, std::move(*encoded)}, timeout); } std::expected abort_cellular_migration_freeze(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& request, milliseconds timeout) { const std::array args{std::to_string(key.world), std::to_string(key.region), uuid(lease.holder), std::to_string(lease.epoch), uuid(request)}; auto rows = db.query(std::string("SELECT ") + columns + " FROM evolution.abort_cellular_migration_freeze(" "$1::evolution.u64,$2::evolution.u64,$3::uuid,$4::evolution.u64,$5::uuid)", args, timeout); if (!rows) return std::unexpected(failure(rows.error())); const auto record = rows->size() == 1 ? parse(rows->front()) : std::nullopt; auto result = record ? checked_record(*record, key, lease) : std::nullopt; if (!result || !result->aborted || result->trace.request.request != request) return std::unexpected(LeaseStoreFailure{LeaseStoreError::unknown, {}}); return *result; } std::expected, RecoveryFailure> load_cellular_migration_freeze(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, const LeaseGrant& lease, milliseconds timeout) { const auto fail = [](RecoveryError kind) { return std::unexpected(RecoveryFailure{kind, {}}); }; const std::array args{std::to_string(key.world), std::to_string(key.region), uuid(lease.holder), std::to_string(lease.epoch)}; auto rows = db.query(std::string("SELECT ") + columns + " FROM evolution.load_cellular_migration_freeze(" "$1::evolution.u64,$2::evolution.u64,$3::uuid,$4::evolution.u64)", args, timeout); if (!rows) return std::unexpected(RecoveryFailure{RecoveryError::store, failure(rows.error())}); if (rows->empty()) return std::optional{}; const auto record = rows->size() == 1 ? parse(rows->front()) : std::nullopt; auto result = record ? checked_record(*record, key, lease) : std::nullopt; if (!result || result->aborted) return fail(RecoveryError::history); const auto s = decode_manifest(record->source), d = decode_manifest(record->destination); if (!s || !d) return fail(RecoveryError::manifest); auto src = objects.get({s->payload_sha256, s->payload_bytes}); auto dst = objects.get({d->payload_sha256, d->payload_bytes}); if (!src || !dst) return fail(RecoveryError::tail); const auto replay = derive(*s, *d, *src, *dst, result->trace.request); if (!replay || *replay != result->trace) return fail(RecoveryError::history); return result; } namespace { constexpr auto stage_columns = "encode(trace,'hex'),encode(source_manifest,'hex')," "encode(destination_manifest,'hex'),aborted::text," "encode(ghost_sha256,'hex'),ghost_bytes::text"; struct StageRecord { Record cut; VerifiedObject ghost; }; std::optional parse_stage(const PgRows::value_type& row) { if (row.size() != 6) return std::nullopt; auto cut = parse(PgRows::value_type(row.begin(), row.begin() + 4)); const auto digest = unhex(row[4].value_or(""), 32); const auto size = row[5].value_or(""); std::uint64_t bytes{}; const auto [end, error] = std::from_chars(size.data(), size.data() + size.size(), bytes); if (!cut || !digest || digest->size() != 32 || error != std::errc{} || end != size.data() + size.size() || bytes == 0) return std::nullopt; VerifiedObject ghost{{}, bytes}; std::ranges::copy(*digest, ghost.sha256.begin()); return StageRecord{std::move(*cut), ghost}; } std::optional checked_stage(const StageRecord& record, RegionKey key, const LeaseGrant& lease) { const auto trace = server::decode_migration_trace(record.cut.trace); if (!trace || key != RegionKey{trace->world, trace->request.destination.region}) return std::nullopt; const auto freeze = checked_record(record.cut, {trace->world, trace->request.source.region}, lease); const auto destination = decode_manifest(record.cut.destination); if (!freeze || !destination || record.ghost.sha256 == Sha256Digest{} || record.ghost.bytes > max_payload_bytes(destination->profile)) return std::nullopt; return CellularMigrationStage{*freeze, record.ghost}; } struct MigrationPayloads { std::vector source, destination; }; std::optional migration_payloads(const CheckpointManifest& s, const CheckpointManifest& d, std::span source, std::span destination, const server::CellularMigrationTrace& trace) { if (s.profile == CheckpointProfile::cellular_lives_v1) { auto src = restore_cellular_payload(source, s), dst = restore_cellular_payload(destination, d); if (!src || !dst) return std::nullopt; auto pair = server::replay_migration_trace(**src, **dst, trace.request, trace); if (!pair) return std::nullopt; return MigrationPayloads{pair->source->snapshot(), pair->destination->snapshot()}; } auto src = restore_population_payload(source, s), dst = restore_population_payload(destination, d); if (!src || !dst) return std::nullopt; auto pair = server::replay_migration_trace(**src, **dst, trace.request, trace); if (!pair) return std::nullopt; return MigrationPayloads{pair->source->snapshot(), pair->destination->snapshot()}; } std::optional> ghost_candidate(const CheckpointManifest& s, const CheckpointManifest& d, std::span source, std::span destination, const server::CellularMigrationTrace& trace) { auto pair = migration_payloads(s, d, source, destination, trace); if (!pair) return std::nullopt; return std::move(pair->destination); } CellularMigrationStageFailure stage_failed(PublicationError kind, LeaseStoreFailure store = {}, ObjectError object = {}, CellularMigrationStagePrepared prepared = {}) { return {kind, store, object, std::move(prepared)}; } } // namespace std::expected retry_cellular_migration_stage(Postgres& db, const LeaseGrant& lease, const CellularMigrationStagePrepared& prepared, milliseconds timeout) { const auto trace = server::decode_migration_trace(prepared.trace); if (!trace || prepared.key != RegionKey{trace->world, trace->request.destination.region} || lease.frame != trace->frame || prepared.ghost.sha256 == Sha256Digest{} || prepared.ghost.bytes == 0) return std::unexpected(stage_failed(PublicationError::candidate)); const std::array args{std::to_string(prepared.key.world), std::to_string(prepared.key.region), uuid(lease.holder), std::to_string(lease.epoch), "\\x" + hex(prepared.trace), "\\x" + hex(prepared.ghost.sha256), std::to_string(prepared.ghost.bytes)}; auto rows = db.query(std::string("SELECT ") + stage_columns + " FROM evolution.stage_cellular_migration($1::evolution.u64," "$2::evolution.u64,$3::uuid,$4::evolution.u64,$5::bytea,$6::bytea," "$7::evolution.u64)", args, timeout); if (!rows) { const auto store = failure(rows.error()); return std::unexpected(stage_failed(store.kind == LeaseStoreError::unknown ? PublicationError::unknown : PublicationError::rejected, store, {}, prepared)); } const auto record = rows->size() == 1 ? parse_stage(rows->front()) : std::nullopt; const auto result = record ? checked_stage(*record, prepared.key, lease) : std::nullopt; if (!result || record->cut.trace != prepared.trace || result->ghost != prepared.ghost) return std::unexpected(stage_failed(PublicationError::unknown, {}, {}, prepared)); return *result; } std::expected stage_cellular_migration(Postgres& db, const FilesystemObjectStore& source_objects, const FilesystemObjectStore& destination_objects, const LeaseGrant& lease, const CommittedCheckpoint& source, const CommittedCheckpoint& destination, const server::CellularMigrationTrace& trace, milliseconds timeout, const PublishHooks& hooks) { const auto s = decode_manifest(source.manifest), d = decode_manifest(destination.manifest); if (!s || !d || !bound(source, *s) || !bound(destination, *d) || !cuts(*s, *d, source.head.digest, destination.head.digest, trace) || lease.frame != d->frame) return std::unexpected(stage_failed(PublicationError::predecessor)); auto src = source_objects.get({s->payload_sha256, s->payload_bytes}); auto dst = destination_objects.get({d->payload_sha256, d->payload_bytes}); if (!src || !dst) return std::unexpected( stage_failed(PublicationError::predecessor, {}, !src ? src.error() : dst.error())); const auto candidate = ghost_candidate(*s, *d, *src, *dst, trace); const auto encoded = server::encode_migration_trace(trace); if (!candidate || !encoded) return std::unexpected(stage_failed(PublicationError::candidate)); auto capsule = destination_objects.put(*src); if (!capsule) return std::unexpected(stage_failed(PublicationError::upload, {}, capsule.error())); auto capsule_bytes = destination_objects.get(*capsule); if (!capsule_bytes || *capsule_bytes != *src || *capsule != VerifiedObject{s->payload_sha256, s->payload_bytes}) return std::unexpected( stage_failed(PublicationError::verify, {}, capsule_bytes ? ObjectError::corrupt : capsule_bytes.error())); auto ghost = destination_objects.put(*candidate); if (!ghost) return std::unexpected(stage_failed(PublicationError::upload, {}, ghost.error())); auto readback = destination_objects.get(*ghost); if (!readback || *readback != *candidate) return std::unexpected(stage_failed(PublicationError::verify, {}, readback ? ObjectError::corrupt : readback.error())); if (hooks.before_transaction) hooks.before_transaction(); return retry_cellular_migration_stage(db, lease, {d->key, *encoded, *ghost}, timeout); } std::expected, RecoveryFailure> load_cellular_migration_stage(Postgres& db, const FilesystemObjectStore& objects, RegionKey key, const LeaseGrant& lease, milliseconds timeout) { const auto fail = [](RecoveryError kind) { return std::unexpected(RecoveryFailure{kind, {}}); }; const std::array args{std::to_string(key.world), std::to_string(key.region), uuid(lease.holder), std::to_string(lease.epoch)}; auto rows = db.query(std::string("SELECT ") + stage_columns + " FROM evolution.load_cellular_migration_stage($1::evolution.u64," "$2::evolution.u64,$3::uuid,$4::evolution.u64)", args, timeout); if (!rows) return std::unexpected(RecoveryFailure{RecoveryError::store, failure(rows.error())}); if (rows->empty()) return std::optional{}; const auto record = rows->size() == 1 ? parse_stage(rows->front()) : std::nullopt; auto result = record ? checked_stage(*record, key, lease) : std::nullopt; if (!result || result->freeze.aborted) return fail(RecoveryError::history); const auto s = decode_manifest(record->cut.source), d = decode_manifest(record->cut.destination); if (!s || !d) return fail(RecoveryError::manifest); auto src = objects.get({s->payload_sha256, s->payload_bytes}); auto dst = objects.get({d->payload_sha256, d->payload_bytes}); auto ghost = objects.get(result->ghost); if (!src || !dst || !ghost) return fail(RecoveryError::tail); const auto candidate = ghost_candidate(*s, *d, *src, *dst, result->freeze.trace); if (!candidate || *candidate != *ghost) return fail(RecoveryError::history); return result; } namespace detail { std::expected, RecoveryFailure> capture_cellular_migrations(Postgres& db, std::uint64_t world, milliseconds timeout) { const std::array args{std::to_string(world)}; const auto rows = db.query("SELECT encode(trace,'hex'),encode(source_manifest,'hex')," "encode(destination_manifest,'hex'),encode(ghost_sha256,'hex')," "pg_catalog.trunc(ghost_bytes)::text,coherent::text " "FROM evolution.load_world_cellular_migrations_v1($1::evolution.u64)", args, timeout); if (!rows) return std::unexpected(RecoveryFailure{RecoveryError::store, failure(rows.error())}); if (rows->size() > server::max_cellular_census_regions) return std::unexpected( RecoveryFailure{RecoveryError::store, {LeaseStoreError::invalid, {PgError::bounds}}}); std::vector result; for (const auto& row : *rows) { if (row.size() != 6 || row[5].value_or("") != "true") return std::unexpected(RecoveryFailure{RecoveryError::history}); auto cut = parse({row[0], row[1], row[2], std::string{"false"}}); if (!cut) return std::unexpected(RecoveryFailure{RecoveryError::history}); std::optional ghost; if (row[3] || row[4]) { auto stage = parse_stage({row[0], row[1], row[2], std::string{"false"}, row[3], row[4]}); if (!stage) return std::unexpected(RecoveryFailure{RecoveryError::history}); ghost = stage->ghost; } result.push_back( {std::move(cut->trace), std::move(cut->source), std::move(cut->destination), ghost}); } return result; } std::expected verify_captured_cellular_migration(const CapturedCellularMigration& record, const FilesystemObjectStore& source_objects, const FilesystemObjectStore& destination_objects) { const auto fail = [](RecoveryError kind) { return std::unexpected(RecoveryFailure{kind}); }; auto trace = server::decode_migration_trace(record.trace); const auto s = decode_manifest(record.source), d = decode_manifest(record.destination); const auto sd = sha256(record.source), dd = sha256(record.destination); if (!trace || !s || !d || !sd || !dd || !cuts(*s, *d, *sd, *dd, *trace)) return fail(RecoveryError::history); const auto source = source_objects.get({s->payload_sha256, s->payload_bytes}); const auto capsule = source_objects.get({d->payload_sha256, d->payload_bytes}); if (!source || !capsule) return fail(RecoveryError::tail); const auto replay = derive(*s, *d, *source, *capsule, trace->request); if (!replay || *replay != *trace) return fail(RecoveryError::history); if (record.ghost) { if (record.ghost->sha256 == Sha256Digest{} || record.ghost->bytes == 0 || record.ghost->bytes > max_payload_bytes(d->profile)) return fail(RecoveryError::history); const auto sc = destination_objects.get({s->payload_sha256, s->payload_bytes}); const auto dc = destination_objects.get({d->payload_sha256, d->payload_bytes}); const auto ghost = destination_objects.get(*record.ghost); if (!sc || !dc || !ghost) return fail(RecoveryError::tail); const auto candidate = ghost_candidate(*s, *d, *sc, *dc, *trace); if (!candidate || *candidate != *ghost) return fail(RecoveryError::history); } return CellularMigrationObservation{std::move(*trace), record.ghost.has_value()}; } } // namespace detail namespace { constexpr auto commit_columns = "encode(trace,'hex'),encode(source_predecessor,'hex')," "encode(destination_predecessor,'hex'),encode(source_result,'hex')," "encode(destination_result,'hex'),encode(ghost_sha256,'hex'),ghost_bytes::text"; std::optional migration_manifest(const CheckpointManifest& p, const Sha256Digest& digest, const Uuid& commit, const LeaseGrant& lease, const Sha256Digest& build, VerifiedObject object) { if (!supported(p) || (p.cellular_versions != cellular_v10_versions && p.cellular_versions != cellular_v11_versions) || p.sequence == 0 || p.sequence == std::numeric_limits::max() || lease.frame != p.frame) return std::nullopt; auto m = p; m.commit = commit; ++m.sequence; m.predecessor_commit = p.commit; m.predecessor_digest = digest; m.begin_tick = m.end_tick; m.holder = lease.holder; m.epoch = lease.epoch; m.build = build; m.payload_bytes = object.bytes; m.payload_sha256 = object.sha256; m.batch = {}; if (!encode_manifest(m)) return std::nullopt; return m; } std::optional checkpoint(std::vector bytes) { const auto m = decode_manifest(bytes); const auto digest = sha256(bytes); if (!m || !digest) return std::nullopt; return CommittedCheckpoint{{m->frame, m->sequence, m->commit, *digest, m->end_tick}, std::move(bytes)}; } struct CommitRecord { StageRecord stage; CellularMigrationCommitted result; }; std::optional parse_commit(const PgRows::value_type& row) { if (row.size() != 7) return std::nullopt; auto stage = parse_stage({row[0], row[1], row[2], std::string{"false"}, row[5], row[6]}); auto s = unhex(row[3].value_or(""), checkpoint_manifest_bytes), d = unhex(row[4].value_or(""), checkpoint_manifest_bytes); auto source = s ? checkpoint(std::move(*s)) : std::nullopt; auto destination = d ? checkpoint(std::move(*d)) : std::nullopt; if (!stage || !source || !destination) return std::nullopt; return CommitRecord{std::move(*stage), {std::move(*source), std::move(*destination)}}; } bool checked_commit(const CommitRecord& record) { const auto t = server::decode_migration_trace(record.stage.cut.trace); const auto s = decode_manifest(record.stage.cut.source), d = decode_manifest(record.stage.cut.destination); const auto sr = decode_manifest(record.result.source.manifest), dr = decode_manifest(record.result.destination.manifest); const auto sd = sha256(record.stage.cut.source), dd = sha256(record.stage.cut.destination); if (!t || !s || !d || !sr || !dr || !sd || !dd || !cuts(*s, *d, *sd, *dd, *t) || record.stage.ghost != VerifiedObject{dr->payload_sha256, dr->payload_bytes}) return false; const auto expected_s = migration_manifest(*s, *sd, t->request.source.commit, {sr->holder, sr->epoch, sr->frame, milliseconds{0}}, sr->build, {sr->payload_sha256, sr->payload_bytes}); const auto expected_d = migration_manifest(*d, *dd, t->request.destination.commit, {dr->holder, dr->epoch, dr->frame, milliseconds{0}}, dr->build, {dr->payload_sha256, dr->payload_bytes}); return expected_s && expected_d && *expected_s == *sr && *expected_d == *dr; } CellularMigrationCommitFailure commit_failed(PublicationError kind, LeaseStoreFailure store = {}, ObjectError object = {}, CellularMigrationCommitPrepared prepared = {}) { return {kind, store, object, std::move(prepared)}; } } // namespace std::expected retry_cellular_migration_commit(Postgres& db, const LeaseGrant& source_lease, const LeaseGrant& destination_lease, const CellularMigrationCommitPrepared& prepared, milliseconds timeout) { const auto s = decode_manifest(prepared.source_manifest), d = decode_manifest(prepared.destination_manifest); if (!s || !d || prepared.request == Uuid{} || s->frame != source_lease.frame || d->frame != destination_lease.frame) return std::unexpected(commit_failed(PublicationError::candidate)); const std::array args{uuid(prepared.request), "\\x" + hex(prepared.source_manifest), "\\x" + hex(prepared.destination_manifest), uuid(source_lease.holder), std::to_string(source_lease.epoch), uuid(destination_lease.holder), std::to_string(destination_lease.epoch)}; auto rows = db.query( std::string("SELECT ") + commit_columns + " FROM evolution.publish_cellular_migration($1::uuid,$2::bytea,$3::bytea,$4::uuid," "$5::evolution.u64,$6::uuid,$7::evolution.u64)", args, timeout); if (!rows) { const auto store = failure(rows.error()); return std::unexpected(commit_failed(store.kind == LeaseStoreError::unknown ? PublicationError::unknown : PublicationError::rejected, store, {}, prepared)); } auto record = rows->size() == 1 ? parse_commit(rows->front()) : std::nullopt; if (!record || !checked_commit(*record) || record->result.source.manifest != prepared.source_manifest || record->result.destination.manifest != prepared.destination_manifest) return std::unexpected(commit_failed(PublicationError::unknown, {}, {}, prepared)); const auto trace = server::decode_migration_trace(record->stage.cut.trace); if (!trace || trace->request.request != prepared.request) return std::unexpected(commit_failed(PublicationError::unknown, {}, {}, prepared)); return std::move(record->result); } std::expected publish_cellular_migration(Postgres& db, const FilesystemObjectStore& source_objects, const FilesystemObjectStore& destination_objects, const LeaseGrant& source_lease, const LeaseGrant& destination_lease, const CommittedCheckpoint& source, const CommittedCheckpoint& destination, const server::CellularMigrationTrace& trace, const Sha256Digest& build, milliseconds timeout, const PublishHooks& hooks) { const auto s = decode_manifest(source.manifest), d = decode_manifest(destination.manifest); if (!s || !d || !bound(source, *s) || !bound(destination, *d) || !cuts(*s, *d, source.head.digest, destination.head.digest, trace)) return std::unexpected(commit_failed(PublicationError::predecessor)); auto src = source_objects.get({s->payload_sha256, s->payload_bytes}), dst = destination_objects.get({d->payload_sha256, d->payload_bytes}); if (!src || !dst) return std::unexpected( commit_failed(PublicationError::predecessor, {}, !src ? src.error() : dst.error())); const auto pair = migration_payloads(*s, *d, *src, *dst, trace); if (!pair) return std::unexpected(commit_failed(PublicationError::candidate)); const auto so = source_objects.put(pair->source), dest = destination_objects.put(pair->destination); if (!so || !dest) return std::unexpected( commit_failed(PublicationError::upload, {}, !so ? so.error() : dest.error())); const auto sb = source_objects.get(*so), dbb = destination_objects.get(*dest); if (!sb || !dbb || *sb != pair->source || *dbb != pair->destination) return std::unexpected(commit_failed(PublicationError::verify)); const auto sm = migration_manifest(*s, source.head.digest, trace.request.source.commit, source_lease, build, *so), dm = migration_manifest(*d, destination.head.digest, trace.request.destination.commit, destination_lease, build, *dest); if (!sm || !dm) return std::unexpected(commit_failed(PublicationError::candidate)); const auto sm_bytes = encode_manifest(*sm), dm_bytes = encode_manifest(*dm); if (!sm_bytes || !dm_bytes) return std::unexpected(commit_failed(PublicationError::candidate)); if (hooks.before_transaction) hooks.before_transaction(); return retry_cellular_migration_commit(db, source_lease, destination_lease, {trace.request.request, *sm_bytes, *dm_bytes}, timeout); } std::expected verify_cellular_migration_checkpoint(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease, const CommittedCheckpoint& committed, std::span payload, milliseconds timeout) { const auto fail = [](RecoveryError kind) { return std::unexpected(RecoveryFailure{kind, {}}); }; const auto m = decode_manifest(committed.manifest); if (!m || !bound(committed, *m) || lease.frame != m->frame) return fail(RecoveryError::manifest); const std::array args{std::to_string(m->key.world), std::to_string(m->key.region), uuid(lease.holder), std::to_string(lease.epoch), uuid(m->commit)}; auto rows = db.query( std::string("SELECT ") + commit_columns + " FROM evolution.load_cellular_migration_commit($1::evolution.u64,$2::evolution.u64," "$3::uuid,$4::evolution.u64,$5::uuid)", args, timeout); if (!rows) return std::unexpected(RecoveryFailure{RecoveryError::store, failure(rows.error())}); if (rows->empty()) return false; const auto record = rows->size() == 1 ? parse_commit(rows->front()) : std::nullopt; if (!record || !checked_commit(*record)) return fail(RecoveryError::history); const auto s = decode_manifest(record->stage.cut.source), d = decode_manifest(record->stage.cut.destination); const auto sr = decode_manifest(record->result.source.manifest), dr = decode_manifest(record->result.destination.manifest); const auto trace = server::decode_migration_trace(record->stage.cut.trace); if (!s || !d || !sr || !dr || !trace) return fail(RecoveryError::history); const bool is_source = m->key == s->key; if (committed.manifest != (is_source ? record->result.source.manifest : record->result.destination.manifest)) return fail(RecoveryError::history); const auto src = objects.get({s->payload_sha256, s->payload_bytes}), dst = objects.get({d->payload_sha256, d->payload_bytes}); if (!src || !dst) return fail(RecoveryError::tail); const auto pair = migration_payloads(*s, *d, *src, *dst, *trace); if (!pair) return fail(RecoveryError::history); const auto sd = sha256(pair->source), dd = sha256(pair->destination); if (!sd || !dd || *sd != sr->payload_sha256 || *dd != dr->payload_sha256 || pair->source.size() != sr->payload_bytes || pair->destination.size() != dr->payload_bytes || !std::ranges::equal(is_source ? pair->source : pair->destination, payload)) return fail(RecoveryError::history); return true; } } // namespace evo::persistence