Add M10.3c durable per-world account control (schema v10)
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

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>
This commit is contained in:
dingo
2026-10-03 21:50:21 -04:00
co-authored by Claude Opus 5.5
parent db19e0bb85
commit 890c57a77f
23 changed files with 1085 additions and 55 deletions
+42
View File
@@ -2,6 +2,7 @@
#include "evo/network/admission.hpp"
#include "evo/world/chunks.hpp"
#include <array>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <expected>
@@ -94,6 +95,14 @@ class SessionControl {
[[nodiscard]] static std::expected<SessionControl, ControlError> create(ControlConfig config);
// Region-wide attempt limit, checked before any signature work. Counts the attempt.
[[nodiscard]] std::expected<void, ControlError> attempt(std::uint64_t now_seconds);
// Whether open() would accept the grant now, without changing anything (no attempt is
// counted). Lets the host take durable control before admitting the session locally.
[[nodiscard]] std::expected<void, ControlError> check(const VerifiedSessionGrant& grant,
std::uint64_t now_seconds) const;
// The id the next successful open() assigns.
[[nodiscard]] std::uint64_t next_session() const noexcept {
return next_session_;
}
// A newer grant for an account that already holds a session takes control: the old
// session is closed first and reported as superseded. Control is never shared.
[[nodiscard]] std::expected<OpenedSession, ControlError> open(const VerifiedSessionGrant& grant,
@@ -125,6 +134,9 @@ class SessionControl {
std::uint32_t attempts{};
};
explicit SessionControl(ControlConfig config) : config_{config} {}
[[nodiscard]] std::optional<ControlError> claims_error(const VerifiedSessionGrant& grant,
std::uint64_t now_seconds) const;
[[nodiscard]] bool forgettable(const Account& account, std::uint64_t now_seconds) const;
[[nodiscard]] std::expected<Account*, ControlError> admit(const VerifiedSessionGrant& grant,
std::uint64_t now_seconds);
void accept(Account& account, const GrantClaims& claims);
@@ -135,4 +147,34 @@ class SessionControl {
std::uint32_t region_attempts_{};
std::vector<Account> accounts_;
};
// Durable per-world account control (10.3c, schema v10). The region host implements it
// over its database lease; tests use fakes. Calls block for one statement each.
enum class ControlLedgerError : std::uint8_t {
stale_grant, // The world already accepted this or a newer serial for the account.
fenced, // This region's lease or this control generation is no longer current.
unavailable // Not decided (database unreachable, timeout, or an unknown outcome).
};
struct ControlLease {
std::uint64_t generation{};
// Another region still held control: do not act for the account before this passes.
std::chrono::milliseconds wait{};
std::chrono::milliseconds granted{}; // From the request send, after the wait.
};
class ControlLedger {
public:
ControlLedger() = default;
ControlLedger(const ControlLedger&) = delete;
ControlLedger& operator=(const ControlLedger&) = delete;
virtual ~ControlLedger() = default;
[[nodiscard]] virtual std::expected<ControlLease, ControlLedgerError>
claim(const AccountId& account, std::uint64_t serial, std::uint64_t session) = 0;
[[nodiscard]] virtual std::expected<std::chrono::milliseconds, ControlLedgerError>
renew(const AccountId& account, std::uint64_t generation) = 0;
virtual void release(const AccountId& account, std::uint64_t generation) = 0;
protected:
ControlLedger(ControlLedger&&) = default;
ControlLedger& operator=(ControlLedger&&) = default;
};
} // namespace evo::network
@@ -0,0 +1,46 @@
#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
@@ -0,0 +1,34 @@
#pragma once
#include "evo/network/session_control.hpp"
#include "evo/persistence/account_control_store.hpp"
#include <chrono>
#include <functional>
#include <optional>
#include <utility>
// M10.3c durable account control over the region's PostgreSQL lease (ADR-0091, schema v10).
// Linux only. Owned by the region host's thread, on the host's own connection. `lease`
// returns the host's current region lease, or nothing while it holds none (every call then
// reports `fenced`). Store failures that may have executed or are undecided report
// `unavailable`; the session layer then keeps or closes sessions by its own deadlines.
namespace evo::server {
class PostgresControlLedger final : public network::ControlLedger {
public:
using LeaseSource = std::function<std::optional<persistence::LeaseGrant>()>;
PostgresControlLedger(persistence::Postgres& db, persistence::RegionKey key, LeaseSource lease,
std::chrono::milliseconds duration = std::chrono::milliseconds{20000},
std::chrono::milliseconds timeout = std::chrono::milliseconds{2000})
: db_{&db}, key_{key}, lease_{std::move(lease)}, duration_{duration}, timeout_{timeout} {}
[[nodiscard]] std::expected<network::ControlLease, network::ControlLedgerError>
claim(const network::AccountId& account, std::uint64_t serial, std::uint64_t session) override;
[[nodiscard]] std::expected<std::chrono::milliseconds, network::ControlLedgerError>
renew(const network::AccountId& account, std::uint64_t generation) override;
void release(const network::AccountId& account, std::uint64_t generation) override;
private:
persistence::Postgres* db_;
persistence::RegionKey key_;
LeaseSource lease_;
std::chrono::milliseconds duration_, timeout_;
};
} // namespace evo::server
+9
View File
@@ -19,6 +19,9 @@
// account's previous connection with `superseded`. Refusals and closes send Close with a
// reason, then the connection is closed after a short flush. Intentions and views are
// later segments (10.4, 10.5). Events carry account log labels only, never grants.
// With a ControlLedger, control is durable and unique per world: a session that loses its
// durable control (another region accepted a newer grant) closes with `superseded`, and
// one whose control cannot be renewed in time closes with `shutdown`.
namespace evo::server {
struct IssuerKey {
std::uint32_t key_id{};
@@ -30,6 +33,12 @@ struct RegionSessionsConfig {
std::vector<IssuerKey> issuer_keys;
std::chrono::milliseconds hello_deadline{5000}; // Connected to Hello (ADR-0089).
std::chrono::milliseconds close_flush{1000}; // Close sent to connection closed.
// Durable per-world control (10.3c), owned by the host; null keeps control in memory
// only (tests, tools). With a ledger, a session is admitted only after its durable
// claim, waits out another region's control before Welcome, renews every
// `renew_interval` and closes before its control can lapse (`safety_margin`).
network::ControlLedger* ledger{};
std::chrono::milliseconds renew_interval{5000}, safety_margin{2000};
};
enum class RegionSessionsError : std::uint8_t { configuration, keys, transport };
struct RegionSessionEvent {