| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- package raft
- import (
- "fmt"
- "reflect"
- "testing"
- )
- var defaultLog = []Entry{{}}
- func TestLeaderElection(t *testing.T) {
- tests := []struct {
- *network
- state stateType
- }{
- {newNetwork(nil, nil, nil), stateLeader},
- {newNetwork(nil, nil, nopStepper), stateLeader},
- {newNetwork(nil, nopStepper, nopStepper), stateCandidate},
- {newNetwork(nil, nopStepper, nopStepper, nil), stateCandidate},
- {newNetwork(nil, nopStepper, nopStepper, nil, nil), stateLeader},
- // three nodes are have logs further along than 0
- {
- newNetwork(
- nil,
- &stateMachine{log: []Entry{{}, {Term: 1}}},
- &stateMachine{log: []Entry{{}, {Term: 2}}},
- &stateMachine{log: []Entry{{}, {Term: 1}, {Term: 3}}},
- nil,
- ),
- stateFollower,
- },
- // logs converge
- {
- newNetwork(
- &stateMachine{log: []Entry{{}, {Term: 1}}},
- nil,
- &stateMachine{log: []Entry{{}, {Term: 2}}},
- &stateMachine{log: []Entry{{}, {Term: 1}}},
- nil,
- ),
- stateLeader,
- },
- }
- for i, tt := range tests {
- tt.step(Message{To: 0, Type: msgHup})
- sm := tt.network.ss[0].(*stateMachine)
- if sm.state != tt.state {
- t.Errorf("#%d: state = %s, want %s", i, sm.state, tt.state)
- }
- if g := sm.term; g != 1 {
- t.Errorf("#%d: term = %d, want %d", i, g, 1)
- }
- }
- }
- func TestDualingCandidates(t *testing.T) {
- a := &stateMachine{log: defaultLog}
- c := &stateMachine{log: defaultLog}
- tt := newNetwork(a, nil, c)
- heal := false
- next := stepperFunc(func(m Message) {
- if heal {
- tt.step(m)
- }
- })
- a.next = next
- c.next = next
- tt.tee = stepperFunc(func(m Message) {
- t.Logf("m = %+v", m)
- })
- tt.step(Message{To: 0, Type: msgHup})
- tt.step(Message{To: 2, Type: msgHup})
- t.Log("healing")
- heal = true
- tt.step(Message{To: 2, Type: msgHup})
- tests := []struct {
- sm *stateMachine
- state stateType
- term int
- }{
- {a, stateFollower, 2},
- {c, stateLeader, 2},
- }
- for i, tt := range tests {
- if g := tt.sm.state; g != tt.state {
- t.Errorf("#%d: state = %s, want %s", i, g, tt.state)
- }
- if g := tt.sm.term; g != tt.term {
- t.Errorf("#%d: term = %d, want %d", i, g, tt.term)
- }
- }
- if g := diffLogs(defaultLog, tt.logs()); g != nil {
- for _, diff := range g {
- t.Errorf("bag log:\n%s", diff)
- }
- }
- }
- func TestCandidateConcede(t *testing.T) {
- a := &stateMachine{log: defaultLog}
- tt := newNetwork(a, nil, nil)
- tt.tee = stepperFunc(func(m Message) {
- t.Logf("m = %+v", m)
- })
- a.next = nopStepper
- tt.step(Message{To: 0, Type: msgHup})
- tt.step(Message{To: 2, Type: msgHup})
- // heal the partition
- a.next = tt
- data := []byte("force follower")
- // send a proposal to 2 to flush out a msgApp to 0
- tt.step(Message{To: 2, Type: msgProp, Data: data})
- if g := a.state; g != stateFollower {
- t.Errorf("state = %s, want %s", g, stateFollower)
- }
- if g := a.term; g != 1 {
- t.Errorf("term = %d, want %d", g, 1)
- }
- wantLog := []Entry{{}, {Term: 1, Data: data}}
- if g := diffLogs(wantLog, tt.logs()); g != nil {
- for _, diff := range g {
- t.Errorf("bag log:\n%s", diff)
- }
- }
- }
- func TestOldMessages(t *testing.T) {
- tt := newNetwork(nil, nil, nil)
- // make 0 leader @ term 3
- tt.step(Message{To: 0, Type: msgHup})
- tt.step(Message{To: 0, Type: msgHup})
- tt.step(Message{To: 0, Type: msgHup})
- // pretend we're an old leader trying to make progress
- tt.step(Message{To: 0, Type: msgApp, Term: 1, Entries: []Entry{{Term: 1}}})
- if g := diffLogs(defaultLog, tt.logs()); g != nil {
- for _, diff := range g {
- t.Errorf("bag log:\n%s", diff)
- }
- }
- }
- // TestOldMessagesReply - optimization - reply with new term.
- func TestProposal(t *testing.T) {
- tests := []struct {
- *network
- success bool
- }{
- {newNetwork(nil, nil, nil), true},
- {newNetwork(nil, nil, nopStepper), true},
- {newNetwork(nil, nopStepper, nopStepper), false},
- {newNetwork(nil, nopStepper, nopStepper, nil), false},
- {newNetwork(nil, nopStepper, nopStepper, nil, nil), true},
- }
- for i, tt := range tests {
- tt.tee = stepperFunc(func(m Message) {
- t.Logf("#%d: m = %+v", i, m)
- })
- step := stepperFunc(func(m Message) {
- defer func() {
- // only recover is we expect it to panic so
- // panics we don't expect go up.
- if !tt.success {
- e := recover()
- if e != nil {
- t.Logf("#%d: err: %s", i, e)
- }
- }
- }()
- tt.step(m)
- })
- data := []byte("somedata")
- // promote 0 the leader
- step(Message{To: 0, Type: msgHup})
- step(Message{To: 0, Type: msgProp, Data: data})
- var wantLog []Entry
- if tt.success {
- wantLog = []Entry{{}, {Term: 1, Data: data}}
- } else {
- wantLog = defaultLog
- }
- if g := diffLogs(wantLog, tt.logs()); g != nil {
- for _, diff := range g {
- t.Errorf("#%d: diff:%s", i, diff)
- }
- }
- sm := tt.network.ss[0].(*stateMachine)
- if g := sm.term; g != 1 {
- t.Errorf("#%d: term = %d, want %d", i, g, 1)
- }
- }
- }
- func TestProposalByProxy(t *testing.T) {
- data := []byte("somedata")
- tests := []*network{
- newNetwork(nil, nil, nil),
- newNetwork(nil, nil, nopStepper),
- }
- for i, tt := range tests {
- tt.tee = stepperFunc(func(m Message) {
- t.Logf("#%d: m = %+v", i, m)
- })
- // promote 0 the leader
- tt.step(Message{To: 0, Type: msgHup})
- // propose via follower
- tt.step(Message{To: 1, Type: msgProp, Data: []byte("somedata")})
- wantLog := []Entry{{}, {Term: 1, Data: data}}
- if g := diffLogs(wantLog, tt.logs()); g != nil {
- for _, diff := range g {
- t.Errorf("#%d: bad entry: %s", i, diff)
- }
- }
- sm := tt.ss[0].(*stateMachine)
- if g := sm.term; g != 1 {
- t.Errorf("#%d: term = %d, want %d", i, g, 1)
- }
- }
- }
- func TestVote(t *testing.T) {
- tests := []struct {
- i, term int
- w int
- }{
- {0, 0, -1},
- {0, 1, -1},
- {0, 2, -1},
- {0, 3, 2},
- {1, 0, -1},
- {1, 1, -1},
- {1, 2, -1},
- {1, 3, 2},
- {2, 0, -1},
- {2, 1, -1},
- {2, 2, 2},
- {2, 3, 2},
- {3, 0, -1},
- {3, 1, -1},
- {3, 2, 2},
- {3, 3, 2},
- }
- for i, tt := range tests {
- called := false
- sm := &stateMachine{log: []Entry{{}, {Term: 2}, {Term: 2}}}
- sm.next = stepperFunc(func(m Message) {
- called = true
- if m.Index != tt.w {
- t.Errorf("#%d, m.Index = %d, want %d", i, m.Index, tt.w)
- }
- })
- sm.step(Message{Type: msgVote, Index: tt.i, LogTerm: tt.term})
- if !called {
- t.Fatal("#%d: not called", i)
- }
- }
- }
- func TestLogDiff(t *testing.T) {
- a := []Entry{{}, {Term: 1}, {Term: 2}}
- b := []Entry{{}, {Term: 1}, {Term: 2}}
- c := []Entry{{}, {Term: 2}}
- d := []Entry(nil)
- w := []diff{
- diff{1, []*Entry{{Term: 1}, {Term: 1}, {Term: 2}, nilLogEntry}},
- diff{2, []*Entry{{Term: 2}, {Term: 2}, noEntry, nilLogEntry}},
- }
- if g := diffLogs(a, [][]Entry{b, c, d}); !reflect.DeepEqual(w, g) {
- t.Errorf("g = %s", g)
- t.Errorf("want %s", w)
- }
- }
- type network struct {
- tee stepper
- ss []stepper
- }
- // newNetwork initializes a network from nodes. A nil node will be replaced
- // with a new *stateMachine. A *stateMachine will get its k, addr, and next
- // fields set.
- func newNetwork(nodes ...stepper) *network {
- nt := &network{ss: nodes}
- for i, n := range nodes {
- switch v := n.(type) {
- case nil:
- nt.ss[i] = newStateMachine(len(nodes), i, nt)
- case *stateMachine:
- v.k = len(nodes)
- v.addr = i
- if v.next == nil {
- v.next = nt
- }
- default:
- nt.ss[i] = v
- }
- }
- return nt
- }
- func (nt network) step(m Message) {
- if nt.tee != nil {
- nt.tee.step(m)
- }
- nt.ss[m.To].step(m)
- }
- // logs returns all logs in nt prepended with want. If a node is not a
- // *stateMachine, its log will be nil.
- func (nt network) logs() [][]Entry {
- ls := make([][]Entry, len(nt.ss))
- for i, node := range nt.ss {
- if sm, ok := node.(*stateMachine); ok {
- ls[i] = sm.log
- }
- }
- return ls
- }
- type diff struct {
- i int
- ents []*Entry // pointers so they can be nil for N/A
- }
- var noEntry = &Entry{}
- var nilLogEntry = &Entry{}
- func (d diff) String() string {
- s := fmt.Sprintf("[%d] ", d.i)
- for i, e := range d.ents {
- switch e {
- case nilLogEntry:
- s += fmt.Sprintf("o")
- case noEntry:
- s += fmt.Sprintf("-")
- case nil:
- s += fmt.Sprintf("<nil>")
- default:
- s += fmt.Sprintf("<%d:%q>", e.Term, string(e.Data))
- }
- if i != len(d.ents)-1 {
- s += "\t\t"
- }
- }
- return s
- }
- func diffLogs(base []Entry, logs [][]Entry) []diff {
- var (
- d []diff
- max int
- )
- logs = append([][]Entry{base}, logs...)
- for _, log := range logs {
- if l := len(log); l > max {
- max = l
- }
- }
- ediff := func(i int) (result []*Entry) {
- e := make([]*Entry, len(logs))
- found := false
- for j, log := range logs {
- if log == nil {
- e[j] = nilLogEntry
- continue
- }
- if len(log) <= i {
- e[j] = noEntry
- found = true
- continue
- }
- e[j] = &log[i]
- if j > 0 {
- switch prev := e[j-1]; {
- case prev == nilLogEntry:
- case prev == noEntry:
- case !reflect.DeepEqual(prev, e[j]):
- found = true
- }
- }
- }
- if found {
- return e
- }
- return nil
- }
- for i := 0; i < max; i++ {
- if e := ediff(i); e != nil {
- d = append(d, diff{i, e})
- }
- }
- return d
- }
- type stepperFunc func(Message)
- func (f stepperFunc) step(m Message) { f(m) }
- var nopStepper = stepperFunc(func(Message) {})
- type nextStepperFunc func(Message, stepper)
|