7#include "raft/client.hpp"
8#include "raft/persister.hpp"
13 constexpr std::pair<uint64_t, uint64_t> DEFAULT_TIMEOUT_INTERVAL_RANGE = {500, 1000};
15 constexpr uint64_t DEFAULT_HEARTBEAT_INTERVAL = 10;
19 uint64_t min = DEFAULT_TIMEOUT_INTERVAL_RANGE.first;
20 uint64_t max = DEFAULT_TIMEOUT_INTERVAL_RANGE.second;
24 [[nodiscard]] uint64_t
sample(std::mt19937& rng)
const
26 std::uniform_int_distribution dist(min, max);
37 bool operator==(
EntryInfo const& other)
const =
default;
43 using CommitCallback = std::function<void(
EntryInfo, std::vector<std::byte>)>;
59 using LeaderChangedCallback =
60 std::function<void(std::optional<Peer> leader,
bool isLeader,
bool lostLeadership)>;
70 std::optional<LeaderChangedCallback>
88 std::function<
void(tl::expected<data::AppendEntriesResponse, Error>)> callback) = 0;
95 std::function<
void(tl::expected<data::RequestVoteResponse, Error>)> callback) = 0;
117 virtual tl::expected<void, Error>
start() = 0;
123 [[nodiscard]]
virtual tl::expected<Peer, Error>
getLeader()
const = 0;
130 virtual tl::expected<EntryInfo, Error>
append(std::vector<std::byte> data) = 0;
150 [[nodiscard]]
virtual tl::expected<uint64_t, Error>
getTerm()
const = 0;
162 [[nodiscard]]
virtual std::string
getId()
const = 0;
166 [[nodiscard]]
virtual tl::expected<Status, Error>
getStatus()
const = 0;
174 tl::expected<std::shared_ptr<Server>, Error> createServer(
ServerCreateConfig& config);
180 struct hash<raft::EntryInfo>
184 std::size_t seed = std::hash<uint64_t> {}(e.
index);
185 raft::impl::hashCombine(seed, e.
term);
Definition server.hpp:114
virtual tl::expected< Peer, Error > getLeader() const =0
virtual void setCommitCallback(CommitCallback callback)=0
virtual tl::expected< EntryInfo, Error > append(std::vector< std::byte > data)=0
virtual void clearCommitCallback()=0
Clears the commit callback.
virtual std::string getId() const =0
virtual tl::expected< void, Error > start()=0
Starts Raft consensus.
virtual void clearLeaderChangedCallback()=0
Clears the leader changed callback.
virtual tl::expected< uint64_t, Error > getTerm() const =0
virtual void shutdown()=0
Shuts down the Raft server.
virtual void setLeaderChangedCallback(LeaderChangedCallback callback)=0
virtual tl::expected< uint64_t, Error > getCommitIndex() const =0
virtual tl::expected< Status, Error > getStatus() const =0
virtual tl::expected< uint64_t, Error > getLogByteCount() const =0
A service handler for the Raft server.
Definition server.hpp:79
virtual void handleRequestVote(const data::RequestVoteRequest &request, std::function< void(tl::expected< data::RequestVoteResponse, Error >)> callback)=0
virtual void handleAppendEntries(const data::AppendEntriesRequest &request, std::function< void(tl::expected< data::AppendEntriesResponse, Error >)> callback)=0
Information about a log entry.
Definition server.hpp:33
uint64_t term
The term of the log entry.
Definition server.hpp:35
uint64_t index
The index of the log entry.
Definition server.hpp:34
A peer in the Raft cluster.
Definition server.hpp:47
std::string address
Definition server.hpp:49
std::string id
The ID of the peer.
Definition server.hpp:48
Configuration for creating a Raft server.
Definition server.hpp:64
std::string id
The ID of the server.
Definition server.hpp:65
std::optional< LeaderChangedCallback > leaderChangedCallback
The leader changed callback to use.
Definition server.hpp:71
std::shared_ptr< Persister > persister
The persister to use.
Definition server.hpp:68
TimeoutInterval timeoutInterval
The election timeout interval.
Definition server.hpp:72
std::vector< Peer > peers
The list of other addresses of Raft servers in the cluster.
Definition server.hpp:67
uint64_t heartbeatInterval
The heartbeat interval.
Definition server.hpp:73
std::shared_ptr< ClientFactory > clientFactory
The client factory to use.
Definition server.hpp:66
uint16_t threadCount
The number of threads to use for network I/O and consensus.
Definition server.hpp:74
std::optional< CommitCallback > commitCallback
The commit callback to use.
Definition server.hpp:69
A consistent snapshot of the server's state.
Definition server.hpp:100
std::optional< Peer > leader
The current leader peer information, or std::nullopt if unknown.
Definition server.hpp:103
uint64_t logByteCount
The total size of the log in bytes.
Definition server.hpp:106
uint64_t term
The current term of the server.
Definition server.hpp:104
bool isLeader
Whether this server is currently the leader.
Definition server.hpp:101
uint64_t commitIndex
The index of the last committed log entry.
Definition server.hpp:105
uint64_t sample(std::mt19937 &rng) const
Definition server.hpp:24
The request message for AppendEntries.
Definition client.hpp:38
The request message for RequestVote.
Definition client.hpp:63