162 lines
4.9 KiB
C++
162 lines
4.9 KiB
C++
#pragma once
|
|
|
|
#include "distributed_host/host_graph.hpp"
|
|
|
|
#include <Void/Net/HttpServer.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace DistributedHost
|
|
{
|
|
/// Validated single-node configuration. Construction opens nothing.
|
|
struct SingleNodeConfig {
|
|
std::string peer_address{"127.0.0.1:0"};
|
|
/// Required peer credential; an empty token fails closed.
|
|
std::string peer_token{};
|
|
std::chrono::milliseconds shutdown_grace{
|
|
std::chrono::seconds{5}};
|
|
std::size_t max_connections{16};
|
|
std::size_t max_append_bytes{64ULL * 1024ULL};
|
|
/// Bounded retained peer log entries.
|
|
std::size_t max_log_entries{1024};
|
|
|
|
[[nodiscard]] Void::Result::Result<void> validate() const;
|
|
};
|
|
|
|
/// Bounded in-memory replicated log served by the peer fixture. The
|
|
/// single-node composition owns it; durable storage arrives with the
|
|
/// multi-node reference in a later task.
|
|
class PeerLog
|
|
{
|
|
public:
|
|
|
|
explicit PeerLog(std::size_t max_entries = 1024);
|
|
|
|
/// Append one opaque entry. Returns the assigned index (1-based).
|
|
[[nodiscard]] Void::Result::Result<std::uint64_t>
|
|
append(std::string entry);
|
|
|
|
[[nodiscard]] std::size_t size() const;
|
|
[[nodiscard]] std::uint64_t last_index() const;
|
|
|
|
private:
|
|
|
|
mutable std::mutex m_mutex;
|
|
std::size_t m_max_entries;
|
|
std::vector<std::string> m_entries;
|
|
};
|
|
|
|
/// Real authenticated peer transport over Void::Net::HttpServer.
|
|
///
|
|
/// Routes: `GET /peer/health` (bounded status, no secrets) and
|
|
/// `POST /peer/append` (bearer-authenticated, bounded body). A missing or
|
|
/// empty configured token rejects every request.
|
|
class HttpPeerTransport final : public PeerTransport
|
|
{
|
|
public:
|
|
|
|
HttpPeerTransport(std::string address, std::string token,
|
|
PeerLog& log, std::size_t max_connections,
|
|
std::size_t max_append_bytes);
|
|
~HttpPeerTransport() noexcept override;
|
|
|
|
HttpPeerTransport(const HttpPeerTransport&) = delete;
|
|
HttpPeerTransport& operator=(const HttpPeerTransport&) = delete;
|
|
|
|
[[nodiscard]] Void::Result::Result<void> listen() override;
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
connect_peers(const std::vector<NodeId>& peers) override;
|
|
void close() noexcept override;
|
|
|
|
[[nodiscard]] std::uint16_t bound_port() const noexcept;
|
|
[[nodiscard]] std::size_t connected_peers() const noexcept
|
|
{
|
|
return m_connected_peers;
|
|
}
|
|
|
|
private:
|
|
|
|
[[nodiscard]] bool authorized(const Void::Net::ServerRequest& req)
|
|
const noexcept;
|
|
|
|
std::string m_address;
|
|
std::string m_token;
|
|
PeerLog& m_log;
|
|
std::size_t m_max_connections;
|
|
std::size_t m_max_append_bytes;
|
|
std::size_t m_connected_peers{0};
|
|
std::unique_ptr<Void::Net::HttpServer> m_server{};
|
|
};
|
|
|
|
/// Single-node replication adapter: a sole voter is always the leader
|
|
/// while started. Multi-node election belongs to the Coordination plan.
|
|
class SingleNodeReplication final : public ReplicationAdapter
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] Void::Result::Result<void> start() override;
|
|
[[nodiscard]] Role role() const noexcept override
|
|
{
|
|
return m_running ? Role::Leader : Role::Follower;
|
|
}
|
|
[[nodiscard]] bool leader_available() const noexcept override
|
|
{
|
|
return m_running;
|
|
}
|
|
void stop() noexcept override { m_running = false; }
|
|
|
|
private:
|
|
|
|
bool m_running{false};
|
|
};
|
|
|
|
/// Single-node reference composition: the host graph wired with the
|
|
/// authenticated HTTP peer fixture and a leader-available replication
|
|
/// adapter. Startup is bounded and rolls back in reverse order.
|
|
class SingleNodeHost
|
|
{
|
|
public:
|
|
|
|
SingleNodeHost(const SingleNodeHost&) = delete;
|
|
SingleNodeHost& operator=(const SingleNodeHost&) = delete;
|
|
/// Movable: every component the host graph points at is heap-owned,
|
|
/// so a move transfers ownership without changing addresses.
|
|
SingleNodeHost(SingleNodeHost&&) noexcept = default;
|
|
SingleNodeHost& operator=(SingleNodeHost&&) noexcept = default;
|
|
~SingleNodeHost();
|
|
|
|
[[nodiscard]] static Void::Result::Result<SingleNodeHost>
|
|
create(SingleNodeConfig config);
|
|
|
|
[[nodiscard]] Void::Result::Result<void> start();
|
|
void stop() noexcept;
|
|
|
|
[[nodiscard]] HealthSnapshot health() const;
|
|
[[nodiscard]] std::uint16_t peer_port() const noexcept;
|
|
[[nodiscard]] const std::vector<std::string>& history() const noexcept;
|
|
|
|
private:
|
|
|
|
struct NoopStorage final : StorageBackend {
|
|
Void::Result::Result<void> open() override { return {}; }
|
|
Void::Result::Result<void> recover() override { return {}; }
|
|
void close() noexcept override {}
|
|
};
|
|
|
|
explicit SingleNodeHost(SingleNodeConfig config);
|
|
|
|
SingleNodeConfig m_config;
|
|
std::unique_ptr<PeerLog> m_log;
|
|
std::unique_ptr<NoopStorage> m_storage;
|
|
std::unique_ptr<HttpPeerTransport> m_transport;
|
|
std::unique_ptr<SingleNodeReplication> m_replication;
|
|
std::unique_ptr<DistributedHostGraph> m_graph;
|
|
};
|
|
} // namespace DistributedHost
|