Add Plugin hot-plug: PluginManager, versioned state ABI, file watching
Backward-compatible C ABI v2 (Plugin.hpp; ABI_VERSION unchanged, IPlugin vtable frozen): optional void_plugin_state_version / serialize_state / free_string / deserialize_state(inst, json, from_version). State transfer prefers serialize->deserialize (survives layout changes; migrations live in the new .so via Json::MigrationChain), falling back to the existing transfer_state, then no-op. PluginLoader::reload gains this chain; the shared transfer_plugin_state helper drives it. Existing ABI-v1 plugins (TestPlugin) load and reload unchanged. PluginManager (Manager.hpp/.cpp): multi-plugin registry with RCU-style pinned handles. acquire(name) is one atomic<shared_ptr> load returning a PluginHandle that keeps its instance alive for the whole call. reload() is transactional: build new instance, transfer state from old, then atomic-exchange it in; on any failure the old instance stays. In-flight handles keep the old instance and its mapped .so alive (dlclose_on_swap= false) until they drop, so work never stops and callers never see a torn or freed object. watch(name) auto-reloads via FileSystem::FileWatcher (debounced); on_reloaded fires after each swap. Reload-safety: dlopen of a fresh-inode hotswap copy is required to load new bytes; deployments must install the new .so by atomic rename, never in-place overwrite of the mapped file (corrupts the running mapping -> SIGBUS). Documented; tests install via copy-to-.incoming + rename. Tests: StatefulPluginV1/V2 (shared IStateful interface, atomic counter; v2 adds a field and migrates v1 JSON). Manager.cpp covers load/acquire, v1->v2 migration, pinned-handle-survives-reload, legacy transfer_state fallback, and watch auto-reload. HotReloadUnderLoad.cpp: 6 reader threads calling through acquire() while 24 v1<->v2 reloads run concurrently -- zero crashes, state survives every migration, ASan+UBSan clean, stable across 10+ runs. 965 tests green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
697e07843c
commit
b343bce396
13 files changed
+1179
-38
No files matched your search
@@ -0,0 +1,98 @@
|
||||
#include "Stateful.hpp"
|
||||
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
// Stateful plugin, schema v1. State is a single counter, serialized as
|
||||
// {"__version":1,"data":{"count":N}} via the reflection JSON serializer.
|
||||
|
||||
namespace
|
||||
{
|
||||
struct StateV1 {
|
||||
int count = 0;
|
||||
};
|
||||
|
||||
class PluginV1 final : public VoidTest::IStateful
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] std::string_view name() const noexcept override
|
||||
{
|
||||
return "StatefulPlugin";
|
||||
}
|
||||
|
||||
[[nodiscard]] int counter() const override
|
||||
{
|
||||
return m_count.load(std::memory_order_relaxed);
|
||||
}
|
||||
void bump() override
|
||||
{
|
||||
m_count.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
[[nodiscard]] int schema_version() const override { return 1; }
|
||||
|
||||
std::atomic<int> m_count{0};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
template <> struct Void::Json::StateTraits<StateV1> {
|
||||
static constexpr std::uint32_t version = 1;
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
|
||||
void* void_plugin_create() { return new PluginV1; }
|
||||
|
||||
void void_plugin_destroy(void* p) { delete static_cast<PluginV1*>(p); }
|
||||
|
||||
Void::Plugin::PluginInfo void_plugin_info()
|
||||
{
|
||||
return {
|
||||
.abi_version = Void::Plugin::ABI_VERSION,
|
||||
.name = "StatefulPlugin",
|
||||
.plugin_version = "1.0.0",
|
||||
.description = "Stateful test plugin (schema v1)",
|
||||
};
|
||||
}
|
||||
|
||||
std::uint32_t void_plugin_state_version() { return 1; }
|
||||
|
||||
const char* void_plugin_serialize_state(void* instance)
|
||||
{
|
||||
const auto* self = static_cast<PluginV1*>(instance);
|
||||
const StateV1 state{.count = self->counter()};
|
||||
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<PluginV1*>(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_count.store(state->count, std::memory_order_relaxed);
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
Reference in new issue
Block a user