Files
Library/tests/plugins/StatefulPluginV2.cpp
T
faumarayandClaude Fable 5 b343bce396 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>
2026-06-13 18:08:06 +03:00

117 lines
2.8 KiB
C++

#include "Stateful.hpp"
#include <Void/Data/Reflect.hpp>
#include <atomic>
#include <cstdlib>
#include <cstring>
#include <string>
// Stateful plugin, schema v2 — same plugin name as v1, so a PluginManager
// reload swaps v1 -> v2 in place. State gains a `tag` field; v1 JSON is
// migrated by the chain living here (the new build knows the old format).
namespace
{
struct StateV2 {
int count = 0;
std::string tag = "v2"; // added in v2
};
class PluginV2 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 2; }
std::atomic<int> m_count{0};
std::string m_tag{"v2"};
};
Void::Json::MigrationChain migrations()
{
Void::Json::MigrationChain chain;
// v1 -> v2: keep count, introduce tag marking the upgrade.
chain.add(1, [](Void::Json::Value data) {
data.as_object().emplace_back("tag", Void::Json::Value{"migrated"});
return data;
});
return chain;
}
} // namespace
template <> struct Void::Json::StateTraits<StateV2> {
static constexpr std::uint32_t version = 2;
};
extern "C" {
void* void_plugin_create() { return new PluginV2; }
void void_plugin_destroy(void* p) { delete static_cast<PluginV2*>(p); }
Void::Plugin::PluginInfo void_plugin_info()
{
return {
.abi_version = Void::Plugin::ABI_VERSION,
.name = "StatefulPlugin",
.plugin_version = "2.0.0",
.description = "Stateful test plugin (schema v2)",
};
}
std::uint32_t void_plugin_state_version() { return 2; }
const char* void_plugin_serialize_state(void* instance)
{
const auto* self = static_cast<PluginV2*>(instance);
const StateV2 state{.count = self->counter(), .tag = self->m_tag};
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<PluginV2*>(instance);
const auto parsed = Void::Json::parse(json);
if (!parsed.has_value()) {
return 1;
}
// from_versioned_json reads the envelope's own __version and migrates
// up to StateV2's version (2) using the chain.
const auto state =
Void::Json::from_versioned_json<StateV2>(*parsed, migrations());
if (!state.has_value()) {
return 2;
}
self->m_count.store(state->count, std::memory_order_relaxed);
self->m_tag = state->tag;
return 0;
}
} // extern "C"