Files
Library/examples/distributed/host-graph/include/distributed_host/replicated_kv.hpp
T

158 lines
5.2 KiB
C++

#pragma once
#include "distributed_host/host_graph.hpp"
#include <Void/Functional/Result.hpp>
#include <cstddef>
#include <cstdint>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
namespace DistributedHost
{
/// One replicated log entry. A missing value is an erase.
struct KvLogEntry {
std::uint64_t term{0};
std::uint64_t index{0};
std::string key;
std::optional<std::string> value;
};
enum class KvWriteOutcome : std::uint8_t {
/// Majority-committed and applied: the only durable reply.
Applied,
/// The entry may exist locally but no majority acknowledged it. It is
/// not durable and a later leader may overwrite it.
NoQuorum,
NotLeader,
NodeDown
};
struct KvWriteReply {
KvWriteOutcome outcome{KvWriteOutcome::NoQuorum};
std::uint64_t index{0};
std::uint64_t term{0};
};
/// Immutable inspection copy of one voter.
struct KvNodeSnapshot {
std::string id;
bool alive{true};
Role role{Role::Follower};
std::uint64_t term{0};
std::uint64_t commit_index{0};
std::uint64_t applied_index{0};
std::size_t log_size{0};
std::optional<std::string> leader;
std::map<std::string, std::string, std::less<>> kv;
};
/// Deterministic single-group replicated key-value cluster.
///
/// Models the safety rules the reference distributed KV depends on
/// without sockets, threads or clocks:
/// * a write is replied Applied only after a majority of matching acks
/// (durable reply), never on local append alone;
/// * a leader in a minority cannot commit and is stepped down when it
/// observes a higher term;
/// * a consistent read is served only by a leader that confirms a
/// majority in its term and has applied every committed entry;
/// * committed entries survive crash/restart and are never overwritten.
///
/// The simulator is single-threaded; tests drive elections, proposals,
/// partitions and crashes explicitly.
class ReplicatedKvCluster
{
public:
explicit ReplicatedKvCluster(std::vector<std::string> voters);
[[nodiscard]] const std::vector<std::string>& voters() const noexcept
{
return m_voters;
}
/// Request a majority election for `candidate` in a new term.
/// Fails without changing the cluster when no majority is
/// reachable.
[[nodiscard]] Void::Result::Result<void>
elect(std::string_view candidate);
/// Append and replicate one command through `leader`. The reply is
/// Applied only when a majority acknowledged the matching entry.
[[nodiscard]] KvWriteReply propose(std::string_view leader,
std::string key,
std::optional<std::string> value);
/// Replicate the leader's log and commit index without appending a
/// new command (the AppendEntries heartbeat). Followers in a newer
/// term step the leader down.
[[nodiscard]] Void::Result::Result<void>
heartbeat(std::string_view leader);
/// Linearizable read: leader-only, majority-confirmed in the
/// leader's term, after applying every committed entry.
[[nodiscard]] Void::Result::Result<std::optional<std::string>>
quorum_read(std::string_view node, std::string_view key);
/// Quorum-confirmed copy of the leader's applied state. Used by
/// compositions that must enumerate keys (e.g. the worker queue).
[[nodiscard]] Void::Result::Result<
std::map<std::string, std::string, std::less<>>>
quorum_snapshot(std::string_view node);
/// Crash/restart keep the durable log, term and vote.
void crash(std::string_view node);
void restart(std::string_view node);
/// Move a node to a partition id. Nodes only communicate within
/// the same partition id.
void set_partition(std::string_view node, int partition);
[[nodiscard]] std::optional<KvNodeSnapshot>
snapshot(std::string_view node) const;
/// Highest commit index across live nodes.
[[nodiscard]] std::uint64_t committed_index() const;
private:
struct Node {
std::string id;
bool alive{true};
int partition{0};
Role role{Role::Follower};
std::uint64_t term{0};
std::optional<std::string> voted_for;
std::optional<std::string> leader;
std::vector<KvLogEntry> log;
std::uint64_t commit_index{0};
std::uint64_t applied_index{0};
std::map<std::string, std::string, std::less<>> kv;
};
[[nodiscard]] Node* find(std::string_view id);
[[nodiscard]] const Node* find(std::string_view id) const;
[[nodiscard]] bool reachable(const Node& a, const Node& b) const;
[[nodiscard]] bool majority(std::size_t acks) const;
/// True when `candidate` has a log at least as up to date as `voter`.
[[nodiscard]] static bool log_up_to_date(const Node& candidate,
const Node& voter);
void apply_committed(Node& node);
/// Majority-confirm `node` as leader in its term, then apply pending
/// committed entries. Shared by quorum reads and snapshots.
[[nodiscard]] Void::Result::Result<void>
confirm_leader(Node& node);
/// Copy the leader's log to one reachable follower. False when the
/// follower cannot be brought in line (e.g. it is in a newer term).
[[nodiscard]] bool replicate_to(Node& leader, Node& follower);
std::vector<std::string> m_voters;
std::vector<Node> m_nodes;
};
} // namespace DistributedHost