feat(Resilience): add validated configs, RAII breaker/resource permits, bounded keyed cleanup, ResilienceAsync composition, and reserve P2 error domains (M-RESILIENCE-01..04)
This commit is contained in:
1 parent
fe99051ded
commit
18a623aa62
19 files changed
+2854
-427
No files matched your search
@@ -1,117 +1,301 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Resilience/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Resilience
|
||||
{
|
||||
|
||||
// ── ResourceMonitor
|
||||
// ─────────────────────────────────────────────────────────── Tracks usage
|
||||
// of a bounded resource (memory budget, in-flight requests, queue depth,
|
||||
// ...) against a limit, with hysteresis watermark callbacks.
|
||||
//
|
||||
// try_acquire() refuses to exceed the limit. Crossing the high watermark
|
||||
// upward fires on_high_watermark once; dropping back to the low watermark
|
||||
// fires on_low_watermark once (the gap prevents callback flapping).
|
||||
// Callbacks run outside the internal lock.
|
||||
class ResourceMonitor;
|
||||
|
||||
class ResourceMonitor
|
||||
{
|
||||
public:
|
||||
namespace Detail
|
||||
{
|
||||
|
||||
using Callback = std::function<void(std::uint64_t usage)>;
|
||||
struct MonitorState {
|
||||
using Callback = std::function<void(std::uint64_t usage)>;
|
||||
|
||||
// high/low are fractions of `limit` in [0, 1]; high should exceed
|
||||
// low.
|
||||
explicit ResourceMonitor(std::uint64_t limit, double high = 0.9,
|
||||
double low = 0.7)
|
||||
: limit_{limit}, high_{static_cast<std::uint64_t>(
|
||||
static_cast<double>(limit) * high)},
|
||||
low_{static_cast<std::uint64_t>(static_cast<double>(limit) *
|
||||
low)}
|
||||
{
|
||||
}
|
||||
std::uint64_t limit{0};
|
||||
std::uint64_t high{0};
|
||||
std::uint64_t low{0};
|
||||
std::uint64_t usage{0};
|
||||
bool alarmed{false};
|
||||
std::uint64_t observer_errors{0};
|
||||
Callback on_high{};
|
||||
Callback on_low{};
|
||||
mutable std::mutex mu{};
|
||||
|
||||
void on_high_watermark(Callback cb) { on_high_ = std::move(cb); }
|
||||
void on_low_watermark(Callback cb) { on_low_ = std::move(cb); }
|
||||
MonitorState(std::uint64_t lim, std::uint64_t hi, std::uint64_t lo) noexcept
|
||||
: limit(lim), high(hi), low(lo)
|
||||
{
|
||||
}
|
||||
|
||||
// Reserve `amount`. Returns false (reserving nothing) if it would
|
||||
// exceed the limit. May fire the high-watermark callback when
|
||||
// crossing upward.
|
||||
[[nodiscard]] bool try_acquire(std::uint64_t amount = 1)
|
||||
{
|
||||
Callback fire;
|
||||
std::uint64_t at = 0;
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
if (usage_ + amount > limit_) {
|
||||
return false;
|
||||
}
|
||||
usage_ += amount;
|
||||
if (!alarmed_ && usage_ >= high_) {
|
||||
alarmed_ = true;
|
||||
fire = on_high_;
|
||||
at = usage_;
|
||||
}
|
||||
}
|
||||
if (fire) {
|
||||
fire(at);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void invoke_callback(const Callback& cb, std::uint64_t at) noexcept
|
||||
{
|
||||
if (!cb) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
cb(at);
|
||||
} catch (...) {
|
||||
std::lock_guard lock{mu};
|
||||
++observer_errors;
|
||||
}
|
||||
}
|
||||
|
||||
// Release `amount` (clamped at zero). May fire the low-watermark
|
||||
// callback when dropping back through the low threshold.
|
||||
void release(std::uint64_t amount = 1)
|
||||
{
|
||||
Callback fire;
|
||||
std::uint64_t at = 0;
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
usage_ = (amount >= usage_) ? 0 : usage_ - amount;
|
||||
if (alarmed_ && usage_ <= low_) {
|
||||
alarmed_ = false;
|
||||
fire = on_low_;
|
||||
at = usage_;
|
||||
}
|
||||
}
|
||||
if (fire) {
|
||||
fire(at);
|
||||
}
|
||||
}
|
||||
void release_amount(std::uint64_t amount, bool clamp) noexcept
|
||||
{
|
||||
Callback fire;
|
||||
std::uint64_t at = 0;
|
||||
{
|
||||
std::lock_guard lock{mu};
|
||||
if (amount >= usage) {
|
||||
usage = clamp ? 0 : (usage - std::min(usage, amount));
|
||||
} else {
|
||||
usage -= amount;
|
||||
}
|
||||
if (alarmed && usage <= low) {
|
||||
alarmed = false;
|
||||
fire = on_low;
|
||||
at = usage;
|
||||
}
|
||||
}
|
||||
invoke_callback(fire, at);
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] std::uint64_t usage() const
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
return usage_;
|
||||
}
|
||||
} // namespace Detail
|
||||
|
||||
[[nodiscard]] std::uint64_t limit() const noexcept
|
||||
{
|
||||
return limit_;
|
||||
}
|
||||
class ResourcePermit
|
||||
{
|
||||
public:
|
||||
ResourcePermit() noexcept = default;
|
||||
|
||||
[[nodiscard]] double utilization() const
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
return limit_ == 0 ? 0.0
|
||||
: static_cast<double>(usage_) /
|
||||
static_cast<double>(limit_);
|
||||
}
|
||||
~ResourcePermit() noexcept { release(); }
|
||||
|
||||
private:
|
||||
ResourcePermit(ResourcePermit&& other) noexcept
|
||||
: state_(std::move(other.state_))
|
||||
, amount_(std::exchange(other.amount_, 0))
|
||||
{
|
||||
}
|
||||
|
||||
std::uint64_t limit_;
|
||||
std::uint64_t high_;
|
||||
std::uint64_t low_;
|
||||
std::uint64_t usage_ = 0;
|
||||
bool alarmed_ = false;
|
||||
Callback on_high_;
|
||||
Callback on_low_;
|
||||
mutable std::mutex mu_;
|
||||
};
|
||||
ResourcePermit& operator=(ResourcePermit&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
release();
|
||||
state_ = std::move(other.state_);
|
||||
amount_ = std::exchange(other.amount_, 0);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
ResourcePermit(const ResourcePermit&) = delete;
|
||||
ResourcePermit& operator=(const ResourcePermit&) = delete;
|
||||
|
||||
void release() noexcept
|
||||
{
|
||||
if (!state_ || amount_ == 0) {
|
||||
return;
|
||||
}
|
||||
const auto amt = std::exchange(amount_, 0);
|
||||
auto st = std::move(state_);
|
||||
st->release_amount(amt, false);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint64_t amount() const noexcept { return amount_; }
|
||||
[[nodiscard]] bool active() const noexcept
|
||||
{
|
||||
return state_ != nullptr && amount_ > 0;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class ResourceMonitor;
|
||||
|
||||
ResourcePermit(std::shared_ptr<Detail::MonitorState> state,
|
||||
std::uint64_t amount) noexcept
|
||||
: state_(std::move(state)), amount_(amount)
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<Detail::MonitorState> state_{};
|
||||
std::uint64_t amount_{0};
|
||||
};
|
||||
|
||||
// ── ResourceMonitor ──────────────────────────────────────────────────────────
|
||||
// Tracks usage of a bounded resource against a limit, with synchronized
|
||||
// watermark callbacks executed outside the internal mutex.
|
||||
|
||||
class ResourceMonitor
|
||||
{
|
||||
public:
|
||||
using Callback = std::function<void(std::uint64_t usage)>;
|
||||
|
||||
explicit ResourceMonitor(std::uint64_t limit,
|
||||
double high = 0.9,
|
||||
double low = 0.7)
|
||||
: state_(std::make_shared<Detail::MonitorState>(
|
||||
limit,
|
||||
clamp_fraction(limit, high, 0.9),
|
||||
clamp_fraction(limit, low, 0.7)))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<ResourceMonitor> create(
|
||||
MonitorConfig config)
|
||||
{
|
||||
if (auto v = config.validate(); !v) {
|
||||
return Void::Result::fail(std::move(v).error());
|
||||
}
|
||||
const auto hi = clamp_fraction(config.limit, config.high_fraction, 0.9);
|
||||
const auto lo = std::min(hi, clamp_fraction(config.limit, config.low_fraction, 0.7));
|
||||
return ResourceMonitor{
|
||||
std::make_shared<Detail::MonitorState>(config.limit, hi, lo)};
|
||||
}
|
||||
|
||||
void on_high_watermark(Callback cb)
|
||||
{
|
||||
std::lock_guard lock{state_->mu};
|
||||
state_->on_high = std::move(cb);
|
||||
}
|
||||
|
||||
void on_low_watermark(Callback cb)
|
||||
{
|
||||
std::lock_guard lock{state_->mu};
|
||||
state_->on_low = std::move(cb);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool try_acquire(std::uint64_t amount = 1)
|
||||
{
|
||||
Callback fire;
|
||||
std::uint64_t at = 0;
|
||||
{
|
||||
std::lock_guard lock{state_->mu};
|
||||
if (amount > state_->limit - state_->usage) {
|
||||
return false;
|
||||
}
|
||||
state_->usage += amount;
|
||||
if (!state_->alarmed && state_->usage >= state_->high) {
|
||||
state_->alarmed = true;
|
||||
fire = state_->on_high;
|
||||
at = state_->usage;
|
||||
}
|
||||
}
|
||||
state_->invoke_callback(fire, at);
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ResourcePermit>
|
||||
try_acquire_checked(std::uint64_t amount = 1)
|
||||
{
|
||||
if (amount == 0) {
|
||||
return Void::Result::fail(make_resilience_error(
|
||||
ResilienceErrorCode::invalid_argument,
|
||||
"ResourceMonitor::try_acquire_checked requires amount > 0"));
|
||||
}
|
||||
Callback fire;
|
||||
std::uint64_t at = 0;
|
||||
{
|
||||
std::lock_guard lock{state_->mu};
|
||||
if (amount > state_->limit - state_->usage) {
|
||||
return Void::Result::fail(make_resilience_error(
|
||||
ResilienceErrorCode::overflow,
|
||||
"ResourceMonitor reservation exceeds configured limit"));
|
||||
}
|
||||
state_->usage += amount;
|
||||
if (!state_->alarmed && state_->usage >= state_->high) {
|
||||
state_->alarmed = true;
|
||||
fire = state_->on_high;
|
||||
at = state_->usage;
|
||||
}
|
||||
}
|
||||
state_->invoke_callback(fire, at);
|
||||
return ResourcePermit{state_, amount};
|
||||
}
|
||||
|
||||
void release(std::uint64_t amount = 1)
|
||||
{
|
||||
state_->release_amount(amount, true);
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
release_checked(std::uint64_t amount = 1)
|
||||
{
|
||||
if (amount == 0) {
|
||||
return Void::Result::fail(make_resilience_error(
|
||||
ResilienceErrorCode::invalid_argument,
|
||||
"ResourceMonitor::release_checked requires amount > 0"));
|
||||
}
|
||||
Callback fire;
|
||||
std::uint64_t at = 0;
|
||||
{
|
||||
std::lock_guard lock{state_->mu};
|
||||
if (amount > state_->usage) {
|
||||
return Void::Result::fail(make_resilience_error(
|
||||
ResilienceErrorCode::over_release,
|
||||
"ResourceMonitor release amount exceeds active usage"));
|
||||
}
|
||||
state_->usage -= amount;
|
||||
if (state_->alarmed && state_->usage <= state_->low) {
|
||||
state_->alarmed = false;
|
||||
fire = state_->on_low;
|
||||
at = state_->usage;
|
||||
}
|
||||
}
|
||||
state_->invoke_callback(fire, at);
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint64_t usage() const
|
||||
{
|
||||
std::lock_guard lock{state_->mu};
|
||||
return state_->usage;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint64_t limit() const noexcept
|
||||
{
|
||||
return state_->limit;
|
||||
}
|
||||
|
||||
[[nodiscard]] double utilization() const
|
||||
{
|
||||
std::lock_guard lock{state_->mu};
|
||||
return state_->limit == 0
|
||||
? 0.0
|
||||
: static_cast<double>(state_->usage) /
|
||||
static_cast<double>(state_->limit);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint64_t observer_errors() const
|
||||
{
|
||||
std::lock_guard lock{state_->mu};
|
||||
return state_->observer_errors;
|
||||
}
|
||||
|
||||
private:
|
||||
explicit ResourceMonitor(std::shared_ptr<Detail::MonitorState> state) noexcept
|
||||
: state_(std::move(state))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] static std::uint64_t clamp_fraction(std::uint64_t limit,
|
||||
double frac,
|
||||
double fallback) noexcept
|
||||
{
|
||||
const double chosen =
|
||||
(std::isfinite(frac) && frac >= 0.0 && frac <= 1.0) ? frac : fallback;
|
||||
return static_cast<std::uint64_t>(
|
||||
std::clamp(static_cast<double>(limit) * chosen,
|
||||
0.0,
|
||||
static_cast<double>(limit)));
|
||||
}
|
||||
|
||||
std::shared_ptr<Detail::MonitorState> state_;
|
||||
};
|
||||
|
||||
} // namespace Void::Resilience
|
||||
Reference in new issue
Block a user