feat(service): immutable snapshots, owning ServiceHandle, DAG lifecycle, and ServicePlugin split
This commit is contained in:
1 parent
6b3dededac
commit
1474a69ab1
12 files changed
+2167
-137
No files matched your search
@@ -237,6 +237,21 @@ if(_has_storage)
|
||||
run_consumer(StorageOnly "${install_prefix}" "StorageOnly" ${_no_external_deps})
|
||||
endif()
|
||||
|
||||
has_modules(_has_service Service Core Functional Time Async LogState)
|
||||
if(_has_service)
|
||||
run_consumer(ServiceOnly "${install_prefix}" "ServiceOnly" ${_no_external_deps})
|
||||
endif()
|
||||
|
||||
has_modules(_has_service_plugin ServicePlugin Service Plugin Functional System Data LogState Text Time Async Core)
|
||||
if(_has_service_plugin)
|
||||
run_consumer(ServicePluginOnly "${install_prefix}" "ServicePluginOnly"
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_Boost=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_LibArchive=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_OpenSSL=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||
endif()
|
||||
|
||||
# Foundation consumer with Core-formatting bridge and zero external packages.
|
||||
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Memory Core)
|
||||
if(_has_foundation)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(VoidServiceOnlyConsumer LANGUAGES CXX)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS Service)
|
||||
|
||||
add_executable(void_service_only_consumer main.cpp)
|
||||
target_link_libraries(void_service_only_consumer PRIVATE Void::Service)
|
||||
target_compile_features(void_service_only_consumer PRIVATE cxx_std_26)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME ServiceOnlyConsumer COMMAND void_service_only_consumer)
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <Void/Service/Registry.hpp>
|
||||
#include <Void/Service/Service.hpp>
|
||||
#include <Void/Service/Types.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct ICounter {
|
||||
virtual ~ICounter() = default;
|
||||
virtual int value() const = 0;
|
||||
};
|
||||
|
||||
struct Counter final : Void::Service::IManagedService, ICounter {
|
||||
std::string_view service_name() const override { return "counter"; }
|
||||
int value() const override { return 42; }
|
||||
};
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Void::Service::ServiceRegistry reg;
|
||||
Void::Service::ProviderSet set;
|
||||
set.add<ICounter>("counter", std::make_shared<Counter>());
|
||||
auto ep = reg.start_and_publish(std::move(set));
|
||||
if (!ep) {
|
||||
return 1;
|
||||
}
|
||||
auto handle = reg.acquire<ICounter>();
|
||||
if (!handle || (*handle)->value() != 42 || handle->generation() != *ep) {
|
||||
return 2;
|
||||
}
|
||||
auto report = reg.close_and_join();
|
||||
return report && !report->clean() ? 0 : 3; // handle still held until return
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(VoidServicePluginOnlyConsumer LANGUAGES CXX)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS ServicePlugin)
|
||||
|
||||
add_executable(void_service_plugin_only_consumer main.cpp)
|
||||
target_link_libraries(void_service_plugin_only_consumer PRIVATE Void::ServicePlugin)
|
||||
target_compile_features(void_service_plugin_only_consumer PRIVATE cxx_std_26)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME ServicePluginOnlyConsumer COMMAND void_service_plugin_only_consumer)
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <Void/Service/PluginService.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
Void::Plugin::PluginManager plugins;
|
||||
Void::Service::ServiceRegistry registry;
|
||||
Void::Service::PluginServiceBinding<Void::Service::IService> binding{
|
||||
plugins, registry, "missing"};
|
||||
auto direct = Void::Service::acquire_plugin_service<Void::Service::IService>(
|
||||
plugins, "missing");
|
||||
return !binding.bind() && !direct.has_value() ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
#include <Void/Service/Registry.hpp>
|
||||
#include <Void/Service/Service.hpp>
|
||||
#include <Void/Service/Types.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <typeindex>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Service::ExpectedEpoch;
|
||||
using Void::Service::IManagedService;
|
||||
using Void::Service::IService;
|
||||
using Void::Service::ProviderSet;
|
||||
using Void::Service::ServiceErrorCode;
|
||||
using Void::Service::ServiceLimits;
|
||||
using Void::Service::ServiceRegistry;
|
||||
using Void::Time::ManualClock;
|
||||
|
||||
struct IA {
|
||||
virtual ~IA() = default;
|
||||
[[nodiscard]] virtual int id() const = 0;
|
||||
};
|
||||
|
||||
struct IB {
|
||||
virtual ~IB() = default;
|
||||
[[nodiscard]] virtual int tag() const = 0;
|
||||
};
|
||||
|
||||
struct IC {
|
||||
virtual ~IC() = default;
|
||||
[[nodiscard]] virtual int rank() const = 0;
|
||||
};
|
||||
|
||||
class TrackedService final : public IManagedService, public IA
|
||||
{
|
||||
public:
|
||||
|
||||
TrackedService(std::string name, int val,
|
||||
std::vector<std::string>* log,
|
||||
bool fail_start = false,
|
||||
bool fail_ready = false,
|
||||
bool throw_stop = false)
|
||||
: m_name(std::move(name)), m_val(val), m_log(log),
|
||||
m_fail_start(fail_start), m_fail_ready(fail_ready),
|
||||
m_throw_stop(throw_stop)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string_view service_name() const override
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
[[nodiscard]] int id() const override { return m_val; }
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> start_checked() override
|
||||
{
|
||||
if (m_log != nullptr) {
|
||||
m_log->push_back("start:" + m_name);
|
||||
}
|
||||
if (m_fail_start) {
|
||||
return Void::Result::fail(Void::Service::make_service_error(
|
||||
ServiceErrorCode::start_failed,
|
||||
"start failed for " + m_name));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
check_readiness() const override
|
||||
{
|
||||
if (m_fail_ready) {
|
||||
return Void::Result::fail(Void::Service::make_service_error(
|
||||
ServiceErrorCode::not_ready,
|
||||
"not ready: " + m_name));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void abort_start() noexcept override
|
||||
{
|
||||
if (m_log != nullptr) {
|
||||
m_log->push_back("abort:" + m_name);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> stop_checked() override
|
||||
{
|
||||
if (m_log != nullptr) {
|
||||
m_log->push_back("stop:" + m_name);
|
||||
}
|
||||
if (m_throw_stop) {
|
||||
return Void::Result::fail(Void::Service::make_service_error(
|
||||
ServiceErrorCode::stop_failed,
|
||||
"stop boom: " + m_name));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::string m_name;
|
||||
int m_val;
|
||||
std::vector<std::string>* m_log;
|
||||
bool m_fail_start;
|
||||
bool m_fail_ready;
|
||||
bool m_throw_stop;
|
||||
};
|
||||
|
||||
class DualInterfaceService final : public IManagedService, public IA, public IB
|
||||
{
|
||||
public:
|
||||
|
||||
explicit DualInterfaceService(std::vector<std::string>* log)
|
||||
: m_log(log)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string_view service_name() const override
|
||||
{
|
||||
return "Dual";
|
||||
}
|
||||
[[nodiscard]] int id() const override { return 10; }
|
||||
[[nodiscard]] int tag() const override { return 20; }
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> start_checked() override
|
||||
{
|
||||
++starts;
|
||||
if (m_log != nullptr) m_log->push_back("dual:start");
|
||||
return {};
|
||||
}
|
||||
[[nodiscard]] Void::Result::Result<void> stop_checked() override
|
||||
{
|
||||
++stops;
|
||||
if (m_log != nullptr) m_log->push_back("dual:stop");
|
||||
return {};
|
||||
}
|
||||
|
||||
int starts{0};
|
||||
int stops{0};
|
||||
std::vector<std::string>* m_log;
|
||||
};
|
||||
|
||||
class ReentrantDtorProvider final : public IA
|
||||
{
|
||||
public:
|
||||
|
||||
ReentrantDtorProvider(ServiceRegistry& reg, int val,
|
||||
std::atomic<int>* observations)
|
||||
: m_reg(reg), m_val(val), m_obs(observations)
|
||||
{
|
||||
}
|
||||
|
||||
~ReentrantDtorProvider() override
|
||||
{
|
||||
if (auto h = m_reg.acquire<IA>(); h) {
|
||||
if (m_obs != nullptr) {
|
||||
m_obs->fetch_add((*h)->id(), std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] int id() const override { return m_val; }
|
||||
|
||||
private:
|
||||
|
||||
ServiceRegistry& m_reg;
|
||||
int m_val;
|
||||
std::atomic<int>* m_obs;
|
||||
};
|
||||
|
||||
class ReentrantStartService final : public IService, public IB
|
||||
{
|
||||
public:
|
||||
|
||||
ReentrantStartService(ServiceRegistry& reg, std::atomic<int>* seen)
|
||||
: m_reg(reg), m_seen(seen)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string_view service_name() const override
|
||||
{
|
||||
return "ReentrantStart";
|
||||
}
|
||||
|
||||
void on_start() override
|
||||
{
|
||||
if (auto a = m_reg.acquire<IA>(); a) {
|
||||
m_seen->store((*a)->id(), std::memory_order_relaxed);
|
||||
}
|
||||
m_reg.provide<IC>(std::make_shared<SimpleC>(99));
|
||||
}
|
||||
|
||||
[[nodiscard]] int tag() const override { return 7; }
|
||||
|
||||
private:
|
||||
|
||||
struct SimpleC final : IC {
|
||||
explicit SimpleC(int r) : rank_val(r) {}
|
||||
[[nodiscard]] int rank() const override { return rank_val; }
|
||||
int rank_val;
|
||||
};
|
||||
|
||||
ServiceRegistry& m_reg;
|
||||
std::atomic<int>* m_seen;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test service_concurrent_remove_acquire_and_reentrant_dtor()
|
||||
{
|
||||
ServiceRegistry reg;
|
||||
std::atomic<int> dtor_observations{0};
|
||||
reg.provide<IA>(
|
||||
std::make_shared<ReentrantDtorProvider>(reg, 1, &dtor_observations));
|
||||
|
||||
auto held_first = reg.acquire<IA>();
|
||||
Void::Testing::expect(held_first.has_value());
|
||||
Void::Testing::expect_eq((*held_first)->id(), 1);
|
||||
|
||||
// Replace with another ReentrantDtorProvider; releasing held_first
|
||||
// later must invoke ~ReentrantDtorProvider cleanly without deadlocking.
|
||||
std::atomic<bool> stop{false};
|
||||
std::thread writer([&] {
|
||||
for (int i = 2; i < 60 && !stop.load(std::memory_order_relaxed); ++i) {
|
||||
reg.provide<IA>(std::make_shared<ReentrantDtorProvider>(
|
||||
reg, i, &dtor_observations));
|
||||
if (i % 7 == 0) {
|
||||
(void)reg.remove<IA>();
|
||||
}
|
||||
}
|
||||
});
|
||||
std::thread reader([&] {
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
if (auto h = reg.acquire<IA>(); h) {
|
||||
Void::Testing::expect((*h)->id() >= 1);
|
||||
}
|
||||
}
|
||||
stop.store(true, std::memory_order_relaxed);
|
||||
});
|
||||
writer.join();
|
||||
reader.join();
|
||||
|
||||
Void::Testing::expect_eq((*held_first)->id(), 1);
|
||||
held_first = Void::Result::fail(Void::Service::make_service_error(
|
||||
ServiceErrorCode::missing_service, "drop"));
|
||||
Void::Testing::expect(dtor_observations.load(std::memory_order_relaxed) >=
|
||||
0);
|
||||
}
|
||||
|
||||
Void::Testing::Test service_start_all_allows_reentrant_acquire_and_provide()
|
||||
{
|
||||
ServiceRegistry reg;
|
||||
std::vector<std::string> log;
|
||||
std::atomic<int> seen{0};
|
||||
reg.provide<IA>(std::make_shared<TrackedService>("A", 42, &log));
|
||||
reg.provide<IB>(std::make_shared<ReentrantStartService>(reg, &seen));
|
||||
|
||||
reg.start_all();
|
||||
Void::Testing::expect_eq(seen.load(std::memory_order_relaxed), 42);
|
||||
auto c = reg.acquire<IC>();
|
||||
Void::Testing::expect(c.has_value());
|
||||
Void::Testing::expect_eq((*c)->rank(), 99);
|
||||
}
|
||||
|
||||
Void::Testing::Test service_dag_ordering_aliases_cycles_and_rollback()
|
||||
{
|
||||
std::vector<std::string> log;
|
||||
ServiceRegistry reg;
|
||||
|
||||
// Baseline snapshot that must remain untouched if staged batch fails.
|
||||
{
|
||||
ProviderSet base;
|
||||
base.add<IA>("baseline", std::make_shared<TrackedService>("base", 100, &log));
|
||||
auto ep = reg.start_and_publish(std::move(base));
|
||||
Void::Testing::expect(ep.has_value());
|
||||
Void::Testing::expect_eq(*ep, std::uint64_t{1});
|
||||
}
|
||||
log.clear();
|
||||
|
||||
// Missing dependency rejected.
|
||||
{
|
||||
ProviderSet bad;
|
||||
bad.add<IB>("B", std::make_shared<DualInterfaceService>(&log),
|
||||
{std::type_index{typeid(IC)}});
|
||||
auto prep = reg.prepare_batch(std::move(bad));
|
||||
Void::Testing::expect(!prep.has_value());
|
||||
Void::Testing::expect_eq(prep.error().code().value,
|
||||
static_cast<std::int32_t>(
|
||||
ServiceErrorCode::missing_dependency));
|
||||
}
|
||||
|
||||
// Cycle rejected.
|
||||
{
|
||||
ProviderSet cyc;
|
||||
auto dual = std::make_shared<DualInterfaceService>(&log);
|
||||
cyc.add<IA>("A", std::make_shared<TrackedService>("A", 1, &log),
|
||||
{std::type_index{typeid(IB)}});
|
||||
cyc.add<IB>("B", dual, {std::type_index{typeid(IA)}});
|
||||
auto prep = reg.prepare_batch(std::move(cyc));
|
||||
Void::Testing::expect(!prep.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
prep.error().code().value,
|
||||
static_cast<std::int32_t>(ServiceErrorCode::cycle_detected));
|
||||
}
|
||||
|
||||
// Middle node failure aborts failed node and rolls back started nodes in
|
||||
// reverse dependency order, leaving baseline untouched.
|
||||
{
|
||||
struct RankImpl final : IManagedService, IC {
|
||||
RankImpl(std::string n, std::vector<std::string>* l, bool fail)
|
||||
: impl(std::move(n), 3, l, fail) {}
|
||||
std::string_view service_name() const override
|
||||
{ return impl.service_name(); }
|
||||
Void::Result::Result<void> start_checked() override
|
||||
{ return impl.start_checked(); }
|
||||
Void::Result::Result<void> check_readiness() const override
|
||||
{ return impl.check_readiness(); }
|
||||
void abort_start() noexcept override { impl.abort_start(); }
|
||||
Void::Result::Result<void> stop_checked() override
|
||||
{ return impl.stop_checked(); }
|
||||
int rank() const override { return 3; }
|
||||
TrackedService impl;
|
||||
};
|
||||
auto dual = std::make_shared<DualInterfaceService>(&log);
|
||||
ProviderSet fail_batch;
|
||||
// IB depends on IA; IA registered second so DAG must still start IA before IB.
|
||||
fail_batch.add<IB>("B", dual, {std::type_index{typeid(IA)}});
|
||||
fail_batch.add<IA>("A", std::make_shared<TrackedService>("A", 1, &log));
|
||||
fail_batch.add<IC>(
|
||||
"C", std::make_shared<RankImpl>("C", &log, true),
|
||||
{std::type_index{typeid(IB)}});
|
||||
|
||||
auto res = reg.start_and_publish(std::move(fail_batch));
|
||||
Void::Testing::expect(!res.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
res.error().code().value,
|
||||
static_cast<std::int32_t>(ServiceErrorCode::start_failed));
|
||||
// Baseline handle still resolves to id 100.
|
||||
auto active = reg.acquire<IA>();
|
||||
Void::Testing::expect(active.has_value());
|
||||
Void::Testing::expect_eq((*active)->id(), 100);
|
||||
// Sequence: start:A, dual:start, start:C, abort:C, dual:stop, stop:A.
|
||||
Void::Testing::expect_eq(log.size(), std::size_t{6});
|
||||
Void::Testing::expect_eq(log[0], std::string{"start:A"});
|
||||
Void::Testing::expect_eq(log[1], std::string{"dual:start"});
|
||||
Void::Testing::expect_eq(log[2], std::string{"start:C"});
|
||||
Void::Testing::expect_eq(log[3], std::string{"abort:C"});
|
||||
Void::Testing::expect_eq(log[4], std::string{"dual:stop"});
|
||||
Void::Testing::expect_eq(log[5], std::string{"stop:A"});
|
||||
}
|
||||
|
||||
// Alias of one lifecycle object starts/stops ONCE.
|
||||
log.clear();
|
||||
auto dual = std::make_shared<DualInterfaceService>(&log);
|
||||
ProviderSet alias_batch;
|
||||
alias_batch.add<IA>("dual.svc", dual);
|
||||
alias_batch.add_alias<IB>("dual.svc.alias", dual);
|
||||
auto ep = reg.start_and_publish(std::move(alias_batch));
|
||||
Void::Testing::expect(ep.has_value());
|
||||
Void::Testing::expect_eq(dual->starts, 1);
|
||||
Void::Testing::expect_eq(reg.acquire<IA>().value()->id(), 10);
|
||||
Void::Testing::expect_eq(reg.acquire<IB>().value()->tag(), 20);
|
||||
}
|
||||
|
||||
Void::Testing::Test service_snapshot_parent_survives_parent_destruction()
|
||||
{
|
||||
std::unique_ptr<ServiceRegistry> child;
|
||||
{
|
||||
ServiceRegistry parent;
|
||||
ProviderSet ps;
|
||||
ps.add<IA>("A", std::make_shared<TrackedService>("A", 77, nullptr));
|
||||
Void::Testing::expect(
|
||||
parent.start_and_publish(std::move(ps)).has_value());
|
||||
|
||||
auto built = ServiceRegistry::from_parent_snapshot(parent.snapshot());
|
||||
Void::Testing::expect(built.has_value());
|
||||
child = std::move(*built);
|
||||
} // parent destroyed here!
|
||||
|
||||
auto h = child->acquire<IA>();
|
||||
Void::Testing::expect(h.has_value());
|
||||
Void::Testing::expect_eq((*h)->id(), 77);
|
||||
}
|
||||
|
||||
Void::Testing::Test service_retention_cap_and_close_incomplete_owners()
|
||||
{
|
||||
ManualClock clock;
|
||||
ServiceLimits limits{};
|
||||
limits.max_retained_generations = 4;
|
||||
|
||||
ServiceRegistry reg{limits, clock};
|
||||
std::vector<std::string> log;
|
||||
std::vector<Void::Service::ServiceHandle<IA>> pinned;
|
||||
|
||||
for (int gen = 1; gen <= 4; ++gen) {
|
||||
ProviderSet ps;
|
||||
ps.add<IA>(
|
||||
"A",
|
||||
std::make_shared<TrackedService>(
|
||||
"A" + std::to_string(gen), gen, &log, false, false, gen == 4));
|
||||
auto ep = reg.start_and_publish(std::move(ps));
|
||||
Void::Testing::expect(ep.has_value());
|
||||
auto h = reg.acquire<IA>();
|
||||
Void::Testing::expect(h.has_value());
|
||||
Void::Testing::expect_eq(h->generation(), *ep);
|
||||
pinned.push_back(std::move(*h));
|
||||
}
|
||||
|
||||
// 4 distinct generations pinned -> 5th publication rejected with retention_limit.
|
||||
{
|
||||
ProviderSet ps5;
|
||||
ps5.add<IA>("A", std::make_shared<TrackedService>("A5", 5, &log));
|
||||
auto prep5 = reg.prepare_batch(std::move(ps5));
|
||||
Void::Testing::expect(!prep5.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
prep5.error().code().value,
|
||||
static_cast<std::int32_t>(ServiceErrorCode::retention_limit));
|
||||
}
|
||||
|
||||
// Dropping oldest handle frees generation 1, allowing generation 5.
|
||||
pinned.erase(pinned.begin());
|
||||
{
|
||||
ProviderSet ps5;
|
||||
ps5.add<IA>("A", std::make_shared<TrackedService>("A5", 5, &log));
|
||||
auto ep5 = reg.start_and_publish(std::move(ps5));
|
||||
Void::Testing::expect(ep5.has_value());
|
||||
}
|
||||
|
||||
// close_and_join closes admission, reports any stop error, and reports
|
||||
// surviving pinned generations in incomplete_owners without freeing them.
|
||||
auto rep = reg.close_and_join();
|
||||
Void::Testing::expect(rep.has_value());
|
||||
Void::Testing::expect(!rep->clean());
|
||||
Void::Testing::expect(!rep->incomplete_owners.empty());
|
||||
|
||||
auto post_close = reg.acquire<IA>();
|
||||
Void::Testing::expect(!post_close.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
post_close.error().code().value,
|
||||
static_cast<std::int32_t>(ServiceErrorCode::service_stopping));
|
||||
|
||||
// Pinned handles remain dereferenceable until dropped.
|
||||
Void::Testing::expect_eq(pinned.front()->id(), 2);
|
||||
}
|
||||
Reference in new issue
Block a user