raft 0.1.0
Loading...
Searching...
No Matches
server.hpp
1#pragma once
2
3#include <optional>
4#include <random>
5
6#include "impl/hash.h"
7#include "raft/client.hpp"
8#include "raft/persister.hpp"
9
10namespace raft
11{
13 constexpr std::pair<uint64_t, uint64_t> DEFAULT_TIMEOUT_INTERVAL_RANGE = {500, 1000};
15 constexpr uint64_t DEFAULT_HEARTBEAT_INTERVAL = 10;
16
18 {
19 uint64_t min = DEFAULT_TIMEOUT_INTERVAL_RANGE.first;
20 uint64_t max = DEFAULT_TIMEOUT_INTERVAL_RANGE.second;
21
24 [[nodiscard]] uint64_t sample(std::mt19937& rng) const
25 {
26 std::uniform_int_distribution dist(min, max);
27 return dist(rng);
28 }
29 };
30
32 struct EntryInfo
33 {
34 uint64_t index;
35 uint64_t term;
36
37 bool operator==(EntryInfo const& other) const = default;
38 };
39
43 using CommitCallback = std::function<void(EntryInfo, std::vector<std::byte>)>;
44
46 struct Peer
47 {
48 std::string id;
49 std::string address;
51 };
52
59 using LeaderChangedCallback =
60 std::function<void(std::optional<Peer> leader, bool isLeader, bool lostLeadership)>;
61
64 {
65 std::string id;
66 std::shared_ptr<ClientFactory> clientFactory;
67 std::vector<Peer> peers;
68 std::shared_ptr<Persister> persister;
69 std::optional<CommitCallback> commitCallback;
70 std::optional<LeaderChangedCallback>
73 uint64_t heartbeatInterval = DEFAULT_HEARTBEAT_INTERVAL;
74 uint16_t threadCount = 1;
75 };
76
79 {
80 public:
81 virtual ~ServiceHandler() = default;
82
86 virtual void handleAppendEntries(
87 const data::AppendEntriesRequest& request,
88 std::function<void(tl::expected<data::AppendEntriesResponse, Error>)> callback) = 0;
89
93 virtual void handleRequestVote(
94 const data::RequestVoteRequest& request,
95 std::function<void(tl::expected<data::RequestVoteResponse, Error>)> callback) = 0;
96 };
97
99 struct Status
100 {
101 bool isLeader;
102 std::optional<Peer>
104 uint64_t term;
105 uint64_t commitIndex;
106 uint64_t logByteCount;
107 };
108
113 class Server : public ServiceHandler
114 {
115 public:
117 virtual tl::expected<void, Error> start() = 0;
119 virtual void shutdown() = 0;
120
123 [[nodiscard]] virtual tl::expected<Peer, Error> getLeader() const = 0;
124
130 virtual tl::expected<EntryInfo, Error> append(std::vector<std::byte> data) = 0;
131
135 virtual void setCommitCallback(CommitCallback callback) = 0;
136
138 virtual void clearCommitCallback() = 0;
139
143 virtual void setLeaderChangedCallback(LeaderChangedCallback callback) = 0;
144
146 virtual void clearLeaderChangedCallback() = 0;
147
150 [[nodiscard]] virtual tl::expected<uint64_t, Error> getTerm() const = 0;
151
154 [[nodiscard]] virtual tl::expected<uint64_t, Error> getCommitIndex() const = 0;
155
158 [[nodiscard]] virtual tl::expected<uint64_t, Error> getLogByteCount() const = 0;
159
162 [[nodiscard]] virtual std::string getId() const = 0;
163
166 [[nodiscard]] virtual tl::expected<Status, Error> getStatus() const = 0;
167 };
168
174 tl::expected<std::shared_ptr<Server>, Error> createServer(ServerCreateConfig& config);
175} // namespace raft
176
177namespace std
178{
179 template<>
180 struct hash<raft::EntryInfo>
181 {
182 std::size_t operator()(const raft::EntryInfo& e) const noexcept
183 {
184 std::size_t seed = std::hash<uint64_t> {}(e.index);
185 raft::impl::hashCombine(seed, e.term);
186
187 return seed;
188 }
189 };
190} // namespace std
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
Definition server.hpp:18
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