Files
aevolution/include/evo/persistence/account_control_store.hpp
T
dingoandClaude Opus 5.5 890c57a77f
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m50s
Foundation / checks (network) (push) Successful in 9m8s
Foundation / checks (client) (push) Successful in 12m10s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 7m33s
Foundation / checks (checks) (push) Canceled after 18m49s
Foundation / checks (tsan) (push) Canceled after 6m38s
Add M10.3c durable per-world account control (schema v10)
Migration v10 keeps, per world and account, the highest accepted grant serial and
the region session in control with a fencing generation and database-time expiry,
reachable only through claim/renew/release functions under the caller's region
lease. Serials survive restarts and apply across regions; a cross-region takeover
returns a wait so control never overlaps. RegionSessions takes durable control via
a ControlLedger before admitting a session, delays Welcome by the wait, renews, and
closes sessions whose control is fenced or cannot be confirmed in time.
PostgresControlLedger implements the ledger. ADR-0091.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 21:50:21 -04:00

47 lines
2.5 KiB
C++

#pragma once
#include "evo/persistence/postgres.hpp"
#include "evo/persistence/region_lease.hpp"
#include <chrono>
#include <cstdint>
#include <expected>
// 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<AccountControlClaim, ControlStoreFailure>
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<std::chrono::milliseconds, ControlStoreFailure>
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);
// True when released, false when control had already moved on.
[[nodiscard]] std::expected<bool, ControlStoreFailure>
release_account_control(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
std::uint64_t generation, std::chrono::milliseconds timeout);
} // namespace evo::persistence