Files
moloapp/example/fw/Resilience.cpp
T
retoorandClaude Sonnet 5.5 62fdd08da8 feat(workflow): example covers security, platform, enterprise and resilience features
105 route tests, matrix crawl, spec and README updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 02:19:52 +02:00

1027 lines
31 KiB
C++

#include "Resilience.hpp"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <thread>
#include "Http.hpp"
namespace molo::example::fw {
using Guard = std::lock_guard<std::recursive_mutex>;
void ManualClock::advance(long ms) {
if (ms < 0) ms = 0;
if (ms > labcaps::kMaxAdvanceMs) ms = labcaps::kMaxAdvanceMs;
now_ += ms;
}
std::string fnv_hex(const std::string& data) {
unsigned long long h = 1469598103934665603ULL;
for (unsigned char c : data) {
h ^= c;
h *= 1099511628211ULL;
}
static const char* d = "0123456789abcdef";
std::string out(16, '0');
for (int i = 15; i >= 0; --i) {
out[(std::size_t)i] = d[h & 0xF];
h >>= 4;
}
return out;
}
std::string base64_encode(const std::string& in) {
static const char* t =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string out;
std::size_t i = 0;
while (i < in.size()) {
unsigned v = (unsigned char)in[i] << 16;
std::size_t n = in.size() - i;
if (n > 1) v |= (unsigned char)in[i + 1] << 8;
if (n > 2) v |= (unsigned char)in[i + 2];
out += t[(v >> 18) & 63];
out += t[(v >> 12) & 63];
out += n > 1 ? t[(v >> 6) & 63] : '=';
out += n > 2 ? t[v & 63] : '=';
i += 3;
}
return out;
}
// ---- Lab -------------------------------------------------------------------
void Lab::note(std::string line) {
log_.push_back(std::move(line));
while (log_.size() > labcaps::kLogLines) log_.pop_front();
}
std::vector<std::string> Lab::log() const {
Guard g(mu_);
return {log_.begin(), log_.end()};
}
std::string Lab::json() const {
Guard g(mu_);
std::string out = "{\"name\":\"" + json_escape(name()) + "\",\"state\":{";
bool first = true;
for (const auto& [k, v] : rows()) {
out += std::string(first ? "" : ",") + "\"" + json_escape(k) + "\":\"" +
json_escape(v) + "\"";
first = false;
}
out += "},\"log\":[";
first = true;
for (const auto& l : log_) {
out += std::string(first ? "" : ",") + "\"" + json_escape(l) + "\"";
first = false;
}
return out + "]}";
}
// ---- CircuitBreaker --------------------------------------------------------
std::string breaker_state_name(CircuitBreaker::State s) {
switch (s) {
case CircuitBreaker::State::Closed: return "closed";
case CircuitBreaker::State::Open: return "open";
case CircuitBreaker::State::HalfOpen: return "half-open";
}
return "?";
}
CircuitBreaker::CircuitBreaker(std::shared_ptr<ManualClock> clock,
int threshold, long open_ms)
: clock_(std::move(clock)), threshold_(threshold), open_ms_(open_ms) {}
CircuitBreaker::State CircuitBreaker::effective() const {
if (state_ == State::Open && clock_->now() - opened_at_ >= open_ms_)
return State::HalfOpen;
return state_;
}
CircuitBreaker::State CircuitBreaker::state() const {
Guard g(mu_);
return effective();
}
bool CircuitBreaker::call(bool success) {
Guard g(mu_);
State before = effective();
if (before == State::Open) {
++rejected_;
note("call rejected: breaker open");
return false;
}
state_ = before;
if (success) {
failures_ = 0;
state_ = State::Closed;
} else if (before == State::HalfOpen || ++failures_ >= threshold_) {
state_ = State::Open;
opened_at_ = clock_->now();
failures_ = 0;
}
note(std::string(success ? "success" : "failure") + ": " +
breaker_state_name(before) + " -> " + breaker_state_name(state_));
return true;
}
void CircuitBreaker::tick(long ms) {
Guard g(mu_);
State before = effective();
clock_->advance(ms);
note("clock +" + std::to_string(ms) + "ms");
if (before != effective())
note(breaker_state_name(before) + " -> " + breaker_state_name(effective()));
}
LabRows CircuitBreaker::rows() const {
Guard g(mu_);
return {{"state", breaker_state_name(effective())},
{"failures", std::to_string(failures_) + "/" +
std::to_string(threshold_)},
{"rejected", std::to_string(rejected_)},
{"clock_ms", std::to_string(clock_->now())},
{"reopens_after_ms", std::to_string(open_ms_)}};
}
// ---- Bulkhead --------------------------------------------------------------
Bulkhead::Bulkhead(int a, int q) : max_active_(a), max_queue_(q) {}
Bulkhead::Admission Bulkhead::acquire() {
Guard g(mu_);
if (active_ < max_active_) {
++active_;
note("admitted");
return Admission::Admitted;
}
if (queued_ < max_queue_) {
++queued_;
note("queued");
return Admission::Queued;
}
++rejected_;
note("rejected: compartment full");
return Admission::Rejected;
}
bool Bulkhead::release() {
Guard g(mu_);
if (active_ == 0) return false;
if (queued_ > 0) {
--queued_;
note("released, queued call promoted");
} else {
--active_;
note("released");
}
return true;
}
LabRows Bulkhead::rows() const {
Guard g(mu_);
return {{"active", std::to_string(active_) + "/" + std::to_string(max_active_)},
{"queued", std::to_string(queued_) + "/" + std::to_string(max_queue_)},
{"rejected", std::to_string(rejected_)}};
}
// ---- Shedder ---------------------------------------------------------------
bool Shedder::enter(bool high) {
Guard g(mu_);
int cap = high ? limit_ : limit_ * 2 / 3;
if (inflight_ >= cap) {
(high ? shed_high_ : shed_low_)++;
note(std::string("shed ") + (high ? "high" : "low") + " priority");
return false;
}
++inflight_;
note(std::string("accepted ") + (high ? "high" : "low") + " priority");
return true;
}
bool Shedder::leave() {
Guard g(mu_);
if (inflight_ == 0) return false;
--inflight_;
note("finished");
return true;
}
LabRows Shedder::rows() const {
Guard g(mu_);
return {{"inflight", std::to_string(inflight_) + "/" + std::to_string(limit_)},
{"low_priority_cutoff", std::to_string(limit_ * 2 / 3)},
{"shed_low", std::to_string(shed_low_)},
{"shed_high", std::to_string(shed_high_)}};
}
// ---- Deadline --------------------------------------------------------------
int DeadlineLab::run(long budget_ms, int hops) {
Guard g(mu_);
budget_ms_ = budget_ms;
remaining_ms_ = budget_ms;
hops_ = hops;
expired_hop_ = 0;
ran_ = true;
for (int h = 1; h <= hops; ++h) {
if (remaining_ms_ < kHopCostMs) {
expired_hop_ = h;
note("hop " + std::to_string(h) + ": deadline exceeded, call skipped");
break;
}
remaining_ms_ -= kHopCostMs;
note("hop " + std::to_string(h) + ": ok, " +
std::to_string(remaining_ms_) + "ms left");
}
return expired_hop_;
}
LabRows DeadlineLab::rows() const {
Guard g(mu_);
if (!ran_) return {{"status", "not run"}, {"hop_cost_ms", "30"}};
return {{"status", expired_hop_ ? "expired" : "completed"},
{"budget_ms", std::to_string(budget_ms_)},
{"hops", std::to_string(hops_)},
{"expired_at_hop", std::to_string(expired_hop_)},
{"remaining_ms", std::to_string(remaining_ms_)},
{"hop_cost_ms", "30"}};
}
// ---- SloBudget -------------------------------------------------------------
void SloBudget::record(bool good) {
Guard g(mu_);
window_.push_back(good);
if (window_.size() > kWindow) window_.pop_front();
note(good ? "good event" : "bad event");
}
void SloBudget::reset() {
Guard g(mu_);
window_.clear();
note("window reset");
}
long SloBudget::budget_remaining() const {
Guard g(mu_);
long bad = std::count(window_.begin(), window_.end(), false);
long allowed = (long)window_.size() * (1000 - target_) / 1000;
return allowed - bad;
}
LabRows SloBudget::rows() const {
Guard g(mu_);
long bad = std::count(window_.begin(), window_.end(), false);
long left = budget_remaining();
return {{"target", std::to_string(target_ / 10) + "." +
std::to_string(target_ % 10) + "%"},
{"events", std::to_string(window_.size()) + "/" +
std::to_string(kWindow)},
{"bad", std::to_string(bad)},
{"budget_remaining", std::to_string(left)},
{"state", bad == 0 ? "healthy" : left >= 0 ? "burning" : "exhausted"}};
}
// ---- ChaosLab --------------------------------------------------------------
ChaosLab::Result ChaosLab::run(int percent, int max_faults, bool armed) {
Guard g(mu_);
percent = std::clamp(percent, 0, labcaps::kMaxPercent);
max_faults = std::clamp(max_faults, 1, labcaps::kMaxFaults);
Result r;
r.percent = percent;
r.max_faults = max_faults;
r.armed = armed;
int acc = 0;
for (int i = 0; i < kCalls && r.planned < max_faults; ++i) {
acc += percent;
if (acc >= 100) {
acc -= 100;
++r.planned;
}
}
r.injected = armed ? r.planned : 0;
last_ = r;
++runs_;
total_injected_ += r.injected;
note(std::string(armed ? "ARMED" : "dry run") + ": " +
std::to_string(r.planned) + " fault(s) planned over " +
std::to_string(kCalls) + " calls, " + std::to_string(r.injected) +
" injected");
return r;
}
LabRows ChaosLab::rows() const {
Guard g(mu_);
return {{"mode", last_.armed ? "armed" : "dry run"},
{"percent", std::to_string(last_.percent) + " (cap " +
std::to_string(labcaps::kMaxPercent) + ")"},
{"max_faults", std::to_string(last_.max_faults) + " (cap " +
std::to_string(labcaps::kMaxFaults) + ")"},
{"planned", std::to_string(last_.planned)},
{"injected", std::to_string(last_.injected)},
{"runs", std::to_string(runs_)},
{"total_injected", std::to_string(total_injected_)}};
}
// ---- DeployLab -------------------------------------------------------------
std::string deploy_phase_name(DeployLab::Phase p) {
switch (p) {
case DeployLab::Phase::Idle: return "idle";
case DeployLab::Phase::InProgress: return "in progress";
case DeployLab::Phase::Done: return "done";
case DeployLab::Phase::RolledBack: return "rolled back";
}
return "?";
}
DeployLab::DeployLab() : fleet_(4, "v1") {}
DeployLab::Phase DeployLab::phase() const {
Guard g(mu_);
return phase_;
}
bool DeployLab::start(const std::string& strategy,
const std::string& version) {
Guard g(mu_);
if (phase_ == Phase::InProgress) return false;
previous_ = fleet_;
strategy_ = strategy;
target_ = version;
green_stage_ = 0;
phase_ = Phase::InProgress;
note("start " + strategy + " to " + version);
return true;
}
bool DeployLab::step() {
Guard g(mu_);
if (phase_ != Phase::InProgress) return false;
if (strategy_ == "rolling") {
for (auto& v : fleet_) {
if (v != target_) {
v = target_;
note("replaced one instance with " + target_);
break;
}
}
if (std::all_of(fleet_.begin(), fleet_.end(),
[&](const std::string& v) { return v == target_; })) {
phase_ = Phase::Done;
note("rolling finished");
}
} else if (green_stage_ == 0) {
green_stage_ = 1;
note("green fleet " + target_ + " prepared, blue still serves");
} else {
std::fill(fleet_.begin(), fleet_.end(), target_);
phase_ = Phase::Done;
note("traffic switched to green");
}
return true;
}
bool DeployLab::rollback() {
Guard g(mu_);
if (phase_ == Phase::Idle || phase_ == Phase::RolledBack) return false;
fleet_ = previous_;
phase_ = Phase::RolledBack;
note("rolled back to previous fleet");
return true;
}
LabRows DeployLab::rows() const {
Guard g(mu_);
std::string fleet;
for (const auto& v : fleet_) fleet += (fleet.empty() ? "" : ",") + v;
return {{"strategy", strategy_},
{"phase", deploy_phase_name(phase_)},
{"target", target_.empty() ? "-" : target_},
{"fleet", fleet}};
}
// ---- CoordLab --------------------------------------------------------------
CoordLab::CoordLab(std::shared_ptr<ManualClock> clock, long ttl_ms)
: clock_(std::move(clock)), ttl_ms_(ttl_ms) {}
bool CoordLab::known_key(const std::string& key) {
return key == "lease" || key == "lock" || key == "leader";
}
CoordLab::Grant CoordLab::acquire(const std::string& key,
const std::string& owner) {
Guard g(mu_);
Slot& s = slots_[key];
bool live = !s.holder.empty() && s.expires > clock_->now();
if (live && s.holder != owner) {
note(owner + " denied " + key + ": held by " + s.holder);
return {false, s.token, s.holder};
}
if (!live) ++s.token; // new term, new fencing token
s.holder = owner;
s.expires = clock_->now() + ttl_ms_;
note(owner + (live ? " renewed " : " acquired ") + key + " token " +
std::to_string(s.token));
return {true, s.token, owner};
}
bool CoordLab::release(const std::string& key, const std::string& owner) {
Guard g(mu_);
auto it = slots_.find(key);
if (it == slots_.end() || it->second.holder != owner) return false;
it->second.holder.clear();
note(owner + " released " + key);
return true;
}
bool CoordLab::write(const std::string& key, long token) {
Guard g(mu_);
Slot& s = slots_[key];
if (token < s.fence) {
note("write with stale token " + std::to_string(token) + " rejected (fence " +
std::to_string(s.fence) + ")");
return false;
}
s.fence = token;
note("write with token " + std::to_string(token) + " accepted");
return true;
}
void CoordLab::tick(long ms) {
Guard g(mu_);
clock_->advance(ms);
note("clock +" + std::to_string(ms) + "ms");
}
LabRows CoordLab::rows() const {
Guard g(mu_);
LabRows out{{"clock_ms", std::to_string(clock_->now())},
{"ttl_ms", std::to_string(ttl_ms_)}};
for (const char* key : {"lease", "lock", "leader"}) {
auto it = slots_.find(key);
if (it == slots_.end()) {
out.emplace_back(key, "free");
continue;
}
const Slot& s = it->second;
bool live = !s.holder.empty() && s.expires > clock_->now();
out.emplace_back(key, (live ? "held by " + s.holder : std::string("free")) +
", token " + std::to_string(s.token) +
", fence " + std::to_string(s.fence));
}
return out;
}
// ---- DebugRegistry ---------------------------------------------------------
void DebugRegistry::add(std::shared_ptr<Lab> lab) {
labs_.push_back(std::move(lab));
}
std::string DebugRegistry::snapshot() const {
std::string out = "{\"count\":" + std::to_string(labs_.size()) + ",\"labs\":{";
bool first = true;
for (const auto& l : labs_) {
out += std::string(first ? "" : ",") + "\"" + json_escape(l->name()) +
"\":" + l->json();
first = false;
}
return out + "}}";
}
// ---- SupplyChainLab --------------------------------------------------------
SupplyChainLab::SupplyChainLab() {
reset();
}
void SupplyChainLab::reset() {
Guard g(mu_);
components_ = {{"molo-example", "1.0.0", "proprietary"},
{"chartjs", "4.4.1", "MIT"},
{"wolfssl", "5.7.2", "GPL-2.0"},
{"sqlite", "3.45.1", "blessing"}};
pins_.clear();
for (const auto& c : components_) pins_[c.name] = c.version;
advisories_.clear();
attested_ = digest();
verified_ = 0;
note("sbom regenerated and attested " + attested_);
}
std::string SupplyChainLab::digest() const {
std::string canon;
for (const auto& c : components_)
canon += c.name + "@" + c.version + ";" + c.license + "\n";
return fnv_hex(canon);
}
bool SupplyChainLab::add_advisory(const std::string& component,
const std::string& version) {
Guard g(mu_);
if (advisories_.size() >= labcaps::kMaxAdvisories) return false;
advisories_.emplace_back(component, version);
note("advisory published for " + component + "@" + version);
return true;
}
void SupplyChainLab::tamper() {
Guard g(mu_);
components_[1].version = "4.4.2";
verified_ = 0;
note("chartjs changed to 4.4.2 after attestation");
}
bool SupplyChainLab::verify(const std::string& claimed) {
Guard g(mu_);
bool ok = claimed == attested_ && claimed == digest();
verified_ = ok ? 1 : -1;
note(std::string("verify ") + (ok ? "ok" : "FAILED"));
return ok;
}
bool SupplyChainLab::pins_ok() const {
for (const auto& c : components_) {
auto it = pins_.find(c.name);
if (it == pins_.end() || it->second != c.version) return false;
}
return true;
}
bool SupplyChainLab::advisories_ok() const {
for (const auto& c : components_)
for (const auto& a : advisories_)
if (a.first == c.name && a.second == c.version) return false;
return true;
}
bool SupplyChainLab::gate() const {
Guard g(mu_);
return pins_ok() && advisories_ok() && attested_ == digest();
}
std::string SupplyChainLab::cyclonedx() const {
Guard g(mu_);
std::string out =
"{\"bomFormat\":\"CycloneDX\",\"specVersion\":\"1.5\",\"components\":[";
bool first = true;
for (const auto& c : components_) {
out += std::string(first ? "" : ",") + "{\"name\":\"" + json_escape(c.name) +
"\",\"version\":\"" + json_escape(c.version) +
"\",\"license\":\"" + json_escape(c.license) + "\"}";
first = false;
}
return out + "],\"digest\":\"" + digest() + "\"}";
}
LabRows SupplyChainLab::rows() const {
Guard g(mu_);
LabRows out;
for (const auto& c : components_)
out.emplace_back(c.name, c.version + " (" + c.license + ")");
out.emplace_back("pins", pins_ok() ? "pass" : "FAIL");
out.emplace_back("advisories",
advisories_ok() ? "pass (" + std::to_string(advisories_.size()) +
" known)"
: "FAIL");
out.emplace_back("attestation", attested_ == digest() ? "pass" : "FAIL");
out.emplace_back("last_verify", verified_ == 0 ? "none" : verified_ > 0 ? "ok" : "failed");
out.emplace_back("sbom_digest", digest());
out.emplace_back("gate", pins_ok() && advisories_ok() && attested_ == digest()
? "PASS"
: "FAIL");
return out;
}
// ---- ClientRetryLab --------------------------------------------------------
ClientRetryLab::Outcome ClientRetryLab::run(int failures, int max_attempts,
int status) {
Guard g(mu_);
max_attempts = std::clamp(max_attempts, 1, labcaps::kMaxAttempts);
Outcome o;
for (int a = 1; a <= max_attempts; ++a) {
o.attempts = a;
o.status = a <= failures ? status : 200;
note("attempt " + std::to_string(a) + " -> " + std::to_string(o.status));
if (o.status == 200) break;
if (o.status < 500) {
o.gave_up = true;
note("4xx is never retried");
break;
}
if (a == max_attempts) {
o.gave_up = true;
note("attempts exhausted");
break;
}
long wait = std::min(kCapMs, kBaseMs << (a - 1));
o.waited_ms += wait;
note("backoff " + std::to_string(wait) + "ms (cap " +
std::to_string(kCapMs) + "ms)");
}
last_ = o;
++runs_;
return o;
}
LabRows ClientRetryLab::rows() const {
Guard g(mu_);
return {{"runs", std::to_string(runs_)},
{"attempts", std::to_string(last_.attempts)},
{"final_status", std::to_string(last_.status)},
{"virtual_wait_ms", std::to_string(last_.waited_ms)},
{"gave_up", last_.gave_up ? "yes" : "no"},
{"backoff", "100ms doubling, cap 800ms"}};
}
// ---- ClientSessionLab ------------------------------------------------------
ClientSessionLab::ClientSessionLab() = default;
bool ClientSessionLab::valid_proxy(const std::string& p) {
if (p.empty() || p == "direct") return true;
for (const char* scheme : {"http://", "https://", "socks5://"}) {
std::string s = scheme;
if (p.compare(0, s.size(), s) != 0) continue;
std::string hp = p.substr(s.size());
std::size_t colon = hp.rfind(':');
if (colon == std::string::npos || colon == 0 || colon + 1 >= hp.size())
return false;
for (std::size_t i = colon + 1; i < hp.size(); ++i)
if (hp[i] < '0' || hp[i] > '9') return false;
return hp.size() - colon - 1 <= 5;
}
return false;
}
std::map<std::string, std::string> ClientSessionLab::request(
const std::string& auth, const std::string& proxy) {
Guard g(mu_);
std::string px = proxy.empty() ? "direct" : proxy;
std::map<std::string, std::string> h{{"Host", "example.internal"}};
if (!jar_.empty()) {
std::string c;
for (const auto& [k, v] : jar_) c += (c.empty() ? "" : "; ") + k + "=" + v;
h["Cookie"] = c;
}
if (auth == "basic")
h["Authorization"] = "Basic " + base64_encode("demo:demo");
else if (auth == "bearer")
h["Authorization"] = "Bearer demo-token";
if (px != proxy_) idle_ = 0; // a new route needs a new connection
proxy_ = px;
auth_ = auth;
if (idle_ > 0) {
--idle_;
++reused_;
note("connection reused");
} else {
++created_;
note("connection created via " + px);
}
++idle_;
++requests_;
jar_["sid"] = "s-1"; // Set-Cookie from the in-process server
jar_["visits"] = std::to_string(requests_);
note("request " + std::to_string(requests_) + " auth=" + auth);
return h;
}
LabRows ClientSessionLab::rows() const {
Guard g(mu_);
std::string jar;
for (const auto& [k, v] : jar_) jar += (jar.empty() ? "" : "; ") + k + "=" + v;
return {{"requests", std::to_string(requests_)},
{"cookie_jar", jar.empty() ? "empty" : jar},
{"pool_created", std::to_string(created_)},
{"pool_reused", std::to_string(reused_)},
{"pool_idle", std::to_string(idle_)},
{"proxy", proxy_},
{"auth_scheme", auth_}};
}
// ---- MigrationLab ----------------------------------------------------------
MigrationLab::MigrationLab() {
steps_ = {{"001", "CREATE TABLE users (id, name)", "users"},
{"002", "ALTER TABLE users ADD COLUMN email", "users"},
{"003", "CREATE TABLE posts (id, user_id, title)", "posts"}};
}
std::vector<std::string> MigrationLab::check() const {
Guard g(mu_);
std::vector<std::string> out;
for (const auto& s : steps_)
if (std::find(applied_.begin(), applied_.end(), s.id) == applied_.end())
out.push_back(s.id + " " + s.sql);
return out;
}
int MigrationLab::apply(int limit) {
Guard g(mu_);
int n = 0;
for (const auto& s : steps_) {
if (n >= limit) break;
if (std::find(applied_.begin(), applied_.end(), s.id) != applied_.end())
continue;
if (s.sql.compare(0, 6, "CREATE") == 0)
tables_[s.table] = s.sql.substr(s.sql.find('(') + 1);
else
tables_[s.table] += " +email";
applied_.push_back(s.id);
note("applied " + s.id);
++n;
}
return n;
}
void MigrationLab::drop_table(const std::string& table) {
Guard g(mu_);
if (tables_.erase(table)) note("table " + table + " dropped out of band");
}
std::vector<std::string> MigrationLab::drift() const {
Guard g(mu_);
std::vector<std::string> out;
for (const auto& s : steps_)
if (s.sql.compare(0, 6, "CREATE") == 0 && !tables_.count(s.table) &&
std::find(applied_.begin(), applied_.end(), s.id) != applied_.end())
out.push_back(s.id + " " + s.table);
return out;
}
int MigrationLab::heal() {
Guard g(mu_);
int n = 0;
for (const auto& s : steps_) {
if (s.sql.compare(0, 6, "CREATE") == 0 && !tables_.count(s.table) &&
std::find(applied_.begin(), applied_.end(), s.id) != applied_.end()) {
tables_[s.table] = s.sql.substr(s.sql.find('(') + 1);
note("healed " + s.table);
++n;
}
}
return n;
}
LabRows MigrationLab::rows() const {
Guard g(mu_);
LabRows out;
std::string pending, applied;
for (const auto& s : steps_) {
bool done = std::find(applied_.begin(), applied_.end(), s.id) != applied_.end();
(done ? applied : pending) += (done ? applied : pending).empty() ? s.id : "," + s.id;
}
out.emplace_back("applied", applied.empty() ? "none" : applied);
out.emplace_back("pending", pending.empty() ? "none" : pending);
for (const auto& [t, cols] : tables_) out.emplace_back("table " + t, cols);
auto d = drift();
out.emplace_back("drift", d.empty() ? "none" : d.front());
return out;
}
// ---- TxLab -----------------------------------------------------------------
TxLab::TxLab() : accounts_{{"alice", 100}, {"bob", 50}} {}
long TxLab::balance(const std::string& who) const {
Guard g(mu_);
auto it = accounts_.find(who);
return it == accounts_.end() ? -1 : it->second;
}
std::string TxLab::transfer(const std::string& from, const std::string& to,
long amount, bool fail_after_debit) {
Guard g(mu_);
auto snapshot = accounts_; // BEGIN
accounts_[from] -= amount;
std::string reason;
if (accounts_[from] < 0) reason = "insufficient funds";
else if (fail_after_debit) reason = "failure after debit";
if (!reason.empty()) {
accounts_ = snapshot; // ROLLBACK
++rollbacks_;
note("ROLLBACK: " + reason);
return "rolled back: " + reason;
}
accounts_[to] += amount; // COMMIT
++commits_;
note("COMMIT: " + from + " -> " + to + " " + std::to_string(amount));
return "committed";
}
LabRows TxLab::rows() const {
Guard g(mu_);
LabRows out;
for (const auto& [k, v] : accounts_) out.emplace_back(k, std::to_string(v));
out.emplace_back("commits", std::to_string(commits_));
out.emplace_back("rollbacks", std::to_string(rollbacks_));
return out;
}
// ---- QueryLab --------------------------------------------------------------
QueryLab::QueryLab() {
users_ = {{{"id", "1"}, {"name", "ada"}}, {{"id", "2"}, {"name", "grace"}}};
posts_ = {{{"id", "1"}, {"user_id", "1"}, {"title", "Alpha"}},
{{"id", "2"}, {"user_id", "1"}, {"title", "Gamma"}},
{{"id", "3"}, {"user_id", "2"}, {"title", "Beta"}},
{{"id", "4"}, {"user_id", "2"}, {"title", "Delta"}},
{{"id", "5"}, {"user_id", "1"}, {"title", "Epsilon"}}};
}
QueryLab::Result QueryLab::run(long user_id, const std::string& order,
int limit) {
Guard g(mu_);
Result r;
r.sql = "SELECT posts.id, posts.title, users.name FROM posts JOIN users ON "
"users.id = posts.user_id";
if (user_id > 0) r.sql += " WHERE posts.user_id = " + std::to_string(user_id);
std::string col = order == "title" ? "title" : "id";
r.sql += " ORDER BY posts." + col + " LIMIT " + std::to_string(limit);
std::vector<std::map<std::string, std::string>> picked;
for (const auto& p : posts_) {
if (user_id > 0 && p.at("user_id") != std::to_string(user_id)) continue;
auto row = p;
for (const auto& u : users_)
if (u.at("id") == p.at("user_id")) row["author"] = u.at("name");
picked.push_back(row);
}
std::stable_sort(picked.begin(), picked.end(), [&](const auto& a, const auto& b) {
if (col == "id") return std::stol(a.at("id")) < std::stol(b.at("id"));
return a.at(col) < b.at(col);
});
if ((int)picked.size() > limit) picked.resize((std::size_t)limit);
r.rows = std::move(picked);
last_ = r;
note(std::to_string(r.rows.size()) + " row(s)");
return r;
}
LabRows QueryLab::rows() const {
Guard g(mu_);
LabRows out{{"sql", last_.sql.empty() ? "not run" : last_.sql}};
for (const auto& r : last_.rows)
out.emplace_back("post " + r.at("id"), r.at("title") + " by " + r.at("author"));
return out;
}
// ---- SingleFlight ----------------------------------------------------------
std::string SingleFlight::run(const std::string& key,
const std::function<std::string()>& loader) {
std::unique_lock<std::mutex> lk(mu_);
auto it = calls_.find(key);
if (it != calls_.end()) {
auto call = it->second;
++call->waiters;
cv_.wait(lk, [&] { return call->done; });
return call->value;
}
auto call = std::make_shared<Call>();
calls_[key] = call;
lk.unlock();
std::string value = loader();
lk.lock();
call->value = value;
call->done = true;
calls_.erase(key);
cv_.notify_all();
return value;
}
int SingleFlight::waiters(const std::string& key) const {
std::lock_guard<std::mutex> lk(mu_);
auto it = calls_.find(key);
return it == calls_.end() ? 0 : it->second->waiters;
}
SingleFlightLab::SingleFlightLab() : flight_(std::make_unique<SingleFlight>()) {}
int SingleFlightLab::run(int callers) {
callers = std::clamp(callers, 1, labcaps::kMaxThreads);
std::atomic<int> loads{0};
SingleFlight* flight = flight_.get();
auto loader = [&]() {
++loads;
// Hold the load open until every other caller has joined (2 s cap), so
// the collapse is deterministic and never depends on scheduling luck.
for (int i = 0; i < 2000 && flight->waiters("report") < callers - 1; ++i)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return std::string("loaded");
};
std::vector<std::thread> pool;
for (int i = 0; i < callers; ++i)
pool.emplace_back([&] { flight->run("report", loader); });
for (auto& t : pool) t.join();
Guard g(mu_);
callers_ = callers;
loads_ = loads.load();
++runs_;
note(std::to_string(callers) + " concurrent callers -> " +
std::to_string(loads_) + " load(s)");
return loads_;
}
LabRows SingleFlightLab::rows() const {
Guard g(mu_);
return {{"runs", std::to_string(runs_)},
{"callers", std::to_string(callers_)},
{"loader_executions", std::to_string(loads_)},
{"thread_cap", std::to_string(labcaps::kMaxThreads)}};
}
// ---- TtlLab ----------------------------------------------------------------
TtlLab::TtlLab(std::shared_ptr<ManualClock> clock) : clock_(std::move(clock)) {}
void TtlLab::publish(const std::string& event) {
events_.push_back(event);
while (events_.size() > labcaps::kLogLines) events_.pop_front();
note("kv.events: " + event);
}
void TtlLab::sweep() {
for (auto it = entries_.begin(); it != entries_.end();) {
if (it->second.expires <= clock_->now()) {
publish("expired " + it->first);
it = entries_.erase(it);
} else {
++it;
}
}
}
void TtlLab::set(const std::string& key, const std::string& value, long ttl_s) {
Guard g(mu_);
sweep();
if (!entries_.count(key) && entries_.size() >= labcaps::kMaxEntries) {
auto lru = std::min_element(
entries_.begin(), entries_.end(),
[](const auto& a, const auto& b) { return a.second.touched < b.second.touched; });
publish("evicted " + lru->first + " (lru)");
entries_.erase(lru);
}
entries_[key] = {value, clock_->now() + ttl_s * 1000, seq_++};
publish("set " + key);
}
bool TtlLab::get(const std::string& key, std::string& value) {
Guard g(mu_);
sweep();
auto it = entries_.find(key);
if (it == entries_.end()) {
++misses_;
note("miss " + key);
return false;
}
it->second.touched = seq_++;
value = it->second.value;
++hits_;
note("hit " + key);
return true;
}
void TtlLab::tick(long ms) {
Guard g(mu_);
clock_->advance(ms);
note("clock +" + std::to_string(ms) + "ms");
sweep();
}
std::size_t TtlLab::size() const {
Guard g(mu_);
return entries_.size();
}
std::vector<std::string> TtlLab::events() const {
Guard g(mu_);
return {events_.begin(), events_.end()};
}
LabRows TtlLab::rows() const {
Guard g(mu_);
LabRows out{{"clock_ms", std::to_string(clock_->now())},
{"entries", std::to_string(entries_.size()) + "/" +
std::to_string(labcaps::kMaxEntries)},
{"hits", std::to_string(hits_)},
{"misses", std::to_string(misses_)}};
for (const auto& [k, e] : entries_)
out.emplace_back("key " + k, e.value + ", " +
std::to_string((e.expires - clock_->now()) / 1000) +
"s left");
return out;
}
} // namespace molo::example::fw