Merge netapp-kv-manual: NetworkedApplications composition skeleton
This commit is contained in:
commit
a2a0e1a4c3
4 files changed
+409
No files matched your search
@@ -0,0 +1,20 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(NetworkedKv LANGUAGES CXX)
|
||||
|
||||
# Reference application, built as an installed-package consumer so it cannot
|
||||
# accidentally rely on whole-repository linkage.
|
||||
set(CMAKE_CXX_STANDARD 26)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS Runtime)
|
||||
|
||||
add_library(networked_kv STATIC src/application.cpp)
|
||||
target_include_directories(networked_kv PUBLIC include)
|
||||
target_link_libraries(networked_kv PUBLIC Void::Runtime)
|
||||
|
||||
add_executable(networked_kv_test tests/application_test.cpp)
|
||||
target_link_libraries(networked_kv_test PRIVATE networked_kv)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME networked_kv_lifecycle COMMAND networked_kv_test)
|
||||
@@ -0,0 +1,101 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Runtime/Host.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
/// Validated listener and request bounds for the reference service.
|
||||
/// Construction alone opens no socket or worker.
|
||||
struct NetworkedConfig {
|
||||
std::string listen_address{"127.0.0.1:0"};
|
||||
std::string admin_address{};
|
||||
std::uint32_t max_connections{64};
|
||||
std::size_t max_request_bytes{1024ULL * 1024ULL};
|
||||
std::size_t max_scan_page{1000};
|
||||
std::chrono::milliseconds request_deadline{
|
||||
std::chrono::seconds{30}};
|
||||
std::chrono::milliseconds shutdown_grace{
|
||||
std::chrono::seconds{5}};
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> validate() const;
|
||||
};
|
||||
|
||||
enum class ResourceOwnership : std::uint8_t { Embedded, Managed };
|
||||
|
||||
/// Module-owned resources supplied by the application.
|
||||
///
|
||||
/// Embedded mode borrows an already-running host and never shuts it down.
|
||||
/// Managed mode lets the application create and own the host.
|
||||
struct ApplicationResources {
|
||||
ResourceOwnership ownership{ResourceOwnership::Embedded};
|
||||
Void::Runtime::Host* borrowed_host{nullptr};
|
||||
Void::Runtime::Options runtime_options{};
|
||||
};
|
||||
|
||||
enum class ApplicationState : std::uint8_t {
|
||||
Created,
|
||||
Running,
|
||||
Stopping,
|
||||
Stopped,
|
||||
Failed
|
||||
};
|
||||
|
||||
struct HealthSnapshot {
|
||||
ApplicationState state{ApplicationState::Created};
|
||||
bool ready{false};
|
||||
bool managed{false};
|
||||
std::size_t started_workers{0};
|
||||
};
|
||||
|
||||
/// Composition surface for the networked key-value reference service.
|
||||
///
|
||||
/// This slice owns lifecycle and resource-mode policy only: it wires no
|
||||
/// HTTP endpoints yet and defines no competing executor, clock, logger or
|
||||
/// error type.
|
||||
class NetworkedApplication
|
||||
{
|
||||
public:
|
||||
|
||||
NetworkedApplication(const NetworkedApplication&) = delete;
|
||||
NetworkedApplication& operator=(const NetworkedApplication&) = delete;
|
||||
NetworkedApplication(NetworkedApplication&&) noexcept;
|
||||
NetworkedApplication& operator=(NetworkedApplication&&) noexcept;
|
||||
~NetworkedApplication();
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<NetworkedApplication>
|
||||
create(ApplicationResources resources, NetworkedConfig config);
|
||||
|
||||
/// Start serving. Managed mode creates and starts the owned host;
|
||||
/// embedded mode requires the borrowed host to already be running.
|
||||
[[nodiscard]] Void::Result::Result<void> start();
|
||||
|
||||
/// Stop serving. Idempotent and bounded. Managed mode shuts the
|
||||
/// owned host down; embedded mode leaves the borrowed host alone.
|
||||
[[nodiscard]] Void::Result::Result<Void::Runtime::ShutdownReport>
|
||||
stop();
|
||||
|
||||
[[nodiscard]] HealthSnapshot health() const;
|
||||
[[nodiscard]] const NetworkedConfig& config() const noexcept
|
||||
{
|
||||
return m_config;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
NetworkedApplication() = default;
|
||||
void stop_owned_host() noexcept;
|
||||
|
||||
NetworkedConfig m_config{};
|
||||
ApplicationResources m_resources{};
|
||||
std::unique_ptr<Void::Runtime::Host> m_owned_host{};
|
||||
Void::Runtime::Host* m_host{nullptr};
|
||||
ApplicationState m_state{ApplicationState::Created};
|
||||
};
|
||||
} // namespace NetworkedKv
|
||||
@@ -0,0 +1,202 @@
|
||||
#include "networked_kv/application.hpp"
|
||||
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error
|
||||
config_error(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::invalid_argument,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Error state_error(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::conflict,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Async::OperationOptions
|
||||
deadline_after(std::chrono::milliseconds grace)
|
||||
{
|
||||
Void::Async::OperationOptions options;
|
||||
options.deadline = Void::Time::SystemMonotonicClock::instance().now() +
|
||||
grace;
|
||||
return options;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<void> NetworkedConfig::validate() const
|
||||
{
|
||||
if (listen_address.empty()) {
|
||||
return std::unexpected{
|
||||
config_error("NetworkedConfig: listen_address must not be empty")};
|
||||
}
|
||||
if (max_connections == 0) {
|
||||
return std::unexpected{
|
||||
config_error("NetworkedConfig: max_connections must be positive")};
|
||||
}
|
||||
if (max_request_bytes == 0) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedConfig: max_request_bytes must be positive")};
|
||||
}
|
||||
if (max_scan_page == 0) {
|
||||
return std::unexpected{
|
||||
config_error("NetworkedConfig: max_scan_page must be positive")};
|
||||
}
|
||||
if (request_deadline.count() <= 0) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedConfig: request_deadline must be positive")};
|
||||
}
|
||||
if (shutdown_grace.count() < 0) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedConfig: shutdown_grace must not be negative")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
NetworkedApplication::NetworkedApplication(NetworkedApplication&& other) noexcept
|
||||
: m_config(std::move(other.m_config))
|
||||
, m_resources(other.m_resources)
|
||||
, m_owned_host(std::move(other.m_owned_host))
|
||||
, m_host(other.m_host)
|
||||
, m_state(other.m_state)
|
||||
{
|
||||
other.m_host = nullptr;
|
||||
other.m_state = ApplicationState::Stopped;
|
||||
}
|
||||
|
||||
NetworkedApplication&
|
||||
NetworkedApplication::operator=(NetworkedApplication&& other) noexcept
|
||||
{
|
||||
if (this != &other) {
|
||||
stop_owned_host();
|
||||
m_config = std::move(other.m_config);
|
||||
m_resources = other.m_resources;
|
||||
m_owned_host = std::move(other.m_owned_host);
|
||||
m_host = other.m_host;
|
||||
m_state = other.m_state;
|
||||
other.m_host = nullptr;
|
||||
other.m_state = ApplicationState::Stopped;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
NetworkedApplication::~NetworkedApplication()
|
||||
{
|
||||
stop_owned_host();
|
||||
}
|
||||
|
||||
Void::Result::Result<NetworkedApplication>
|
||||
NetworkedApplication::create(ApplicationResources resources,
|
||||
NetworkedConfig config)
|
||||
{
|
||||
if (auto valid = config.validate(); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
if (resources.ownership == ResourceOwnership::Embedded &&
|
||||
(resources.borrowed_host == nullptr ||
|
||||
!resources.borrowed_host->valid())) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedApplication: embedded mode requires a valid borrowed "
|
||||
"host")};
|
||||
}
|
||||
NetworkedApplication app;
|
||||
app.m_config = std::move(config);
|
||||
app.m_resources = resources;
|
||||
return app;
|
||||
}
|
||||
|
||||
Void::Result::Result<void> NetworkedApplication::start()
|
||||
{
|
||||
if (m_state == ApplicationState::Running) {
|
||||
return std::unexpected{
|
||||
state_error("NetworkedApplication: already running")};
|
||||
}
|
||||
if (m_resources.ownership == ResourceOwnership::Managed) {
|
||||
if (!m_owned_host) {
|
||||
auto created =
|
||||
Void::Runtime::Host::create(m_resources.runtime_options);
|
||||
if (!created) {
|
||||
m_state = ApplicationState::Failed;
|
||||
return std::unexpected{std::move(created).error()};
|
||||
}
|
||||
m_owned_host =
|
||||
std::make_unique<Void::Runtime::Host>(std::move(*created));
|
||||
m_host = m_owned_host.get();
|
||||
}
|
||||
if (!m_host->is_running()) {
|
||||
if (auto started = m_host->start(); !started) {
|
||||
m_state = ApplicationState::Failed;
|
||||
return started;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m_host = m_resources.borrowed_host;
|
||||
if (m_host == nullptr || !m_host->is_running()) {
|
||||
return std::unexpected{state_error(
|
||||
"NetworkedApplication: embedded mode requires a running "
|
||||
"borrowed host")};
|
||||
}
|
||||
}
|
||||
m_state = ApplicationState::Running;
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<Void::Runtime::ShutdownReport>
|
||||
NetworkedApplication::stop()
|
||||
{
|
||||
if (m_state == ApplicationState::Stopped) {
|
||||
return Void::Runtime::ShutdownReport{};
|
||||
}
|
||||
if (m_state != ApplicationState::Running) {
|
||||
m_state = ApplicationState::Stopped;
|
||||
return Void::Runtime::ShutdownReport{};
|
||||
}
|
||||
m_state = ApplicationState::Stopping;
|
||||
if (m_resources.ownership == ResourceOwnership::Managed && m_host) {
|
||||
auto report =
|
||||
m_host->shutdown_blocking(deadline_after(m_config.shutdown_grace));
|
||||
m_state = ApplicationState::Stopped;
|
||||
return report;
|
||||
}
|
||||
// Embedded: the borrowed host belongs to the application, so only
|
||||
// this composition's view stops.
|
||||
m_state = ApplicationState::Stopped;
|
||||
Void::Runtime::ShutdownReport report;
|
||||
report.complete = true;
|
||||
report.state = m_host ? m_host->state()
|
||||
: Void::Runtime::HostState::stopped;
|
||||
return report;
|
||||
}
|
||||
|
||||
HealthSnapshot NetworkedApplication::health() const
|
||||
{
|
||||
HealthSnapshot snapshot;
|
||||
snapshot.state = m_state;
|
||||
snapshot.managed = m_resources.ownership == ResourceOwnership::Managed;
|
||||
if (m_host != nullptr) {
|
||||
snapshot.ready = m_host->is_ready();
|
||||
snapshot.started_workers = m_host->started_worker_count();
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
void NetworkedApplication::stop_owned_host() noexcept
|
||||
{
|
||||
if (m_resources.ownership != ResourceOwnership::Managed ||
|
||||
!m_owned_host || m_state != ApplicationState::Running) {
|
||||
return;
|
||||
}
|
||||
m_state = ApplicationState::Stopping;
|
||||
(void)m_owned_host->shutdown_blocking(
|
||||
deadline_after(m_config.shutdown_grace));
|
||||
m_state = ApplicationState::Stopped;
|
||||
}
|
||||
} // namespace NetworkedKv
|
||||
@@ -0,0 +1,86 @@
|
||||
#include "networked_kv/application.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace NetworkedKv;
|
||||
|
||||
void config_validation()
|
||||
{
|
||||
NetworkedConfig bad;
|
||||
bad.listen_address.clear();
|
||||
assert(!bad.validate().has_value());
|
||||
|
||||
NetworkedConfig no_connections;
|
||||
no_connections.max_connections = 0;
|
||||
assert(!no_connections.validate().has_value());
|
||||
|
||||
NetworkedConfig ok;
|
||||
assert(ok.validate().has_value());
|
||||
}
|
||||
|
||||
void managed_mode_owns_the_host()
|
||||
{
|
||||
ApplicationResources resources;
|
||||
resources.ownership = ResourceOwnership::Managed;
|
||||
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
||||
assert(app.has_value());
|
||||
|
||||
// Construction opens no listener or worker.
|
||||
const auto created = app->health();
|
||||
assert(created.state == ApplicationState::Created);
|
||||
assert(!created.ready);
|
||||
|
||||
assert(app->start().has_value());
|
||||
const auto running = app->health();
|
||||
assert(running.state == ApplicationState::Running);
|
||||
assert(running.managed);
|
||||
|
||||
auto report = app->stop();
|
||||
assert(report.has_value());
|
||||
assert(app->health().state == ApplicationState::Stopped);
|
||||
|
||||
// Idempotent.
|
||||
assert(app->stop().has_value());
|
||||
}
|
||||
|
||||
void embedded_mode_borrows_the_host()
|
||||
{
|
||||
auto host = Void::Runtime::Host::create();
|
||||
assert(host.has_value());
|
||||
assert(host->start().has_value());
|
||||
|
||||
ApplicationResources resources;
|
||||
resources.ownership = ResourceOwnership::Embedded;
|
||||
resources.borrowed_host = &*host;
|
||||
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
||||
assert(app.has_value());
|
||||
assert(app->start().has_value());
|
||||
assert(app->health().state == ApplicationState::Running);
|
||||
|
||||
assert(app->stop().has_value());
|
||||
// The borrowed host is not shut down by the composition.
|
||||
assert(host->is_running());
|
||||
(void)host->shutdown_blocking();
|
||||
}
|
||||
|
||||
void embedded_requires_a_host()
|
||||
{
|
||||
ApplicationResources resources;
|
||||
resources.ownership = ResourceOwnership::Embedded;
|
||||
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
||||
assert(!app.has_value());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
config_validation();
|
||||
managed_mode_owns_the_host();
|
||||
embedded_mode_borrows_the_host();
|
||||
embedded_requires_a_host();
|
||||
std::puts("networked-kv lifecycle ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user