/
state.go
239 lines (207 loc) · 5.19 KB
/
state.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
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
package raft
import (
"sync"
"github.com/souleb/raft/log"
"golang.org/x/net/context"
)
// stateFn represents the state of the RaftNode
type stateFn func(ctx context.Context) stateFn
type state struct {
isLeader bool
//latest term server has seen (initialized to 0, increases monotonically).
currentTerm uint64
// candidateId that received vote in current term (or null if none)
votedFor int32
// index of highest log entry known to be committed (initialized to 0, increases monotonically).
commitIndex uint64
// index of highest log entry applied to state machine (initialized to 0, increases monotonically).
lastApplied uint64
// log entries; each entry contains command for state machine, and term when entry was received by leader (first index is 1)
log log.LogEntries
// for each server, index of the next log entry to send to that server (initialized to leader last log index + 1)
nextIndex map[uint]uint64
// for each server, index of highest log entry known to be replicated on server (initialized to 0, increases monotonically)
matchIndex map[uint]uint64
// Observer is a list of observers that are notified when the RaftNode
// observes a change in leadership.
observers []Observer
mu sync.RWMutex
}
func (s *state) setTermAndVote(term uint64, id int32) {
s.mu.Lock()
defer s.mu.Unlock()
s.currentTerm = term
s.votedFor = id
}
func (s *state) resetElectionFields(term uint64, leader bool) {
s.mu.Lock()
defer s.mu.Unlock()
s.currentTerm = term
s.votedFor = -1
if leader {
s.isLeader = false
for _, o := range s.observers {
o.Notify(false)
}
}
}
// SetLogs sets the log entries for the RaftNode.
func (s *state) SetLogs(log log.LogEntries) {
s.mu.Lock()
defer s.mu.Unlock()
s.log = log
}
func (s *state) getVotedFor() int32 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.votedFor
}
func (s *state) setVotedFor(id int32) {
s.mu.Lock()
defer s.mu.Unlock()
s.votedFor = id
}
func (s *state) getCurrentTerm() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.currentTerm
}
func (s *state) setCurrentTerm(term uint64) {
s.mu.Lock()
defer s.mu.Unlock()
s.currentTerm = term
}
func (s *state) setLeader(leader bool) {
s.mu.Lock()
defer s.mu.Unlock()
s.isLeader = leader
for _, o := range s.observers {
o.Notify(leader)
}
}
func (s *state) getPeerNextIndex(peer uint) uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.nextIndex[peer]
}
func (s *state) decrementPeerNextIndex(peer uint) {
s.mu.Lock()
defer s.mu.Unlock()
if s.nextIndex[peer] > 1 {
s.nextIndex[peer]--
}
}
func (s *state) updatePeerNextIndex(peer uint, index uint64) {
s.mu.Lock()
defer s.mu.Unlock()
s.nextIndex[peer] = index
}
func (s *state) updatePeerMatchIndex(peer uint, index uint64) {
s.mu.Lock()
defer s.mu.Unlock()
s.matchIndex[peer] = index
}
func (s *state) appendEntry(entry log.LogEntry) {
s.mu.Lock()
defer s.mu.Unlock()
s.log.AppendEntry(entry)
}
func (s *state) getLogs(index uint64) []*log.LogEntry {
s.mu.RLock()
defer s.mu.RUnlock()
return s.log.GetEntriesSlice(index)
}
func (s *state) getEntriesFromIndex(index uint64) log.LogEntries {
s.mu.RLock()
defer s.mu.RUnlock()
return s.log.GetEntriesFromIndex(index)
}
func (s *state) getEntries(minIndex, maxIndex uint64) log.LogEntries {
s.mu.RLock()
defer s.mu.RUnlock()
return s.log.GetEntries(minIndex, maxIndex)
}
func (s *state) getCommitIndex() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.commitIndex
}
func (s *state) setCommitIndex(index uint64) {
s.mu.Lock()
defer s.mu.Unlock()
s.commitIndex = index
}
func (s *state) getLogTerm(index uint64) uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.log.GetLogTerm(index)
}
func (s *state) matchEntry(prevLogIndex, prevLogTerm uint64) bool {
s.mu.Lock()
defer s.mu.Unlock()
if prevLogIndex == 0 {
return true
}
return s.log.MatchEntry(prevLogIndex, prevLogTerm)
}
func (s *state) getLastApplied() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.lastApplied
}
func (s *state) setLastApplied(index uint64) {
s.mu.Lock()
defer s.mu.Unlock()
s.lastApplied = index
}
func (s *state) getLastIndex() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.log.LastIndex()
}
func (s *state) getLastSN() int64 {
s.mu.RLock()
defer s.mu.RUnlock()
return s.log.LastSN()
}
func (s *state) initState() {
s.mu.Lock()
defer s.mu.Unlock()
s.lastApplied = 0
s.commitIndex = 0
s.currentTerm = 0
s.votedFor = -1
s.log = []log.LogEntry{{}} // first index is 1
}
func (s *state) initLeaderVolatileState(peers map[uint]string) {
s.nextIndex = make(map[uint]uint64)
s.matchIndex = make(map[uint]uint64)
for i := range peers {
s.nextIndex[i] = s.log.LastIndex() + 1
s.matchIndex[i] = 0
}
}
func (s *state) updateCommitIndex(commitIndex uint64) {
s.mu.Lock()
defer s.mu.Unlock()
for n := commitIndex + 1; n <= s.log.LastIndex(); n++ {
if s.log.GetLogTerm(n) == s.currentTerm {
// initialize to 1 because we count the leader itself
c := 1
for _, match := range s.matchIndex {
if match >= n {
c++
}
if c > len(s.matchIndex)/2 {
s.commitIndex = n
return
}
}
}
}
}
func (s *state) storeEntriesFromIndex(index uint64, entries log.LogEntries) {
s.mu.Lock()
defer s.mu.Unlock()
s.log.StoreEntriesFromIndex(index, entries)
}