-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
171 lines (133 loc) · 3.05 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
package raft
import "sync"
func newState(store Store) (*state_, error) {
s := &state_{
store: store,
keyCurrentTerm: []byte("state.CurrentTerm"),
keyVotedFor: []byte("state.VotedFor"),
}
err := s.loadCurrentTerm()
if err != nil {
return nil, err
}
err = s.loadVotedFor()
if err != nil {
return nil, err
}
return s, nil
}
// state raft state
type state interface {
// ------------------------------------------------------
// Persistent state on all servers:
// (Updated on stable storage before responding to RPCs)
// ------------------------------------------------------
// CurrentIndex
// latest term server has seen (initialized to 0
// on first boot, increases monotonically)
GetCurrentTerm() uint64
SetCurrentTerm(uint64) error
// VotedFor
// candidateId that received vote in current term (or null if none)
GetVotedFor() RaftId
SetVotedFor(RaftId) error
// ------------------------------------------------------
// Volatile state on all servers:
// ------------------------------------------------------
// CommitIndex
// index of highest log entry known to be
// committed (initialized to 0, increases monotonically)
GetCommitIndex() uint64
SetCommitIndex(uint64)
// LastApplied
// index of highest log entry applied to state machine
// (initialized to 0, increases monotonically)
GetLastApplied() uint64
SetLastApplied(uint64)
}
var _ state = (*state_)(nil)
type state_ struct {
mu sync.Mutex
store Store
keyCurrentTerm []byte
keyVotedFor []byte
currentTerm uint64
votedFor RaftId
commitIndex uint64
lastApplied uint64
}
func (s *state_) loadCurrentTerm() error {
s.mu.Lock()
defer s.mu.Unlock()
term, err := s.store.GetUint64(s.keyCurrentTerm)
if err != nil {
return err
}
s.currentTerm = term
return nil
}
func (s *state_) loadVotedFor() error {
s.mu.Lock()
defer s.mu.Unlock()
value, err := s.store.Get(s.keyVotedFor)
if err != nil {
return err
}
if len(value) == 0 {
return nil
}
votedFor := RaftId(value)
s.votedFor = votedFor
return nil
}
func (s *state_) GetCurrentTerm() uint64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.currentTerm
}
func (s *state_) SetCurrentTerm(term uint64) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.currentTerm >= term {
return nil
}
s.currentTerm = term
return s.store.SetUint64(s.keyCurrentTerm, term)
}
func (s *state_) GetVotedFor() RaftId {
s.mu.Lock()
defer s.mu.Unlock()
return s.votedFor
}
func (s *state_) SetVotedFor(votedFor RaftId) error {
s.mu.Lock()
defer s.mu.Unlock()
s.votedFor = votedFor
return s.store.Set(s.keyVotedFor, []byte(votedFor))
}
func (s *state_) GetCommitIndex() uint64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.commitIndex
}
func (s *state_) SetCommitIndex(i uint64) {
s.mu.Lock()
defer s.mu.Unlock()
if s.commitIndex >= i {
return
}
s.commitIndex = i
}
func (s *state_) GetLastApplied() uint64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.lastApplied
}
func (s *state_) SetLastApplied(i uint64) {
s.mu.Lock()
defer s.mu.Unlock()
if s.lastApplied >= i {
return
}
s.lastApplied = i
}