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
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Plugin/Manager.hpp>
|
||||
#include <Void/Service/Registry.hpp>
|
||||
#include <Void/Service/Service.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Service
|
||||
{
|
||||
/// C entry point a plugin exports to expose a service:
|
||||
/// extern "C" Void::Service::IService* void_plugin_service(void* inst);
|
||||
/// It returns the IService subobject of the plugin's service object
|
||||
/// (which must derive the host interface I, and I must derive
|
||||
/// IService). The host recovers I* by static_cast — no dynamic_cast, so
|
||||
/// it is robust across RTLD_LOCAL plugin boundaries.
|
||||
using service_factory_fn = IService* (*)(void* instance);
|
||||
|
||||
inline constexpr const char* kServiceSymbol = "void_plugin_service";
|
||||
|
||||
/// Binds a plugin-provided service (interface I) into a ServiceRegistry
|
||||
/// and keeps it bound across hot reloads: after each reload the new
|
||||
/// instance's service (carrying state migrated by the plugin's v2 ABI)
|
||||
/// is re-resolved and re-registered, so FSMs resolving I through their
|
||||
/// context transparently pick up the new implementation on their next
|
||||
/// call. The registry entry pins the plugin instance, so in-flight
|
||||
/// callers never see a freed object.
|
||||
///
|
||||
/// Requires: I derives Void::Service::IService; the plugin's service
|
||||
/// type derives I; the plugin exports void_plugin_service.
|
||||
template <typename I> class PluginServiceBinding
|
||||
{
|
||||
public:
|
||||
|
||||
static_assert(std::is_base_of_v<IService, I>,
|
||||
"plugin service interface must derive IService");
|
||||
|
||||
PluginServiceBinding(Plugin::PluginManager& manager,
|
||||
ServiceRegistry& registry, std::string name)
|
||||
: m_manager(&manager), m_registry(®istry),
|
||||
m_name(std::move(name))
|
||||
{
|
||||
}
|
||||
|
||||
/// Resolve the current instance's service and (re)register it.
|
||||
[[nodiscard]] bool bind()
|
||||
{
|
||||
auto handle = m_manager->acquire(m_name);
|
||||
if (!handle) {
|
||||
return false;
|
||||
}
|
||||
auto* fn = reinterpret_cast<service_factory_fn>(
|
||||
m_manager->raw_symbol(m_name, kServiceSymbol));
|
||||
if (fn == nullptr) {
|
||||
return false;
|
||||
}
|
||||
IService* base = fn(handle.get());
|
||||
if (base == nullptr) {
|
||||
return false;
|
||||
}
|
||||
I* iface = static_cast<I*>(base); // I : IService
|
||||
|
||||
// Keep the plugin instance (and its mapped .so) alive for
|
||||
// as long as the registry holds this service.
|
||||
auto owner = std::make_shared<Plugin::PluginHandle>(handle);
|
||||
std::shared_ptr<I> pinned(std::move(owner), iface);
|
||||
m_registry->provide<I>(std::move(pinned));
|
||||
Void::Log::debug("Service.Plugin",
|
||||
"bound service from plugin '{}'", m_name);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Auto-rebind on hot reload (debounced file watching). The
|
||||
/// manager keeps old instances alive while pinned, so the swap
|
||||
/// is seamless.
|
||||
void enable_hot_reload()
|
||||
{
|
||||
m_manager->on_reloaded([this](std::string_view name) {
|
||||
if (name == m_name) {
|
||||
(void)bind();
|
||||
}
|
||||
});
|
||||
m_manager->watch(m_name);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Plugin::PluginManager* m_manager;
|
||||
ServiceRegistry* m_registry;
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
} // namespace Void::Service
|
||||
Reference in new issue
Block a user