#pragma once #include "evo/persistence/postgres.hpp" #include "evo/persistence/region_lease.hpp" #include #include #include // Linux-only calls to the durable account control functions of schema v10 (ADR-0091, // migrations/0010_account_control.sql). Each is one statement under the caller's region // lease: a fenced or expired lease refuses everything. No retries. namespace evo::persistence { enum class ControlStoreError : std::uint8_t { stale_grant, // A grant with this or a newer serial was already accepted in the world. fenced, // Region lease or this control generation is no longer current. exhausted, // Generations 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 ControlStoreFailure { ControlStoreError kind{}; PgFailure cause{}; }; struct AccountControlClaim { std::uint64_t generation{}; // Remaining database time of another region's (or epoch's) unexpired control. The // claimant must not act for the account before it passes (measured from its send). std::chrono::milliseconds wait{}; std::chrono::milliseconds granted{}; // Valid after the wait, from the request send. }; // Takes control of `account` in the lease's world for `session`, recording `serial` as the // world's newest accepted grant serial for the account. Duration 1..60 s. [[nodiscard]] std::expected claim_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, std::uint64_t serial, std::uint64_t session, 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_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, std::uint64_t generation, std::chrono::milliseconds duration, std::chrono::milliseconds timeout); // M10.8 (ADR-0096, schema v12): takes control of `account` for `session` on the strength of // probe transfer `transfer` committed to the caller's region (no serial: a handoff ticket // has none). Fenced unless that transfer is committed to this region for this account. [[nodiscard]] std::expected handoff_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, const Uuid& transfer, std::uint64_t session, std::chrono::milliseconds duration, std::chrono::milliseconds timeout); // True when released, false when control had already moved on. [[nodiscard]] std::expected release_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, std::uint64_t generation, std::chrono::milliseconds timeout); } // namespace evo::persistence