DistributedApplications: host graph composition with dependency-ordered lifecycle and readiness (P-DISTRIBUTED-02)

This commit is contained in:
faumaray committed 2026-12-04 10:19:19 +03:00
1 parent 0f1cf89cfc
commit 1fec965c68
4 files changed
+825

No files matched your search

@@ -0,0 +1,299 @@
#include "distributed_host/host_graph.hpp"
#include <string>
#include <utility>
namespace DistributedHost
{
namespace
{
[[nodiscard]] Void::Result::Error invalid(std::string message)
{
return Void::Result::Error{
Void::Result::ErrorKind::invalid_argument, std::move(message)};
}
[[nodiscard]] Void::Result::Error conflict(std::string message)
{
return Void::Result::Error{Void::Result::ErrorKind::conflict,
std::move(message)};
}
[[nodiscard]] Void::Result::Error unready(std::string message)
{
return Void::Result::Error{Void::Result::ErrorKind::timeout,
std::move(message)};
}
[[nodiscard]] std::string validate(const HostConfig& config)
{
if (config.cluster.empty()) {
return "HostConfig: cluster must not be empty";
}
if (config.self.empty()) {
return "HostConfig: self node id must not be empty";
}
for (const auto& peer : config.peers) {
if (peer.empty()) {
return "HostConfig: peer node id must not be empty";
}
if (peer == config.self) {
return "HostConfig: self must not appear in the peer list";
}
}
if (config.startup_deadline.count() <= 0) {
return "HostConfig: startup_deadline must be positive";
}
return {};
}
} // namespace
DistributedHostGraph::DistributedHostGraph(HostResources resources,
HostConfig config)
: m_resources(resources), m_config(std::move(config))
{
}
DistributedHostGraph::~DistributedHostGraph()
{
stop();
}
DistributedHostGraph::DistributedHostGraph(
DistributedHostGraph&& other) noexcept
: m_resources(other.m_resources)
, m_config(std::move(other.m_config))
, m_history(std::move(other.m_history))
, m_started(other.m_started)
, m_storage_open(other.m_storage_open)
, m_transport_live(other.m_transport_live)
, m_replication_started(other.m_replication_started)
, m_directory_published(other.m_directory_published)
, m_stopped(other.m_stopped)
, m_readiness(other.m_readiness)
, m_last_error(std::move(other.m_last_error))
{
// The moved-from graph no longer owns any live component, so its
// destructor must not stop anything twice.
other.m_started = 0;
other.m_storage_open = false;
other.m_transport_live = false;
other.m_replication_started = false;
other.m_directory_published = false;
other.m_stopped = true;
}
Void::Result::Result<DistributedHostGraph>
DistributedHostGraph::compose(HostResources resources, HostConfig config)
{
if (const std::string error = validate(config); !error.empty()) {
return std::unexpected{invalid(error)};
}
if (resources.storage == nullptr || resources.transport == nullptr ||
resources.replication == nullptr) {
return std::unexpected{invalid(
"HostResources: storage, transport and replication are "
"required")};
}
return DistributedHostGraph{resources, std::move(config)};
}
void DistributedHostGraph::record(std::string step)
{
m_history.push_back(std::move(step));
}
Void::Result::Result<void> DistributedHostGraph::start_storage()
{
if (auto opened = m_resources.storage->open(); !opened) {
return opened;
}
record("storage.open");
m_storage_open = true;
++m_started;
if (auto recovered = m_resources.storage->recover(); !recovered) {
return recovered;
}
record("storage.recover");
return {};
}
Void::Result::Result<void> DistributedHostGraph::start_transport()
{
if (auto listening = m_resources.transport->listen(); !listening) {
return listening;
}
record("transport.listen");
m_transport_live = true;
++m_started;
if (auto connected =
m_resources.transport->connect_peers(m_config.peers);
!connected) {
return connected;
}
record("transport.connect_peers");
return {};
}
Void::Result::Result<void> DistributedHostGraph::start_replication()
{
if (auto started = m_resources.replication->start(); !started) {
return started;
}
record("replication.start");
m_replication_started = true;
++m_started;
const Role role = m_resources.replication->role();
const bool leader = m_resources.replication->leader_available();
if (m_config.require_leader_readiness && !leader) {
m_readiness = Readiness::FollowerHealthy;
return std::unexpected{unready(
"DistributedHost: no leader path within the startup deadline")};
}
m_readiness = leader ? Readiness::WriteReady
: Readiness::FollowerHealthy;
record("replication.readiness");
return {};
}
Void::Result::Result<void> DistributedHostGraph::publish_directory()
{
if (!m_config.publish_discovery || m_resources.directory == nullptr) {
return {};
}
// Discovery is an observation adapter: a failure is recorded but
// never weakens quorum or readiness.
auto published = m_resources.directory->publish(m_config.self);
record("directory.publish");
if (!published) {
m_last_error = published.error().message();
return {};
}
m_directory_published = true;
return {};
}
Void::Result::Result<void> DistributedHostGraph::start()
{
if (m_stopped) {
return std::unexpected{
conflict("DistributedHost: graph was already stopped")};
}
if (m_started != 0) {
return std::unexpected{
conflict("DistributedHost: graph is already started")};
}
m_readiness = Readiness::Starting;
m_last_error.clear();
auto storage = start_storage();
if (!storage) {
m_last_error = storage.error().message();
m_readiness = Readiness::Failed;
unwind();
return storage;
}
auto transport = start_transport();
if (!transport) {
m_last_error = transport.error().message();
m_readiness = Readiness::Failed;
unwind();
return transport;
}
auto replication = start_replication();
if (!replication) {
m_last_error = replication.error().message();
m_readiness = Readiness::Failed;
unwind();
return replication;
}
if (auto directory = publish_directory(); !directory) {
m_last_error = directory.error().message();
m_readiness = Readiness::Failed;
unwind();
return directory;
}
return {};
}
void DistributedHostGraph::stop() noexcept
{
if (m_stopped) {
return;
}
if (m_readiness != Readiness::Starting &&
m_readiness != Readiness::Failed) {
m_readiness = Readiness::Stopping;
}
// Reverse startup order, never destroying borrowed resources.
if (m_directory_published) {
m_resources.directory->withdraw();
record("directory.withdraw");
m_directory_published = false;
}
if (m_replication_started) {
m_resources.replication->stop();
record("replication.stop");
m_replication_started = false;
}
if (m_transport_live) {
m_resources.transport->close();
record("transport.close");
m_transport_live = false;
}
if (m_storage_open) {
m_resources.storage->close();
record("storage.close");
m_storage_open = false;
}
m_stopped = true;
m_started = 0;
if (m_readiness != Readiness::Failed) {
m_readiness = Readiness::Stopped;
}
}
void DistributedHostGraph::unwind() noexcept
{
// Rollback keeps the original failure visible in health().last_error.
const std::string preserved = m_last_error;
if (m_directory_published) {
m_resources.directory->withdraw();
record("directory.withdraw");
m_directory_published = false;
}
if (m_replication_started) {
m_resources.replication->stop();
record("replication.stop");
m_replication_started = false;
}
if (m_transport_live) {
m_resources.transport->close();
record("transport.close");
m_transport_live = false;
}
if (m_storage_open) {
m_resources.storage->close();
record("storage.close");
m_storage_open = false;
}
m_started = 0;
m_last_error = preserved;
}
HealthSnapshot DistributedHostGraph::health() const
{
HealthSnapshot snapshot;
snapshot.readiness = m_readiness;
snapshot.role = m_resources.replication != nullptr
? m_resources.replication->role()
: Role::Follower;
snapshot.quorum_available =
m_resources.replication != nullptr &&
m_resources.replication->leader_available();
snapshot.started_components = m_started;
snapshot.last_error = m_last_error;
return snapshot;
}
} // namespace DistributedHost