-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft_impl.h
275 lines (196 loc) · 6.84 KB
/
raft_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#pragma once
#include <chrono>
#include <set>
#include <map>
#include <deque>
#include <memory>
#include <functional>
#include <stdint.h>
#include "utils.h"
#include "raft.pb.h"
#include "raft_config.h"
#include "random_utils.h"
namespace raft {
class ReplicateTracker;
enum class RaftRole : uint8_t {
LEADER = 1,
CANDIDATE = 2,
FOLLOWER = 3,
};
extern const size_t MAX_BATCH_SIZE;
// not thread safe
class RaftImpl {
private:
using TimeoutHandler =
std::function<MessageType(
RaftImpl&,
std::chrono::time_point<std::chrono::system_clock>)>;
using StepMessageHandler =
std::function<MessageType(RaftImpl&, const Message&)>;
public:
RaftImpl(uint64_t logid,
uint64_t selfid,
const std::set<uint64_t>& group_ids,
int min_election_timeout,
int max_election_timeout);
RaftImpl(
uint64_t logid,
uint64_t selfid,
const std::set<uint64_t>& group_ids,
int min_election_timeout,
int max_election_timeout,
const std::deque<std::unique_ptr<Entry>>& entry_queue,
uint64_t commited_index,
const RaftState* raft_state);
~RaftImpl();
MessageType CheckTerm(uint64_t msg_term);
MessageType CheckTimout(
std::chrono::time_point<std::chrono::system_clock> time_now);
MessageType step(const Message& msg);
std::vector<std::unique_ptr<Message>>
produceRsp(const Message& req_msg, MessageType rsp_msg_type);
std::unique_ptr<Message> buildMsgApp(
uint64_t peer_id, uint64_t next_index, size_t max_batch_size);
std::unique_ptr<Message>
buildMsgHeartbeat(uint64_t peer_id, uint64_t next_index) const;
bool isUpToDate(
uint64_t peer_log_term, uint64_t peer_max_index);
int appendLogs(const std::vector<const Entry*>& entries);
int appendEntries(
uint64_t prev_log_index,
uint64_t prev_log_term,
uint64_t leader_commited_index,
const std::vector<const Entry*>& entries);
int checkAndAppendEntries(
uint64_t prev_log_index,
const std::vector<const Entry*>& entries);
std::vector<std::unique_ptr<raft::Entry>>
getLogEntriesAfter(uint64_t log_index) const;
std::unique_ptr<raft::RaftState>
getCurrentRaftState() const;
std::unique_ptr<raft::RaftState>
getPendingRaftState() const;
std::vector<std::unique_ptr<raft::Entry>>
getPendingLogEntries() const;
// test helper function
void makeElectionTimeout(
std::chrono::time_point<std::chrono::system_clock> tp);
void makeHeartbeatTimeout(
std::chrono::time_point<std::chrono::system_clock> tp);
void assertNoPending() const;
int applyUnCommitedConfEntry(const Entry& conf_entry);
int applyCommitedConfEntry(const Entry& conf_entry);
int reconstructCurrentConfig();
const RaftConfig& GetCurrentConfig() const {
return current_config_;
}
const RaftConfig& GetCommitedConfig() const {
return commited_config_;
}
ReplicateTracker& GetReplicateTracker() {
assert(nullptr != replicate_states_);
return *replicate_states_;
}
public:
uint64_t getLogId() const {
return logid_;
}
uint64_t getSelfId() const {
return selfid_;
}
RaftRole getRole() const {
return role_;
}
void setRole(RaftRole new_role);
uint64_t getTerm() const {
return term_;
}
void setTerm(uint64_t new_term);
uint64_t getCommitedIndex() const {
return commited_index_;
}
uint64_t getVoteFor() const {
return vote_for_;
}
void setVoteFor(bool reset, uint64_t candidate);
uint64_t getLeader() const {
return leader_id_;
}
void setLeader(bool reset, uint64_t leader_id);
uint64_t assignStoreSeq(uint64_t index);
// meta_seq, log_idx, log_seq
std::tuple<uint64_t, uint64_t, uint64_t> getStoreSeq() const;
// commited
void commitedStoreSeq(
uint64_t meta_seq, uint64_t log_idx, uint64_t log_seq);
int getElectionTimeout() const {
return election_timeout_.count();
}
int getHeartbeatTimeout() const {
return hb_timeout_.count();
}
void updateActiveTime(
std::chrono::time_point<std::chrono::system_clock> time_now);
uint64_t getLastLogTerm() const;
uint64_t getLastLogIndex() const;
uint64_t getBaseLogTerm() const;
uint64_t getBaseLogIndex() const;
// base_index, last_index
std::tuple<uint64_t, uint64_t> getInMemIndex() const;
const Entry* getLogEntry(uint64_t log_index) const;
uint64_t getLogTerm(uint64_t log_index) const;
bool isIndexInMem(uint64_t log_index) const;
void beginVote();
void updateVote(uint64_t peer_id, bool current_rsp);
bool isMajorVoteYes() const;
bool isPeerUpToDate(uint64_t peer_commited_index) const;
void updateLeaderCommitedIndex(uint64_t new_commited_index);
void updateFollowerCommitedIndex(uint64_t leader_commited_index);
bool isMatch(uint64_t log_index, uint64_t log_term) const;
uint64_t findConflict(const std::vector<const Entry*>& entries) const;
bool updateReplicateState(
uint64_t peer_id,
bool reject, uint64_t reject_hint, uint64_t peer_next_index);
void becomeFollower(uint64_t term);
void becomeCandidate();
void becomeLeader();
void resetElectionTimeout();
bool isHeartbeatTimeout(
std::chrono::time_point<
std::chrono::system_clock> time_now);
void updateHeartbeatTime(
std::chrono::time_point<
std::chrono::system_clock> next_hb_time);
private:
RaftRole role_ = RaftRole::FOLLOWER;
TimeoutHandler timeout_handler_;
StepMessageHandler step_handler_;
uint64_t logid_ = 0;
uint64_t selfid_ = 0;
RaftConfig current_config_;
RaftConfig commited_config_;
uint64_t term_ = 0;
uint64_t vote_for_ = 0;
uint64_t commited_index_ = 0;
uint64_t leader_id_ = 0;
std::deque<std::unique_ptr<Entry>> logs_;
uint64_t store_seq_ = 0;
uint64_t pending_meta_seq_ = 0;
uint64_t pending_log_idx_ = 0;
uint64_t pending_log_seq_ = 0;
cutils::RandomTimeout rtimeout_;
std::chrono::milliseconds election_timeout_;
std::chrono::time_point<std::chrono::system_clock> active_time_;
// status of peers
// # candidcate #
std::map<uint64_t, bool> vote_resps_;
// # leader #
std::unique_ptr<ReplicateTracker> replicate_states_;
std::chrono::milliseconds hb_timeout_;
std::chrono::time_point<std::chrono::system_clock> hb_time_;
// TODO:
// trace peer_ids need log entries not in mem
// ?? way out ??
std::set<uint64_t> ids_not_in_mem_;
};
} // namespace raft;