DistributedApplications: host graph composition with dependency-ordered lifecycle and readiness (P-DISTRIBUTED-02)
This commit is contained in:
1 parent
0f1cf89cfc
commit
1fec965c68
4 files changed
+825
No files matched your search
@@ -0,0 +1,190 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace DistributedHost
|
||||
{
|
||||
using NodeId = std::string;
|
||||
|
||||
struct HostConfig {
|
||||
std::string cluster{"cluster"};
|
||||
NodeId self{"node-1"};
|
||||
std::vector<NodeId> peers{};
|
||||
/// Require an available leader path before reporting write readiness.
|
||||
bool require_leader_readiness{true};
|
||||
/// Optional discovery publication; disabled means no adapter call.
|
||||
bool publish_discovery{true};
|
||||
std::chrono::milliseconds startup_deadline{
|
||||
std::chrono::seconds{5}};
|
||||
};
|
||||
|
||||
/// Readiness is per capability, not a single boolean:
|
||||
/// a follower may be live and readable while not accepting writes.
|
||||
enum class Readiness : std::uint8_t {
|
||||
Starting,
|
||||
/// Process is live but no leader path is known: reads by explicit
|
||||
/// policy only, never writes.
|
||||
FollowerHealthy,
|
||||
/// Leader path available and local state recovered.
|
||||
WriteReady,
|
||||
Stopping,
|
||||
Stopped,
|
||||
Failed
|
||||
};
|
||||
|
||||
enum class Role : std::uint8_t { Follower, Candidate, Leader };
|
||||
|
||||
[[nodiscard]] constexpr std::string_view
|
||||
readiness_name(Readiness readiness) noexcept
|
||||
{
|
||||
switch (readiness) {
|
||||
case Readiness::Starting: return "starting";
|
||||
case Readiness::FollowerHealthy: return "follower-healthy";
|
||||
case Readiness::WriteReady: return "write-ready";
|
||||
case Readiness::Stopping: return "stopping";
|
||||
case Readiness::Stopped: return "stopped";
|
||||
case Readiness::Failed: return "failed";
|
||||
}
|
||||
return "failed";
|
||||
}
|
||||
|
||||
struct HealthSnapshot {
|
||||
Readiness readiness{Readiness::Starting};
|
||||
Role role{Role::Follower};
|
||||
bool quorum_available{false};
|
||||
std::size_t started_components{0};
|
||||
std::string last_error;
|
||||
};
|
||||
|
||||
/// Storage boundary owned by the Storage subsystem in the real platform.
|
||||
/// Tests supply deterministic fakes; the host graph never implements it.
|
||||
class StorageBackend
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~StorageBackend() = default;
|
||||
[[nodiscard]] virtual Void::Result::Result<void> open() = 0;
|
||||
[[nodiscard]] virtual Void::Result::Result<void> recover() = 0;
|
||||
virtual void close() noexcept = 0;
|
||||
};
|
||||
|
||||
/// Authenticated peer transport boundary.
|
||||
class PeerTransport
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~PeerTransport() = default;
|
||||
[[nodiscard]] virtual Void::Result::Result<void> listen() = 0;
|
||||
[[nodiscard]] virtual Void::Result::Result<void>
|
||||
connect_peers(const std::vector<NodeId>& peers) = 0;
|
||||
virtual void close() noexcept = 0;
|
||||
};
|
||||
|
||||
/// Replication adapter supplying role and leader availability.
|
||||
class ReplicationAdapter
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~ReplicationAdapter() = default;
|
||||
[[nodiscard]] virtual Void::Result::Result<void> start() = 0;
|
||||
[[nodiscard]] virtual Role role() const noexcept = 0;
|
||||
[[nodiscard]] virtual bool leader_available() const noexcept = 0;
|
||||
virtual void stop() noexcept = 0;
|
||||
};
|
||||
|
||||
/// Optional directory publication (Discovery). Failures are observation
|
||||
/// failures, never quorum votes.
|
||||
class DirectoryPublisher
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~DirectoryPublisher() = default;
|
||||
[[nodiscard]] virtual Void::Result::Result<void>
|
||||
publish(const NodeId& self) = 0;
|
||||
virtual void withdraw() noexcept = 0;
|
||||
};
|
||||
|
||||
/// Borrowed resources. The graph never owns or destroys them; they must
|
||||
/// outlive the graph through final join.
|
||||
struct HostResources {
|
||||
StorageBackend* storage{nullptr};
|
||||
PeerTransport* transport{nullptr};
|
||||
ReplicationAdapter* replication{nullptr};
|
||||
/// Optional: null disables discovery even when configured on.
|
||||
DirectoryPublisher* directory{nullptr};
|
||||
};
|
||||
|
||||
/// Single-node composition skeleton for the distributed reference host.
|
||||
///
|
||||
/// Startup is a dependency-ordered transaction: storage open/recover,
|
||||
/// peer transport, replication, readiness, then optional discovery. A
|
||||
/// failure unwinds started components in reverse order and preserves the
|
||||
/// original error. Construction starts nothing.
|
||||
class DistributedHostGraph
|
||||
{
|
||||
public:
|
||||
|
||||
DistributedHostGraph(const DistributedHostGraph&) = delete;
|
||||
DistributedHostGraph& operator=(const DistributedHostGraph&) = delete;
|
||||
DistributedHostGraph(DistributedHostGraph&& other) noexcept;
|
||||
DistributedHostGraph& operator=(DistributedHostGraph&&) = delete;
|
||||
~DistributedHostGraph();
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<DistributedHostGraph>
|
||||
compose(HostResources resources, HostConfig config = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> start();
|
||||
|
||||
/// Stop external admission first, then components in reverse
|
||||
/// startup order. Idempotent and never destroys borrowed
|
||||
/// resources.
|
||||
void stop() noexcept;
|
||||
|
||||
[[nodiscard]] HealthSnapshot health() const;
|
||||
|
||||
[[nodiscard]] Readiness readiness() const noexcept
|
||||
{
|
||||
return m_readiness;
|
||||
}
|
||||
|
||||
/// Deterministic records used by tests and inspect: the exact
|
||||
/// order of component lifecycle calls.
|
||||
[[nodiscard]] const std::vector<std::string>& history() const noexcept
|
||||
{
|
||||
return m_history;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
DistributedHostGraph(HostResources resources, HostConfig config);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> start_storage();
|
||||
[[nodiscard]] Void::Result::Result<void> start_transport();
|
||||
[[nodiscard]] Void::Result::Result<void> start_replication();
|
||||
[[nodiscard]] Void::Result::Result<void> publish_directory();
|
||||
/// Reverse-order unwind of everything recorded as started.
|
||||
void unwind() noexcept;
|
||||
|
||||
void record(std::string step);
|
||||
|
||||
HostResources m_resources;
|
||||
HostConfig m_config;
|
||||
std::vector<std::string> m_history;
|
||||
std::size_t m_started{0};
|
||||
bool m_storage_open{false};
|
||||
bool m_transport_live{false};
|
||||
bool m_replication_started{false};
|
||||
bool m_directory_published{false};
|
||||
bool m_stopped{false};
|
||||
Readiness m_readiness{Readiness::Starting};
|
||||
std::string m_last_error;
|
||||
};
|
||||
} // namespace DistributedHost
|
||||
Reference in new issue
Block a user