-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplication.go
599 lines (546 loc) · 13.4 KB
/
replication.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright 2019 Santhosh Kumar Tekuri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package raft
import (
"bytes"
"fmt"
"io"
"net"
"time"
"github.com/santhosh-tekuri/raft/log"
)
type replication struct {
rtime randTime
status replicationStatus // owned by ldr goroutine
connPool *connPool
log *log.Log
snaps *snapshots
hbTimeout time.Duration
timer *safeTimer
bandwidth int64
ldrStartIndex uint64
ldrLastIndex uint64 // todo: directly use log.lastIndex
matchIndex uint64
nextIndex uint64
node Node
// from this time node is unreachable
// zero value means node is reachable
noContact time.Time
leaderUpdateCh chan leaderUpdate
replUpdateCh chan<- replUpdate
stopCh chan struct{}
}
func (r *replication) runLoop(req *appendReq) {
if trace {
println(r, "repl.start")
}
var c *conn
defer func() {
if c != nil && c.rwc != nil {
r.connPool.returnConn(c)
}
if v := recover(); v != nil {
r.notifyLdr(recoverErr(v))
}
}()
failures, err := uint64(0), error(nil)
for {
if failures > 0 {
if failures == 1 {
r.notifyNoContact(err)
}
r.timer.reset(backOff(failures, r.hbTimeout/2))
select {
case <-r.stopCh:
return
case <-r.timer.C:
r.timer.active = false
}
if _, err = r.checkLeaderUpdate(r.stopCh, req, false); err == errStop {
return
}
}
if c == nil {
if c, err = r.connPool.getConn(r.deadline()); err != nil {
failures++
continue
}
if failures > 0 {
failures = 0
r.notifyNoContact(nil)
if _, err = r.checkLeaderUpdate(r.stopCh, req, false); err == errStop {
return
}
}
}
assert(r.matchIndex < r.nextIndex)
err = r.replicate(c, req)
if err == errStop {
return
} else if _, ok := err.(OpError); ok {
panic(err)
} else if remoteErr, ok := err.(remoteError); ok {
err = remoteErr.error
}
failures++
if c.rwc != nil {
_ = c.rwc.Close()
}
c = nil
}
}
// always returns non-nil error
func (r *replication) replicate(c *conn, req *appendReq) error {
resp := &appendResp{}
for {
// find matchIndex ---------------------------------------------------
for {
err := r.writeAppendEntriesReq(c, req, false)
if err == log.ErrNotFound {
if err = r.sendInstallSnapReq(c, req); err == nil {
continue
}
}
if err != nil {
return err
}
if err = c.readResp(resp, r.deadline()); err != nil {
return err
}
if err = r.onAppendEntriesResp(resp, r.nextIndex-1); err != nil {
return err
}
if _, err = r.checkLeaderUpdate(r.stopCh, req, false); err != nil {
return err
}
if r.matchIndex+1 == r.nextIndex {
break
}
}
if r.nextIndex < r.ldrLastIndex && !r.log.Contains(r.nextIndex) {
if err := r.sendInstallSnapReq(c, req); err == nil {
continue
}
}
// todo: before starting pipeline, check if sending snap
// is better than sending lots of entries
if trace {
println(r, "pipelining.............................")
}
// pipelining ---------------------------------------------------------
type result struct {
lastIndex uint64
err error
}
var (
resultCh = make(chan result, 128)
stopCh = make(chan struct{})
)
go func() {
defer func() {
close(resultCh)
if v := recover(); v != nil {
select {
case <-stopCh:
return
case resultCh <- result{0, recoverErr(v)}:
}
}
}()
for {
err := r.writeAppendEntriesReq(c, req, true)
select {
case <-stopCh:
return
case resultCh <- result{r.nextIndex - 1, err}:
}
if err != nil {
return
}
for {
ldrUpdate, errStop := r.checkLeaderUpdate(stopCh, req, true)
if errStop != nil {
return
}
if ldrUpdate || len(resultCh) == 0 || r.nextIndex <= r.ldrLastIndex {
break
}
// we have not received responses for all requests yet
// so no need to flood him with heartbeat
// take a nap and check again
if trace {
println(r, "no heartbeat, pending resps:", len(resultCh))
}
}
}
}()
drainResps := func() error {
for range resultCh {
if err := c.readResp(resp, r.deadline()); err != nil {
return err
}
}
return nil
}
drainRespsTimeout := func(timeout time.Duration) {
drained := make(chan error, 1)
go func() {
drained <- drainResps()
}()
select {
case err := <-drained:
if trace {
println(r, "drain completed with error:", err)
}
if err != nil {
_ = c.rwc.Close()
c.rwc = nil // to signal runLoop that we closed the conn
}
case <-time.After(timeout):
if trace {
println(r, "drain timeout, closing conn")
}
_ = c.rwc.Close()
<-drained
if trace {
println(r, "drain completed")
}
c.rwc = nil // to signal runLoop that we closed the conn
}
}
var err error
for {
var result result
select {
case <-r.stopCh:
if trace {
println(r, "ending pipeline, got stop signal from ldr")
}
close(stopCh)
drainRespsTimeout(r.hbTimeout / 2)
return errStop
case result = <-resultCh:
}
if result.err != nil {
if trace {
println(r, "pipeline ended with", result.err)
}
if result.err == log.ErrNotFound {
break
}
return result.err
}
err = c.readResp(resp, r.deadline())
if err != nil {
if trace {
println(r, "ending pipeline, error in reading resp:", err)
}
close(stopCh)
_ = c.rwc.Close()
for range resultCh {
}
c.rwc = nil
return err
}
if resp.result == success {
_ = r.onAppendEntriesResp(resp, result.lastIndex)
} else {
if trace {
println(r, "ending pipeline, got resp.result", resp.result)
}
close(stopCh)
if resp.result == staleTerm {
drainRespsTimeout(r.hbTimeout / 2)
return r.onAppendEntriesResp(resp, result.lastIndex) // notifies ldr and return errStop
}
if err = drainResps(); err != nil {
return err
}
break
}
}
}
}
const maxAppendEntries = 64
// note: never access f.matchIndex in this method, because this is used by pipeline writer also
func (r *replication) writeAppendEntriesReq(c *conn, req *appendReq, sendEntries bool) error {
snapIndex, snapTerm := r.snaps.latest()
// fill req.prevLogXXX
req.prevLogIndex = r.nextIndex - 1
if req.prevLogIndex == 0 {
req.prevLogTerm = 0
} else if req.prevLogIndex == snapIndex {
req.prevLogTerm = snapTerm
} else {
term, err := r.getEntryTerm(req.prevLogIndex)
if err != nil { // should be in snapshot
return err
}
req.prevLogTerm = term
}
req.numEntries = 0
if sendEntries {
req.numEntries = min(r.ldrLastIndex-req.prevLogIndex, maxAppendEntries)
if req.numEntries > 0 && !r.log.Contains(r.nextIndex) {
return log.ErrNotFound
}
}
if trace {
if sendEntries && req.numEntries == 0 {
println(r, ">> heartbeat")
} else {
println(r, ">>", req)
}
}
if err := c.writeReq(req, r.deadline()); err != nil {
return err
}
if req.numEntries > 0 {
if err := r.writeEntriesTo(c, r.nextIndex, req.numEntries); err != nil {
return err
}
r.nextIndex += req.numEntries
if trace {
println(r, "nextIndex:", r.nextIndex)
}
}
return nil
}
func (r *replication) onAppendEntriesResp(resp *appendResp, reqLastIndex uint64) error {
if trace {
println(r, "<<", resp)
}
switch resp.result {
case staleTerm:
r.notifyLdr(newTerm{resp.getTerm()})
return errStop
case success:
if reqLastIndex > r.matchIndex {
r.matchIndex = reqLastIndex
if trace {
println(r, "matchIndex:", r.matchIndex)
}
r.notifyLdr(matchIndex{r.matchIndex})
}
return nil
case prevEntryNotFound, prevTermMismatch:
if resp.lastLogIndex < r.matchIndex {
// this happens if someone restarted follower storage with empty storage
return ErrFaultyFollower
}
r.nextIndex = min(r.nextIndex-1, resp.lastLogIndex+1)
if trace {
println(r, "nextIndex:", r.nextIndex)
}
return nil
case unexpectedErr:
return remoteError{resp.err}
default:
panic(fmt.Errorf("[BUG] appendEntries.result==%v", resp.result))
}
}
func (r *replication) sendInstallSnapReq(c *conn, appReq *appendReq) error {
snap, err := r.snaps.open()
if err != nil {
return opError(err, "snapshots.open")
}
defer snap.release()
req := &installSnapReq{
req: appReq.req,
lastIndex: snap.meta.index,
lastTerm: snap.meta.term,
lastConfig: snap.meta.config,
size: snap.meta.size,
}
if trace {
println(r, ">>", req)
}
if err = c.writeReq(req, r.deadline()); err != nil {
return err
}
if err := c.rwc.SetWriteDeadline(r.deadlineSize(req.size)); err != nil {
return err
}
if _, err = io.Copy(c.rwc, snap.file); err != nil { // will use sendFile
return err
}
resp := &installSnapResp{}
if err = c.readResp(resp, time.Now().Add(4*r.hbTimeout)); err != nil { // todo: is 2*hbTimeout enough for saving snap
return err
}
switch resp.result {
case staleTerm:
r.notifyLdr(newTerm{resp.getTerm()})
return errStop
case success:
// case: snapshot was taken before we got leaderUpdate about lastLogIndex
// we should wait until we get our logview gets updated
for req.lastIndex > r.ldrLastIndex {
if _, err := r.checkLeaderUpdate(r.stopCh, appReq, false); err != nil {
return err
}
}
r.matchIndex = req.lastIndex
r.nextIndex = r.matchIndex + 1
if trace {
println(r, "matchIndex:", r.matchIndex, "nextIndex:", r.nextIndex)
}
r.notifyLdr(matchIndex{r.matchIndex})
return nil
case unexpectedErr:
return remoteError{resp.err}
default:
panic(fmt.Errorf("[BUG] installSnapResp.result==%v", resp.result))
}
}
func (r *replication) checkLeaderUpdate(stopCh <-chan struct{}, req *appendReq, sendEntries bool) (ldrUpdate bool, err error) {
if sendEntries && r.nextIndex > r.ldrLastIndex {
// for nonvoter, dont send heartbeats
var timerCh <-chan time.Time
if r.node.Voter {
r.timer.reset(r.hbTimeout / 2)
timerCh = r.timer.C
}
// nothing to send. start heartbeat timer
select {
case <-stopCh:
return false, errStop
case update := <-r.leaderUpdateCh:
r.onLeaderUpdate(update, req)
return true, nil
case <-timerCh:
r.timer.active = false
return false, nil
}
} else {
// check signal if any, without blocking
select {
case <-stopCh:
return false, errStop
case update := <-r.leaderUpdateCh:
r.onLeaderUpdate(update, req)
return true, nil
default:
}
}
return false, nil
}
func (r *replication) onLeaderUpdate(u leaderUpdate, req *appendReq) {
if trace {
println(r, u)
}
if u.log.PrevIndex() > r.log.PrevIndex() {
r.notifyLdr(removeLTE{u.log.PrevIndex()})
}
r.log = u.log
r.ldrLastIndex, req.ldrCommitIndex = u.log.LastIndex(), u.commitIndex
if u.config != nil {
r.node = u.config.Nodes[r.status.id]
}
}
func (r *replication) notifyLdr(u interface{}) {
select {
case <-r.stopCh:
case r.replUpdateCh <- replUpdate{&r.status, u}:
}
}
func (r *replication) notifyNoContact(err error) {
if err != nil {
r.noContact = time.Now()
if trace {
println(r, "noContact", err)
}
r.notifyLdr(noContact{r.noContact, err})
} else {
r.noContact = time.Time{} // zeroing
if trace {
println(r, "yesContact")
}
r.notifyLdr(noContact{r.noContact, nil})
}
}
func (r *replication) getEntryTerm(i uint64) (uint64, error) {
b, err := r.log.Get(i)
if err == log.ErrNotFound {
return 0, err
} else if err != nil {
panic(opError(err, "Log.Get(%d)", i))
}
e := &entry{}
if err = e.decode(bytes.NewReader(b)); err != nil {
panic(opError(err, "log.Get(%d).decode()", i))
}
return e.term, nil
}
func (r *replication) writeEntriesTo(c *conn, from uint64, n uint64) error {
buffs, err := r.log.GetN(from, n)
if err != nil {
panic(opError(err, "Log.GetN(%d, %d)", from, n))
}
nbuffs := net.Buffers(buffs)
if err := c.rwc.SetWriteDeadline(r.deadlineSize(size(nbuffs))); err != nil {
return err
}
_, err = nbuffs.WriteTo(c.rwc)
return err
}
func (r *replication) deadline() time.Time {
return time.Now().Add(2 * r.hbTimeout)
}
func (r *replication) deadlineSize(size int64) time.Time {
timeout := durationFor(r.bandwidth, size)
if timeout < 2*r.hbTimeout {
timeout = 2 * r.hbTimeout
}
return time.Now().Add(timeout)
}
// ------------------------------------------------
type leaderUpdate struct {
log *log.Log
commitIndex uint64
config *Config // nil if config not changed
}
type replUpdate struct {
status *replicationStatus
update interface{}
}
type matchIndex struct {
val uint64
}
type noContact struct {
time time.Time
err error
}
type removeLTE struct {
val uint64
}
type newTerm struct {
val uint64
}
type replicationStatus struct {
id uint64
// true when the replication is removed
// used to ignore replUpdate from removed replication
removed bool
// owned exclusively by leader goroutine
// used to compute majorityMatchIndex
matchIndex uint64
// from what time the replication unable to reach this node
// zero value means it is reachable
noContact time.Time
err error
node Node
round *round // nil if no promotion required
removeLTE uint64
}