M12.6: capture consistent regional census snapshots
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m20s
Foundation / checks (network) (push) Successful in 21m3s
Foundation / checks (client) (push) Successful in 22m14s
Foundation / checks (tsan) (push) Successful in 24m52s
Foundation / checks (postgres) (push) Failing after 36m19s
Foundation / checks (checks) (push) Successful in 1h10m25s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m20s
Foundation / checks (network) (push) Successful in 21m3s
Foundation / checks (client) (push) Successful in 22m14s
Foundation / checks (tsan) (push) Successful in 24m52s
Foundation / checks (postgres) (push) Failing after 36m19s
Foundation / checks (checks) (push) Successful in 1h10m25s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#include "evo/persistence/cellular_census_snapshot.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
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, {}});
|
||||
}
|
||||
// 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<std::vector<CommittedCheckpoint>, Failure>
|
||||
capture(Postgres& db, std::uint64_t world, std::span<const CellularCensusRegionRequest> requests,
|
||||
std::chrono::milliseconds timeout, const CellularCensusSnapshotHooks& hooks) {
|
||||
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<std::vector<CommittedCheckpoint>, Failure> {
|
||||
std::vector<CommittedCheckpoint> cuts;
|
||||
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 (hooks.after_head)
|
||||
hooks.after_head(i);
|
||||
}
|
||||
return cuts;
|
||||
}();
|
||||
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;
|
||||
}
|
||||
} // 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) {
|
||||
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<std::uint64_t> 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);
|
||||
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];
|
||||
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 CellularCensusSnapshot{std::move(*cuts), std::move(*census)};
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
@@ -119,6 +119,9 @@ struct Postgres::Impl {
|
||||
};
|
||||
Postgres::Postgres(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
|
||||
Postgres::~Postgres() = default;
|
||||
void Postgres::disconnect() noexcept {
|
||||
impl_->connection.reset();
|
||||
}
|
||||
bool Postgres::transaction_idle() const noexcept {
|
||||
return impl_->connection && PQstatus(impl_->connection.get()) == CONNECTION_OK &&
|
||||
PQtransactionStatus(impl_->connection.get()) == PQTRANS_IDLE;
|
||||
|
||||
Reference in New Issue
Block a user