clboss/Boss/Mod/RebalanceModeManager.cpp
Ken Sedgwick cbdbae2be1
Add rebalancer mode selector (classic/off) as a dynamic option
Introduces a single source of truth for which rebalancing track is
active.  Boss::Mod::RebalanceModeManager owns the mode in memory (no
sqlite, so a restart reverts to the configured default, giving a
known-good baseline on every boot) and registers clboss-rebalance-mode
as a dynamic option: the config file sets the startup default and
`setconfig clboss-rebalance-mode <mode>` switches it at runtime without
a restart.  It answers RequestRebalanceMode queries and reports the mode
under clboss-status.

Modes are "classic" (run the rebalancer) and "off" (a real quiesce,
also the supported way to disable rebalancing entirely).  This is the
seam that later lets a second rebalancing track coexist and be toggled
without a restart.

The classic-track rebalancers self-gate on the mode at their existing
decision points, modeled on RebalanceUnmanager: EarningsRebalancer gates
its trigger, InitialRebalancer gates its run, and JitRebalancer gates
the top of htlc_accepted so that in off mode it does not defer the HTLC
and adds no forwarding latency.  A header-only Boss::ModG::
RebalanceModeProxy provides get_mode for the gate sites.  off composes
with the existing per-peer unmanage balance tag: off wins globally,
otherwise the per-peer tag still excludes specific peers.

The three rebalancers' unit tests now install a RebalanceModeManager on
the test bus so the self-gate query is answered (default classic, so
they behave as before).  Without a responder the RequestRebalanceMode
ReqResp is never satisfied and leaks, which the valgrind-checked tests
flag as a failure.

New files: Boss/RebalanceMode.hpp, Boss/Msg/RequestRebalanceMode.hpp,
Boss/Msg/ResponseRebalanceMode.hpp, Boss/ModG/RebalanceModeProxy.hpp,
Boss/Mod/RebalanceModeManager.{hpp,cpp}.
2026-06-23 14:12:03 -07:00

118 lines
2.9 KiB
C++

#include"Boss/Mod/RebalanceModeManager.hpp"
#include"Boss/Msg/Manifestation.hpp"
#include"Boss/Msg/ManifestOption.hpp"
#include"Boss/Msg/Option.hpp"
#include"Boss/Msg/OptionType.hpp"
#include"Boss/Msg/ProvideStatus.hpp"
#include"Boss/Msg/RequestRebalanceMode.hpp"
#include"Boss/Msg/ResponseRebalanceMode.hpp"
#include"Boss/Msg/SolicitStatus.hpp"
#include"Boss/RebalanceMode.hpp"
#include"Boss/log.hpp"
#include"Ev/Io.hpp"
#include"Json/Out.hpp"
#include"S/Bus.hpp"
#include"Util/make_unique.hpp"
namespace {
/* Single dynamic option: the config file sets the startup default and
* `setconfig clboss-rebalance-mode <mode>` switches it at runtime. */
auto const option_name = std::string("clboss-rebalance-mode");
}
namespace Boss { namespace Mod {
class RebalanceModeManager::Impl {
private:
S::Bus& bus;
RebalanceMode mode;
void start() {
mode = default_rebalance_mode;
bus.subscribe<Msg::Manifestation
>([this](Msg::Manifestation const& _) {
return bus.raise(Msg::ManifestOption{
option_name,
Msg::OptionType_String,
Json::Out::direct(std::string(
rebalance_mode_to_string(
default_rebalance_mode
)
)),
"Rebalancer mode: \"classic\" (the original "
"rebalancer) or \"off\" (disable rebalancing). "
" Set in the config for the startup default or "
"at runtime with "
"`setconfig clboss-rebalance-mode <mode>`.",
true /* dynamic: runtime-settable via setconfig */
});
});
bus.subscribe<Msg::Option
>([this](Msg::Option const& o) {
if (o.name != option_name)
return Ev::lift();
auto s = std::string(o.value);
auto m = RebalanceMode();
if (!rebalance_mode_from_string(s, m))
return Boss::log( bus, Error
, "RebalanceModeManager: "
"ignoring unrecognized "
"%s value \"%s\"; "
"keeping \"%s\"."
, option_name.c_str()
, s.c_str()
, rebalance_mode_to_string(mode)
);
if (m == mode)
return Ev::lift();
mode = m;
return Boss::log( bus, Info
, "RebalanceModeManager: "
"mode set to \"%s\"."
, rebalance_mode_to_string(mode)
);
});
bus.subscribe<Msg::RequestRebalanceMode
>([this](Msg::RequestRebalanceMode const& m) {
return bus.raise(Msg::ResponseRebalanceMode{
m.requester, mode
});
});
bus.subscribe<Msg::SolicitStatus
>([this](Msg::SolicitStatus const& _) {
auto out = Json::Out();
out.start_object()
.field("mode", std::string(rebalance_mode_to_string(mode)))
.end_object()
;
return bus.raise(Msg::ProvideStatus{
"rebalance_mode", std::move(out)
});
});
}
public:
Impl() =delete;
Impl(Impl&&) =delete;
Impl(Impl const&) =delete;
explicit
Impl(S::Bus& bus_) : bus(bus_) {
start();
}
};
RebalanceModeManager::RebalanceModeManager(RebalanceModeManager&&) =default;
RebalanceModeManager::~RebalanceModeManager() =default;
RebalanceModeManager::RebalanceModeManager(S::Bus& bus)
: pimpl(Util::make_unique<Impl>(bus)) { }
}}