feat(runtime): add 8-step Host start, SignalGate, and 11-phase structured shutdown (S-RUNTIME-02/03)
This commit is contained in:
1 parent
50029e1f7b
commit
7bea7df46d
6 files changed
+3816
-27
No files matched your search
+231
-27
@@ -1,15 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Async/TaskScope.hpp>
|
||||
#include <Void/Async/Timer.hpp>
|
||||
#include <Void/IO/Driver.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
#include <Void/Runtime/Lifecycle.hpp>
|
||||
#include <Void/Runtime/Options.hpp>
|
||||
#include <Void/Runtime/ResourceGraph.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -60,6 +68,14 @@ struct ShutdownReport {
|
||||
HostState state{HostState::stopped};
|
||||
std::size_t pending_owners{0};
|
||||
std::vector<std::string> pending_owner_ids{};
|
||||
ShutdownPhase phase{ShutdownPhase::not_started};
|
||||
std::size_t pending_business_tasks{0};
|
||||
std::size_t pending_cpu_admitted{0};
|
||||
std::size_t pending_io_operations{0};
|
||||
std::size_t retained_buffer_bytes{0};
|
||||
bool drain_grace_expired{false};
|
||||
bool export_flush_expired{false};
|
||||
bool owner_release_pending{false};
|
||||
};
|
||||
|
||||
[[nodiscard]] inline ResourceGraph make_default_resource_graph(bool embedded_mode)
|
||||
@@ -79,9 +95,16 @@ struct ShutdownReport {
|
||||
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.
|
||||
} // namespace Void::Runtime
|
||||
|
||||
#include <Void/Runtime/Detail/HostCore.hpp>
|
||||
|
||||
namespace Void::Runtime
|
||||
{
|
||||
|
||||
/// Runtime composition handle managing startup, readiness, business execution,
|
||||
/// and phased structured shutdown. Destruction blocks until shutdown is complete
|
||||
/// in managed mode (or in embedded mode with a ManualExecutor).
|
||||
class Host
|
||||
{
|
||||
public:
|
||||
@@ -107,7 +130,8 @@ public:
|
||||
std::move(options),
|
||||
std::move(*plan),
|
||||
EmbeddedResources{},
|
||||
std::move(budget)};
|
||||
std::move(budget),
|
||||
{}};
|
||||
}
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<Host> create_embedded(
|
||||
@@ -134,25 +158,152 @@ public:
|
||||
std::move(options),
|
||||
std::move(*plan),
|
||||
embedded,
|
||||
std::move(budget)};
|
||||
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]] bool valid() const noexcept { return core_ != nullptr; }
|
||||
|
||||
[[nodiscard]] HostState state() const noexcept
|
||||
{
|
||||
return core_ ? core_->current_state() : HostState::stopped;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_running() const noexcept
|
||||
{
|
||||
return state() == HostState::running;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_ready() const noexcept
|
||||
{
|
||||
return core_ ? core_->current_ready() : false;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t started_worker_count() const noexcept
|
||||
{
|
||||
return core_ ? core_->current_workers() : 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t cpu_admitted() const noexcept
|
||||
{
|
||||
return core_ ? core_->cpu_admitted_count() : 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] Async::Executor* executor() noexcept
|
||||
{
|
||||
return core_ ? core_->active_executor() : nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] IO::Driver* io() noexcept
|
||||
{
|
||||
return core_ ? core_->get_io() : nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<std::thread::id> owner_thread() const noexcept
|
||||
{
|
||||
return core_ ? core_->current_owner_thread() : std::nullopt;
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] const Options& options() const noexcept
|
||||
{
|
||||
assert(valid());
|
||||
return core_->options;
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] const LifecyclePlan& lifecycle_plan() const noexcept
|
||||
{
|
||||
assert(valid());
|
||||
return core_->plan;
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] const EmbeddedResources& embedded_resources() const noexcept
|
||||
{
|
||||
assert(valid());
|
||||
return core_->embedded;
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] const Void::Time::MonotonicClock& clock() const noexcept
|
||||
{
|
||||
return embedded_.clock ? *embedded_.clock
|
||||
: Void::Time::SystemMonotonicClock::instance();
|
||||
assert(valid());
|
||||
return core_->active_clock();
|
||||
}
|
||||
|
||||
/// Precondition: `valid()`.
|
||||
[[nodiscard]] Void::Memory::BudgetResource& memory_budget() noexcept
|
||||
{
|
||||
return embedded_.memory_budget ? *embedded_.memory_budget : *owned_budget_;
|
||||
assert(valid());
|
||||
return core_->active_budget();
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> start(StartOptions opts = {})
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_state,
|
||||
"Cannot start a moved-from Host")};
|
||||
}
|
||||
return core_->start_impl(std::move(opts));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Async::JoinHandle, Async::AdmissionError> spawn(
|
||||
Coro::Task<void>&& task, Async::OperationOptions opts = {})
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{Async::AdmissionError::stopped};
|
||||
}
|
||||
return core_->spawn_impl(std::move(task), std::move(opts));
|
||||
}
|
||||
|
||||
void begin_shutdown() noexcept
|
||||
{
|
||||
if (core_) {
|
||||
core_->begin_shutdown_impl();
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ShutdownReport>
|
||||
poll_shutdown(Async::OperationOptions opts = {})
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_state,
|
||||
"Cannot poll_shutdown on a moved-from Host")};
|
||||
}
|
||||
return core_->poll_shutdown_impl(std::move(opts));
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ShutdownReport>
|
||||
shutdown_blocking(Async::OperationOptions opts = {})
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_state,
|
||||
"Cannot shutdown_blocking on a moved-from Host")};
|
||||
}
|
||||
return core_->shutdown_blocking_impl(std::move(opts), false);
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ShutdownReport> shutdown_pumping(
|
||||
Async::ManualExecutor& manual,
|
||||
Async::OperationOptions opts = {},
|
||||
Async::TimerQueue* timers = nullptr)
|
||||
{
|
||||
if (!core_) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_state,
|
||||
"Cannot shutdown_pumping on a moved-from Host")};
|
||||
}
|
||||
return core_->shutdown_pumping_impl(
|
||||
manual, std::move(opts), timers, false);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<ShutdownReport> last_shutdown_report() const
|
||||
{
|
||||
return core_ ? core_->current_last_report() : std::nullopt;
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -161,20 +312,18 @@ private:
|
||||
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))
|
||||
std::unique_ptr<Void::Memory::BudgetResource> owned_budget,
|
||||
std::vector<std::shared_ptr<detail::ComponentEntry>> components)
|
||||
: core_(std::make_unique<detail::HostCore>(
|
||||
std::move(options),
|
||||
std::move(plan),
|
||||
embedded,
|
||||
std::move(owned_budget),
|
||||
std::move(components)))
|
||||
{
|
||||
}
|
||||
|
||||
HostState state_{HostState::constructed};
|
||||
std::size_t started_workers_{0};
|
||||
Options options_{};
|
||||
LifecyclePlan plan_{};
|
||||
EmbeddedResources embedded_{};
|
||||
std::unique_ptr<Void::Memory::BudgetResource> owned_budget_{};
|
||||
std::unique_ptr<detail::HostCore> core_{};
|
||||
};
|
||||
|
||||
class HostBuilder
|
||||
@@ -190,6 +339,7 @@ public:
|
||||
options_ = std::move(options);
|
||||
if (!embedded_.has_value()) {
|
||||
graph_ = make_default_resource_graph(options_.embedded_mode);
|
||||
components_.clear();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@@ -199,6 +349,7 @@ public:
|
||||
options_.embedded_mode = true;
|
||||
embedded_ = embedded;
|
||||
graph_ = make_default_resource_graph(true);
|
||||
components_.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -214,6 +365,34 @@ public:
|
||||
std::move(dependent), std::move(dependency), required);
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> add_component(
|
||||
std::string id,
|
||||
ResourceKind kind,
|
||||
std::shared_ptr<Component> component,
|
||||
std::vector<std::string> deps = {},
|
||||
bool critical = true)
|
||||
{
|
||||
if (id.empty() || !component) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"Component id must be non-empty and component pointer must be non-null")};
|
||||
}
|
||||
if (auto nr = graph_.add_node(
|
||||
ResourceNode{id, kind, ResourceOwnership::owned, critical});
|
||||
!nr.has_value()) {
|
||||
return nr;
|
||||
}
|
||||
for (auto& d : deps) {
|
||||
if (auto dr = graph_.add_dependency(id, std::move(d), true);
|
||||
!dr.has_value()) {
|
||||
return dr;
|
||||
}
|
||||
}
|
||||
components_.push_back(ComponentSpec{
|
||||
std::move(id), kind, std::move(component), critical});
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<Host> build() const
|
||||
{
|
||||
Options opts = options_;
|
||||
@@ -237,13 +416,38 @@ public:
|
||||
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)};
|
||||
|
||||
std::vector<std::shared_ptr<detail::ComponentEntry>> entries;
|
||||
entries.reserve(components_.size());
|
||||
for (const auto& c : components_) {
|
||||
auto entry = std::make_shared<detail::ComponentEntry>();
|
||||
entry->id = c.id;
|
||||
entry->kind = c.kind;
|
||||
entry->critical = c.critical;
|
||||
entry->component = c.component;
|
||||
entries.push_back(std::move(entry));
|
||||
}
|
||||
|
||||
return Host{
|
||||
std::move(opts),
|
||||
std::move(*plan),
|
||||
emb,
|
||||
std::move(budget),
|
||||
std::move(entries)};
|
||||
}
|
||||
|
||||
private:
|
||||
struct ComponentSpec {
|
||||
std::string id{};
|
||||
ResourceKind kind{ResourceKind::service};
|
||||
std::shared_ptr<Component> component{};
|
||||
bool critical{true};
|
||||
};
|
||||
|
||||
Options options_{};
|
||||
std::optional<EmbeddedResources> embedded_{};
|
||||
ResourceGraph graph_;
|
||||
std::vector<ComponentSpec> components_{};
|
||||
};
|
||||
|
||||
} // namespace Void::Runtime
|
||||
} // namespace Void::Runtime
|
||||
Reference in new issue
Block a user