123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- // Copyright 2015 The etcd Authors
- //
- // 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 (
- "reflect"
- "testing"
- pb "go.etcd.io/etcd/raft/raftpb"
- )
- func TestUnstableMaybeFirstIndex(t *testing.T) {
- tests := []struct {
- entries []pb.Entry
- offset uint64
- snap *pb.Snapshot
- wok bool
- windex uint64
- }{
- // no snapshot
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- false, 0,
- },
- {
- []pb.Entry{}, 0, nil,
- false, 0,
- },
- // has snapshot
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- true, 5,
- },
- {
- []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- true, 5,
- },
- }
- for i, tt := range tests {
- u := unstable{
- entries: tt.entries,
- offset: tt.offset,
- snapshot: tt.snap,
- logger: raftLogger,
- }
- index, ok := u.maybeFirstIndex()
- if ok != tt.wok {
- t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
- }
- if index != tt.windex {
- t.Errorf("#%d: index = %d, want %d", i, index, tt.windex)
- }
- }
- }
- func TestMaybeLastIndex(t *testing.T) {
- tests := []struct {
- entries []pb.Entry
- offset uint64
- snap *pb.Snapshot
- wok bool
- windex uint64
- }{
- // last in entries
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- true, 5,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- true, 5,
- },
- // last in snapshot
- {
- []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- true, 4,
- },
- // empty unstable
- {
- []pb.Entry{}, 0, nil,
- false, 0,
- },
- }
- for i, tt := range tests {
- u := unstable{
- entries: tt.entries,
- offset: tt.offset,
- snapshot: tt.snap,
- logger: raftLogger,
- }
- index, ok := u.maybeLastIndex()
- if ok != tt.wok {
- t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
- }
- if index != tt.windex {
- t.Errorf("#%d: index = %d, want %d", i, index, tt.windex)
- }
- }
- }
- func TestUnstableMaybeTerm(t *testing.T) {
- tests := []struct {
- entries []pb.Entry
- offset uint64
- snap *pb.Snapshot
- index uint64
- wok bool
- wterm uint64
- }{
- // term from entries
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- 5,
- true, 1,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- 6,
- false, 0,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- 4,
- false, 0,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 5,
- true, 1,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 6,
- false, 0,
- },
- // term from snapshot
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 4,
- true, 1,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 3,
- false, 0,
- },
- {
- []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 5,
- false, 0,
- },
- {
- []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 4,
- true, 1,
- },
- {
- []pb.Entry{}, 0, nil,
- 5,
- false, 0,
- },
- }
- for i, tt := range tests {
- u := unstable{
- entries: tt.entries,
- offset: tt.offset,
- snapshot: tt.snap,
- logger: raftLogger,
- }
- term, ok := u.maybeTerm(tt.index)
- if ok != tt.wok {
- t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
- }
- if term != tt.wterm {
- t.Errorf("#%d: term = %d, want %d", i, term, tt.wterm)
- }
- }
- }
- func TestUnstableRestore(t *testing.T) {
- u := unstable{
- entries: []pb.Entry{{Index: 5, Term: 1}},
- offset: 5,
- snapshot: &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- logger: raftLogger,
- }
- s := pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 6, Term: 2}}
- u.restore(s)
- if u.offset != s.Metadata.Index+1 {
- t.Errorf("offset = %d, want %d", u.offset, s.Metadata.Index+1)
- }
- if len(u.entries) != 0 {
- t.Errorf("len = %d, want 0", len(u.entries))
- }
- if !reflect.DeepEqual(u.snapshot, &s) {
- t.Errorf("snap = %v, want %v", u.snapshot, &s)
- }
- }
- func TestUnstableStableTo(t *testing.T) {
- tests := []struct {
- entries []pb.Entry
- offset uint64
- snap *pb.Snapshot
- index, term uint64
- woffset uint64
- wlen int
- }{
- {
- []pb.Entry{}, 0, nil,
- 5, 1,
- 0, 0,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- 5, 1, // stable to the first entry
- 6, 0,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}}, 5, nil,
- 5, 1, // stable to the first entry
- 6, 1,
- },
- {
- []pb.Entry{{Index: 6, Term: 2}}, 6, nil,
- 6, 1, // stable to the first entry and term mismatch
- 6, 1,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- 4, 1, // stable to old entry
- 5, 1,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- 4, 2, // stable to old entry
- 5, 1,
- },
- // with snapshot
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 5, 1, // stable to the first entry
- 6, 0,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 5, 1, // stable to the first entry
- 6, 1,
- },
- {
- []pb.Entry{{Index: 6, Term: 2}}, 6, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 5, Term: 1}},
- 6, 1, // stable to the first entry and term mismatch
- 6, 1,
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
- 4, 1, // stable to snapshot
- 5, 1,
- },
- {
- []pb.Entry{{Index: 5, Term: 2}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 2}},
- 4, 1, // stable to old entry
- 5, 1,
- },
- }
- for i, tt := range tests {
- u := unstable{
- entries: tt.entries,
- offset: tt.offset,
- snapshot: tt.snap,
- logger: raftLogger,
- }
- u.stableTo(tt.index, tt.term)
- if u.offset != tt.woffset {
- t.Errorf("#%d: offset = %d, want %d", i, u.offset, tt.woffset)
- }
- if len(u.entries) != tt.wlen {
- t.Errorf("#%d: len = %d, want %d", i, len(u.entries), tt.wlen)
- }
- }
- }
- func TestUnstableTruncateAndAppend(t *testing.T) {
- tests := []struct {
- entries []pb.Entry
- offset uint64
- snap *pb.Snapshot
- toappend []pb.Entry
- woffset uint64
- wentries []pb.Entry
- }{
- // append to the end
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- []pb.Entry{{Index: 6, Term: 1}, {Index: 7, Term: 1}},
- 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}},
- },
- // replace the unstable entries
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- []pb.Entry{{Index: 5, Term: 2}, {Index: 6, Term: 2}},
- 5, []pb.Entry{{Index: 5, Term: 2}, {Index: 6, Term: 2}},
- },
- {
- []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
- []pb.Entry{{Index: 4, Term: 2}, {Index: 5, Term: 2}, {Index: 6, Term: 2}},
- 4, []pb.Entry{{Index: 4, Term: 2}, {Index: 5, Term: 2}, {Index: 6, Term: 2}},
- },
- // truncate the existing entries and append
- {
- []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}}, 5, nil,
- []pb.Entry{{Index: 6, Term: 2}},
- 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 2}},
- },
- {
- []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}}, 5, nil,
- []pb.Entry{{Index: 7, Term: 2}, {Index: 8, Term: 2}},
- 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 2}, {Index: 8, Term: 2}},
- },
- }
- for i, tt := range tests {
- u := unstable{
- entries: tt.entries,
- offset: tt.offset,
- snapshot: tt.snap,
- logger: raftLogger,
- }
- u.truncateAndAppend(tt.toappend)
- if u.offset != tt.woffset {
- t.Errorf("#%d: offset = %d, want %d", i, u.offset, tt.woffset)
- }
- if !reflect.DeepEqual(u.entries, tt.wentries) {
- t.Errorf("#%d: entries = %v, want %v", i, u.entries, tt.wentries)
- }
- }
- }
|