#include "evo/persistence/region_lease.hpp" #include #include #if defined(__linux__) #include #endif namespace evo::persistence { namespace { using std::chrono::milliseconds; constexpr milliseconds minimum_duration{1000}; constexpr milliseconds maximum_duration{60000}; constexpr milliseconds maximum_io{30000}; // Postgres adapter deadline bound. } // namespace bool valid(const LeasePolicy& p) noexcept { if (p.duration < minimum_duration || p.duration > maximum_duration) return false; // Bound every term by the (<=60s) duration before any arithmetic can overflow. for (const auto term : {p.renew_interval, p.safety_margin, p.renew_timeout, p.commit_timeout}) if (term <= milliseconds{} || term >= p.duration) return false; if (p.renew_timeout > maximum_io || p.commit_timeout > maximum_io) return false; const auto usable = p.duration - p.safety_margin; return p.safety_margin * 10 >= p.duration && (p.renew_interval + p.renew_timeout) * 2 <= usable && p.commit_timeout < usable; } LeaseClock::time_point LeaseClock::now() noexcept { #if defined(__linux__) timespec ts{}; // CLOCK_BOOTTIME cannot fail with a valid clock id and pointer. static_cast(clock_gettime(CLOCK_BOOTTIME, &ts)); return time_point{std::chrono::seconds{ts.tv_sec} + std::chrono::nanoseconds{ts.tv_nsec}}; #else // Storage hosts are Linux-only; elsewhere suspend behavior is unqualified. return time_point{ std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch())}; #endif } bool LeaseAuthority::acquired(const LeaseGrant& grant, LeaseClock::time_point sent, LeaseClock::time_point received) noexcept { // The duration equals the validated policy before it enters time arithmetic. if (!valid(policy_) || grant.epoch == 0 || grant.epoch <= highest_epoch_ || grant.frame == 0 || grant.duration != policy_.duration || received < sent) { fence(); return false; } const auto deadline = sent + grant.duration - policy_.safety_margin; if (received >= deadline) { fence(); return false; } grant_ = grant; highest_epoch_ = grant.epoch; last_sent_ = sent; deadline_ = deadline; fenced_ = false; return true; } bool LeaseAuthority::renewed(std::uint64_t epoch, milliseconds granted, LeaseClock::time_point sent, LeaseClock::time_point received) noexcept { if (!grant_ || fenced_ || epoch != grant_->epoch || granted != policy_.duration || sent < last_sent_ || received < sent || received >= deadline_) { fence(); return false; } // Database expiry is its execution time plus granted, which is after sent. deadline_ = sent + granted - policy_.safety_margin; last_sent_ = sent; return true; } void LeaseAuthority::renewal_unknown(milliseconds requested, LeaseClock::time_point sent) noexcept { if (!grant_ || fenced_) return; // The database rejects durations outside 1..60s, so such a request cannot have // changed expiry. A non-positive request is a caller error: fence. if (requested <= milliseconds{}) { fence(); return; } if (requested <= maximum_duration) deadline_ = std::min(deadline_, sent + requested - policy_.safety_margin); last_sent_ = std::max(last_sent_, sent); } void LeaseAuthority::fence() noexcept { fenced_ = true; } LeaseState LeaseAuthority::state(LeaseClock::time_point now) const noexcept { if (!grant_) return LeaseState::absent; if (fenced_ || now >= deadline_) return LeaseState::fenced; return now >= last_sent_ + policy_.renew_interval ? LeaseState::renew_due : LeaseState::active; } bool LeaseAuthority::may_originate(LeaseClock::time_point now) const noexcept { const auto s = state(now); return s == LeaseState::active || s == LeaseState::renew_due; } bool LeaseAuthority::may_begin_commit(LeaseClock::time_point now) const noexcept { return may_originate(now) && now + policy_.commit_timeout <= deadline_; } } // namespace evo::persistence