feat(Runtime): add inert Host, validated RuntimeLimits, EmbeddedResources, and topological ResourceGraph (S-RUNTIME-01)
This commit is contained in:
1 parent
673bb7e8b4
commit
1b3331e3a8
9 files changed
+812
-1
No files matched your search
@@ -0,0 +1,249 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
#include <Void/Runtime/Options.hpp>
|
||||
#include <Void/Runtime/ResourceGraph.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Runtime
|
||||
{
|
||||
|
||||
enum class HostState {
|
||||
constructed,
|
||||
starting,
|
||||
running,
|
||||
stopping_admission,
|
||||
draining,
|
||||
joining,
|
||||
stopped,
|
||||
failed_start,
|
||||
shutdown_incomplete
|
||||
};
|
||||
|
||||
struct EmbeddedResources {
|
||||
Async::Executor* executor{nullptr};
|
||||
IO::Driver* io{nullptr};
|
||||
const Void::Time::MonotonicClock* clock{&Void::Time::SystemMonotonicClock::instance()};
|
||||
Void::Memory::BudgetResource* memory_budget{nullptr};
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> validate() const
|
||||
{
|
||||
if (executor == nullptr) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"EmbeddedResources::executor must not be null")};
|
||||
}
|
||||
if (io == nullptr) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"EmbeddedResources::io must not be null")};
|
||||
}
|
||||
if (clock == nullptr) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"EmbeddedResources::clock must not be null")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
struct ShutdownReport {
|
||||
bool complete{true};
|
||||
HostState state{HostState::stopped};
|
||||
std::size_t pending_owners{0};
|
||||
std::vector<std::string> pending_owner_ids{};
|
||||
};
|
||||
|
||||
[[nodiscard]] inline ResourceGraph make_default_resource_graph(bool embedded_mode)
|
||||
{
|
||||
const ResourceOwnership own =
|
||||
embedded_mode ? ResourceOwnership::borrowed : ResourceOwnership::owned;
|
||||
ResourceGraph g;
|
||||
(void)g.add_node(ResourceNode{"clock", ResourceKind::clock, own, true});
|
||||
(void)g.add_node(ResourceNode{"memory_budget", ResourceKind::memory_budget, own, true});
|
||||
(void)g.add_node(ResourceNode{"executor", ResourceKind::executor, own, true});
|
||||
(void)g.add_node(ResourceNode{"io_driver", ResourceKind::io_driver, own, true});
|
||||
(void)g.add_dependency("executor", "clock");
|
||||
(void)g.add_dependency("executor", "memory_budget");
|
||||
(void)g.add_dependency("io_driver", "clock");
|
||||
(void)g.add_dependency("io_driver", "memory_budget");
|
||||
(void)g.add_dependency("io_driver", "executor");
|
||||
return g;
|
||||
}
|
||||
|
||||
// Inert P1 Runtime composition handle: validates options, memory/time/IO limits,
|
||||
// borrowed resources, and topological resource lifecycle order without implicitly
|
||||
// spawning threads or opening descriptors on construction.
|
||||
class Host
|
||||
{
|
||||
public:
|
||||
Host(const Host&) = delete;
|
||||
Host& operator=(const Host&) = delete;
|
||||
Host(Host&&) noexcept = default;
|
||||
Host& operator=(Host&&) noexcept = default;
|
||||
~Host() = default;
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<Host> create(Options options = {})
|
||||
{
|
||||
if (auto ok = validate_options(options); !ok.has_value()) {
|
||||
return std::unexpected{std::move(ok.error())};
|
||||
}
|
||||
ResourceGraph g = make_default_resource_graph(options.embedded_mode);
|
||||
auto plan = g.validate_and_plan();
|
||||
if (!plan.has_value()) {
|
||||
return std::unexpected{std::move(plan.error())};
|
||||
}
|
||||
auto budget = std::make_unique<Void::Memory::BudgetResource>(
|
||||
options.limits.max_owned_memory_bytes);
|
||||
return Host{
|
||||
std::move(options),
|
||||
std::move(*plan),
|
||||
EmbeddedResources{},
|
||||
std::move(budget)};
|
||||
}
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<Host> create_embedded(
|
||||
Options options, EmbeddedResources embedded)
|
||||
{
|
||||
options.embedded_mode = true;
|
||||
if (auto ok = validate_options(options); !ok.has_value()) {
|
||||
return std::unexpected{std::move(ok.error())};
|
||||
}
|
||||
if (auto eok = embedded.validate(); !eok.has_value()) {
|
||||
return std::unexpected{std::move(eok.error())};
|
||||
}
|
||||
ResourceGraph g = make_default_resource_graph(true);
|
||||
auto plan = g.validate_and_plan();
|
||||
if (!plan.has_value()) {
|
||||
return std::unexpected{std::move(plan.error())};
|
||||
}
|
||||
std::unique_ptr<Void::Memory::BudgetResource> budget;
|
||||
if (embedded.memory_budget == nullptr) {
|
||||
budget = std::make_unique<Void::Memory::BudgetResource>(
|
||||
options.limits.max_owned_memory_bytes);
|
||||
}
|
||||
return Host{
|
||||
std::move(options),
|
||||
std::move(*plan),
|
||||
embedded,
|
||||
std::move(budget)};
|
||||
}
|
||||
|
||||
[[nodiscard]] HostState state() const noexcept { return state_; }
|
||||
[[nodiscard]] bool is_running() const noexcept { return state_ == HostState::running; }
|
||||
[[nodiscard]] std::size_t started_worker_count() const noexcept { return started_workers_; }
|
||||
[[nodiscard]] const Options& options() const noexcept { return options_; }
|
||||
[[nodiscard]] const LifecyclePlan& lifecycle_plan() const noexcept { return plan_; }
|
||||
[[nodiscard]] const EmbeddedResources& embedded_resources() const noexcept { return embedded_; }
|
||||
|
||||
[[nodiscard]] const Void::Time::MonotonicClock& clock() const noexcept
|
||||
{
|
||||
return embedded_.clock ? *embedded_.clock
|
||||
: Void::Time::SystemMonotonicClock::instance();
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Memory::BudgetResource& memory_budget() noexcept
|
||||
{
|
||||
return embedded_.memory_budget ? *embedded_.memory_budget : *owned_budget_;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class HostBuilder;
|
||||
|
||||
Host(Options options,
|
||||
LifecyclePlan plan,
|
||||
EmbeddedResources embedded,
|
||||
std::unique_ptr<Void::Memory::BudgetResource> owned_budget) noexcept
|
||||
: options_(std::move(options))
|
||||
, plan_(std::move(plan))
|
||||
, embedded_(embedded)
|
||||
, owned_budget_(std::move(owned_budget))
|
||||
{
|
||||
}
|
||||
|
||||
HostState state_{HostState::constructed};
|
||||
std::size_t started_workers_{0};
|
||||
Options options_{};
|
||||
LifecyclePlan plan_{};
|
||||
EmbeddedResources embedded_{};
|
||||
std::unique_ptr<Void::Memory::BudgetResource> owned_budget_{};
|
||||
};
|
||||
|
||||
class HostBuilder
|
||||
{
|
||||
public:
|
||||
HostBuilder()
|
||||
: graph_(make_default_resource_graph(false))
|
||||
{
|
||||
}
|
||||
|
||||
HostBuilder& with_options(Options options)
|
||||
{
|
||||
options_ = std::move(options);
|
||||
if (!embedded_.has_value()) {
|
||||
graph_ = make_default_resource_graph(options_.embedded_mode);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
HostBuilder& with_embedded(EmbeddedResources embedded)
|
||||
{
|
||||
options_.embedded_mode = true;
|
||||
embedded_ = embedded;
|
||||
graph_ = make_default_resource_graph(true);
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> add_resource(ResourceNode node)
|
||||
{
|
||||
return graph_.add_node(std::move(node));
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> add_dependency(
|
||||
std::string dependent, std::string dependency, bool required = true)
|
||||
{
|
||||
return graph_.add_dependency(
|
||||
std::move(dependent), std::move(dependency), required);
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<Host> build() const
|
||||
{
|
||||
Options opts = options_;
|
||||
if (embedded_.has_value()) {
|
||||
opts.embedded_mode = true;
|
||||
if (auto eok = embedded_->validate(); !eok.has_value()) {
|
||||
return std::unexpected{std::move(eok.error())};
|
||||
}
|
||||
}
|
||||
if (auto ok = validate_options(opts); !ok.has_value()) {
|
||||
return std::unexpected{std::move(ok.error())};
|
||||
}
|
||||
auto plan = graph_.validate_and_plan();
|
||||
if (!plan.has_value()) {
|
||||
return std::unexpected{std::move(plan.error())};
|
||||
}
|
||||
|
||||
const EmbeddedResources emb = embedded_.value_or(EmbeddedResources{});
|
||||
std::unique_ptr<Void::Memory::BudgetResource> budget;
|
||||
if (emb.memory_budget == nullptr) {
|
||||
budget = std::make_unique<Void::Memory::BudgetResource>(
|
||||
opts.limits.max_owned_memory_bytes);
|
||||
}
|
||||
return Host{std::move(opts), std::move(*plan), emb, std::move(budget)};
|
||||
}
|
||||
|
||||
private:
|
||||
Options options_{};
|
||||
std::optional<EmbeddedResources> embedded_{};
|
||||
ResourceGraph graph_;
|
||||
};
|
||||
|
||||
} // namespace Void::Runtime
|
||||
Reference in new issue
Block a user