105 lines
3.4 KiB
C++
105 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include "distributed_host/replicated_kv.hpp"
|
|
#include "distributed_host/replicated_queue.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}};
|
|
/// Queue policy shared by every node handler.
|
|
QueueConfig queue{};
|
|
/// Bounded recorded outbox deliveries.
|
|
std::size_t max_delivered{1024};
|
|
};
|
|
|
|
/// 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
|
|
///
|
|
/// Replicated worker queue over the same group:
|
|
/// POST /queue/publish?key=K body = payload
|
|
/// POST /queue/claim?consumer=C
|
|
/// POST /queue/ack?consumer=C&id=J
|
|
/// POST /queue/nack?consumer=C&id=J body = reason
|
|
/// POST /queue/recover?consumer=C
|
|
/// POST /queue/dispatch deliver pending outbox effects
|
|
/// GET /queue/status?id=J
|
|
/// GET /queue/dlq
|
|
/// GET /queue/delivered
|
|
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;
|
|
std::vector<std::string> m_delivered;
|
|
};
|
|
} // namespace DistributedHost
|