DistributedApplications: three-node networked KV fixture with quorum reads, durable replies and crash histories (P-DISTRIBUTED-04)
This commit is contained in:
1 parent
cfa860f944
commit
23a899a892
4 files changed
+677
No files matched your search
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
|
||||
#include "distributed_host/replicated_kv.hpp"
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Net/HttpServer.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace DistributedHost
|
||||
{
|
||||
struct ClusterFixtureConfig {
|
||||
/// Required bearer token for every peer and admin route.
|
||||
std::string token{"peer-secret"};
|
||||
std::size_t max_connections{8};
|
||||
std::chrono::milliseconds shutdown_grace{
|
||||
std::chrono::seconds{2}};
|
||||
};
|
||||
|
||||
/// Three-node replicated KV reference over real loopback HTTP listeners.
|
||||
///
|
||||
/// Each voter owns one `Void::Net::HttpServer`. Peer routes drive the
|
||||
/// deterministic replication core (elect, propose, read, heartbeat); an
|
||||
/// authenticated admin route models partitions for crash/partition
|
||||
/// histories. The transport is real; consensus timing is deterministic.
|
||||
///
|
||||
/// Routes (all require `Authorization: Bearer <token>`):
|
||||
/// GET /peer/health role/term/commit/applied
|
||||
/// POST /peer/elect become a candidate
|
||||
/// PUT /peer/propose?key=K body = value
|
||||
/// DELETE /peer/propose?key=K erase
|
||||
/// GET /peer/read?key=K consistent quorum read
|
||||
/// POST /peer/heartbeat replicate log and commit index
|
||||
/// POST /admin/partition?node=X&partition=N
|
||||
/// POST /admin/crash?node=X
|
||||
/// POST /admin/restart?node=X
|
||||
class ClusterFixture
|
||||
{
|
||||
public:
|
||||
|
||||
ClusterFixture(const ClusterFixture&) = delete;
|
||||
ClusterFixture& operator=(const ClusterFixture&) = delete;
|
||||
ClusterFixture(ClusterFixture&&) = delete;
|
||||
ClusterFixture& operator=(ClusterFixture&&) = delete;
|
||||
~ClusterFixture();
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<
|
||||
std::unique_ptr<ClusterFixture>>
|
||||
create(ClusterFixtureConfig config = {});
|
||||
|
||||
/// Bind and start every listener. Construction alone opens none.
|
||||
[[nodiscard]] Void::Result::Result<void> start();
|
||||
|
||||
/// Bounded, idempotent shutdown of every listener.
|
||||
void stop() noexcept;
|
||||
|
||||
/// `http://127.0.0.1:<port>` for one voter, or an empty string
|
||||
/// when it is unknown or not listening.
|
||||
[[nodiscard]] std::string endpoint(std::string_view node) const;
|
||||
|
||||
[[nodiscard]] const std::vector<std::string>& voters() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
struct PeerServer {
|
||||
std::string id;
|
||||
std::unique_ptr<Void::Net::HttpServer> server;
|
||||
};
|
||||
|
||||
explicit ClusterFixture(ClusterFixtureConfig config);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
register_routes(PeerServer& peer);
|
||||
|
||||
ClusterFixtureConfig m_config;
|
||||
ReplicatedKvCluster m_cluster;
|
||||
mutable std::mutex m_mutex;
|
||||
std::vector<std::unique_ptr<PeerServer>> m_peers;
|
||||
};
|
||||
} // namespace DistributedHost
|
||||
Reference in new issue
Block a user