feat(plugin): add CAbi.h v1, CModuleHost quiesce/reload/pins, and unlocked PluginManager staging
This commit is contained in:
1 parent
1474a69ab1
commit
a24e086e47
16 files changed
+3961
-164
No files matched your search
@@ -0,0 +1,350 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Operation.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Plugin/CAbi.h>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Plugin
|
||||
{
|
||||
enum class PluginErrorCode : std::int32_t {
|
||||
none = 0,
|
||||
invalid_argument = 1,
|
||||
file_not_found = 2,
|
||||
dlopen_failed = 3,
|
||||
symbol_missing = 4,
|
||||
abi_mismatch = 5,
|
||||
activation_failed = 6,
|
||||
not_ready = 7,
|
||||
quiesce_failed = 8,
|
||||
resume_degraded = 9,
|
||||
checkpoint_failed = 10,
|
||||
restore_failed = 11,
|
||||
restart_required = 12,
|
||||
cycle_detected = 13,
|
||||
missing_dependency = 14,
|
||||
retention_limit = 15,
|
||||
budget_exceeded = 16,
|
||||
conflict = 17,
|
||||
cancelled = 18,
|
||||
timeout = 19,
|
||||
not_found = 20,
|
||||
internal = 21,
|
||||
};
|
||||
|
||||
[[nodiscard]] inline Void::Result::Error
|
||||
make_plugin_error(PluginErrorCode code, std::string message = {})
|
||||
{
|
||||
using Void::Result::Error;
|
||||
using Void::Result::ErrorCode;
|
||||
using Void::Result::ErrorKind;
|
||||
|
||||
ErrorKind kind = ErrorKind::internal;
|
||||
switch (code) {
|
||||
case PluginErrorCode::none:
|
||||
kind = ErrorKind::internal;
|
||||
break;
|
||||
case PluginErrorCode::invalid_argument:
|
||||
case PluginErrorCode::abi_mismatch:
|
||||
case PluginErrorCode::cycle_detected:
|
||||
kind = ErrorKind::invalid_argument;
|
||||
break;
|
||||
case PluginErrorCode::file_not_found:
|
||||
case PluginErrorCode::symbol_missing:
|
||||
case PluginErrorCode::missing_dependency:
|
||||
case PluginErrorCode::not_found:
|
||||
kind = ErrorKind::not_found;
|
||||
break;
|
||||
case PluginErrorCode::dlopen_failed:
|
||||
kind = ErrorKind::io;
|
||||
break;
|
||||
case PluginErrorCode::conflict:
|
||||
case PluginErrorCode::restart_required:
|
||||
kind = ErrorKind::conflict;
|
||||
break;
|
||||
case PluginErrorCode::cancelled:
|
||||
kind = ErrorKind::cancelled;
|
||||
break;
|
||||
case PluginErrorCode::timeout:
|
||||
kind = ErrorKind::timeout;
|
||||
break;
|
||||
case PluginErrorCode::retention_limit:
|
||||
case PluginErrorCode::budget_exceeded:
|
||||
kind = ErrorKind::exhausted;
|
||||
break;
|
||||
case PluginErrorCode::activation_failed:
|
||||
case PluginErrorCode::not_ready:
|
||||
case PluginErrorCode::quiesce_failed:
|
||||
case PluginErrorCode::resume_degraded:
|
||||
case PluginErrorCode::checkpoint_failed:
|
||||
case PluginErrorCode::restore_failed:
|
||||
case PluginErrorCode::internal:
|
||||
kind = ErrorKind::internal;
|
||||
break;
|
||||
}
|
||||
return Error{kind, std::move(message),
|
||||
ErrorCode{Void::Result::kPluginErrorDomain,
|
||||
static_cast<std::int32_t>(code)}};
|
||||
}
|
||||
|
||||
struct PluginHostLimits {
|
||||
std::size_t max_modules{64};
|
||||
std::size_t max_interfaces_per_module{256};
|
||||
std::size_t max_retained_generations{4};
|
||||
std::size_t max_mapped_bytes_per_module{64 * 1024 * 1024};
|
||||
std::size_t max_checkpoint_bytes{4 * 1024 * 1024};
|
||||
std::size_t max_config_bytes{1 * 1024 * 1024};
|
||||
std::size_t max_async_activities{1024};
|
||||
std::size_t max_pending_callbacks{256};
|
||||
std::chrono::milliseconds prepare_timeout{
|
||||
std::chrono::seconds{5}};
|
||||
std::chrono::milliseconds quiesce_timeout{
|
||||
std::chrono::seconds{5}};
|
||||
std::chrono::milliseconds readiness_timeout{
|
||||
std::chrono::seconds{10}};
|
||||
std::chrono::milliseconds total_reload_timeout{
|
||||
std::chrono::seconds{30}};
|
||||
bool allow_safe_dlclose{false};
|
||||
};
|
||||
|
||||
enum class GenerationHealth : std::uint8_t {
|
||||
Healthy = 0,
|
||||
Quiescing,
|
||||
Degraded,
|
||||
Retiring,
|
||||
Retired,
|
||||
};
|
||||
|
||||
struct GenerationSummary {
|
||||
std::uint64_t generation{0};
|
||||
std::string module_id{};
|
||||
std::string version{};
|
||||
std::uint64_t image_bytes{0};
|
||||
std::size_t external_refs{0};
|
||||
std::size_t activity_pins{0};
|
||||
GenerationHealth health{GenerationHealth::Healthy};
|
||||
bool dlclosed{false};
|
||||
};
|
||||
|
||||
struct ReloadEvent {
|
||||
std::string module_id{};
|
||||
std::uint64_t generation{0};
|
||||
bool rollback{false};
|
||||
std::string detail{};
|
||||
};
|
||||
|
||||
struct BatchModuleSpec {
|
||||
std::filesystem::path path{};
|
||||
std::string config{};
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
struct GenerationRecord;
|
||||
}
|
||||
|
||||
/// RAII pin holding an async activity count on a generation. Destroying
|
||||
/// it may schedule asynchronous host retirement if no refs/pins remain.
|
||||
class ActivityPin
|
||||
{
|
||||
public:
|
||||
|
||||
ActivityPin() noexcept = default;
|
||||
~ActivityPin();
|
||||
ActivityPin(ActivityPin&& other) noexcept;
|
||||
ActivityPin& operator=(ActivityPin&& other) noexcept;
|
||||
ActivityPin(const ActivityPin&) = delete;
|
||||
ActivityPin& operator=(const ActivityPin&) = delete;
|
||||
|
||||
[[nodiscard]] explicit operator bool() const noexcept
|
||||
{
|
||||
return m_rec != nullptr;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t generation() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
friend class CModuleHost;
|
||||
friend class GenerationLease;
|
||||
explicit ActivityPin(std::shared_ptr<detail::GenerationRecord> rec)
|
||||
: m_rec(std::move(rec))
|
||||
{
|
||||
}
|
||||
std::shared_ptr<detail::GenerationRecord> m_rec{};
|
||||
};
|
||||
|
||||
/// Owning lease to a queried C interface function table from an exact
|
||||
/// generation. Releases via release_interface before dropping generation ref.
|
||||
class InterfaceLease
|
||||
{
|
||||
public:
|
||||
|
||||
InterfaceLease() noexcept = default;
|
||||
~InterfaceLease();
|
||||
InterfaceLease(InterfaceLease&& other) noexcept;
|
||||
InterfaceLease& operator=(InterfaceLease&& other) noexcept;
|
||||
InterfaceLease(const InterfaceLease&) = delete;
|
||||
InterfaceLease& operator=(const InterfaceLease&) = delete;
|
||||
|
||||
[[nodiscard]] explicit operator bool() const noexcept
|
||||
{
|
||||
return m_view.function_table != nullptr;
|
||||
}
|
||||
|
||||
template <typename TableT>
|
||||
[[nodiscard]] const TableT* table() const noexcept
|
||||
{
|
||||
if (m_view.function_table == nullptr ||
|
||||
m_view.table_size < sizeof(TableT)) {
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<const TableT*>(m_view.function_table);
|
||||
}
|
||||
|
||||
[[nodiscard]] const void_interface_view_v1& view() const noexcept
|
||||
{
|
||||
return m_view;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t generation() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
friend class GenerationLease;
|
||||
InterfaceLease(std::shared_ptr<detail::GenerationRecord> rec,
|
||||
void_interface_id_v1 id,
|
||||
void_interface_view_v1 view) noexcept
|
||||
: m_rec(std::move(rec)), m_id(id), m_view(view)
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<detail::GenerationRecord> m_rec{};
|
||||
void_interface_id_v1 m_id{};
|
||||
void_interface_view_v1 m_view{};
|
||||
};
|
||||
|
||||
/// Owning generation reference acquired from CModuleHost. Prevents module
|
||||
/// deactivation and dlclose while held, and supports activity pinning,
|
||||
/// C interface querying, and bounded async operation submission.
|
||||
class GenerationLease
|
||||
{
|
||||
public:
|
||||
|
||||
GenerationLease() noexcept = default;
|
||||
~GenerationLease();
|
||||
GenerationLease(const GenerationLease& other);
|
||||
GenerationLease& operator=(const GenerationLease& other);
|
||||
GenerationLease(GenerationLease&& other) noexcept;
|
||||
GenerationLease& operator=(GenerationLease&& other) noexcept;
|
||||
|
||||
[[nodiscard]] explicit operator bool() const noexcept
|
||||
{
|
||||
return m_rec != nullptr;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t generation() const noexcept;
|
||||
[[nodiscard]] std::string_view module_id() const noexcept;
|
||||
[[nodiscard]] std::string_view version() const noexcept;
|
||||
[[nodiscard]] GenerationHealth health() const noexcept;
|
||||
|
||||
[[nodiscard]] ActivityPin acquire_activity_pin() const;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<InterfaceLease>
|
||||
query_interface(const void_interface_id_v1& id,
|
||||
std::uint32_t min_major,
|
||||
std::uint32_t min_table_size) const;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::vector<std::uint8_t>>
|
||||
run_async(std::uint32_t op_kind,
|
||||
const std::vector<std::uint8_t>& payload,
|
||||
Void::Async::OperationOptions options = {}) const;
|
||||
|
||||
private:
|
||||
|
||||
friend class CModuleHost;
|
||||
struct AlreadyRefCountedTag {};
|
||||
explicit GenerationLease(
|
||||
std::shared_ptr<detail::GenerationRecord> rec);
|
||||
GenerationLease(std::shared_ptr<detail::GenerationRecord> rec,
|
||||
AlreadyRefCountedTag) noexcept
|
||||
: m_rec(std::move(rec))
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<detail::GenerationRecord> m_rec{};
|
||||
};
|
||||
|
||||
/// Hardened C-ABI module host implementing size/version negotiation,
|
||||
/// independent external_refs & activity_pins, host-thread retirement queue,
|
||||
/// unlocked quiesce/checkpoint/restore/readiness/resume reload, and
|
||||
/// dependency-ordered batch loading.
|
||||
class CModuleHost
|
||||
{
|
||||
public:
|
||||
|
||||
struct Impl;
|
||||
|
||||
explicit CModuleHost(
|
||||
PluginHostLimits limits = {},
|
||||
const Void::Time::MonotonicClock& clock =
|
||||
Void::Time::SystemMonotonicClock::instance());
|
||||
~CModuleHost();
|
||||
|
||||
CModuleHost(const CModuleHost&) = delete;
|
||||
CModuleHost& operator=(const CModuleHost&) = delete;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::string>
|
||||
load(const std::filesystem::path& path,
|
||||
std::string_view config = {},
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::vector<std::string>>
|
||||
load_batch(const std::vector<BatchModuleSpec>& specs,
|
||||
Void::Async::OperationOptions options = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::uint64_t>
|
||||
replace(std::string_view module_id,
|
||||
const std::filesystem::path& new_path,
|
||||
std::string_view config = {},
|
||||
std::optional<std::uint64_t> expected_generation =
|
||||
std::nullopt,
|
||||
Void::Async::OperationOptions options = {},
|
||||
std::function<Void::Result::Result<void>(
|
||||
const GenerationLease&)>
|
||||
on_publish_locked = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
unload(std::string_view module_id);
|
||||
|
||||
[[nodiscard]] GenerationLease
|
||||
acquire(std::string_view module_id) const;
|
||||
|
||||
[[nodiscard]] std::vector<GenerationSummary>
|
||||
retained_generations(std::string_view module_id) const;
|
||||
|
||||
[[nodiscard]] std::size_t
|
||||
mapped_bytes(std::string_view module_id) const;
|
||||
|
||||
std::size_t add_observer(std::function<void(const ReloadEvent&)> cb);
|
||||
void remove_observer(std::size_t id);
|
||||
|
||||
/// Drain retirement queue and shutdown background retirement worker.
|
||||
void close_and_join();
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<Impl> m_impl;
|
||||
};
|
||||
|
||||
} // namespace Void::Plugin
|
||||
Reference in new issue
Block a user