#include "evo/persistence/population_interval_store.hpp" #include #include namespace evo::persistence { namespace { LeaseStoreFailure failure(const PgFailure& cause) { const std::string_view state(cause.sqlstate.data(), 5); auto kind = LeaseStoreError::store; if (cause.kind == PgError::unknown) kind = LeaseStoreError::unknown; else if (cause.kind == PgError::config || cause.kind == PgError::bounds || state == "22023") kind = LeaseStoreError::invalid; 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" || state == "EVL18") kind = LeaseStoreError::commit_conflict; return {kind, cause}; } std::string hex(std::span bytes) { constexpr std::string_view digits = "0123456789abcdef"; std::string out; for (const auto b : bytes) { out += digits[b >> 4U]; out += digits[b & 15U]; } return out; } std::string uuid(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); } template std::optional> unhex(std::string_view value) { if (value.size() != N * 2) return std::nullopt; std::array out{}; for (std::size_t i = 0; i < N; ++i) { unsigned byte{}; const auto [end, error] = std::from_chars(value.data() + i * 2, value.data() + i * 2 + 2, byte, 16); if (error != std::errc{} || end != value.data() + i * 2 + 2 || byte > 255) return std::nullopt; out[i] = static_cast(byte); } return out; } std::optional number(std::string_view value) { std::uint64_t out{}; const auto [end, error] = std::from_chars(value.data(), value.data() + value.size(), out); if (error != std::errc{} || end != value.data() + value.size()) return std::nullopt; return out; } std::optional parse(const PgRows::value_type& row) { if (row.size() != 5) return std::nullopt; const auto text = row[0].value_or(""); if (text.size() != 36 || text[8] != '-' || text[13] != '-' || text[18] != '-' || text[23] != '-') return std::nullopt; std::string compact; for (const auto c : text) if (c != '-') compact += c; const auto source = unhex<16>(compact); const auto digest = unhex<32>(row[1].value_or("")); const auto begin = number(row[2].value_or("")), end = number(row[3].value_or("")); if (!source || !digest || !begin || !end || *end <= *begin || *end - *begin > 64 || *source == Uuid{} || *digest == Sha256Digest{} || row[4].value_or("") != "1") return std::nullopt; return PopulationInterval{*source, *digest, *begin, *end, 1}; } constexpr auto columns = "source_commit::text,encode(source_manifest_sha256,'hex'),begin_tick::" "text,end_tick::text,controller_version::text"; } // namespace std::expected admit_population_interval(Postgres& db, RegionKey key, const LeaseGrant& lease, const RegionHead& source, std::uint64_t end_tick, std::chrono::milliseconds timeout) { const std::array parameters{ std::to_string(key.world), std::to_string(key.region), uuid(lease.holder), std::to_string(lease.epoch), uuid(source.commit), "\\x" + hex(source.digest), std::to_string(end_tick)}; auto rows = db.query(std::string("SELECT ") + columns + " FROM evolution.admit_population_interval(" "$1::evolution.u64,$2::evolution.u64,$3::uuid,$4::evolution.u64,$5::" "uuid,$6::bytea,$7::evolution.u64)", parameters, timeout); if (!rows) return std::unexpected(failure(rows.error())); const auto interval = rows->size() == 1 ? parse(rows->front()) : std::nullopt; if (!interval || interval->source_commit != source.commit || interval->source_manifest_sha256 != source.digest || interval->begin_tick != source.end_tick || interval->end_tick != end_tick) return std::unexpected(LeaseStoreFailure{LeaseStoreError::unknown, {}}); return *interval; } std::expected, LeaseStoreFailure> load_population_interval(Postgres& db, RegionKey key, const LeaseGrant& lease, std::chrono::milliseconds timeout) { const std::array parameters{std::to_string(key.world), std::to_string(key.region), uuid(lease.holder), std::to_string(lease.epoch)}; auto rows = db.query(std::string("SELECT ") + columns + " FROM evolution.load_population_interval(" "$1::evolution.u64,$2::evolution.u64,$3::uuid,$4::evolution.u64)", parameters, timeout); if (!rows) return std::unexpected(failure(rows.error())); if (rows->empty()) return std::optional{}; const auto interval = rows->size() == 1 ? parse(rows->front()) : std::nullopt; if (!interval) return std::unexpected(LeaseStoreFailure{LeaseStoreError::store, {}}); return interval; } } // namespace evo::persistence