Add typed primitive life reservation adapter and reconciliation tests
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m0s
Foundation / checks (client) (push) Successful in 12m15s
Foundation / checks (network) (push) Successful in 13m46s
Foundation / checks (tsan) (push) Successful in 8m9s
Foundation / checks (checks) (push) Successful in 33m12s
Foundation / checks (postgres) (push) Successful in 46m13s
Foundation / postgres-tools (push) Successful in 48s
Foundation / image (push) Successful in 2m45s
Foundation / server-image (push) Successful in 4m27s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m0s
Foundation / checks (client) (push) Successful in 12m15s
Foundation / checks (network) (push) Successful in 13m46s
Foundation / checks (tsan) (push) Successful in 8m9s
Foundation / checks (checks) (push) Successful in 33m12s
Foundation / checks (postgres) (push) Successful in 46m13s
Foundation / postgres-tools (push) Successful in 48s
Foundation / image (push) Successful in 2m45s
Foundation / server-image (push) Successful in 4m27s
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "evo/persistence/region_lease_store.hpp"
|
||||
#include "evo/persistence/account_control_store.hpp"
|
||||
#include "evo/persistence/life_reservation_store.hpp"
|
||||
#include "evo/persistence/probe_store.hpp"
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
@@ -577,6 +578,89 @@ release_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, co
|
||||
return text == "true";
|
||||
}
|
||||
|
||||
namespace {
|
||||
LifeReservationFailure life_failure(const PgFailure& f) {
|
||||
const std::string_view state(f.sqlstate.data(), 5);
|
||||
auto kind = LifeReservationError::store;
|
||||
if (f.kind == PgError::unknown)
|
||||
kind = LifeReservationError::unknown;
|
||||
else if (f.kind == PgError::config || f.kind == PgError::bounds || state == "22023")
|
||||
kind = LifeReservationError::invalid;
|
||||
else if (state == "EVL16")
|
||||
kind = LifeReservationError::existing_life;
|
||||
else if (state == "EVL02" || state == "EVL06")
|
||||
kind = LifeReservationError::fenced;
|
||||
else if (state == "EVL04")
|
||||
kind = LifeReservationError::not_available;
|
||||
else if (state == "EVL05")
|
||||
kind = LifeReservationError::exhausted;
|
||||
return {kind, f};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<LifeReservation, LifeReservationFailure>
|
||||
reserve_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
||||
std::uint64_t generation, std::uint64_t request_id, std::uint32_t rules,
|
||||
milliseconds timeout) {
|
||||
// Avoid a PostgreSQL integer cast failure for arbitrary unsigned rule versions.
|
||||
if (rules != 1)
|
||||
return std::unexpected{LifeReservationFailure{LifeReservationError::invalid, {}}};
|
||||
const auto base = control_parameters(key, lease, account, generation);
|
||||
const std::array<std::string, 8> p{base[0],
|
||||
base[1],
|
||||
base[2],
|
||||
base[3],
|
||||
base[4],
|
||||
base[5],
|
||||
std::to_string(request_id),
|
||||
std::to_string(rules)};
|
||||
auto rows = db.query("SELECT * FROM evolution.reserve_primitive_life($1::evolution.u64, "
|
||||
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, "
|
||||
"$6::evolution.u64, $7::evolution.u64, $8::integer)",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{life_failure(rows.error())};
|
||||
const auto unknown = LifeReservationFailure{LifeReservationError::unknown, {}};
|
||||
if (rows->size() != 1 || (*rows)[0].size() != 3)
|
||||
return std::unexpected{unknown};
|
||||
const auto identity = parse_integer<std::uint64_t>((*rows)[0][0]);
|
||||
const auto region = parse_integer<std::uint64_t>((*rows)[0][1]);
|
||||
const auto refusal = parse_integer<std::uint32_t>((*rows)[0][2]);
|
||||
if (!identity || *identity == 0 || !region || *region == 0 || !refusal || *refusal > 3)
|
||||
return std::unexpected{unknown};
|
||||
return LifeReservation{*identity, *region, static_cast<LifeRefusal>(*refusal)};
|
||||
}
|
||||
|
||||
std::expected<LifeRefusal, LifeReservationFailure>
|
||||
reject_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
||||
std::uint64_t generation, std::uint64_t request_id, LifeRefusal reason,
|
||||
milliseconds timeout) {
|
||||
const auto value = static_cast<std::uint8_t>(reason);
|
||||
if (value < 1 || value > 3)
|
||||
return std::unexpected{LifeReservationFailure{LifeReservationError::invalid, {}}};
|
||||
const auto base = control_parameters(key, lease, account, generation);
|
||||
const std::array<std::string, 8> p{base[0],
|
||||
base[1],
|
||||
base[2],
|
||||
base[3],
|
||||
base[4],
|
||||
base[5],
|
||||
std::to_string(request_id),
|
||||
std::to_string(value)};
|
||||
auto rows = db.query("SELECT evolution.reject_primitive_life($1::evolution.u64, "
|
||||
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, "
|
||||
"$6::evolution.u64, $7::evolution.u64, $8::smallint)",
|
||||
p, timeout);
|
||||
if (!rows)
|
||||
return std::unexpected{life_failure(rows.error())};
|
||||
const auto refusal = rows->size() == 1 && (*rows)[0].size() == 1
|
||||
? parse_integer<std::uint32_t>((*rows)[0][0])
|
||||
: std::nullopt;
|
||||
if (!refusal || *refusal < 1 || *refusal > 3)
|
||||
return std::unexpected{LifeReservationFailure{LifeReservationError::unknown, {}}};
|
||||
return static_cast<LifeRefusal>(*refusal);
|
||||
}
|
||||
|
||||
// Schema v12 probe records and handoff (ADR-0096). Shares this file's parsing rules.
|
||||
namespace {
|
||||
ProbeStoreFailure probe_failure(const PgFailure& f) {
|
||||
|
||||
Reference in New Issue
Block a user