Plugins provide services: hot-reload + state-saving via the registry
Re-expresses the old FSM-plugin hot-plug through the service layer, reusing the unchanged Plugin module (PluginManager, v2 serialize/ deserialize ABI, FileWatcher, atomic-rename install). Service/PluginService.hpp: PluginServiceBinding<I> resolves a plugin's exported service (extern "C" void_plugin_service(instance) -> IService*) via PluginManager::raw_symbol and registers it under interface I. The registry entry pins the plugin instance (and its mapped .so) so in-flight callers never see a freed object. enable_hot_reload() re-binds on each reload; the FSMs resolve I through their context, so a running FSM picks up the new implementation on its next service<I>() call with no code patched. I must derive IService (single inheritance) so the host recovers it from IService* by static_cast — no dynamic_cast across RTLD_LOCAL. Tests: ServicePluginV1/V2 (the plugin object is the service: IPlugin + ICalc : IService; v2 migrates v1 state via Json::MigrationChain). FsmServiceHotReload.cpp: an Orchestrator-launched FSM accumulates through the plugin service while a reloader thread flips v1<->v2 under load — state survives every migration, the FSM runs to completion, no crash; and a deterministic reload test proving the implementation swaps (version 1->2) with the running total migrated. Docs: CLAUDE.md/README updated for the new Fsm, Service, and Orchestrator (StateMachine row removed). 965 tests green, ASan+UBSan clean, stable across runs, -Werror clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d5e53a4c75
commit
7b6cd7341a
8 files changed
+552
-5
No files matched your search
@@ -116,4 +116,25 @@ if(TARGET Void::Plugin)
|
||||
target_include_directories(VoidTests PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins")
|
||||
add_dependencies(VoidTests VoidStatefulPluginV1 VoidStatefulPluginV2)
|
||||
|
||||
# Service plugins (a plugin provides an IService-derived interface) for
|
||||
# the FSM x Service hot-reload integration test. Same plugin name; v2
|
||||
# migrates v1 state.
|
||||
if(TARGET Void::Service AND TARGET Void::Data)
|
||||
add_library(VoidServicePluginV1 SHARED
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins/ServicePluginV1.cpp")
|
||||
add_library(VoidServicePluginV2 SHARED
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins/ServicePluginV2.cpp")
|
||||
foreach(_sp VoidServicePluginV1 VoidServicePluginV2)
|
||||
target_link_libraries(${_sp}
|
||||
PRIVATE Void::Plugin Void::Service Void::Data)
|
||||
target_include_directories(${_sp} PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins")
|
||||
endforeach()
|
||||
target_compile_definitions(VoidTests PRIVATE
|
||||
"VOID_SERVICE_V1_PATH=\"$<TARGET_FILE:VoidServicePluginV1>\""
|
||||
"VOID_SERVICE_V2_PATH=\"$<TARGET_FILE:VoidServicePluginV2>\""
|
||||
)
|
||||
add_dependencies(VoidTests VoidServicePluginV1 VoidServicePluginV2)
|
||||
endif()
|
||||
endif()
|
||||
@@ -0,0 +1,147 @@
|
||||
#include <Void/Fsm/Fsm.hpp>
|
||||
#include <Void/Fsm/Orchestrator.hpp>
|
||||
#include <Void/Plugin/Manager.hpp>
|
||||
#include <Void/Service/PluginService.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include "ServiceInterface.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::MachineOptions;
|
||||
using Void::Fsm::Step;
|
||||
|
||||
#if defined(VOID_SERVICE_V1_PATH) && defined(VOID_SERVICE_V2_PATH) && \
|
||||
__has_include(<dlfcn.h>)
|
||||
|
||||
namespace
|
||||
{
|
||||
void install(const std::filesystem::path& src,
|
||||
const std::filesystem::path& dst)
|
||||
{
|
||||
auto staging = dst;
|
||||
staging += ".incoming";
|
||||
std::filesystem::copy_file(
|
||||
src, staging, std::filesystem::copy_options::overwrite_existing);
|
||||
std::filesystem::rename(staging, dst);
|
||||
}
|
||||
|
||||
// An FSM that drives the plugin-provided ICalc service: each Work step
|
||||
// accumulates and loops until a target total is reached. It resolves
|
||||
// the service every step, so it picks up a reloaded implementation.
|
||||
class Accumulate : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
struct Work : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& svc = service<VoidTest::ICalc>();
|
||||
const int total = svc.accumulate(1);
|
||||
if (total >= 500) {
|
||||
co_return succeed(total);
|
||||
}
|
||||
co_return next<Work>();
|
||||
}
|
||||
};
|
||||
|
||||
Accumulate() { initial<Work>(); } // own worker
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// A plugin provides the ICalc service; an FSM (via the orchestrator's
|
||||
// registry) calls it continuously while the plugin is hot-reloaded v1<->v2
|
||||
// under load. The running total survives every migration and the FSM keeps
|
||||
// progressing to completion.
|
||||
Void::Testing::Test internal_fsm_service_hot_reload_under_load()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_svcplugin"};
|
||||
const auto so = tmp.path() / "calc.so";
|
||||
std::filesystem::copy_file(VOID_SERVICE_V1_PATH, so);
|
||||
|
||||
Void::Fsm::Orchestrator orch;
|
||||
Void::Plugin::PluginManager plugins;
|
||||
Void::Testing::expect(plugins.load(so).has_value());
|
||||
|
||||
// Bind the plugin's service into the orchestrator's registry, with
|
||||
// hot-reload re-binding.
|
||||
Void::Service::PluginServiceBinding<VoidTest::ICalc> binding{
|
||||
plugins, orch.services(), "CalcPlugin"};
|
||||
Void::Testing::expect(binding.bind());
|
||||
|
||||
// Launch the FSM; it resolves ICalc each step.
|
||||
auto handle = orch.launch<Accumulate>();
|
||||
Void::Testing::expect(static_cast<bool>(handle));
|
||||
|
||||
// Hammer reloads v1<->v2 while the FSM runs.
|
||||
std::atomic<bool> stop{false};
|
||||
std::thread reloader([&] {
|
||||
int i = 0;
|
||||
while (!stop.load(std::memory_order_relaxed)) {
|
||||
const char* src =
|
||||
(i % 2 == 0) ? VOID_SERVICE_V2_PATH : VOID_SERVICE_V1_PATH;
|
||||
install(src, so);
|
||||
if (plugins.reload("CalcPlugin").has_value()) {
|
||||
(void)binding.bind(); // re-register the new instance
|
||||
}
|
||||
++i;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{3});
|
||||
}
|
||||
});
|
||||
|
||||
// Wait for the FSM to finish (it accumulates to >= 500).
|
||||
for (int i = 0; i < 1000 && !handle.finished(); ++i) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
||||
}
|
||||
stop.store(true);
|
||||
reloader.join();
|
||||
|
||||
Void::Testing::expect(handle.status() == Void::Fsm::Status::Succeeded);
|
||||
|
||||
// The accumulated total survived migrations: a fresh resolve sees a
|
||||
// total at least as large as the FSM's completion threshold.
|
||||
auto calc = orch.services().get<VoidTest::ICalc>();
|
||||
Void::Testing::expect(calc != nullptr);
|
||||
Void::Testing::expect(calc->accumulate(0) >= 500);
|
||||
|
||||
orch.stop_all();
|
||||
}
|
||||
|
||||
// The seamless-swap path without load: bind v1, reload to v2, confirm the
|
||||
// service the FSM resolves is now v2 with the v1 total migrated.
|
||||
Void::Testing::Test internal_service_reload_migrates_and_swaps()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_svcplugin"};
|
||||
const auto so = tmp.path() / "calc.so";
|
||||
std::filesystem::copy_file(VOID_SERVICE_V1_PATH, so);
|
||||
|
||||
Void::Service::ServiceRegistry registry;
|
||||
Void::Plugin::PluginManager plugins;
|
||||
Void::Testing::expect(plugins.load(so).has_value());
|
||||
|
||||
Void::Service::PluginServiceBinding<VoidTest::ICalc> binding{
|
||||
plugins, registry, "CalcPlugin"};
|
||||
Void::Testing::expect(binding.bind());
|
||||
|
||||
auto v1 = registry.get<VoidTest::ICalc>();
|
||||
Void::Testing::expect_eq(v1->version(), 1);
|
||||
v1->accumulate(40); // total = 40
|
||||
|
||||
install(VOID_SERVICE_V2_PATH, so);
|
||||
Void::Testing::expect(plugins.reload("CalcPlugin").has_value());
|
||||
Void::Testing::expect(binding.bind());
|
||||
|
||||
auto v2 = registry.get<VoidTest::ICalc>();
|
||||
Void::Testing::expect_eq(v2->version(), 2); // implementation swapped
|
||||
Void::Testing::expect_eq(v2->accumulate(0), 40); // state migrated
|
||||
}
|
||||
|
||||
#endif // service plugins available
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Service/Service.hpp>
|
||||
|
||||
// Shared host/plugin interface for the service hot-reload tests. It
|
||||
// derives Void::Service::IService so the host recovers it from the
|
||||
// plugin's exported IService* by a single-inheritance static_cast (no
|
||||
// dynamic_cast across the RTLD_LOCAL boundary).
|
||||
namespace VoidTest
|
||||
{
|
||||
class ICalc : public Void::Service::IService
|
||||
{
|
||||
public:
|
||||
|
||||
/// Returns an accumulated value; each call adds `delta` to the
|
||||
/// service's running total and returns the new total. The total
|
||||
/// is the state migrated across plugin reloads.
|
||||
virtual int accumulate(int delta) = 0;
|
||||
|
||||
/// Schema version of this build (1 or 2) — lets tests confirm a
|
||||
/// reload actually swapped the implementation.
|
||||
[[nodiscard]] virtual int version() const = 0;
|
||||
};
|
||||
} // namespace VoidTest
|
||||
@@ -0,0 +1,116 @@
|
||||
#include "ServiceInterface.hpp"
|
||||
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <Void/Plugin/Plugin.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
// Service plugin, schema v1. The plugin object is the service: it derives
|
||||
// IPlugin (for the loader) and ICalc (the host interface, which derives
|
||||
// IService). State (the running total) survives reloads via the v2 state
|
||||
// ABI, serialized with the reflection JSON serializer.
|
||||
|
||||
namespace
|
||||
{
|
||||
struct StateV1 {
|
||||
int total = 0;
|
||||
};
|
||||
|
||||
class CalcPlugin final : public Void::Plugin::IPlugin,
|
||||
public VoidTest::ICalc
|
||||
{
|
||||
public:
|
||||
|
||||
// IPlugin
|
||||
[[nodiscard]] std::string_view name() const noexcept override
|
||||
{
|
||||
return "CalcPlugin";
|
||||
}
|
||||
|
||||
// IService
|
||||
[[nodiscard]] std::string_view service_name() const override
|
||||
{
|
||||
return "CalcPlugin";
|
||||
}
|
||||
|
||||
// ICalc
|
||||
int accumulate(int delta) override
|
||||
{
|
||||
return m_total.fetch_add(delta, std::memory_order_relaxed) +
|
||||
delta;
|
||||
}
|
||||
[[nodiscard]] int version() const override { return 1; }
|
||||
|
||||
std::atomic<int> m_total{0};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
template <> struct Void::Json::StateTraits<StateV1> {
|
||||
static constexpr std::uint32_t version = 1;
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
|
||||
void* void_plugin_create() { return new CalcPlugin; }
|
||||
|
||||
void void_plugin_destroy(void* p) { delete static_cast<CalcPlugin*>(p); }
|
||||
|
||||
Void::Plugin::PluginInfo void_plugin_info()
|
||||
{
|
||||
return {
|
||||
.abi_version = Void::Plugin::ABI_VERSION,
|
||||
.name = "CalcPlugin",
|
||||
.plugin_version = "1.0.0",
|
||||
.description = "Calc service plugin (schema v1)",
|
||||
};
|
||||
}
|
||||
|
||||
// Hand the host the IService subobject of the plugin's ICalc.
|
||||
Void::Service::IService* void_plugin_service(void* instance)
|
||||
{
|
||||
auto* self = static_cast<CalcPlugin*>(instance);
|
||||
return static_cast<Void::Service::IService*>(
|
||||
static_cast<VoidTest::ICalc*>(self));
|
||||
}
|
||||
|
||||
std::uint32_t void_plugin_state_version() { return 1; }
|
||||
|
||||
const char* void_plugin_serialize_state(void* instance)
|
||||
{
|
||||
const auto* self = static_cast<CalcPlugin*>(instance);
|
||||
const StateV1 state{.total = self->m_total.load(std::memory_order_relaxed)};
|
||||
const auto json =
|
||||
Void::Json::serialize(Void::Json::to_versioned_json(state));
|
||||
char* out = static_cast<char*>(std::malloc(json.size() + 1));
|
||||
if (out != nullptr) {
|
||||
std::memcpy(out, json.c_str(), json.size() + 1);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void void_plugin_free_string(const char* s)
|
||||
{
|
||||
std::free(const_cast<char*>(s)); // NOLINT
|
||||
}
|
||||
|
||||
int void_plugin_deserialize_state(void* instance, const char* json,
|
||||
std::uint32_t /*from_version*/)
|
||||
{
|
||||
auto* self = static_cast<CalcPlugin*>(instance);
|
||||
const auto parsed = Void::Json::parse(json);
|
||||
if (!parsed.has_value()) {
|
||||
return 1;
|
||||
}
|
||||
const auto state = Void::Json::from_versioned_json<StateV1>(*parsed);
|
||||
if (!state.has_value()) {
|
||||
return 2;
|
||||
}
|
||||
self->m_total.store(state->total, std::memory_order_relaxed);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,121 @@
|
||||
#include "ServiceInterface.hpp"
|
||||
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <Void/Plugin/Plugin.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
// Service plugin, schema v2. The plugin object is the service: it derives
|
||||
// IPlugin (for the loader) and ICalc (the host interface, which derives
|
||||
// IService). State (the running total) survives reloads via the v2 state
|
||||
// ABI, serialized with the reflection JSON serializer.
|
||||
|
||||
namespace
|
||||
{
|
||||
struct StateV2 {
|
||||
int total = 0;
|
||||
};
|
||||
|
||||
class CalcPlugin final : public Void::Plugin::IPlugin,
|
||||
public VoidTest::ICalc
|
||||
{
|
||||
public:
|
||||
|
||||
// IPlugin
|
||||
[[nodiscard]] std::string_view name() const noexcept override
|
||||
{
|
||||
return "CalcPlugin";
|
||||
}
|
||||
|
||||
// IService
|
||||
[[nodiscard]] std::string_view service_name() const override
|
||||
{
|
||||
return "CalcPlugin";
|
||||
}
|
||||
|
||||
// ICalc
|
||||
int accumulate(int delta) override
|
||||
{
|
||||
return m_total.fetch_add(delta, std::memory_order_relaxed) +
|
||||
delta;
|
||||
}
|
||||
[[nodiscard]] int version() const override { return 2; }
|
||||
|
||||
std::atomic<int> m_total{0};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
template <> struct Void::Json::StateTraits<StateV2> {
|
||||
static constexpr std::uint32_t version = 2;
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
|
||||
void* void_plugin_create() { return new CalcPlugin; }
|
||||
|
||||
void void_plugin_destroy(void* p) { delete static_cast<CalcPlugin*>(p); }
|
||||
|
||||
Void::Plugin::PluginInfo void_plugin_info()
|
||||
{
|
||||
return {
|
||||
.abi_version = Void::Plugin::ABI_VERSION,
|
||||
.name = "CalcPlugin",
|
||||
.plugin_version = "2.0.0",
|
||||
.description = "Calc service plugin (schema v2)",
|
||||
};
|
||||
}
|
||||
|
||||
// Hand the host the IService subobject of the plugin's ICalc.
|
||||
Void::Service::IService* void_plugin_service(void* instance)
|
||||
{
|
||||
auto* self = static_cast<CalcPlugin*>(instance);
|
||||
return static_cast<Void::Service::IService*>(
|
||||
static_cast<VoidTest::ICalc*>(self));
|
||||
}
|
||||
|
||||
std::uint32_t void_plugin_state_version() { return 2; }
|
||||
|
||||
const char* void_plugin_serialize_state(void* instance)
|
||||
{
|
||||
const auto* self = static_cast<CalcPlugin*>(instance);
|
||||
const StateV2 state{.total = self->m_total.load(std::memory_order_relaxed)};
|
||||
const auto json =
|
||||
Void::Json::serialize(Void::Json::to_versioned_json(state));
|
||||
char* out = static_cast<char*>(std::malloc(json.size() + 1));
|
||||
if (out != nullptr) {
|
||||
std::memcpy(out, json.c_str(), json.size() + 1);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
void void_plugin_free_string(const char* s)
|
||||
{
|
||||
std::free(const_cast<char*>(s)); // NOLINT
|
||||
}
|
||||
|
||||
int void_plugin_deserialize_state(void* instance, const char* json,
|
||||
std::uint32_t /*from_version*/)
|
||||
{
|
||||
auto* self = static_cast<CalcPlugin*>(instance);
|
||||
const auto parsed = Void::Json::parse(json);
|
||||
if (!parsed.has_value()) {
|
||||
return 1;
|
||||
}
|
||||
// Migrate v1 envelopes up to v2 (the `total` field is unchanged, so
|
||||
// the step is identity — the running total carries across the swap).
|
||||
Void::Json::MigrationChain migrations;
|
||||
migrations.add(1, [](Void::Json::Value data) { return data; });
|
||||
const auto state =
|
||||
Void::Json::from_versioned_json<StateV2>(*parsed, migrations);
|
||||
if (!state.has_value()) {
|
||||
return 2;
|
||||
}
|
||||
self->m_total.store(state->total, std::memory_order_relaxed);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
Reference in new issue
Block a user