#include "evo/persistence/cellular_census_snapshot.hpp" #include "cellular_migration_observation_detail.hpp" #include #include #include #include #include #include namespace evo::persistence { namespace { using Failure = CellularSnapshotFailure; using Error = CellularSnapshotError; auto transaction_failure(PgFailure error) { return std::unexpected(Failure{Error::transaction, 0, error, {}, {}}); } auto history_failure(std::uint64_t region, CellularHistoryFailure error) { return std::unexpected(Failure{Error::history, region, {}, error, {}}); } auto coverage_failure(CellularCoverageError kind, PgFailure cause = {}) { return std::unexpected(Failure{Error::coverage, 0, {}, {}, {}, {kind, cause}}); } auto migration_failure(RecoveryFailure cause, std::uint64_t region = 0) { return std::unexpected(Failure{Error::migration, region, {}, {}, {}, {}, cause}); } struct Captured { std::vector catalog; std::vector cuts; Uuid history_domain{}; std::vector migrations; }; std::expected history_domain(Postgres& db, std::chrono::milliseconds timeout) { const auto rows = db.query("SELECT pg_catalog.encode(pg_catalog.uuid_send(" "evolution.load_history_domain_v1()),'hex')", {}, timeout); if (!rows) return transaction_failure(rows.error()); if (rows->size() != 1 || rows->front().size() != 1) return std::unexpected(Failure{Error::context}); const auto& encoded = rows->front()[0]; if (!encoded || encoded->size() != 32) return std::unexpected(Failure{Error::context}); const auto& text = *encoded; Uuid result{}; for (std::size_t i = 0; i < result.size(); ++i) { unsigned byte{}; const auto* begin = text.data() + 2 * i; const auto [end, error] = std::from_chars(begin, begin + 2, byte, 16); if (error != std::errc{} || end != begin + 2 || byte > 255) return std::unexpected(Failure{Error::context}); result[i] = static_cast(byte); } if (result == Uuid{}) return std::unexpected(Failure{Error::context}); return result; } std::expected, Failure> catalog(Postgres& db, std::uint64_t world, std::chrono::milliseconds timeout) { const std::array parameters{std::to_string(world)}; const auto rows = db.query("SELECT pg_catalog.trunc(region_id)::text," "pg_catalog.trunc(frame_id)::text FROM " "evolution.load_world_region_catalog_v1($1::evolution.u64)", parameters, timeout); if (!rows) return coverage_failure(CellularCoverageError::store, rows.error()); if (rows->empty()) return coverage_failure(CellularCoverageError::unknown_world); if (rows->size() > server::max_cellular_census_regions) return coverage_failure(CellularCoverageError::bounds); const auto number = [](const std::optional& text, std::uint64_t& value) { if (!text) return false; const auto [end, error] = std::from_chars(text->data(), text->data() + text->size(), value); return error == std::errc{} && end == text->data() + text->size() && value != 0; }; std::vector result; result.reserve(rows->size()); for (const auto& row : *rows) { RegisteredCellularRegion entry; if (row.size() != 2 || !number(row[0], entry.region) || !number(row[1], entry.frame) || (!result.empty() && entry.region <= result.back().region)) return coverage_failure(CellularCoverageError::malformed); result.push_back(entry); } return result; } // Exception-safe capture ownership: explicit completion disarms this guard. // Closing cannot allocate/throw or leave a caller with an open failed transaction. struct CaptureGuard { Postgres& db; bool active = true; ~CaptureGuard() { if (active) db.disconnect(); } }; std::expected capture(Postgres& db, std::uint64_t world, std::span requests, std::chrono::milliseconds timeout, const CellularCensusSnapshotHooks& hooks, bool registered) { const auto begun = db.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY", {}, timeout); if (!begun) { if (!db.transaction_idle()) db.disconnect(); return transaction_failure(begun.error()); } CaptureGuard guard{db}; auto result = [&]() -> std::expected { Captured captured; if (registered) { auto domain = history_domain(db, timeout); if (!domain) return std::unexpected(domain.error()); captured.history_domain = *domain; auto required = catalog(db, world, timeout); if (!required) return std::unexpected(required.error()); if (required->size() != requests.size()) return coverage_failure(CellularCoverageError::regions); for (std::size_t i = 0; i < requests.size(); ++i) { if ((*required)[i].region != requests[i].region) return coverage_failure(CellularCoverageError::regions); if ((*required)[i].frame != requests[i].lease.frame) return coverage_failure(CellularCoverageError::frame); } captured.catalog = std::move(*required); auto pending = detail::capture_cellular_migrations(db, world, timeout); if (!pending) return migration_failure(pending.error()); captured.migrations = std::move(*pending); } captured.cuts.reserve(requests.size()); for (std::size_t i = 0; i < requests.size(); ++i) { auto head = load_region_checkpoint(db, {world, requests[i].region}, timeout); if (!head) return history_failure(requests[i].region, {CellularHistoryError::recovery, {RecoveryError::store, head.error()}}); if (registered && head->head.frame != captured.catalog[i].frame) return coverage_failure(CellularCoverageError::frame); captured.cuts.push_back(std::move(*head)); if (hooks.after_head) hooks.after_head(i); } return captured; }(); const auto done = db.query(result ? "COMMIT" : "ROLLBACK", {}, timeout); if (!done) return transaction_failure(done.error()); if (!db.transaction_idle()) return transaction_failure({PgError::sql, {}}); guard.active = false; return result; } std::expected load_snapshot(Postgres& db, std::uint64_t world, std::span requests, std::span builds, std::chrono::milliseconds timeout, const CellularCensusSnapshotHooks& hooks, bool registered) { if (!db.transaction_idle() || world == 0 || requests.empty() || requests.size() > server::max_cellular_census_regions || timeout.count() <= 0 || timeout.count() > 30000) return std::unexpected(Failure{Error::invalid_request, 0, {}, {}, {}}); std::vector required; required.reserve(requests.size()); for (const auto& request : requests) { if (!request.objects || request.region == 0 || request.lease.frame == 0 || (!required.empty() && request.region <= required.back())) return std::unexpected(Failure{Error::invalid_request, 0, {}, {}, {}}); required.push_back(request.region); } auto cuts = capture(db, world, requests, timeout, hooks, registered); if (!cuts) return std::unexpected(cuts.error()); std::vector migrations; for (const auto& pending : cuts->migrations) { const auto source_manifest = decode_manifest(pending.source); const auto destination_manifest = decode_manifest(pending.destination); if (!source_manifest || !destination_manifest) return migration_failure({RecoveryError::manifest}); if (std::ranges::find(builds, source_manifest->build) == builds.end() || std::ranges::find(builds, destination_manifest->build) == builds.end()) return migration_failure({RecoveryError::unsupported}); const auto trace = server::decode_migration_trace(pending.trace); if (!trace || trace->world != world || (!migrations.empty() && trace->request.request <= migrations.back().trace.request.request)) return migration_failure({RecoveryError::history}); const auto endpoint = [&](std::uint64_t region) { return std::ranges::lower_bound(requests, region, {}, &CellularCensusRegionRequest::region); }; const auto s = endpoint(trace->request.source.region); const auto d = endpoint(trace->request.destination.region); if (s == requests.end() || d == requests.end() || s->region != trace->request.source.region || d->region != trace->request.destination.region || s->lease.frame != trace->frame || d->lease.frame != trace->frame) return migration_failure({RecoveryError::history}); const auto si = static_cast(s - requests.begin()); const auto di = static_cast(d - requests.begin()); if (cuts->cuts[si].manifest != pending.source || (pending.ghost && cuts->cuts[di].manifest != pending.destination)) return migration_failure({RecoveryError::history}, s->region); auto verified = detail::verify_captured_cellular_migration(pending, *s->objects, *d->objects); if (!verified) return migration_failure(verified.error(), s->region); migrations.push_back(std::move(*verified)); } std::vector observations; observations.reserve(requests.size()); for (std::size_t i = 0; i < requests.size(); ++i) { const auto& request = requests[i]; const auto& cut = cuts->cuts[i]; auto page = load_cellular_census_history(db, *request.objects, {world, request.region}, request.lease, cut.head, cut.head.sequence, 1, builds, timeout); if (!page) return history_failure(request.region, page.error()); if (page->entries.size() != 1 || page->entries.front().committed.head != cut.head || page->entries.front().committed.manifest != cut.manifest) return history_failure(request.region, {CellularHistoryError::cut_mismatch, {}}); observations.push_back(std::move(page->entries.front().census)); } const auto& first = observations.front().boundary(); auto census = server::CellularCensusSet::observe({first.world, first.frame, first.tick, first.owner_format, first.rules, first.quantum_microseconds}, required, observations); if (!census) return std::unexpected(Failure{Error::census, 0, {}, {}, census.error()}); return RegisteredCellularCensusSnapshot{std::move(cuts->catalog), {std::move(cuts->cuts), std::move(*census)}, cuts->history_domain, std::move(migrations)}; } } // namespace std::expected load_cellular_census_snapshot( Postgres& db, std::uint64_t world, std::span requests, std::span builds, std::chrono::milliseconds timeout, const CellularCensusSnapshotHooks& hooks) { auto result = load_snapshot(db, world, requests, builds, timeout, hooks, false); if (!result) return std::unexpected(result.error()); return std::move(result->snapshot); } std::expected load_registered_cellular_census_snapshot(Postgres& db, std::uint64_t world, std::span requests, std::span builds, std::chrono::milliseconds timeout, const CellularCensusSnapshotHooks& hooks) { return load_snapshot(db, world, requests, builds, timeout, hooks, true); } } // namespace evo::persistence