-
Notifications
You must be signed in to change notification settings - Fork 2
/
raft.h
executable file
·76 lines (61 loc) · 1.8 KB
/
raft.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
#pragma once
#include "kvraft.pb.h"
#include "kvraft_client_uthread.h"
#include "phxrpc/network.h"
#include "snapshot.h"
#include <mutex>
#include <string>
#include <thread>
#include <vector>
using std::string;
using std::vector;
namespace raftkv {
using namespace phxrpc;
enum NodeState { Follower, Candidate, Leader };
struct LogEntry {
enum operation { GET, PUT, DEL };
int index;
operation op;
string key;
string value;
int term;
};
class Raft {
public:
Raft(int me, int num_of_server);
~Raft();
std::pair<int, bool> GetState();
void AppendEntries(const kvraft::AppendEntriesArgs &req, kvraft::AppendEntriesReply *resp);
void RequestVote(const kvraft::RequestVoteArgs &req, kvraft::RequestVoteReply *resp);
void SendRequestVotesToAll(const kvraft::RequestVoteArgs &req);
void SendAppendEntriesToAll();
void HandleAppendEntries(int server, const kvraft::AppendEntriesReply &resp);
void HandleRequestVote(const kvraft::RequestVoteReply &resp);
void CommitLog();
std::pair<int, bool> Start(const raftkv::LogEntry::operation &op, const string &key,
const string &value, const int &evfd);
void HandleTimeout(UThreadSocket_t *socket);
void ResetTimer();
void RunTimer();
friend class Snapshot;
private:
std::mutex raft_mutex_;
int me_;
int num_of_server_;
int current_term_{0};
int voted_for_{-1};
vector<LogEntry> log_;
int commit_index_{0};
int last_applied_{0};
vector<int> next_index_;
vector<int> match_index_;
NodeState state_{Follower};
int sum_of_vote_{0};
int timer_fd_;
std::thread thread_;
UThreadEpollScheduler scheduler_;
KVRaftClientUThread client_;
std::map<int, int> channel_;
Snapshot snapshot_;
};
} // namespace raftkv