-
Notifications
You must be signed in to change notification settings - Fork 1
/
meta.go
195 lines (166 loc) · 3.9 KB
/
meta.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
package raft
import (
"encoding/binary"
"github.com/dzdx/raft/raftpb"
"github.com/golang/protobuf/proto"
"github.com/dzdx/raft/store"
"time"
)
type State int
const (
KeyLastVoted = "KeyLastVoted"
KeyCurrentTerm = "KeyCurrentTerm"
)
const (
Follower State = iota
Candidate
Leader
)
const None string = ""
type configurations struct {
last *raftpb.Configuration
committed *raftpb.Configuration
}
type raftState struct {
lastVotedFor string
lastVotedTerm uint64
currentTerm uint64
lastLogIndex uint64
lastLogTerm uint64
lastApplied uint64
commitIndex uint64
configurations configurations
state State
localID string
leader string
lastContactLeader time.Time
}
func (r *RaftNode) getCurrentTerm() uint64 {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.currentTerm
}
func (r *RaftNode) setCurrentTerm(term uint64) {
if r.currentTerm == term {
return
}
data := make([]byte, 8)
binary.BigEndian.PutUint64(data, term)
r.metaStore.SetKV(KeyCurrentTerm, data)
r.currentTerm = term
}
func (r *RaftNode) getLastVoted() (votedFor string, votedTerm uint64) {
r.mutex.Lock()
defer r.mutex.Unlock()
votedFor = r.lastVotedFor
votedTerm = r.lastVotedTerm
return
}
func (r *RaftNode) setLastVoted(votedFor string) {
if r.lastVotedFor == votedFor && r.lastVotedTerm == r.currentTerm {
return
}
lastVoted := &raftpb.LastVoted{
VotedFor: votedFor,
VotedTerm: r.currentTerm,
}
data, _ := proto.Marshal(lastVoted)
r.metaStore.SetKV(KeyLastVoted, data)
r.mutex.Lock()
defer r.mutex.Unlock()
r.lastVotedFor = votedFor
r.lastVotedTerm = r.currentTerm
}
func (r *RaftNode) restoreMeta() {
var lastVotedFor string
var lastVotedTerm uint64
if data, err := r.metaStore.GetKV(KeyLastVoted); err != nil {
if _, ok := err.(*store.ErrNotFound); ok {
lastVotedFor = None
lastVotedTerm = 0
} else {
r.logger.Fatalf("read last voted failed %s", err.Error())
}
} else {
lastVoted := &raftpb.LastVoted{}
proto.Unmarshal(data, lastVoted)
lastVotedFor = lastVoted.VotedFor
lastVotedTerm = lastVoted.VotedTerm
}
r.lastVotedFor = lastVotedFor
r.lastVotedTerm = lastVotedTerm
var currentTerm uint64
if data, err := r.metaStore.GetKV(KeyCurrentTerm); err != nil {
if _, ok := err.(*store.ErrNotFound); ok {
currentTerm = 0
} else {
r.logger.Fatalf("read current term failed %s", err.Error())
}
} else {
currentTerm = binary.BigEndian.Uint64(data)
}
r.currentTerm = currentTerm
if err := r.syncLastLog(); err != nil {
r.logger.Fatal(err)
}
}
func (r *RaftNode) syncLastLog() error {
var lastIndex, lastTerm uint64
var err error
lastIndex, err = r.entryStore.LastIndex()
if err != nil {
r.logger.Error(err)
return err
}
if lastIndex != 0 {
var lastLog *raftpb.LogEntry
if lastLog, err = r.entryStore.GetEntry(lastIndex); err != nil {
if _, ok := err.(*store.ErrNotFound); !ok {
return err
}
} else {
lastTerm = lastLog.Term
}
}
if lastIndex == 0 {
// log store is empty, fetch index and term from snapshot
if snap := r.snapshoter.Last(); snap != nil {
lastIndex = snap.Index
lastTerm = snap.Term
}
}
r.setLastLog(lastTerm, lastIndex)
return nil
}
func (r *RaftNode) getState() State {
return r.state
}
func (r *RaftNode) setState(state State) {
if r.state != state {
r.state = state
}
}
func (r *RaftNode) setLastContactLeader(leaderID string) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.lastContactLeader = time.Now()
r.leader = leaderID
}
func (r *RaftNode) getLastContactLeader() (leaderID string, lastContact time.Time) {
r.mutex.Lock()
defer r.mutex.Unlock()
leaderID = r.leader
lastContact = r.lastContactLeader
return
}
func (r *RaftNode) setLastLog(term uint64, index uint64) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.lastLogTerm = term
r.lastLogIndex = index
}
func (r *RaftNode) getLastLog() (term uint64, index uint64) {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.lastLogTerm, r.lastLogIndex
}