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,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