-
Notifications
You must be signed in to change notification settings - Fork 0
/
member.go
358 lines (305 loc) · 7.64 KB
/
member.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package raft
import (
"context"
"fmt"
log "github.com/treeforest/logger"
"github.com/treeforest/raft/pb"
"google.golang.org/grpc"
"sync"
"sync/atomic"
"time"
)
// member 集群中的成员
type member struct {
pb.Member
sync.RWMutex
server *server
dialTimeout time.Duration
dialOptions []grpc.DialOption
cc *grpc.ClientConn
lastActivity time.Time
prevLogIndex uint64
stop chan bool
state int32 // 0:running, 1:stopping, 2:state
}
func newMember(m pb.Member, server *server) *member {
return &member{
server: server,
dialTimeout: server.config.DialTimeout,
dialOptions: server.config.DialOptions,
Member: m,
cc: nil,
lastActivity: time.Time{},
prevLogIndex: 0,
state: 0,
}
}
func (m *member) setPrevLogIndex(index uint64) {
m.Lock()
defer m.Unlock()
m.prevLogIndex = index
}
func (m *member) getPrevLogIndex() uint64 {
m.RLock()
defer m.RUnlock()
return m.prevLogIndex
}
func (m *member) setLastActivity(now time.Time) {
m.Lock()
defer m.Unlock()
m.lastActivity = now
}
func (m *member) LastActivity() time.Time {
m.RLock()
defer m.RUnlock()
return m.lastActivity
}
func (m *member) sendAppendEntriesRequest(req *pb.AppendEntriesRequest) error {
client, err := m.client()
if err != nil {
return err
}
resp, err := client.AppendEntries(context.Background(), req)
if err != nil {
return err
}
m.setLastActivity(time.Now())
m.Lock()
if resp.Success {
if len(req.Entries) > 0 {
m.prevLogIndex = req.Entries[len(req.Entries)-1].Index
// log.Debug("change prevLogIndex = ", m.prevLogIndex)
} else {
// heartbeat
m.Unlock()
return nil
}
} else {
if resp.Term > m.server.CurrentTerm() {
// 出现了任期比自己新的leader
} else if resp.Term == req.Term && resp.CommitIndex >= m.prevLogIndex {
// 可能由于节点的丢包,导致leader没有即使更新prevLogIndex值,
// 直接采用更新prevLogIndex的操作
m.prevLogIndex = resp.CommitIndex
} else if m.prevLogIndex > 0 {
// 没有找到匹配的日志索引,对prevLogIndex采用自减操作,直到匹配
m.prevLogIndex--
if m.prevLogIndex > resp.Index {
// 采用直接设置为resp.Index
m.prevLogIndex = resp.Index
}
}
}
m.Unlock()
// 通知server处理AppendEntriesResponse
m.server.sendAsync(&AppendEntriesResponse{AppendEntriesResponse: resp, Id: m.Id})
return nil
}
// sendVoteRequest 发送投票请求
func (m *member) sendVoteRequest(req *pb.RequestVoteRequest, c chan *pb.RequestVoteResponse) error {
client, err := m.client()
if err != nil {
return err
}
resp, err := client.RequestVote(context.Background(), req)
if err != nil {
return fmt.Errorf("requestVote grpc error: %v", err)
}
m.setLastActivity(time.Now())
c <- resp
return nil
}
func (m *member) sendSnapshotAskRequest() error {
client, err := m.client()
if err != nil {
return err
}
snapshot := m.server.snapshot
resp, err := client.SnapshotAsk(context.Background(), &pb.SnapshotAskRequest{
LeaderId: m.server.ID(),
LastIndex: snapshot.LastIndex,
LastTerm: snapshot.LastTerm,
})
if err != nil {
log.Warnf("send SnapshotRequest failed: %v", err)
return err
}
m.setLastActivity(time.Now())
if resp.Success {
_ = m.sendSnapshotRecoveryRequest(&snapshot.Snapshot)
}
return nil
}
func (m *member) sendSnapshotRecoveryRequest(snapshot *pb.Snapshot) error {
client, err := m.client()
if err != nil {
return err
}
req := &pb.SnapshotRecoveryRequest{
LeaderId: m.server.CurrentTerm(),
LastIndex: snapshot.LastIndex,
LastTerm: snapshot.LastTerm,
Members: m.server.Members(),
State: snapshot.State,
}
resp, err := client.SnapshotRecovery(context.Background(), req)
if err != nil {
log.Warnf("send SnapshotRecovery failed: %v", err)
return err
}
m.setLastActivity(time.Now())
if resp.Success {
m.prevLogIndex = req.LastIndex
} else {
log.Debug("send snapshot recovery failed: ", m.Id)
}
return nil
}
func (m *member) sendAddMemberRequest(req *pb.AddMemberRequest) error {
client, err := m.client()
if err != nil {
return err
}
err = timeoutFunc(m.dialTimeout, func() error {
_, err = client.AddMember(context.Background(), req)
return err
})
return err
}
func (m *member) sendRemoveMemberRequest(req *pb.RemoveMemberRequest) error {
client, err := m.client()
if err != nil {
return err
}
err = timeoutFunc(m.dialTimeout, func() error {
_, err = client.RemoveMember(context.Background(), req)
return err
})
return err
}
func (m *member) sendMembershipRequest(req *pb.MembershipRequest) (*pb.MembershipResponse, error) {
client, err := m.client()
if err != nil {
return nil, err
}
var resp *pb.MembershipResponse
err = timeoutFunc(m.dialTimeout, func() error {
resp, err = client.Membership(context.Background(), req)
return err
})
return resp, err
}
func (m *member) client() (pb.RaftClient, error) {
if m.getCC() == nil {
if err := m.dial(); err != nil {
return nil, fmt.Errorf("dial failed: %v", err)
}
}
return pb.NewRaftClient(m.getCC()), nil
}
func (m *member) startHeartbeat() {
m.stop = make(chan bool, 1)
go m.heartbeat()
}
func (m *member) stopHeartbeat(flush bool) {
m.stop <- flush
}
func (m *member) heartbeat() {
stop := m.stop
ticker := time.NewTicker(m.server.config.HeartbeatInterval)
log.Debugf("heartbeat, id:%d heartbeatInterval:%dms", m.Id, m.server.config.HeartbeatInterval.Milliseconds())
// 确保定时器触发的间隔不会导致新任期的出现
_ = m.flush()
for {
select {
case flush := <-stop:
if flush {
log.Debug("stop heartbeat with flush")
_ = m.flush()
} else {
log.Debug("stop heartbeat")
}
log.Infof("stop heartbeat with %d", m.Id)
atomic.SwapInt32(&m.state, 2)
return
case <-ticker.C:
if atomic.LoadInt32(&m.state) != 0 {
break
}
if err := m.flush(); err != nil {
log.Warnf("flush failed, (id:%d, err:%v)", m.Id, err)
if err == StopError {
m.server.locker.Lock()
m.server.removeMember(m.Id)
m.server.locker.Unlock()
} else {
// 暂停心跳
ticker.Stop()
// 心跳失败, 尝试重连3次
var i = 0
for i < 3 {
if err = m.dial(); err == nil {
break
}
i++
}
if i == 3 {
// 移除节点,不需要return,在RemoveMember方法里会向stop通道传值
m.setCC(nil)
m.server.removeMember(m.Id)
atomic.SwapInt32(&m.state, 1)
break
}
// 恢复心跳
ticker.Reset(m.server.config.HeartbeatInterval)
}
}
}
}
}
func (m *member) flush() (err error) {
// log.Debug("flush: ", m.Id)
prevLogIndex := m.getPrevLogIndex()
term := m.server.CurrentTerm()
entries, prevLogTerm := m.server.log.getEntriesAfter(prevLogIndex, m.server.config.MaxLogEntriesPerRequest)
// log.Debugf("flush, id:%d prevLogIndex:%d prevLogTerm:%d entries:%v", m.Id, prevLogIndex, prevLogTerm, entries)
if entries != nil {
err = m.sendAppendEntriesRequest(&pb.AppendEntriesRequest{
Term: term,
PrevLogIndex: prevLogIndex,
PrevLogTerm: prevLogTerm,
CommitIndex: m.server.log.CommitIndex(),
LeaderId: m.server.ID(),
Entries: entries,
})
} else {
err = m.sendSnapshotAskRequest()
}
return
}
func (m *member) getCC() *grpc.ClientConn {
m.RLock()
defer m.RUnlock()
return m.cc
}
func (m *member) setCC(cc *grpc.ClientConn) {
m.Lock()
defer m.Unlock()
m.cc = cc
}
func (m *member) dial() error {
cc := m.getCC()
if cc != nil {
if ping(cc, m.dialTimeout) == true {
return nil
}
// ping failed, reconnect
}
cc, err := dial(m.Address, m.dialTimeout, m.dialOptions)
if err != nil {
return err
}
// log.Infof("dial %s success", m.Address)
m.setCC(cc)
return nil
}