#include "evo/server/load_governor.hpp" #include #include namespace evo::server { std::expected LoadGovernor::create(LoadPolicy p) { if (p.relax_us == 0 || p.relax_us >= p.refuse_us || p.refuse_us > p.shed_us || p.shed_us > p.tick_budget_us || p.step_us == 0 || p.settle_us == 0 || p.max_shed > 3) return std::unexpected(LoadPolicyError::invalid); return LoadGovernor{p}; } namespace { using Window = std::array; // The first `n` samples sorted; nearest rank: the smallest sample with at least `percent` of // the window at or below it. Window sorted_of(const Window& w, std::size_t n) { Window sorted{}; std::copy_n(w.begin(), n, sorted.begin()); std::sort(sorted.begin(), sorted.begin() + static_cast(n)); return sorted; } std::uint64_t rank(const Window& sorted, std::size_t n, std::size_t percent) { return sorted.at((((n * percent) + 99) / 100) - 1); } } // namespace bool LoadGovernor::observe(std::uint64_t work_us, std::uint64_t session_us, std::uint64_t tick_debt, std::uint64_t now_us) { window_.at(next_) = work_us; session_window_.at(next_) = std::min(session_us, work_us); next_ = (next_ + 1) % window_.size(); state_.samples = std::min(state_.samples + 1, window_.size()); const auto n = state_.samples; const auto sorted = sorted_of(window_, n); state_.p95_us = rank(sorted, n, 95); state_.p99_us = rank(sorted, n, 99); state_.max_us = sorted.at(n - 1); const auto sessions = sorted_of(session_window_, n); state_.session_p95_us = rank(sessions, n, 95); state_.session_p99_us = rank(sessions, n, 99); const auto before = std::pair{state_.refusing, state_.shed}; // Work counts only over a full window: after a (re)start the first ticks (cold caches, the // first commits) would otherwise be the p95 alone (seen on evolution-dev, 10.9d). Tick // debt is acted on at once. const bool full = n == window_.size(); const bool hot = (full && state_.p95_us >= policy_.refuse_us) || tick_debt >= refuse_debt_ticks; const bool overloaded = (full && state_.p95_us >= policy_.shed_us) || tick_debt >= shed_debt_ticks; if (hot) state_.refusing = true; if (overloaded && state_.shed < policy_.max_shed && (!stepped_ || now_us - last_step_us_ >= policy_.step_us)) { ++state_.shed; stepped_ = true; last_step_us_ = now_us; } if (state_.p95_us >= policy_.relax_us || tick_debt > 0) { calm_ = false; } else if (!calm_) { calm_ = true; calm_since_us_ = now_us; } else if (now_us - calm_since_us_ >= policy_.settle_us) { if (state_.shed > 0) --state_.shed; // One level per settle interval, then logins. else state_.refusing = false; calm_since_us_ = now_us; } return before != std::pair{state_.refusing, state_.shed}; } } // namespace evo::server