Add M10.8a probe handoff schema v12 and store
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (client) (push) Successful in 12m32s
Foundation / checks (network) (push) Successful in 12m17s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 2m2s
Foundation / checks (postgres) (push) Canceled after 3m48s
Foundation / checks (checks) (push) Canceled after 16m29s

ADR-0096, the persistence handoff ADR (EXPERIMENT probes, ADR-0094). Durable
probe records hold one owning region per account, so ownership is unique and
the test inventory is conserved by construction. A fenced transfer state
machine (prepared, staged, committed, aborted) under each caller's exact
region lease returns the recorded outcome on every retry; a commit requires
staging by the destination's current epoch; aborts are durable and only
before commit. handoff_account_control moves account control on a committed
transfer.

A PostgreSQL fault matrix covers restarts at each transition, stale epochs,
failed destinations, partitions and commit/abort races, checking one owner and
the conserved inventory after every scenario.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-06 18:15:49 -04:00
co-authored by Claude Opus 5.5
parent 5728e7682e
commit 6a645ef7e4
18 changed files with 1355 additions and 16 deletions
+215
View File
@@ -1,5 +1,6 @@
#include "evo/persistence/region_lease_store.hpp"
#include "evo/persistence/account_control_store.hpp"
#include "evo/persistence/probe_store.hpp"
#include <array>
#include <charconv>
#include <optional>
@@ -471,6 +472,31 @@ claim_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, cons
return AccountControlClaim{*generation, milliseconds{*wait}, milliseconds{*granted}};
}
std::expected<AccountControlClaim, ControlStoreFailure>
handoff_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
const Uuid& transfer, std::uint64_t session, milliseconds duration,
milliseconds timeout) {
const std::array<std::string, 8> p{std::to_string(key.world), std::to_string(key.region),
uuid_text(lease.holder), std::to_string(lease.epoch),
uuid_text(account), uuid_text(transfer),
std::to_string(session), std::to_string(duration.count())};
auto rows = db.query("SELECT * FROM evolution.handoff_account_control($1::evolution.u64, "
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, $6::uuid, "
"$7::evolution.u64, $8::integer)",
p, timeout);
if (!rows)
return std::unexpected{control_failure(rows.error())};
if (rows->size() != 1 || (*rows)[0].size() != 3)
return std::unexpected{ControlStoreFailure{ControlStoreError::unknown, {}}};
const auto& r = (*rows)[0];
const auto generation = parse_integer<std::uint64_t>(r[0]);
const auto wait = parse_integer<std::int64_t>(r[1]);
const auto granted = parse_integer<std::int64_t>(r[2]);
if (!generation || !wait || !granted || *generation == 0)
return std::unexpected{ControlStoreFailure{ControlStoreError::unknown, {}}};
return AccountControlClaim{*generation, milliseconds{*wait}, milliseconds{*granted}};
}
std::expected<milliseconds, ControlStoreFailure>
renew_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
std::uint64_t generation, milliseconds duration, milliseconds timeout) {
@@ -506,4 +532,193 @@ release_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, co
return std::unexpected{ControlStoreFailure{ControlStoreError::unknown, {}}};
return text == "true";
}
// Schema v12 probe records and handoff (ADR-0096). Shares this file's parsing rules.
namespace {
ProbeStoreFailure probe_failure(const PgFailure& f) {
const std::string_view state(f.sqlstate.data(), 5);
auto kind = ProbeStoreError::store;
if (f.kind == PgError::unknown)
kind = ProbeStoreError::unknown;
else if (f.kind == PgError::config || f.kind == PgError::bounds || state == "22023" ||
state == "EVL04")
kind = ProbeStoreError::invalid;
else if (state == "EVL02" || state == "EVL06" || state == "EVL13")
kind = ProbeStoreError::fenced;
else if (state == "EVL14")
kind = ProbeStoreError::unknown_transfer;
else if (state == "EVL15")
kind = ProbeStoreError::not_staged;
else if (state == "EVL05")
kind = ProbeStoreError::exhausted;
return {kind, f};
}
std::optional<std::int64_t> parse_signed(const std::optional<std::string>& s) {
if (!s || s->empty() || s->size() > 20 || s->front() == '+')
return std::nullopt;
std::int64_t value{};
const auto* end = s->data() + s->size();
const auto [at, error] = std::from_chars(s->data(), end, value);
if (error != std::errc{} || at != end)
return std::nullopt;
return value;
}
std::optional<TransferState> parse_state(const std::optional<std::string>& s) {
if (s == "prepared")
return TransferState::prepared;
if (s == "staged")
return TransferState::staged;
if (s == "committed")
return TransferState::committed;
if (s == "aborted")
return TransferState::aborted;
return std::nullopt;
}
// One row of evolution.probe_transfer_row (9 columns).
template <typename Row> std::optional<ProbeTransfer> parse_transfer(const Row& r) {
if (r.size() != 9)
return std::nullopt;
const auto state = parse_state(r[0]);
const auto account = parse_uuid(r[1]);
const auto generation = parse_integer<std::uint64_t>(r[2]);
const auto source = parse_integer<std::uint64_t>(r[3]);
const auto destination = parse_integer<std::uint64_t>(r[4]);
const auto x = parse_signed(r[5]);
const auto y = parse_signed(r[6]);
const auto z = parse_signed(r[7]);
const auto inventory = parse_integer<std::uint64_t>(r[8]);
if (!state || !account || !generation || !source || !destination || !x || !y || !z ||
!inventory)
return std::nullopt;
return ProbeTransfer{*state, *account, *generation, *source, *destination,
*x, *y, *z, *inventory};
}
std::array<std::string, 5> transfer_parameters(RegionKey key, const LeaseGrant& lease,
const Uuid& transfer) {
return {std::to_string(key.world), std::to_string(key.region), uuid_text(lease.holder),
std::to_string(lease.epoch), uuid_text(transfer)};
}
// Calls a transfer function whose parameters are the lease and one transfer id.
std::expected<ProbeTransfer, ProbeStoreFailure>
transfer_call(Postgres& db, std::string_view function, RegionKey key, const LeaseGrant& lease,
const Uuid& transfer, milliseconds timeout) {
auto rows = db.query("SELECT * FROM evolution." + std::string{function} +
"($1::evolution.u64, $2::evolution.u64, $3::uuid, "
"$4::evolution.u64, $5::uuid)",
transfer_parameters(key, lease, transfer), timeout);
if (!rows)
return std::unexpected{probe_failure(rows.error())};
const auto t = rows->size() == 1 ? parse_transfer((*rows)[0]) : std::nullopt;
if (!t)
return std::unexpected{ProbeStoreFailure{ProbeStoreError::unknown, {}}};
return *t;
}
} // namespace
std::expected<ProbeRecord, ProbeStoreFailure>
open_probe(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
std::int64_t x, std::int64_t y, std::int64_t z, std::uint64_t inventory,
milliseconds timeout) {
const std::array<std::string, 9> p{
std::to_string(key.world), std::to_string(key.region), uuid_text(lease.holder),
std::to_string(lease.epoch), uuid_text(account), std::to_string(x),
std::to_string(y), std::to_string(z), std::to_string(inventory)};
auto rows = db.query("SELECT * FROM evolution.open_probe($1::evolution.u64, "
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, $6::bigint, "
"$7::bigint, $8::bigint, $9::evolution.u64)",
p, timeout);
if (!rows)
return std::unexpected{probe_failure(rows.error())};
if (rows->size() != 1 || (*rows)[0].size() != 7)
return std::unexpected{ProbeStoreFailure{ProbeStoreError::unknown, {}}};
const auto& r = (*rows)[0];
const auto region = parse_integer<std::uint64_t>(r[0]);
const auto generation = parse_integer<std::uint64_t>(r[1]);
const auto px = parse_signed(r[2]);
const auto py = parse_signed(r[3]);
const auto pz = parse_signed(r[4]);
const auto held = parse_integer<std::uint64_t>(r[5]);
const auto transfer = r[6] ? parse_uuid(r[6]) : std::nullopt;
if (!region || !generation || !px || !py || !pz || !held || (r[6] && !transfer))
return std::unexpected{ProbeStoreFailure{ProbeStoreError::unknown, {}}};
return ProbeRecord{*region, *generation, *px, *py, *pz, *held, transfer};
}
std::expected<void, ProbeStoreFailure> save_probe(Postgres& db, RegionKey key,
const LeaseGrant& lease, const Uuid& account,
std::uint64_t generation, std::int64_t x,
std::int64_t y, std::int64_t z,
milliseconds timeout) {
const std::array<std::string, 9> p{
std::to_string(key.world), std::to_string(key.region), uuid_text(lease.holder),
std::to_string(lease.epoch), uuid_text(account), std::to_string(generation),
std::to_string(x), std::to_string(y), std::to_string(z)};
auto rows = db.query("SELECT evolution.save_probe($1::evolution.u64, $2::evolution.u64, "
"$3::uuid, $4::evolution.u64, $5::uuid, $6::evolution.u64, $7::bigint, "
"$8::bigint, $9::bigint)::text",
p, timeout);
if (!rows)
return std::unexpected{probe_failure(rows.error())};
return {};
}
std::expected<ProbeTransfer, ProbeStoreFailure>
prepare_probe_transfer(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
std::uint64_t generation, const Uuid& transfer, std::uint64_t destination,
std::int64_t x, std::int64_t y, std::int64_t z, milliseconds timeout) {
const std::array<std::string, 11> p{std::to_string(key.world), std::to_string(key.region),
uuid_text(lease.holder), std::to_string(lease.epoch),
uuid_text(account), std::to_string(generation),
uuid_text(transfer), std::to_string(destination),
std::to_string(x), std::to_string(y),
std::to_string(z)};
auto rows = db.query("SELECT * FROM evolution.prepare_probe_transfer($1::evolution.u64, "
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, "
"$6::evolution.u64, $7::uuid, $8::evolution.u64, $9::bigint, "
"$10::bigint, $11::bigint)",
p, timeout);
if (!rows)
return std::unexpected{probe_failure(rows.error())};
const auto t = rows->size() == 1 ? parse_transfer((*rows)[0]) : std::nullopt;
if (!t)
return std::unexpected{ProbeStoreFailure{ProbeStoreError::unknown, {}}};
return *t;
}
std::expected<ProbeTransfer, ProbeStoreFailure> stage_probe_transfer(Postgres& db, RegionKey key,
const LeaseGrant& lease,
const Uuid& transfer,
milliseconds timeout) {
return transfer_call(db, "stage_probe_transfer", key, lease, transfer, timeout);
}
std::expected<ProbeTransfer, ProbeStoreFailure> commit_probe_transfer(Postgres& db, RegionKey key,
const LeaseGrant& lease,
const Uuid& transfer,
milliseconds timeout) {
return transfer_call(db, "commit_probe_transfer", key, lease, transfer, timeout);
}
std::expected<ProbeTransfer, ProbeStoreFailure> abort_probe_transfer(Postgres& db, RegionKey key,
const LeaseGrant& lease,
const Uuid& transfer,
milliseconds timeout) {
return transfer_call(db, "abort_probe_transfer", key, lease, transfer, timeout);
}
std::expected<std::optional<ProbeTransfer>, ProbeStoreFailure>
find_probe_transfer(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& transfer,
milliseconds timeout) {
auto rows = db.query("SELECT * FROM evolution.find_probe_transfer($1::evolution.u64, "
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)",
transfer_parameters(key, lease, transfer), timeout);
if (!rows)
return std::unexpected{probe_failure(rows.error())};
if (rows->empty())
return std::optional<ProbeTransfer>{};
const auto t = rows->size() == 1 ? parse_transfer((*rows)[0]) : std::nullopt;
if (!t)
return std::unexpected{ProbeStoreFailure{ProbeStoreError::unknown, {}}};
return t;
}
} // namespace evo::persistence