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
+87
-22
@@ -22,6 +22,8 @@ std::string_view to_string(LoadError e) noexcept
|
||||
case LoadError::AbiMismatch: return "ABI version mismatch";
|
||||
case LoadError::CreateFailed: return "plugin create() returned null";
|
||||
case LoadError::NotSupported: return "platform does not support dlopen";
|
||||
case LoadError::StateRejected: return "new instance rejected state";
|
||||
case LoadError::UnknownPlugin: return "no plugin registered under name";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
@@ -40,11 +42,53 @@ void PluginLoader::destroy_current() noexcept
|
||||
dlclose(handle_);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
destroy_ = nullptr;
|
||||
transfer_ = nullptr;
|
||||
destroy_ = nullptr;
|
||||
transfer_ = nullptr;
|
||||
state_version_ = nullptr;
|
||||
serialize_ = nullptr;
|
||||
free_string_ = nullptr;
|
||||
deserialize_ = nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Move plugin state old -> new across a reload. Prefers the v2
|
||||
// serialize/deserialize protocol (survives layout changes between
|
||||
// versions); falls back to the raw transfer_state hook; else no-op.
|
||||
// Returns false only when the new instance actively rejects the state.
|
||||
bool transfer_plugin_state(const PluginLoader& from, PluginLoader& to)
|
||||
{
|
||||
if (!from.plugin_ || !to.plugin_) {
|
||||
return true;
|
||||
}
|
||||
if (from.serialize_ && to.deserialize_) {
|
||||
const std::uint32_t from_version =
|
||||
from.state_version_ ? from.state_version_() : 0;
|
||||
const char* json = from.serialize_(from.plugin_);
|
||||
if (json != nullptr) {
|
||||
const int rc = to.deserialize_(to.plugin_, json, from_version);
|
||||
if (from.free_string_) {
|
||||
from.free_string_(json);
|
||||
}
|
||||
if (rc != 0) {
|
||||
Void::Log::warn("Plugin",
|
||||
"deserialize_state rejected state "
|
||||
"(from v{}, rc={})",
|
||||
from_version, rc);
|
||||
return false;
|
||||
}
|
||||
Void::Log::debug("Plugin",
|
||||
"state transferred via v2 protocol (v{})",
|
||||
from_version);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (to.transfer_) {
|
||||
to.transfer_(from.plugin_, to.plugin_);
|
||||
Void::Log::debug("Plugin", "state transferred via transfer_state");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::expected<void, LoadError>
|
||||
PluginLoader::load_from(const std::filesystem::path& path)
|
||||
{
|
||||
@@ -73,11 +117,18 @@ PluginLoader::load_from(const std::filesystem::path& path)
|
||||
return std::unexpected{LoadError::CreateFailed};
|
||||
}
|
||||
|
||||
handle_ = h;
|
||||
plugin_ = static_cast<IPlugin*>(raw);
|
||||
destroy_ = sym_destroy;
|
||||
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
info_ = info;
|
||||
handle_ = h;
|
||||
plugin_ = static_cast<IPlugin*>(raw);
|
||||
destroy_ = sym_destroy;
|
||||
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
state_version_ = reinterpret_cast<state_version_fn>(
|
||||
dlsym(h, kStateVersionSymbol));
|
||||
serialize_ = reinterpret_cast<serialize_fn>(dlsym(h, kSerializeSymbol));
|
||||
free_string_ = reinterpret_cast<free_string_fn>(
|
||||
dlsym(h, kFreeStringSymbol));
|
||||
deserialize_ = reinterpret_cast<deserialize_fn>(
|
||||
dlsym(h, kDeserializeSymbol));
|
||||
info_ = info;
|
||||
return {};
|
||||
#else
|
||||
(void)path;
|
||||
@@ -111,24 +162,32 @@ PluginLoader::load(std::filesystem::path path)
|
||||
PluginLoader::~PluginLoader() { destroy_current(); }
|
||||
|
||||
PluginLoader::PluginLoader(PluginLoader&& o) noexcept
|
||||
: handle_ {std::exchange(o.handle_, nullptr)}
|
||||
, plugin_ {std::exchange(o.plugin_, nullptr)}
|
||||
, destroy_ {std::exchange(o.destroy_, nullptr)}
|
||||
, transfer_{std::exchange(o.transfer_, nullptr)}
|
||||
, info_ {o.info_}
|
||||
, path_ {std::move(o.path_)}
|
||||
: handle_ {std::exchange(o.handle_, nullptr)}
|
||||
, plugin_ {std::exchange(o.plugin_, nullptr)}
|
||||
, destroy_ {std::exchange(o.destroy_, nullptr)}
|
||||
, transfer_ {std::exchange(o.transfer_, nullptr)}
|
||||
, state_version_{std::exchange(o.state_version_, nullptr)}
|
||||
, serialize_ {std::exchange(o.serialize_, nullptr)}
|
||||
, free_string_ {std::exchange(o.free_string_, nullptr)}
|
||||
, deserialize_ {std::exchange(o.deserialize_, nullptr)}
|
||||
, info_ {o.info_}
|
||||
, path_ {std::move(o.path_)}
|
||||
{}
|
||||
|
||||
PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
destroy_current();
|
||||
handle_ = std::exchange(o.handle_, nullptr);
|
||||
plugin_ = std::exchange(o.plugin_, nullptr);
|
||||
destroy_ = std::exchange(o.destroy_, nullptr);
|
||||
transfer_ = std::exchange(o.transfer_, nullptr);
|
||||
info_ = o.info_;
|
||||
path_ = std::move(o.path_);
|
||||
handle_ = std::exchange(o.handle_, nullptr);
|
||||
plugin_ = std::exchange(o.plugin_, nullptr);
|
||||
destroy_ = std::exchange(o.destroy_, nullptr);
|
||||
transfer_ = std::exchange(o.transfer_, nullptr);
|
||||
state_version_ = std::exchange(o.state_version_, nullptr);
|
||||
serialize_ = std::exchange(o.serialize_, nullptr);
|
||||
free_string_ = std::exchange(o.free_string_, nullptr);
|
||||
deserialize_ = std::exchange(o.deserialize_, nullptr);
|
||||
info_ = o.info_;
|
||||
path_ = std::move(o.path_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -159,9 +218,15 @@ std::expected<void, LoadError> PluginLoader::reload()
|
||||
return r;
|
||||
}
|
||||
|
||||
// Transfer state with both instances alive.
|
||||
if (plugin_ && next.plugin_ && next.transfer_)
|
||||
next.transfer_(plugin_, next.plugin_);
|
||||
// Transfer state with both instances alive. A rejection aborts the
|
||||
// reload and keeps the old instance.
|
||||
if (!transfer_plugin_state(*this, next)) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' aborted: state rejected",
|
||||
path_.string());
|
||||
return std::unexpected{LoadError::StateRejected};
|
||||
}
|
||||
|
||||
// Destroy old, install new.
|
||||
destroy_current();
|
||||
|
||||
Reference in new issue
Block a user