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>
134 lines
5.7 KiB
C++
134 lines
5.7 KiB
C++
#include "evo/persistence/region_lease.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
namespace {
|
|
using namespace evo::persistence;
|
|
using namespace std::chrono_literals;
|
|
using Time = LeaseClock::time_point;
|
|
void check(bool ok, std::source_location at = std::source_location::current()) {
|
|
if (!ok)
|
|
throw std::runtime_error("region lease line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
constexpr Time at(std::chrono::milliseconds t) {
|
|
return Time{t};
|
|
}
|
|
LeaseGrant grant(std::uint64_t epoch) {
|
|
return {{1}, epoch, 7, LeasePolicy{}.duration};
|
|
}
|
|
|
|
void policies() {
|
|
CHECK(valid(LeasePolicy{}));
|
|
CHECK(valid(LeasePolicy{1000ms, 200ms, 100ms, 100ms, 300ms}));
|
|
const auto invalid = [](auto change) {
|
|
LeasePolicy p;
|
|
change(p);
|
|
return !valid(p);
|
|
};
|
|
CHECK(invalid([](LeasePolicy& p) { p.duration = 999ms; }));
|
|
CHECK(invalid([](LeasePolicy& p) { p.duration = 60001ms; }));
|
|
CHECK(invalid([](LeasePolicy& p) { p.safety_margin = 1999ms; })); // under 10%
|
|
CHECK(invalid([](LeasePolicy& p) { p.safety_margin = 20000ms; }));
|
|
CHECK(invalid([](LeasePolicy& p) { p.renew_interval = 0ms; }));
|
|
CHECK(invalid([](LeasePolicy& p) { p.renew_timeout = -1ms; }));
|
|
CHECK(invalid([](LeasePolicy& p) { p.renew_timeout = 30001ms; }));
|
|
CHECK(invalid([](LeasePolicy& p) { p.commit_timeout = 18000ms; }));
|
|
// Two complete renewal attempts must fit before local fencing: 2*(7+2.5) > 18.
|
|
CHECK(invalid([](LeasePolicy& p) {
|
|
p.renew_interval = 7000ms;
|
|
p.renew_timeout = 2500ms;
|
|
}));
|
|
CHECK(!valid(LeasePolicy{2000ms, 400ms, 200ms, 200ms, 1800ms}));
|
|
LeaseAuthority broken(LeasePolicy{999ms, 200ms, 100ms, 100ms, 300ms});
|
|
CHECK(!broken.acquired({{1}, 1, 7, 999ms}, at(0ms), at(1ms)));
|
|
CHECK(broken.state(at(1ms)) == LeaseState::absent);
|
|
}
|
|
|
|
void deadlines_follow_send_time() {
|
|
LeaseAuthority a{LeasePolicy{}};
|
|
CHECK(a.state(at(0ms)) == LeaseState::absent && !a.may_originate(at(0ms)));
|
|
// Reply arrival does not move the deadline: the DB clock started after sending.
|
|
CHECK(a.acquired(grant(1), at(1000ms), at(4000ms)));
|
|
CHECK(a.deadline() == at(19000ms));
|
|
CHECK(a.state(at(5999ms)) == LeaseState::active);
|
|
CHECK(a.state(at(6000ms)) == LeaseState::renew_due && a.may_originate(at(6000ms)));
|
|
CHECK(a.may_begin_commit(at(14000ms)) && !a.may_begin_commit(at(14001ms)));
|
|
CHECK(a.may_originate(at(18999ms)));
|
|
CHECK(a.state(at(19000ms)) == LeaseState::fenced && !a.may_originate(at(19000ms)));
|
|
// A renewal answered after the local deadline cannot revive authority.
|
|
CHECK(!a.renewed(1, 20000ms, at(18000ms), at(19000ms)));
|
|
CHECK(a.state(at(18500ms)) == LeaseState::fenced);
|
|
}
|
|
|
|
void renewals() {
|
|
LeaseAuthority a{LeasePolicy{}};
|
|
CHECK(a.acquired(grant(4), at(0ms), at(10ms)));
|
|
CHECK(a.renewed(4, 20000ms, at(5000ms), at(5100ms)));
|
|
CHECK(a.deadline() == at(23000ms) && a.state(at(9999ms)) == LeaseState::active);
|
|
CHECK(a.state(at(10000ms)) == LeaseState::renew_due);
|
|
// Ambiguous renewal keeps the earlier bound; retries still fit before fencing.
|
|
a.renewal_unknown(20000ms, at(10000ms));
|
|
CHECK(a.deadline() == at(23000ms) && a.state(at(14999ms)) == LeaseState::active);
|
|
a.renewal_unknown(5000ms, at(15000ms)); // Could have shortened: take the earlier.
|
|
CHECK(a.deadline() == at(18000ms));
|
|
CHECK(a.renewed(4, 20000ms, at(16000ms), at(16500ms)) && a.deadline() == at(34000ms));
|
|
CHECK(!a.renewed(4, 20000ms, at(15000ms), at(16600ms))); // Out of order: fence.
|
|
CHECK(a.state(at(16600ms)) == LeaseState::fenced);
|
|
CHECK(!a.renewed(4, 20000ms, at(17000ms), at(17001ms))); // Sticky.
|
|
|
|
LeaseAuthority b{LeasePolicy{}};
|
|
CHECK(b.acquired(grant(2), at(0ms), at(1ms)));
|
|
CHECK(!b.renewed(3, 20000ms, at(1000ms), at(1001ms))); // Foreign epoch.
|
|
CHECK(b.state(at(1001ms)) == LeaseState::fenced);
|
|
LeaseAuthority c{LeasePolicy{}};
|
|
CHECK(c.acquired(grant(2), at(0ms), at(1ms)));
|
|
CHECK(!c.renewed(2, 19000ms, at(1000ms), at(1001ms))); // Unexpected duration.
|
|
LeaseAuthority d{LeasePolicy{}};
|
|
CHECK(d.acquired(grant(2), at(0ms), at(1ms)));
|
|
d.fence();
|
|
CHECK(d.state(at(2ms)) == LeaseState::fenced && !d.may_begin_commit(at(2ms)));
|
|
}
|
|
|
|
void epochs_are_never_reused() {
|
|
LeaseAuthority a{LeasePolicy{}};
|
|
CHECK(!a.acquired(grant(0), at(0ms), at(1ms)));
|
|
CHECK(!a.acquired({{1}, 1, 0, 20000ms}, at(0ms), at(1ms))); // Zero frame.
|
|
CHECK(!a.acquired({{1}, 1, 7, 10000ms}, at(0ms), at(1ms))); // Not the policy.
|
|
CHECK(!a.acquired(grant(1), at(10ms), at(9ms))); // Reply before send.
|
|
CHECK(!a.acquired(grant(1), at(0ms), at(18000ms))); // Too late to use.
|
|
CHECK(a.acquired(grant(3), at(0ms), at(1ms)));
|
|
CHECK(!a.acquired(grant(3), at(2ms), at(3ms)));
|
|
CHECK(a.state(at(3ms)) == LeaseState::fenced);
|
|
CHECK(!a.acquired(grant(2), at(4ms), at(5ms)));
|
|
// Only a strictly newer database epoch clears fencing (followed by recovery).
|
|
CHECK(a.acquired(grant(4), at(6ms), at(7ms)) && a.state(at(7ms)) == LeaseState::active);
|
|
const auto& current = a.grant();
|
|
CHECK(current && current->epoch == 4);
|
|
}
|
|
|
|
void clock_is_monotonic() {
|
|
const auto first = LeaseClock::now();
|
|
const auto second = LeaseClock::now();
|
|
CHECK(second >= first && first.time_since_epoch().count() > 0);
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
policies();
|
|
deadlines_follow_send_time();
|
|
renewals();
|
|
epochs_are_never_reused();
|
|
clock_is_monotonic();
|
|
std::puts("region lease contracts passed");
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::fprintf(stderr, "%s\n", e.what());
|
|
return 1;
|
|
}
|
|
}
|