#pragma once #include "evo/persistence/postgres.hpp" #include "evo/persistence/region_lease.hpp" #include #include #include // Linux-only calls to the fenced database functions of migration v2 (ADR-0060). // Each call is one statement on a caller-owned connection; outside an explicit // transaction it commits by itself. No retries. Rejections are ordinary results. namespace evo::persistence { enum class LeaseStoreError : std::uint8_t { busy, // Another holder's lease is unexpired in database time. fenced, // Holder/epoch superseded, released or expired: stop originating. head_mismatch, // Expected predecessor is not the committed head. not_provisioned, // No genesis; runtime can never create one. exhausted, // Epoch or commit sequence would wrap: fail closed. invalid, // Rejected arguments; nothing changed. store, // Other SQL/configuration failure; nothing committed. unknown, // May have executed; reconnect and reconcile before reuse. }; struct LeaseStoreFailure { LeaseStoreError kind{}; PgFailure cause{}; }; struct HeadAdvance { Uuid commit{}; Sha256Digest digest{}; std::uint64_t end_tick{}; }; [[nodiscard]] std::expected acquire_region_lease(Postgres& db, RegionKey key, std::chrono::milliseconds duration, std::chrono::milliseconds timeout); // Returns the granted duration; the caller measures it from its request send time. [[nodiscard]] std::expected renew_region_lease(Postgres& db, RegionKey key, const LeaseGrant& lease, std::chrono::milliseconds duration, std::chrono::milliseconds timeout); [[nodiscard]] std::expected release_region_lease(Postgres& db, RegionKey key, const LeaseGrant& lease, std::chrono::milliseconds timeout); [[nodiscard]] std::expected load_region_head(Postgres& db, RegionKey key, std::chrono::milliseconds timeout); // Locks the lease then the head; requires unexpired holder/epoch and the exact // expected predecessor. head_mismatch after unknown: load the head and compare. [[nodiscard]] std::expected advance_region_head(Postgres& db, RegionKey key, const LeaseGrant& lease, const RegionHead& expected, const HeadAdvance& next, std::chrono::milliseconds timeout); } // namespace evo::persistence