|
|
@@ -2,7 +2,6 @@ package raft
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
- "fmt"
|
|
|
"math/rand"
|
|
|
"reflect"
|
|
|
"sort"
|
|
|
@@ -781,45 +780,24 @@ func TestRestore(t *testing.T) {
|
|
|
Nodes: []int64{0, 1, 2},
|
|
|
}
|
|
|
|
|
|
- tests := []struct {
|
|
|
- snapshoter Snapshoter
|
|
|
- wallow bool
|
|
|
- }{
|
|
|
- {nil, false},
|
|
|
- {new(logSnapshoter), true},
|
|
|
- }
|
|
|
-
|
|
|
- for i, tt := range tests {
|
|
|
- func() {
|
|
|
- defer func() {
|
|
|
- if r := recover(); r != nil {
|
|
|
- if tt.wallow == true {
|
|
|
- t.Errorf("%d: allow = %v, want %v", i, false, true)
|
|
|
- }
|
|
|
- }
|
|
|
- }()
|
|
|
-
|
|
|
- sm := newStateMachine(0, []int64{0, 1})
|
|
|
- sm.setSnapshoter(tt.snapshoter)
|
|
|
- sm.restore(s)
|
|
|
+ sm := newStateMachine(0, []int64{0, 1})
|
|
|
+ sm.restore(s)
|
|
|
|
|
|
- if sm.raftLog.lastIndex() != s.Index {
|
|
|
- t.Errorf("#%d: log.lastIndex = %d, want %d", i, sm.raftLog.lastIndex(), s.Index)
|
|
|
- }
|
|
|
- if sm.raftLog.term(s.Index) != s.Term {
|
|
|
- t.Errorf("#%d: log.lastTerm = %d, want %d", i, sm.raftLog.term(s.Index), s.Term)
|
|
|
- }
|
|
|
- sg := int64Slice(sm.nodes())
|
|
|
- sw := int64Slice(s.Nodes)
|
|
|
- sort.Sort(sg)
|
|
|
- sort.Sort(sw)
|
|
|
- if !reflect.DeepEqual(sg, sw) {
|
|
|
- t.Errorf("#%d: sm.Nodes = %+v, want %+v", i, sg, sw)
|
|
|
- }
|
|
|
- if !reflect.DeepEqual(sm.snapshoter.GetSnap(), s) {
|
|
|
- t.Errorf("%d: snapshoter.getSnap = %+v, want %+v", sm.snapshoter.GetSnap(), s)
|
|
|
- }
|
|
|
- }()
|
|
|
+ if sm.raftLog.lastIndex() != s.Index {
|
|
|
+ t.Errorf("log.lastIndex = %d, want %d", sm.raftLog.lastIndex(), s.Index)
|
|
|
+ }
|
|
|
+ if sm.raftLog.term(s.Index) != s.Term {
|
|
|
+ t.Errorf("log.lastTerm = %d, want %d", sm.raftLog.term(s.Index), s.Term)
|
|
|
+ }
|
|
|
+ sg := int64Slice(sm.nodes())
|
|
|
+ sw := int64Slice(s.Nodes)
|
|
|
+ sort.Sort(sg)
|
|
|
+ sort.Sort(sw)
|
|
|
+ if !reflect.DeepEqual(sg, sw) {
|
|
|
+ t.Errorf("sm.Nodes = %+v, want %+v", sg, sw)
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(sm.raftLog.snapshot, s) {
|
|
|
+ t.Errorf("snapshot = %+v, want %+v", sm.raftLog.snapshot, s)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -830,7 +808,6 @@ func TestProvideSnap(t *testing.T) {
|
|
|
Nodes: []int64{0, 1},
|
|
|
}
|
|
|
sm := newStateMachine(0, []int64{0})
|
|
|
- sm.setSnapshoter(new(logSnapshoter))
|
|
|
// restore the statemachin from a snapshot
|
|
|
// so it has a compacted log and a snapshot
|
|
|
sm.restore(s)
|
|
|
@@ -872,11 +849,10 @@ func TestRestoreFromSnapMsg(t *testing.T) {
|
|
|
m := Message{Type: msgSnap, From: 0, Term: 1, Snapshot: s}
|
|
|
|
|
|
sm := newStateMachine(1, []int64{0, 1})
|
|
|
- sm.setSnapshoter(new(logSnapshoter))
|
|
|
sm.Step(m)
|
|
|
|
|
|
- if !reflect.DeepEqual(sm.snapshoter.GetSnap(), s) {
|
|
|
- t.Errorf("snapshot = %+v, want %+v", sm.snapshoter.GetSnap(), s)
|
|
|
+ if !reflect.DeepEqual(sm.raftLog.snapshot, s) {
|
|
|
+ t.Errorf("snapshot = %+v, want %+v", sm.raftLog.snapshot, s)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -890,16 +866,14 @@ func TestSlowNodeRestore(t *testing.T) {
|
|
|
}
|
|
|
lead := nt.peers[0].(*stateMachine)
|
|
|
lead.nextEnts()
|
|
|
- if !lead.maybeCompact() {
|
|
|
- t.Errorf("compacted = false, want true")
|
|
|
- }
|
|
|
+ lead.compact(nil)
|
|
|
|
|
|
nt.recover()
|
|
|
nt.send(Message{From: 0, To: 0, Type: msgBeat})
|
|
|
|
|
|
follower := nt.peers[2].(*stateMachine)
|
|
|
- if !reflect.DeepEqual(follower.snapshoter.GetSnap(), lead.snapshoter.GetSnap()) {
|
|
|
- t.Errorf("follower.snap = %+v, want %+v", follower.snapshoter.GetSnap(), lead.snapshoter.GetSnap())
|
|
|
+ if !reflect.DeepEqual(follower.raftLog.snapshot, lead.raftLog.snapshot) {
|
|
|
+ t.Errorf("follower.snap = %+v, want %+v", follower.raftLog.snapshot, lead.raftLog.snapshot)
|
|
|
}
|
|
|
|
|
|
committed := follower.raftLog.lastIndex()
|
|
|
@@ -979,7 +953,6 @@ func newNetwork(peers ...Interface) *network {
|
|
|
switch v := p.(type) {
|
|
|
case nil:
|
|
|
sm := newStateMachine(nid, defaultPeerAddrs)
|
|
|
- sm.setSnapshoter(new(logSnapshoter))
|
|
|
npeers[nid] = sm
|
|
|
case *stateMachine:
|
|
|
v.id = nid
|
|
|
@@ -1070,22 +1043,3 @@ func (blackHole) Step(Message) bool { return true }
|
|
|
func (blackHole) Msgs() []Message { return nil }
|
|
|
|
|
|
var nopStepper = &blackHole{}
|
|
|
-
|
|
|
-type logSnapshoter struct {
|
|
|
- snapshot Snapshot
|
|
|
-}
|
|
|
-
|
|
|
-func (s *logSnapshoter) Snap(index, term int64, nodes []int64) {
|
|
|
- s.snapshot = Snapshot{
|
|
|
- Index: index,
|
|
|
- Term: term,
|
|
|
- Nodes: nodes,
|
|
|
- Data: []byte(fmt.Sprintf("%d:%d", term, index)),
|
|
|
- }
|
|
|
-}
|
|
|
-func (s *logSnapshoter) Restore(ss Snapshot) {
|
|
|
- s.snapshot = ss
|
|
|
-}
|
|
|
-func (s *logSnapshoter) GetSnap() Snapshot {
|
|
|
- return s.snapshot
|
|
|
-}
|