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,20 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(DistributedHostGraph LANGUAGES CXX)
|
||||
|
||||
# Reference platform component, 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 Functional)
|
||||
|
||||
add_library(distributed_host_graph STATIC src/host_graph.cpp)
|
||||
target_include_directories(distributed_host_graph PUBLIC include)
|
||||
target_link_libraries(distributed_host_graph PUBLIC Void::Functional)
|
||||
|
||||
add_executable(distributed_host_graph_test tests/host_graph_test.cpp)
|
||||
target_link_libraries(distributed_host_graph_test PRIVATE distributed_host_graph)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME distributed_host_graph COMMAND distributed_host_graph_test)
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,316 @@
|
||||
#include "distributed_host/host_graph.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace DistributedHost;
|
||||
|
||||
/// Deterministic component fakes: they record nothing themselves (the
|
||||
/// graph's history is the ordered record) but can fail at a chosen step.
|
||||
struct FakeStorage final : StorageBackend {
|
||||
bool fail_open{false};
|
||||
bool fail_recover{false};
|
||||
int opens{0};
|
||||
int closes{0};
|
||||
|
||||
Void::Result::Result<void> open() override
|
||||
{
|
||||
++opens;
|
||||
if (fail_open) {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::io, "storage open failed"}};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
Void::Result::Result<void> recover() override
|
||||
{
|
||||
if (fail_recover) {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::corrupt,
|
||||
"storage recovery required"}};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
void close() noexcept override { ++closes; }
|
||||
};
|
||||
|
||||
struct FakeTransport final : PeerTransport {
|
||||
bool fail_listen{false};
|
||||
bool fail_connect{false};
|
||||
int closes{0};
|
||||
|
||||
Void::Result::Result<void> listen() override
|
||||
{
|
||||
if (fail_listen) {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::io, "listen failed"}};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
Void::Result::Result<void>
|
||||
connect_peers(const std::vector<NodeId>& peers) override
|
||||
{
|
||||
if (fail_connect) {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::io, "peer connect failed"}};
|
||||
}
|
||||
(void)peers;
|
||||
return {};
|
||||
}
|
||||
void close() noexcept override { ++closes; }
|
||||
};
|
||||
|
||||
struct FakeReplication final : ReplicationAdapter {
|
||||
bool fail_start{false};
|
||||
bool leader{true};
|
||||
Role current_role{Role::Leader};
|
||||
int stops{0};
|
||||
|
||||
Void::Result::Result<void> start() override
|
||||
{
|
||||
if (fail_start) {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::internal,
|
||||
"replication start failed"}};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
Role role() const noexcept override { return current_role; }
|
||||
bool leader_available() const noexcept override { return leader; }
|
||||
void stop() noexcept override { ++stops; }
|
||||
};
|
||||
|
||||
struct FakeDirectory final : DirectoryPublisher {
|
||||
bool fail_publish{false};
|
||||
int published{0};
|
||||
int withdrawals{0};
|
||||
|
||||
Void::Result::Result<void> publish(const NodeId& self) override
|
||||
{
|
||||
(void)self;
|
||||
++published;
|
||||
if (fail_publish) {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::io, "directory unavailable"}};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
void withdraw() noexcept override { ++withdrawals; }
|
||||
};
|
||||
|
||||
struct World {
|
||||
FakeStorage storage;
|
||||
FakeTransport transport;
|
||||
FakeReplication replication;
|
||||
FakeDirectory directory;
|
||||
|
||||
HostResources resources()
|
||||
{
|
||||
return HostResources{&storage, &transport, &replication,
|
||||
&directory};
|
||||
}
|
||||
};
|
||||
|
||||
HostConfig config()
|
||||
{
|
||||
HostConfig cfg;
|
||||
cfg.cluster = "test-cluster";
|
||||
cfg.self = "node-1";
|
||||
cfg.peers = {"node-2", "node-3"};
|
||||
return cfg;
|
||||
}
|
||||
|
||||
std::vector<std::string> expected_happy_start()
|
||||
{
|
||||
return {"storage.open", "storage.recover", "transport.listen",
|
||||
"transport.connect_peers", "replication.start",
|
||||
"replication.readiness", "directory.publish"};
|
||||
}
|
||||
|
||||
void compose_validates_configuration()
|
||||
{
|
||||
World world;
|
||||
HostConfig empty_cluster = config();
|
||||
empty_cluster.cluster.clear();
|
||||
assert(!DistributedHostGraph::compose(world.resources(), empty_cluster)
|
||||
.has_value());
|
||||
|
||||
HostConfig self_as_peer = config();
|
||||
self_as_peer.peers.push_back("node-1");
|
||||
assert(!DistributedHostGraph::compose(world.resources(), self_as_peer)
|
||||
.has_value());
|
||||
|
||||
HostConfig zero_deadline = config();
|
||||
zero_deadline.startup_deadline = std::chrono::milliseconds{0};
|
||||
assert(!DistributedHostGraph::compose(world.resources(), zero_deadline)
|
||||
.has_value());
|
||||
|
||||
HostResources incomplete{&world.storage, nullptr, &world.replication,
|
||||
nullptr};
|
||||
assert(!DistributedHostGraph::compose(incomplete, config()).has_value());
|
||||
}
|
||||
|
||||
void happy_path_starts_in_order_and_stops_in_reverse()
|
||||
{
|
||||
World world;
|
||||
auto graph = DistributedHostGraph::compose(world.resources(), config());
|
||||
assert(graph.has_value());
|
||||
|
||||
// Construction starts nothing.
|
||||
assert(graph->history().empty());
|
||||
assert(graph->readiness() == Readiness::Starting);
|
||||
|
||||
assert(graph->start().has_value());
|
||||
assert(graph->readiness() == Readiness::WriteReady);
|
||||
assert(graph->health().quorum_available);
|
||||
assert(graph->health().started_components == 3);
|
||||
assert(graph->history() == expected_happy_start());
|
||||
|
||||
graph->stop();
|
||||
const std::vector<std::string> expected_stop{
|
||||
"directory.withdraw", "replication.stop", "transport.close",
|
||||
"storage.close"};
|
||||
assert(graph->history().size() ==
|
||||
expected_happy_start().size() + expected_stop.size());
|
||||
assert(std::equal(expected_stop.begin(), expected_stop.end(),
|
||||
graph->history().end() - expected_stop.size()));
|
||||
assert(graph->readiness() == Readiness::Stopped);
|
||||
assert(graph->health().started_components == 0);
|
||||
assert(world.storage.closes == 1);
|
||||
assert(world.transport.closes == 1);
|
||||
assert(world.replication.stops == 1);
|
||||
|
||||
// Idempotent.
|
||||
graph->stop();
|
||||
assert(world.storage.closes == 1);
|
||||
}
|
||||
|
||||
void follower_is_healthy_but_not_write_ready()
|
||||
{
|
||||
World world;
|
||||
world.replication.leader = false;
|
||||
world.replication.current_role = Role::Follower;
|
||||
HostConfig cfg = config();
|
||||
cfg.require_leader_readiness = false;
|
||||
auto graph = DistributedHostGraph::compose(world.resources(), cfg);
|
||||
assert(graph.has_value());
|
||||
assert(graph->start().has_value());
|
||||
assert(graph->readiness() == Readiness::FollowerHealthy);
|
||||
assert(!graph->health().quorum_available);
|
||||
}
|
||||
|
||||
void missing_leader_path_fails_startup_and_unwinds()
|
||||
{
|
||||
World world;
|
||||
world.replication.leader = false;
|
||||
auto graph = DistributedHostGraph::compose(world.resources(), config());
|
||||
assert(graph.has_value());
|
||||
auto started = graph->start();
|
||||
assert(!started.has_value());
|
||||
assert(graph->readiness() == Readiness::Failed);
|
||||
assert(graph->health().last_error.find("no leader path") !=
|
||||
std::string::npos);
|
||||
|
||||
// Everything started before the failure is closed in reverse order.
|
||||
const std::vector<std::string> expected{
|
||||
"storage.open", "storage.recover", "transport.listen",
|
||||
"transport.connect_peers", "replication.start", "replication.stop",
|
||||
"transport.close", "storage.close"};
|
||||
assert(graph->history() == expected);
|
||||
assert(world.storage.closes == 1);
|
||||
assert(world.transport.closes == 1);
|
||||
assert(world.replication.stops == 1);
|
||||
}
|
||||
|
||||
void storage_recovery_failure_unwinds_opened_storage()
|
||||
{
|
||||
World world;
|
||||
world.storage.fail_recover = true;
|
||||
auto graph = DistributedHostGraph::compose(world.resources(), config());
|
||||
assert(graph.has_value());
|
||||
assert(!graph->start().has_value());
|
||||
assert(graph->readiness() == Readiness::Failed);
|
||||
assert(graph->health().last_error == "storage recovery required");
|
||||
const std::vector<std::string> expected{"storage.open",
|
||||
"storage.close"};
|
||||
assert(graph->history() == expected);
|
||||
assert(world.storage.closes == 1);
|
||||
}
|
||||
|
||||
void peer_connect_failure_keeps_original_error()
|
||||
{
|
||||
World world;
|
||||
world.transport.fail_connect = true;
|
||||
auto graph = DistributedHostGraph::compose(world.resources(), config());
|
||||
assert(graph.has_value());
|
||||
assert(!graph->start().has_value());
|
||||
assert(graph->health().last_error == "peer connect failed");
|
||||
assert(graph->readiness() == Readiness::Failed);
|
||||
const std::vector<std::string> expected{
|
||||
"storage.open", "storage.recover", "transport.listen",
|
||||
"transport.close", "storage.close"};
|
||||
assert(graph->history() == expected);
|
||||
}
|
||||
|
||||
void discovery_failure_does_not_weaken_quorum()
|
||||
{
|
||||
World world;
|
||||
world.directory.fail_publish = true;
|
||||
auto graph = DistributedHostGraph::compose(world.resources(), config());
|
||||
assert(graph.has_value());
|
||||
assert(graph->start().has_value());
|
||||
// Still write-ready: discovery is an observation adapter.
|
||||
assert(graph->readiness() == Readiness::WriteReady);
|
||||
assert(graph->health().quorum_available);
|
||||
assert(graph->health().last_error == "directory unavailable");
|
||||
assert(world.directory.published == 1);
|
||||
// Nothing to withdraw: the publication was never recorded.
|
||||
graph->stop();
|
||||
assert(world.directory.withdrawals == 0);
|
||||
}
|
||||
|
||||
void discovery_can_be_disabled()
|
||||
{
|
||||
World world;
|
||||
HostConfig cfg = config();
|
||||
cfg.publish_discovery = false;
|
||||
auto graph = DistributedHostGraph::compose(world.resources(), cfg);
|
||||
assert(graph.has_value());
|
||||
assert(graph->start().has_value());
|
||||
assert(world.directory.published == 0);
|
||||
for (const auto& step : graph->history()) {
|
||||
assert(step != "directory.publish");
|
||||
}
|
||||
}
|
||||
|
||||
void double_start_is_rejected()
|
||||
{
|
||||
World world;
|
||||
auto graph = DistributedHostGraph::compose(world.resources(), config());
|
||||
assert(graph.has_value());
|
||||
assert(graph->start().has_value());
|
||||
assert(!graph->start().has_value());
|
||||
graph->stop();
|
||||
assert(!graph->start().has_value());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
compose_validates_configuration();
|
||||
happy_path_starts_in_order_and_stops_in_reverse();
|
||||
follower_is_healthy_but_not_write_ready();
|
||||
missing_leader_path_fails_startup_and_unwinds();
|
||||
storage_recovery_failure_unwinds_opened_storage();
|
||||
peer_connect_failure_keeps_original_error();
|
||||
discovery_failure_does_not_weaken_quorum();
|
||||
discovery_can_be_disabled();
|
||||
double_start_is_rejected();
|
||||
std::puts("distributed host graph ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user