Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m56s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 5m17s
Foundation / checks (checks) (push) Canceled after 5m21s
Foundation / checks (network) (push) Canceled after 3m14s
1256 lines
64 KiB
C++
1256 lines
64 KiB
C++
#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>
|
|
#include <limits>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
|
|
namespace evo::persistence {
|
|
namespace {
|
|
using std::chrono::milliseconds;
|
|
constexpr std::string_view digits = "0123456789abcdef";
|
|
|
|
LeaseStoreFailure classify(const PgFailure& f) {
|
|
const std::string_view state(f.sqlstate.data(), 5);
|
|
auto kind = LeaseStoreError::store;
|
|
if (f.kind == PgError::unknown)
|
|
kind = LeaseStoreError::unknown;
|
|
else if (f.kind == PgError::config || f.kind == PgError::bounds || state == "22023")
|
|
kind = LeaseStoreError::invalid;
|
|
else if (state == "EVL01")
|
|
kind = LeaseStoreError::busy;
|
|
else if (state == "EVL02")
|
|
kind = LeaseStoreError::fenced;
|
|
else if (state == "EVL03")
|
|
kind = LeaseStoreError::head_mismatch;
|
|
else if (state == "EVL04")
|
|
kind = LeaseStoreError::not_provisioned;
|
|
else if (state == "EVL05")
|
|
kind = LeaseStoreError::exhausted;
|
|
else if (state == "EVL06")
|
|
kind = LeaseStoreError::unsupported;
|
|
else if (state == "EVL07")
|
|
kind = LeaseStoreError::commit_conflict;
|
|
else if (state == "EVL08")
|
|
kind = LeaseStoreError::outcome_final;
|
|
else if (state == "EVL09")
|
|
kind = LeaseStoreError::immutable;
|
|
return {kind, f};
|
|
}
|
|
LeaseStoreFailure malformed(bool mutation) {
|
|
// A mutation whose reply cannot be parsed may still have committed.
|
|
return {mutation ? LeaseStoreError::unknown : LeaseStoreError::store, {}};
|
|
}
|
|
std::string hex(std::span<const std::uint8_t> bytes) {
|
|
std::string out;
|
|
out.reserve(bytes.size() * 2);
|
|
for (const auto b : bytes) {
|
|
out.push_back(digits[static_cast<std::size_t>(b >> 4U)]);
|
|
out.push_back(digits[static_cast<std::size_t>(b & 0x0FU)]);
|
|
}
|
|
return out;
|
|
}
|
|
std::optional<std::uint8_t> nibble(char c) {
|
|
const auto at = digits.find(c);
|
|
if (at == std::string_view::npos)
|
|
return std::nullopt;
|
|
return static_cast<std::uint8_t>(at);
|
|
}
|
|
template <std::size_t N> std::optional<std::array<std::uint8_t, N>> unhex(std::string_view s) {
|
|
if (s.size() != N * 2)
|
|
return std::nullopt;
|
|
std::array<std::uint8_t, N> out{};
|
|
for (std::size_t i = 0; i < N; ++i) {
|
|
const auto high = nibble(s[i * 2]);
|
|
const auto low = nibble(s[(i * 2) + 1]);
|
|
if (!high || !low)
|
|
return std::nullopt;
|
|
out[i] = static_cast<std::uint8_t>((*high << 4U) | *low);
|
|
}
|
|
return out;
|
|
}
|
|
std::optional<std::vector<std::uint8_t>> unhex_bytes(std::string_view s) {
|
|
if (s.size() % 2 != 0)
|
|
return std::nullopt;
|
|
std::vector<std::uint8_t> out(s.size() / 2);
|
|
for (std::size_t i = 0; i < out.size(); ++i) {
|
|
const auto high = nibble(s[i * 2]);
|
|
const auto low = nibble(s[(i * 2) + 1]);
|
|
if (!high || !low)
|
|
return std::nullopt;
|
|
out[i] = static_cast<std::uint8_t>((*high << 4U) | *low);
|
|
}
|
|
return out;
|
|
}
|
|
std::string uuid_text(const Uuid& id) {
|
|
const auto h = hex(id);
|
|
return h.substr(0, 8) + '-' + h.substr(8, 4) + '-' + h.substr(12, 4) + '-' + h.substr(16, 4) +
|
|
'-' + h.substr(20);
|
|
}
|
|
std::optional<Uuid> parse_uuid(const std::optional<std::string>& s) {
|
|
if (!s || s->size() != 36)
|
|
return std::nullopt;
|
|
std::string compact;
|
|
for (std::size_t i = 0; i < s->size(); ++i) {
|
|
const bool dash = i == 8 || i == 13 || i == 18 || i == 23;
|
|
if (dash != ((*s)[i] == '-'))
|
|
return std::nullopt;
|
|
if (!dash)
|
|
compact.push_back((*s)[i]);
|
|
}
|
|
return unhex<16>(compact);
|
|
}
|
|
template <typename T> std::optional<T> parse_integer(const std::optional<std::string>& s) {
|
|
if (!s || s->empty() || s->size() > 20 || s->front() == '-' || s->front() == '+')
|
|
return std::nullopt;
|
|
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::uint64_t little(std::span<const std::uint8_t> bytes, std::size_t at, std::size_t width) {
|
|
std::uint64_t value{};
|
|
for (std::size_t i = 0; i < width; ++i)
|
|
value |= static_cast<std::uint64_t>(bytes[at + i]) << (i * 8);
|
|
return value;
|
|
}
|
|
std::optional<CommittedActionRecord> parse_action_record(const PgRows::value_type& row,
|
|
std::size_t at) {
|
|
if (row.size() < at + 6)
|
|
return std::nullopt;
|
|
const auto region = parse_integer<std::uint64_t>(row[at]);
|
|
const auto tick = parse_integer<std::uint64_t>(row[at + 1]);
|
|
const auto sequence = parse_integer<std::uint64_t>(row[at + 2]);
|
|
const auto kind = parse_integer<std::uint16_t>(row[at + 3]);
|
|
const auto result = parse_integer<std::uint8_t>(row[at + 4]);
|
|
if (!region || !tick || !sequence || !kind || !result)
|
|
return std::nullopt;
|
|
std::optional<std::vector<std::uint8_t>> input;
|
|
if (row[at + 5]) {
|
|
input = unhex_bytes(*row[at + 5]);
|
|
if (!input)
|
|
return std::nullopt;
|
|
}
|
|
if (*kind == 1) {
|
|
if (*result != 1 || input)
|
|
return std::nullopt;
|
|
} else if (*kind == 2) {
|
|
if (*result != 5 || !input || input->size() != 24 || little(*input, 0, 8) != *tick ||
|
|
(*input)[12] > 1 || (*input)[13] != 0 || (*input)[14] != 0 || (*input)[15] != 0 ||
|
|
((*input)[12] == 0 && little(*input, 16, 8) != 0))
|
|
return std::nullopt;
|
|
} else {
|
|
return std::nullopt;
|
|
}
|
|
return CommittedActionRecord{CommittedAction{*region, *tick, *sequence}, *kind, *result,
|
|
std::move(input)};
|
|
}
|
|
std::string bytea(std::span<const std::uint8_t> b) {
|
|
return "\\x" + hex(b);
|
|
}
|
|
// Head columns: frame, sequence, commit, digest hex, end tick (+ optional extras).
|
|
std::optional<RegionHead> parse_head(const PgRows& rows, std::size_t columns = 5) {
|
|
if (rows.size() != 1 || rows[0].size() != columns)
|
|
return std::nullopt;
|
|
const auto& r = rows[0];
|
|
const auto frame = parse_integer<std::uint64_t>(r[0]);
|
|
const auto sequence = parse_integer<std::uint64_t>(r[1]);
|
|
const auto commit = parse_uuid(r[2]);
|
|
const auto& digest_text = r[3];
|
|
const auto digest = digest_text ? unhex<32>(*digest_text) : std::nullopt;
|
|
const auto tick = parse_integer<std::uint64_t>(r[4]);
|
|
if (!frame || !sequence || !commit || !digest || !tick)
|
|
return std::nullopt;
|
|
return RegionHead{*frame, *sequence, *commit, *digest, *tick};
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<LeaseGrant, LeaseStoreFailure> acquire_region_lease(Postgres& db, RegionKey key,
|
|
const Sha256Digest& build,
|
|
milliseconds duration,
|
|
milliseconds timeout) {
|
|
const std::array<std::string, 4> p{std::to_string(key.world), std::to_string(key.region),
|
|
std::to_string(duration.count()), bytea(build)};
|
|
auto rows = db.query("SELECT * FROM evolution.acquire_region_lease($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::integer, $4::bytea)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 4)
|
|
return std::unexpected{malformed(true)};
|
|
const auto& r = (*rows)[0];
|
|
const auto holder = parse_uuid(r[0]);
|
|
const auto epoch = parse_integer<std::uint64_t>(r[1]);
|
|
const auto frame = parse_integer<std::uint64_t>(r[2]);
|
|
const auto granted = parse_integer<std::int64_t>(r[3]);
|
|
if (!holder || !epoch || !frame || !granted || *epoch == 0)
|
|
return std::unexpected{malformed(true)};
|
|
return LeaseGrant{*holder, *epoch, *frame, milliseconds{*granted}};
|
|
}
|
|
|
|
std::expected<milliseconds, LeaseStoreFailure> renew_region_lease(Postgres& db, RegionKey key,
|
|
const LeaseGrant& lease,
|
|
milliseconds duration,
|
|
milliseconds timeout) {
|
|
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(lease.holder), std::to_string(lease.epoch),
|
|
std::to_string(duration.count())};
|
|
auto rows = db.query("SELECT evolution.renew_region_lease($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::integer)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
const auto granted = rows->size() == 1 && (*rows)[0].size() == 1
|
|
? parse_integer<std::int64_t>((*rows)[0][0])
|
|
: std::nullopt;
|
|
if (!granted)
|
|
return std::unexpected{malformed(true)};
|
|
return milliseconds{*granted};
|
|
}
|
|
|
|
std::expected<void, LeaseStoreFailure>
|
|
release_region_lease(Postgres& db, RegionKey key, const LeaseGrant& lease, milliseconds timeout) {
|
|
const std::array<std::string, 4> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(lease.holder), std::to_string(lease.epoch)};
|
|
auto rows = db.query("SELECT evolution.release_region_lease($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->size() != 1)
|
|
return std::unexpected{malformed(true)};
|
|
return {};
|
|
}
|
|
|
|
std::expected<RegionHead, LeaseStoreFailure> load_region_head(Postgres& db, RegionKey key,
|
|
milliseconds timeout) {
|
|
const std::array<std::string, 2> p{std::to_string(key.world), std::to_string(key.region)};
|
|
auto rows =
|
|
db.query("SELECT * FROM evolution.load_region_head($1::evolution.u64, $2::evolution.u64)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
auto head = parse_head(*rows);
|
|
if (!head)
|
|
return std::unexpected{malformed(false)};
|
|
return *head;
|
|
}
|
|
|
|
std::expected<CommittedCheckpoint, LeaseStoreFailure>
|
|
load_region_checkpoint(Postgres& db, RegionKey key, milliseconds timeout) {
|
|
const std::array<std::string, 2> p{std::to_string(key.world), std::to_string(key.region)};
|
|
auto rows = db.query(
|
|
"SELECT * FROM evolution.load_region_checkpoint($1::evolution.u64, $2::evolution.u64)", p,
|
|
timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
auto head = parse_head(*rows, 6);
|
|
const auto& manifest_text = head ? (*rows)[0][5] : std::optional<std::string>{};
|
|
auto manifest = manifest_text ? unhex_bytes(*manifest_text) : std::nullopt;
|
|
if (!head || !manifest)
|
|
return std::unexpected{malformed(false)};
|
|
return CommittedCheckpoint{*head, std::move(*manifest)};
|
|
}
|
|
|
|
std::expected<CommittedCheckpoint, LeaseStoreFailure>
|
|
load_region_commit(Postgres& db, RegionKey key, std::uint64_t sequence, milliseconds timeout) {
|
|
const std::array<std::string, 3> p{std::to_string(key.world), std::to_string(key.region),
|
|
std::to_string(sequence)};
|
|
auto rows = db.query("SELECT * FROM evolution.load_region_commit($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::evolution.u64)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
auto head = parse_head(*rows, 6);
|
|
if (!head || rows->size() != 1)
|
|
return std::unexpected{malformed(false)};
|
|
const auto& manifest_text = (*rows)[0][5];
|
|
auto manifest = manifest_text ? unhex_bytes(*manifest_text) : std::nullopt;
|
|
if (!manifest)
|
|
return std::unexpected{malformed(false)};
|
|
return CommittedCheckpoint{*head, std::move(*manifest)};
|
|
}
|
|
|
|
std::expected<RegionHead, LeaseStoreFailure>
|
|
publish_region_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
|
|
std::span<const std::uint8_t> decisions, milliseconds timeout) {
|
|
const std::array<std::string, 2> p{bytea(manifest), bytea(decisions)};
|
|
auto rows = db.query("SELECT * FROM evolution.publish_region_checkpoint($1::bytea, $2::bytea)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
auto head = parse_head(*rows);
|
|
if (!head)
|
|
return std::unexpected{malformed(true)};
|
|
return *head;
|
|
}
|
|
|
|
std::expected<ResolvedCommit, LeaseStoreFailure>
|
|
resolve_region_commit(Postgres& db, RegionKey key, const Uuid& commit,
|
|
std::span<const std::uint8_t> manifest,
|
|
std::span<const std::uint8_t> decisions, milliseconds timeout) {
|
|
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(commit), bytea(manifest), bytea(decisions)};
|
|
auto rows = db.query("SELECT * FROM evolution.resolve_region_commit($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::bytea, $5::bytea)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 6)
|
|
return std::unexpected{malformed(false)};
|
|
const auto& status_text = (*rows)[0][0];
|
|
if (!status_text)
|
|
return std::unexpected{malformed(false)};
|
|
const auto status = *status_text;
|
|
if (status == "absent")
|
|
return ResolvedCommit{Resolution::absent, {}};
|
|
if (status == "conflict")
|
|
return ResolvedCommit{Resolution::conflict, {}};
|
|
const PgRows receipt{
|
|
std::vector<std::optional<std::string>>((*rows)[0].begin() + 1, (*rows)[0].end())};
|
|
auto head = status == "committed" ? parse_head(receipt) : std::nullopt;
|
|
if (!head)
|
|
return std::unexpected{malformed(false)};
|
|
return ResolvedCommit{Resolution::committed, *head};
|
|
}
|
|
|
|
std::expected<RegionHead, LeaseStoreFailure>
|
|
publish_region_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
|
|
std::span<const std::uint8_t> decisions,
|
|
std::span<const std::uint8_t> actions, milliseconds timeout) {
|
|
const std::array<std::string, 3> p{bytea(manifest), bytea(decisions), bytea(actions)};
|
|
auto rows = db.query(
|
|
"SELECT * FROM evolution.publish_region_checkpoint($1::bytea, $2::bytea, $3::bytea)", p,
|
|
timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
auto head = parse_head(*rows);
|
|
if (!head)
|
|
return std::unexpected{malformed(true)};
|
|
return *head;
|
|
}
|
|
|
|
std::expected<RegionHead, LeaseStoreFailure>
|
|
publish_cellular_checkpoint(Postgres& db, std::span<const std::uint8_t> manifest,
|
|
std::span<const std::uint8_t> decisions,
|
|
std::span<const std::uint8_t> actions,
|
|
std::span<const std::uint8_t> trace, milliseconds timeout) {
|
|
const std::array<std::string, 4> p{bytea(manifest), bytea(decisions), bytea(actions),
|
|
bytea(trace)};
|
|
auto rows = db.query(
|
|
"SELECT * FROM evolution.publish_cellular_checkpoint($1::bytea, $2::bytea, $3::bytea, "
|
|
"$4::bytea)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
auto head = parse_head(*rows);
|
|
if (!head)
|
|
return std::unexpected{malformed(true)};
|
|
return *head;
|
|
}
|
|
|
|
std::expected<RegionHead, LeaseStoreFailure>
|
|
publish_cellular_profile_migration(Postgres& db, std::span<const std::uint8_t> manifest,
|
|
milliseconds timeout) {
|
|
const std::array<std::string, 1> parameters{bytea(manifest)};
|
|
auto rows = db.query("SELECT * FROM evolution.publish_cellular_profile_migration($1::bytea)",
|
|
parameters, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
const auto head = parse_head(*rows);
|
|
if (!head)
|
|
return std::unexpected{malformed(true)};
|
|
return *head;
|
|
}
|
|
|
|
std::expected<std::optional<CellularProfileMigrationRecord>, LeaseStoreFailure>
|
|
load_cellular_profile_migration(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
const Uuid& commit, milliseconds timeout) {
|
|
const std::array<std::string, 5> parameters{std::to_string(key.world),
|
|
std::to_string(key.region), uuid_text(lease.holder),
|
|
std::to_string(lease.epoch), uuid_text(commit)};
|
|
auto rows = db.query(
|
|
"SELECT source_sequence, source_commit, encode(source_manifest_sha256, 'hex'), "
|
|
"encode(source_payload_sha256, 'hex'), encode(source_versions, 'hex'), target_sequence, "
|
|
"target_commit, encode(target_manifest_sha256, 'hex'), "
|
|
"encode(target_payload_sha256, 'hex'), encode(target_versions, 'hex') "
|
|
"FROM evolution.load_cellular_profile_migration($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)",
|
|
parameters, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->empty())
|
|
return std::optional<CellularProfileMigrationRecord>{};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 10)
|
|
return std::unexpected{malformed(false)};
|
|
const auto& row = (*rows)[0];
|
|
const auto source_sequence = parse_integer<std::uint64_t>(row[0]);
|
|
const auto source_commit = parse_uuid(row[1]);
|
|
const auto source_manifest = row[2] ? unhex<32>(*row[2]) : std::nullopt;
|
|
const auto source_payload = row[3] ? unhex<32>(*row[3]) : std::nullopt;
|
|
const auto source_versions = row[4] ? unhex_bytes(*row[4]) : std::nullopt;
|
|
const auto target_sequence = parse_integer<std::uint64_t>(row[5]);
|
|
const auto target_commit = parse_uuid(row[6]);
|
|
const auto target_manifest = row[7] ? unhex<32>(*row[7]) : std::nullopt;
|
|
const auto target_payload = row[8] ? unhex<32>(*row[8]) : std::nullopt;
|
|
const auto target_versions = row[9] ? unhex_bytes(*row[9]) : std::nullopt;
|
|
if (!source_sequence || !source_commit || !source_manifest || !source_payload ||
|
|
!source_versions || source_versions->size() != 36 || !target_sequence || !target_commit ||
|
|
!target_manifest || !target_payload || !target_versions || target_versions->size() != 36 ||
|
|
*source_sequence == std::numeric_limits<std::uint64_t>::max() ||
|
|
*target_sequence != *source_sequence + 1)
|
|
return std::unexpected{malformed(false)};
|
|
return std::optional<CellularProfileMigrationRecord>{CellularProfileMigrationRecord{
|
|
*source_sequence, *source_commit, *source_manifest, *source_payload,
|
|
std::move(*source_versions), *target_sequence, *target_commit, *target_manifest,
|
|
*target_payload, std::move(*target_versions)}};
|
|
}
|
|
|
|
std::expected<ResolvedCommit, LeaseStoreFailure>
|
|
resolve_region_commit(Postgres& db, RegionKey key, const Uuid& commit,
|
|
std::span<const std::uint8_t> manifest,
|
|
std::span<const std::uint8_t> decisions,
|
|
std::span<const std::uint8_t> actions, milliseconds timeout) {
|
|
const std::array<std::string, 6> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(commit), bytea(manifest),
|
|
bytea(decisions), bytea(actions)};
|
|
auto rows = db.query("SELECT * FROM evolution.resolve_region_commit($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::bytea, $5::bytea, $6::bytea)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 6)
|
|
return std::unexpected{malformed(false)};
|
|
const auto& status_text = (*rows)[0][0];
|
|
if (!status_text)
|
|
return std::unexpected{malformed(false)};
|
|
const auto status = *status_text;
|
|
if (status == "absent")
|
|
return ResolvedCommit{Resolution::absent, {}};
|
|
if (status == "conflict")
|
|
return ResolvedCommit{Resolution::conflict, {}};
|
|
const PgRows receipt{
|
|
std::vector<std::optional<std::string>>((*rows)[0].begin() + 1, (*rows)[0].end())};
|
|
auto head = status == "committed" ? parse_head(receipt) : std::nullopt;
|
|
if (!head)
|
|
return std::unexpected{malformed(false)};
|
|
return ResolvedCommit{Resolution::committed, *head};
|
|
}
|
|
|
|
std::expected<ResolvedCommit, LeaseStoreFailure> resolve_cellular_region_commit(
|
|
Postgres& db, RegionKey key, const Uuid& commit, std::span<const std::uint8_t> manifest,
|
|
std::span<const std::uint8_t> decisions, std::span<const std::uint8_t> actions,
|
|
std::span<const std::uint8_t> trace, milliseconds timeout) {
|
|
const std::array<std::string, 7> p{std::to_string(key.world),
|
|
std::to_string(key.region),
|
|
uuid_text(commit),
|
|
bytea(manifest),
|
|
bytea(decisions),
|
|
bytea(actions),
|
|
bytea(trace)};
|
|
auto rows =
|
|
db.query("SELECT * FROM evolution.resolve_cellular_region_commit($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::bytea, $5::bytea, $6::bytea, $7::bytea)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 6 || !(*rows)[0][0])
|
|
return std::unexpected{malformed(false)};
|
|
const auto& status = *(*rows)[0][0];
|
|
if (status == "absent")
|
|
return ResolvedCommit{Resolution::absent, {}};
|
|
if (status == "conflict")
|
|
return ResolvedCommit{Resolution::conflict, {}};
|
|
const PgRows receipt{
|
|
std::vector<std::optional<std::string>>((*rows)[0].begin() + 1, (*rows)[0].end())};
|
|
auto head = status == "committed" ? parse_head(receipt) : std::nullopt;
|
|
if (!head)
|
|
return std::unexpected{malformed(false)};
|
|
return ResolvedCommit{Resolution::committed, *head};
|
|
}
|
|
|
|
std::expected<std::optional<std::vector<std::uint8_t>>, LeaseStoreFailure>
|
|
load_cellular_publication_actions(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
const Uuid& commit, milliseconds timeout) {
|
|
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(lease.holder), std::to_string(lease.epoch),
|
|
uuid_text(commit)};
|
|
auto rows = db.query("SELECT encode(actions, 'hex') "
|
|
"FROM evolution.load_cellular_publication_actions($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->empty())
|
|
return std::optional<std::vector<std::uint8_t>>{};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 1 || !(*rows)[0][0])
|
|
return std::unexpected{malformed(false)};
|
|
auto actions = unhex_bytes(*(*rows)[0][0]);
|
|
if (!actions)
|
|
return std::unexpected{malformed(false)};
|
|
return std::optional<std::vector<std::uint8_t>>{std::move(*actions)};
|
|
}
|
|
|
|
std::expected<std::vector<CellularBirthBinding>, LeaseStoreFailure>
|
|
load_cellular_publication_births(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
const Uuid& commit, milliseconds timeout) {
|
|
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(lease.holder), std::to_string(lease.epoch),
|
|
uuid_text(commit)};
|
|
auto rows =
|
|
db.query("SELECT account_id::text, request_id::text, identity::text, born::text, "
|
|
"commit_sequence::text FROM evolution.load_cellular_publication_births("
|
|
"$1::evolution.u64, $2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
std::vector<CellularBirthBinding> bindings;
|
|
bindings.reserve(rows->size());
|
|
for (const auto& row : *rows) {
|
|
if (row.size() != 5)
|
|
return std::unexpected{malformed(false)};
|
|
const auto account = parse_uuid(row[0]);
|
|
const auto request = parse_integer<std::uint64_t>(row[1]);
|
|
const auto identity = parse_integer<std::uint64_t>(row[2]);
|
|
const auto born = parse_integer<std::uint64_t>(row[3]);
|
|
const auto sequence = parse_integer<std::uint64_t>(row[4]);
|
|
if (!account || !request || !identity || !born || !sequence)
|
|
return std::unexpected{malformed(false)};
|
|
bindings.push_back({*account, commit, *request, *identity, *born, *sequence});
|
|
}
|
|
return bindings;
|
|
}
|
|
|
|
std::expected<std::optional<CommittedAction>, LeaseStoreFailure>
|
|
find_region_action(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t action_id, milliseconds timeout) {
|
|
const std::array<std::string, 6> 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(action_id)};
|
|
auto rows = db.query("SELECT * FROM evolution.find_region_action($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, "
|
|
"$6::evolution.u64)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->empty())
|
|
return std::optional<CommittedAction>{};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 3)
|
|
return std::unexpected{malformed(false)};
|
|
const auto region = parse_integer<std::uint64_t>((*rows)[0][0]);
|
|
const auto tick = parse_integer<std::uint64_t>((*rows)[0][1]);
|
|
const auto sequence = parse_integer<std::uint64_t>((*rows)[0][2]);
|
|
if (!region || !tick || !sequence)
|
|
return std::unexpected{malformed(false)};
|
|
return std::optional<CommittedAction>{CommittedAction{*region, *tick, *sequence}};
|
|
}
|
|
|
|
std::expected<std::optional<CommittedActionRecord>, LeaseStoreFailure>
|
|
find_region_action_record(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t action_id, milliseconds timeout) {
|
|
const std::array<std::string, 6> 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(action_id)};
|
|
auto rows = db.query("SELECT action_region, action_tick, action_commit, action_kind, "
|
|
"action_result, encode(action_input, 'hex') "
|
|
"FROM evolution.find_region_action_record($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, "
|
|
"$6::evolution.u64)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->empty())
|
|
return std::optional<CommittedActionRecord>{};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 6)
|
|
return std::unexpected{malformed(false)};
|
|
const auto record = parse_action_record((*rows)[0], 0);
|
|
if (!record)
|
|
return std::unexpected{malformed(false)};
|
|
return std::optional<CommittedActionRecord>{*record};
|
|
}
|
|
|
|
std::expected<std::vector<std::optional<CommittedAction>>, LeaseStoreFailure>
|
|
find_region_actions(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
std::span<const ActionRef> actions, milliseconds timeout) {
|
|
std::vector<std::optional<CommittedAction>> out(actions.size());
|
|
if (actions.empty())
|
|
return out;
|
|
// Array literals of canonical uuids and decimal ids: no quoting is needed.
|
|
std::string accounts = "{", ids = "{";
|
|
for (std::size_t i = 0; i < actions.size(); ++i) {
|
|
accounts += (i == 0 ? "" : ",") + uuid_text(actions[i].account);
|
|
ids += (i == 0 ? "" : ",") + std::to_string(actions[i].action_id);
|
|
}
|
|
accounts += "}";
|
|
ids += "}";
|
|
const std::array<std::string, 6> p{std::to_string(key.world),
|
|
std::to_string(key.region),
|
|
uuid_text(lease.holder),
|
|
std::to_string(lease.epoch),
|
|
accounts,
|
|
ids};
|
|
// The schema v11 function per pair (its lease check included); rows name their pair.
|
|
auto rows = db.query("SELECT x.n, f.action_region, f.action_tick, f.action_commit "
|
|
"FROM unnest($5::uuid[], $6::numeric[]) WITH ORDINALITY AS x(a, i, n) "
|
|
"CROSS JOIN LATERAL evolution.find_region_action($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, x.a, "
|
|
"x.i::evolution.u64) AS f",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
for (const auto& r : *rows) {
|
|
if (r.size() != 4)
|
|
return std::unexpected{malformed(false)};
|
|
const auto n = parse_integer<std::uint64_t>(r[0]);
|
|
const auto region = parse_integer<std::uint64_t>(r[1]);
|
|
const auto tick = parse_integer<std::uint64_t>(r[2]);
|
|
const auto sequence = parse_integer<std::uint64_t>(r[3]);
|
|
if (!n || *n == 0 || *n > actions.size() || out[*n - 1].has_value() || !region || !tick ||
|
|
!sequence)
|
|
return std::unexpected{malformed(false)};
|
|
out[*n - 1] = CommittedAction{*region, *tick, *sequence};
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::vector<std::optional<CommittedActionRecord>>, LeaseStoreFailure>
|
|
find_region_action_records(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
std::span<const ActionRef> actions, milliseconds timeout) {
|
|
std::vector<std::optional<CommittedActionRecord>> out(actions.size());
|
|
if (actions.empty())
|
|
return out;
|
|
std::string accounts = "{", ids = "{";
|
|
for (std::size_t i = 0; i < actions.size(); ++i) {
|
|
accounts += (i == 0 ? "" : ",") + uuid_text(actions[i].account);
|
|
ids += (i == 0 ? "" : ",") + std::to_string(actions[i].action_id);
|
|
}
|
|
accounts += "}";
|
|
ids += "}";
|
|
const std::array<std::string, 6> p{std::to_string(key.world),
|
|
std::to_string(key.region),
|
|
uuid_text(lease.holder),
|
|
std::to_string(lease.epoch),
|
|
accounts,
|
|
ids};
|
|
auto rows = db.query("SELECT x.n, f.action_region, f.action_tick, f.action_commit, "
|
|
"f.action_kind, f.action_result, encode(f.action_input, 'hex') "
|
|
"FROM unnest($5::uuid[], $6::numeric[]) WITH ORDINALITY AS x(a, i, n) "
|
|
"CROSS JOIN LATERAL evolution.find_region_action_record("
|
|
"$1::evolution.u64, $2::evolution.u64, $3::uuid, $4::evolution.u64, "
|
|
"x.a, x.i::evolution.u64) AS f",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
for (const auto& row : *rows) {
|
|
if (row.size() != 7)
|
|
return std::unexpected{malformed(false)};
|
|
const auto ordinal = parse_integer<std::uint64_t>(row[0]);
|
|
const auto record = parse_action_record(row, 1);
|
|
if (!ordinal || *ordinal == 0 || *ordinal > actions.size() ||
|
|
out[*ordinal - 1].has_value() || !record)
|
|
return std::unexpected{malformed(false)};
|
|
out[*ordinal - 1] = *record;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
namespace {
|
|
std::expected<std::vector<CommittedDecision>, LeaseStoreFailure>
|
|
decisions_from(const PgRows& rows) {
|
|
std::vector<CommittedDecision> out;
|
|
out.reserve(rows.size());
|
|
for (const auto& r : rows) {
|
|
if (r.size() != 5)
|
|
return std::unexpected{malformed(false)};
|
|
const auto sequence = parse_integer<std::uint64_t>(r[0]);
|
|
const auto tick = parse_integer<std::uint64_t>(r[1]);
|
|
const auto actor = parse_integer<std::uint32_t>(r[2]);
|
|
const auto outcome = parse_integer<std::uint32_t>(r[3]);
|
|
const auto organism = parse_integer<std::uint64_t>(r[4]);
|
|
if (!sequence || !tick || !actor || !outcome || !organism || *actor > 1 || *outcome != 1)
|
|
return std::unexpected{malformed(false)};
|
|
out.push_back({*sequence, {*tick, static_cast<Actor>(*actor), Outcome::death, *organism}});
|
|
}
|
|
return out;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<std::vector<CommittedDecision>, LeaseStoreFailure>
|
|
load_region_decisions(Postgres& db, RegionKey key, milliseconds timeout) {
|
|
const std::array<std::string, 2> p{std::to_string(key.world), std::to_string(key.region)};
|
|
auto rows = db.query(
|
|
"SELECT * FROM evolution.load_region_decisions($1::evolution.u64, $2::evolution.u64)", p,
|
|
timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
return decisions_from(*rows);
|
|
}
|
|
|
|
std::expected<std::vector<CommittedDecision>, LeaseStoreFailure>
|
|
pending_region_outbox(Postgres& db, RegionKey key, std::uint32_t limit, milliseconds timeout) {
|
|
const std::array<std::string, 3> p{std::to_string(key.world), std::to_string(key.region),
|
|
std::to_string(limit)};
|
|
auto rows = db.query("SELECT * FROM evolution.pending_region_outbox($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::integer)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
return decisions_from(*rows);
|
|
}
|
|
|
|
std::expected<bool, LeaseStoreFailure> acknowledge_region_outbox(Postgres& db, RegionKey key,
|
|
const BatchDecision& d,
|
|
milliseconds timeout) {
|
|
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
|
std::to_string(static_cast<std::uint32_t>(d.outcome)),
|
|
std::to_string(static_cast<std::uint32_t>(d.actor)),
|
|
std::to_string(d.tick)};
|
|
auto rows = db.query("SELECT evolution.acknowledge_region_outbox($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::integer, $4::integer, $5::evolution.u64)::text",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{classify(rows.error())};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 1)
|
|
return std::unexpected{malformed(true)};
|
|
const auto& marked = (*rows)[0][0];
|
|
if (!marked)
|
|
return std::unexpected{malformed(true)};
|
|
return *marked == "true";
|
|
}
|
|
|
|
// Schema v10 account control (ADR-0091). Shares this file's parsing and SQLSTATE rules.
|
|
namespace {
|
|
ControlStoreFailure control_failure(const PgFailure& f) {
|
|
const std::string_view state(f.sqlstate.data(), 5);
|
|
auto kind = ControlStoreError::store;
|
|
if (f.kind == PgError::unknown)
|
|
kind = ControlStoreError::unknown;
|
|
else if (f.kind == PgError::config || f.kind == PgError::bounds || state == "22023" ||
|
|
state == "EVL04")
|
|
kind = ControlStoreError::invalid;
|
|
else if (state == "EVL11")
|
|
kind = ControlStoreError::stale_grant;
|
|
else if (state == "EVL02" || state == "EVL06" || state == "EVL12")
|
|
kind = ControlStoreError::fenced;
|
|
else if (state == "EVL05")
|
|
kind = ControlStoreError::exhausted;
|
|
return {kind, f};
|
|
}
|
|
std::array<std::string, 6> control_parameters(RegionKey key, const LeaseGrant& lease,
|
|
const Uuid& account, std::uint64_t value) {
|
|
return {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(value)};
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<AccountControlClaim, ControlStoreFailure>
|
|
claim_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t serial, std::uint64_t session, milliseconds duration,
|
|
milliseconds timeout) {
|
|
const auto base = control_parameters(key, lease, account, serial);
|
|
const std::array<std::string, 8> p{base[0],
|
|
base[1],
|
|
base[2],
|
|
base[3],
|
|
base[4],
|
|
base[5],
|
|
std::to_string(session),
|
|
std::to_string(duration.count())};
|
|
auto rows = db.query("SELECT * FROM evolution.claim_account_control($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{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<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) {
|
|
const auto base = control_parameters(key, lease, account, generation);
|
|
const std::array<std::string, 7> p{
|
|
base[0], base[1], base[2], base[3], base[4], base[5], std::to_string(duration.count())};
|
|
auto rows = db.query("SELECT evolution.renew_account_control($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, "
|
|
"$6::evolution.u64, $7::integer)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{control_failure(rows.error())};
|
|
const auto granted = rows->size() == 1 && (*rows)[0].size() == 1
|
|
? parse_integer<std::int64_t>((*rows)[0][0])
|
|
: std::nullopt;
|
|
if (!granted)
|
|
return std::unexpected{ControlStoreFailure{ControlStoreError::unknown, {}}};
|
|
return milliseconds{*granted};
|
|
}
|
|
|
|
std::expected<bool, ControlStoreFailure>
|
|
release_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t generation, milliseconds timeout) {
|
|
const auto p = control_parameters(key, lease, account, generation);
|
|
auto rows = db.query("SELECT evolution.release_account_control($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, "
|
|
"$6::evolution.u64)::text",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{control_failure(rows.error())};
|
|
const auto text = rows->size() == 1 && (*rows)[0].size() == 1 ? (*rows)[0][0] : std::nullopt;
|
|
if (text != "true" && text != "false")
|
|
return std::unexpected{ControlStoreFailure{ControlStoreError::unknown, {}}};
|
|
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 > 4)
|
|
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);
|
|
}
|
|
|
|
std::expected<std::vector<PrimitiveLifeBinding>, LifeReservationFailure>
|
|
load_primitive_lives(Postgres& db, RegionKey key, milliseconds timeout) {
|
|
const std::array<std::string, 2> p{std::to_string(key.world), std::to_string(key.region)};
|
|
auto rows = db.query("SELECT * FROM evolution.load_primitive_lives($1::evolution.u64, "
|
|
"$2::evolution.u64)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{life_failure(rows.error())};
|
|
if (rows->size() > 256)
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}};
|
|
std::vector<PrimitiveLifeBinding> out;
|
|
out.reserve(rows->size());
|
|
for (const auto& row : *rows) {
|
|
if (row.size() != 6)
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}};
|
|
const auto account = parse_uuid(row[0]);
|
|
const auto request = parse_integer<std::uint64_t>(row[1]);
|
|
const auto identity = parse_integer<std::uint64_t>(row[2]);
|
|
const auto born = parse_integer<std::uint64_t>(row[3]);
|
|
const auto commit = parse_uuid(row[4]);
|
|
const auto sequence = parse_integer<std::uint64_t>(row[5]);
|
|
if (!account || !request || *request == 0 || !identity || *identity == 0 || !born ||
|
|
*born == 0 || !commit || !sequence)
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}};
|
|
out.push_back({*account, *request, *identity, *born, *commit, *sequence});
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::optional<PrimitiveLifeOutcome>, LifeReservationFailure>
|
|
query_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t generation, std::uint64_t request_id, milliseconds timeout) {
|
|
const auto base = control_parameters(key, lease, account, generation);
|
|
const std::array<std::string, 7> p{
|
|
base[0], base[1], base[2], base[3], base[4], base[5], std::to_string(request_id)};
|
|
auto rows = db.query("SELECT * FROM evolution.query_primitive_life($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, "
|
|
"$6::evolution.u64, $7::evolution.u64)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{life_failure(rows.error())};
|
|
if (rows->empty())
|
|
return std::optional<PrimitiveLifeOutcome>{};
|
|
const auto& row = (*rows)[0];
|
|
if (rows->size() != 1 || row.size() != 6)
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}};
|
|
const auto identity = parse_integer<std::uint64_t>(row[0]);
|
|
const auto region = parse_integer<std::uint64_t>(row[1]);
|
|
const auto refusal = parse_integer<std::uint32_t>(row[2]);
|
|
if (!identity || *identity == 0 || !region || *region == 0 || !refusal || *refusal > 4)
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}};
|
|
PrimitiveLifeOutcome out{
|
|
LifeReservation{*identity, *region, static_cast<LifeRefusal>(*refusal)}, std::nullopt};
|
|
if (row[3] || row[4] || row[5]) {
|
|
const auto born = parse_integer<std::uint64_t>(row[3]);
|
|
const auto commit = parse_uuid(row[4]);
|
|
const auto sequence = parse_integer<std::uint64_t>(row[5]);
|
|
if (!born || *born == 0 || !commit || !sequence || *sequence == 0)
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}};
|
|
out.published =
|
|
PrimitiveLifeBinding{account, request_id, *identity, *born, *commit, *sequence};
|
|
}
|
|
return std::optional<PrimitiveLifeOutcome>{out};
|
|
}
|
|
|
|
std::expected<std::optional<std::uint64_t>, LifeReservationFailure>
|
|
primitive_life_owner(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
milliseconds timeout) {
|
|
if (key.world == 0 || key.region == 0 || lease.holder == Uuid{} || lease.epoch == 0 ||
|
|
account == Uuid{})
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::invalid, {}}};
|
|
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(lease.holder), std::to_string(lease.epoch),
|
|
uuid_text(account)};
|
|
auto rows = db.query("SELECT * FROM evolution.primitive_life_owner($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{life_failure(rows.error())};
|
|
if (rows->empty())
|
|
return std::optional<std::uint64_t>{};
|
|
if (rows->size() != 1 || (*rows)[0].size() != 1)
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}};
|
|
const auto owner = parse_integer<std::uint64_t>((*rows)[0][0]);
|
|
if (!owner || *owner == 0)
|
|
return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}};
|
|
return std::optional<std::uint64_t>{*owner};
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
std::expected<std::vector<IncomingTransfer>, ProbeStoreFailure>
|
|
incoming_probe_transfers(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
milliseconds timeout) {
|
|
const std::array<std::string, 4> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(lease.holder), std::to_string(lease.epoch)};
|
|
auto rows = db.query("SELECT * FROM evolution.incoming_probe_transfers($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{probe_failure(rows.error())};
|
|
std::vector<IncomingTransfer> out;
|
|
for (const auto& r : *rows) {
|
|
if (r.size() != 6)
|
|
return std::unexpected{ProbeStoreFailure{ProbeStoreError::unknown, {}}};
|
|
const auto transfer = parse_uuid(r[0]);
|
|
const auto account = parse_uuid(r[1]);
|
|
const auto source = parse_integer<std::uint64_t>(r[2]);
|
|
const auto x = parse_signed(r[3]);
|
|
const auto y = parse_signed(r[4]);
|
|
const auto z = parse_signed(r[5]);
|
|
if (!transfer || !account || !source || !x || !y || !z)
|
|
return std::unexpected{ProbeStoreFailure{ProbeStoreError::unknown, {}}};
|
|
out.push_back({*transfer, *account, *source, *x, *y, *z});
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::vector<Uuid>, ProbeStoreFailure>
|
|
overdue_probe_transfers(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
std::chrono::seconds age, milliseconds timeout) {
|
|
if (age.count() < 1 || age.count() > 86400)
|
|
return std::unexpected{ProbeStoreFailure{ProbeStoreError::invalid, {}}};
|
|
const std::array<std::string, 5> p{std::to_string(key.world), std::to_string(key.region),
|
|
uuid_text(lease.holder), std::to_string(lease.epoch),
|
|
std::to_string(age.count())};
|
|
auto rows = db.query("SELECT * FROM evolution.overdue_probe_transfers($1::evolution.u64, "
|
|
"$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::integer)",
|
|
p, timeout);
|
|
if (!rows)
|
|
return std::unexpected{probe_failure(rows.error())};
|
|
std::vector<Uuid> out;
|
|
for (const auto& r : *rows) {
|
|
const auto transfer = r.size() == 1 ? parse_uuid(r[0]) : std::nullopt;
|
|
if (!transfer)
|
|
return std::unexpected{ProbeStoreFailure{ProbeStoreError::unknown, {}}};
|
|
out.push_back(*transfer);
|
|
}
|
|
return out;
|
|
}
|
|
} // namespace evo::persistence
|