-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft_rpc_handlers.go
183 lines (149 loc) · 5 KB
/
raft_rpc_handlers.go
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
package raft
import "time"
// Handles an incoming RPC RequestVote request
type RequestVoteArgs struct {
Term int
CandidateId int
LastLogIndex int
LastLogTerm int
Latency int
}
type RequestVoteReply struct {
Term int
VoteGranted bool
}
// RequestVote RPC. This is the function that is executed by the node that
// RECEIVES the RequestVote.
func (this *RaftNode) HandleRequestVote(args RequestVoteArgs, reply *RequestVoteReply) error {
this.mu.Lock()
defer this.mu.Unlock()
if this.state == "Dead" {
return nil
}
var nodeLastLogIndex, nodeLastLogTerm int
if len(this.log) > 0 {
lastIndex := len(this.log) - 1
nodeLastLogIndex, nodeLastLogTerm = lastIndex, this.log[lastIndex].Term
} else {
nodeLastLogIndex, nodeLastLogTerm = -1, -1
}
if LogVoteRequestMessages {
this.write_log("Received Vote Request from NODE %d; Args: %+v [currentTerm=%d, votedFor=%d, log index/term=(%d, %d)]", args.CandidateId, args, this.currentTerm, this.votedFor, nodeLastLogIndex, nodeLastLogTerm)
}
if args.Term > this.currentTerm {
this.becomeFollower(args.Term)
}
// IMPLEMENT THE LOGIC FOR WHETHER THIS NODE VOTES FOR THE CANDIDATE THAT SENT
// THIS REQUEST, OR NOT
// All the variables that you need for the conditions have been defined above.
//-------------------------------------------------------------------------------------------/
if args.Term == this.currentTerm && (this.votedFor == -1 || this.votedFor == args.CandidateId) &&
(nodeLastLogTerm < args.LastLogTerm || (nodeLastLogTerm == args.LastLogTerm && nodeLastLogIndex <= args.LastLogIndex)) { // TODO: what are the conditions necessary to vote? HINT: there's multiple.
// TODO: indicate that it has voted.
this.votedFor = args.CandidateId
reply.VoteGranted = true
} else {
reply.VoteGranted = false
}
//-------------------------------------------------------------------------------------------/
reply.Term = this.currentTerm
if LogVoteRequestMessages {
this.write_log("Sending Request Vote Reply: %+v", reply)
}
return nil
}
// Handles an incoming RPC AppendEntries request
type AppendEntriesArgs struct {
Term int
LeaderId int
PrevLogIndex int
PrevLogTerm int
Entries []LogEntry
LeaderCommit int
Latency int
}
type AppendEntriesReply struct {
Term int
Success bool
}
func (this *RaftNode) HandleAppendEntries(args AppendEntriesArgs, reply *AppendEntriesReply) error {
this.mu.Lock()
defer this.mu.Unlock()
if this.state == "Dead" {
return nil
}
var aeType string
if len(args.Entries) > 0 {
aeType = "AppendEntries"
} else {
aeType = "Heartbeat"
}
if (aeType == "Heartbeat" && LogHeartbeatMessages) || aeType == "AppendEntries" {
this.write_log("Received %s from NODE %d; args: %+v", aeType, args.LeaderId, args)
}
if args.Term > this.currentTerm {
this.becomeFollower(args.Term)
}
reply.Success = false
if args.Term == this.currentTerm {
if this.state != "Follower" {
this.becomeFollower(args.Term)
}
this.lastElectionTimerStartedTime = time.Now()
// Does our log contain an entry at PrevLogIndex whose term matches PrevLogTerm?
if args.PrevLogIndex == -1 ||
(args.PrevLogIndex < len(this.log) && args.PrevLogTerm == this.log[args.PrevLogIndex].Term) {
reply.Success = true
// Find an insertion point - where there's a term mismatch between
// the existing log starting at PrevLogIndex+1 and the new entries sent
// in the RPC.
logInsertIndex := args.PrevLogIndex + 1
newEntriesIndex := 0
for {
if logInsertIndex >= len(this.log) || newEntriesIndex >= len(args.Entries) {
break
}
if this.log[logInsertIndex].Term != args.Entries[newEntriesIndex].Term {
break
}
logInsertIndex++
newEntriesIndex++
}
// At the end of this loop:
// - logInsertIndex points at the end of the log, or an index where the
// term mismatches with an entry from the leader
// - newEntriesIndex points at the end of Entries, or an index where the
// term mismatches with the corresponding log entry
if newEntriesIndex < len(args.Entries) {
this.log = append(this.log[:logInsertIndex], args.Entries[newEntriesIndex:]...)
this.write_log("Log is now: %v", this.log)
}
// Set commit index.
if args.LeaderCommit > this.commitIndex {
if args.LeaderCommit < len(this.log)-1 {
this.commitIndex = len(this.log) - 1
} else {
this.commitIndex = args.LeaderCommit
}
this.notifyToApplyCommit <- 1
}
}
}
reply.Term = this.currentTerm
if (aeType == "Heartbeat" && LogHeartbeatMessages) || aeType == "AppendEntries" {
this.write_log("Sending %s reply: %+v", aeType, *reply)
}
return nil
}
// Either handle Command or tell to divert it to Leader
func (this *RaftNode) ReceiveClientCommand(command interface{}) bool {
this.mu.Lock()
defer this.mu.Unlock()
this.write_log("ReceiveClientCommand received by %s: %v", this.state, command)
if this.state == "Leader" {
this.log = append(this.log, LogEntry{Command: command, Term: this.currentTerm})
this.write_log("Log=%v", this.log)
return true
}
return false
}