M12.7: require registered regions in census snapshots
Foundation / checks (deploy) (push) Successful in 58s
Foundation / checks (ecs) (push) Successful in 2m36s
Foundation / checks (network) (push) Successful in 20m56s
Foundation / checks (client) (push) Successful in 22m19s
Foundation / checks (tsan) (push) Successful in 25m18s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 44m49s
Foundation / checks (postgres) (push) Canceled after 44m54s
Foundation / checks (deploy) (push) Successful in 58s
Foundation / checks (ecs) (push) Successful in 2m36s
Foundation / checks (network) (push) Successful in 20m56s
Foundation / checks (client) (push) Successful in 22m19s
Foundation / checks (tsan) (push) Successful in 25m18s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 44m49s
Foundation / checks (postgres) (push) Canceled after 44m54s
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
#include "evo/persistence/cellular_census_snapshot.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::persistence {
|
||||
@@ -12,6 +16,43 @@ auto transaction_failure(PgFailure 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}});
|
||||
}
|
||||
struct Captured {
|
||||
std::vector<RegisteredCellularRegion> catalog;
|
||||
std::vector<CommittedCheckpoint> cuts;
|
||||
};
|
||||
std::expected<std::vector<RegisteredCellularRegion>, Failure>
|
||||
catalog(Postgres& db, std::uint64_t world, std::chrono::milliseconds timeout) {
|
||||
const std::array<std::string, 1> 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<std::string>& 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<RegisteredCellularRegion> 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 {
|
||||
@@ -22,9 +63,11 @@ struct CaptureGuard {
|
||||
db.disconnect();
|
||||
}
|
||||
};
|
||||
std::expected<std::vector<CommittedCheckpoint>, Failure>
|
||||
capture(Postgres& db, std::uint64_t world, std::span<const CellularCensusRegionRequest> requests,
|
||||
std::chrono::milliseconds timeout, const CellularCensusSnapshotHooks& hooks) {
|
||||
std::expected<Captured, Failure> capture(Postgres& db, std::uint64_t world,
|
||||
std::span<const CellularCensusRegionRequest> 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())
|
||||
@@ -32,19 +75,35 @@ capture(Postgres& db, std::uint64_t world, std::span<const CellularCensusRegionR
|
||||
return transaction_failure(begun.error());
|
||||
}
|
||||
CaptureGuard guard{db};
|
||||
auto result = [&]() -> std::expected<std::vector<CommittedCheckpoint>, Failure> {
|
||||
std::vector<CommittedCheckpoint> cuts;
|
||||
cuts.reserve(requests.size());
|
||||
auto result = [&]() -> std::expected<Captured, Failure> {
|
||||
Captured captured;
|
||||
if (registered) {
|
||||
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);
|
||||
}
|
||||
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()}});
|
||||
cuts.push_back(std::move(*head));
|
||||
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 cuts;
|
||||
return captured;
|
||||
}();
|
||||
const auto done = db.query(result ? "COMMIT" : "ROLLBACK", {}, timeout);
|
||||
if (!done)
|
||||
@@ -54,12 +113,11 @@ capture(Postgres& db, std::uint64_t world, std::span<const CellularCensusRegionR
|
||||
guard.active = false;
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularCensusSnapshot, CellularSnapshotFailure> load_cellular_census_snapshot(
|
||||
Postgres& db, std::uint64_t world, std::span<const CellularCensusRegionRequest> requests,
|
||||
std::span<const Sha256Digest> builds, std::chrono::milliseconds timeout,
|
||||
const CellularCensusSnapshotHooks& hooks) {
|
||||
std::expected<RegisteredCellularCensusSnapshot, Failure>
|
||||
load_snapshot(Postgres& db, std::uint64_t world,
|
||||
std::span<const CellularCensusRegionRequest> requests,
|
||||
std::span<const Sha256Digest> 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)
|
||||
@@ -72,14 +130,14 @@ std::expected<CellularCensusSnapshot, CellularSnapshotFailure> load_cellular_cen
|
||||
return std::unexpected(Failure{Error::invalid_request, 0, {}, {}, {}});
|
||||
required.push_back(request.region);
|
||||
}
|
||||
auto cuts = capture(db, world, requests, timeout, hooks);
|
||||
auto cuts = capture(db, world, requests, timeout, hooks, registered);
|
||||
if (!cuts)
|
||||
return std::unexpected(cuts.error());
|
||||
std::vector<server::CellularCensus> observations;
|
||||
observations.reserve(requests.size());
|
||||
for (std::size_t i = 0; i < requests.size(); ++i) {
|
||||
const auto& request = requests[i];
|
||||
const auto& cut = (*cuts)[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);
|
||||
@@ -97,6 +155,27 @@ std::expected<CellularCensusSnapshot, CellularSnapshotFailure> load_cellular_cen
|
||||
required, observations);
|
||||
if (!census)
|
||||
return std::unexpected(Failure{Error::census, 0, {}, {}, census.error()});
|
||||
return CellularCensusSnapshot{std::move(*cuts), std::move(*census)};
|
||||
return RegisteredCellularCensusSnapshot{std::move(cuts->catalog),
|
||||
{std::move(cuts->cuts), std::move(*census)}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<CellularCensusSnapshot, CellularSnapshotFailure> load_cellular_census_snapshot(
|
||||
Postgres& db, std::uint64_t world, std::span<const CellularCensusRegionRequest> requests,
|
||||
std::span<const Sha256Digest> 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<RegisteredCellularCensusSnapshot, CellularSnapshotFailure>
|
||||
load_registered_cellular_census_snapshot(Postgres& db, std::uint64_t world,
|
||||
std::span<const CellularCensusRegionRequest> requests,
|
||||
std::span<const Sha256Digest> builds,
|
||||
std::chrono::milliseconds timeout,
|
||||
const CellularCensusSnapshotHooks& hooks) {
|
||||
return load_snapshot(db, world, requests, builds, timeout, hooks, true);
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
|
||||
Reference in New Issue
Block a user