ADR-0060 and schema v2 add database-time region leases and a fenced head advance. Acquire/renew/release/advance lock the lease row, check holder, epoch and expiry with clock_timestamp() after the lock wait, and the advance compares the exact predecessor. Holders are DB-generated tokens; epochs never wrap or repeat. The runtime group role gets EXECUTE on five SECURITY DEFINER functions only - no table, provisioning or CREATE rights. Migration history admits clean->v2, v1->v2 and a v2 no-op, and refuses privileged runtime roles. Host side: portable LeaseAuthority fences from send-time deadlines on CLOCK_BOOTTIME, keeps the earlier deadline on unknown renewal and stays fenced until a newer epoch. Selected timings: 20s lease, 5s renew, 2s margin, 2s renew and 5s commit deadlines. Linux evo::postgres wraps the functions with typed LeaseStoreError results. Validated: Windows CTest 55/55; WSL Release, clang-tidy and ASan/UBSan private-PostgreSQL suites (contenders, expiry, paused/queued stale writers, unknown renewal, exhaustion, bypass denial, upgrade path). Three SQL mutations were each caught. Docker/Gitea CI, remote TLS and cluster runs were not executed. No director, host or deployment added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
2.5 KiB
C++
51 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 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<LeaseGrant, LeaseStoreFailure>
|
|
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<std::chrono::milliseconds, LeaseStoreFailure>
|
|
renew_region_lease(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
std::chrono::milliseconds duration, std::chrono::milliseconds timeout);
|
|
[[nodiscard]] std::expected<void, LeaseStoreFailure>
|
|
release_region_lease(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
std::chrono::milliseconds timeout);
|
|
[[nodiscard]] std::expected<RegionHead, LeaseStoreFailure>
|
|
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<RegionHead, LeaseStoreFailure>
|
|
advance_region_head(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
const RegionHead& expected, const HeadAdvance& next,
|
|
std::chrono::milliseconds timeout);
|
|
} // namespace evo::persistence
|