2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2014-06-28 03:15:04 +04:00
|
|
|
package raft
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-08-28 05:53:18 +04:00
|
|
|
|
|
|
|
pb "github.com/coreos/etcd/raft/raftpb"
|
2014-06-28 03:15:04 +04:00
|
|
|
)
|
|
|
|
|
2014-10-26 04:49:49 +03:00
|
|
|
func TestFindConflict(t *testing.T) {
|
2014-11-25 04:13:47 +03:00
|
|
|
previousEnts := []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}
|
2014-10-26 04:49:49 +03:00
|
|
|
tests := []struct {
|
|
|
|
ents []pb.Entry
|
|
|
|
wconflict uint64
|
|
|
|
}{
|
|
|
|
// no conflict, empty ent
|
2014-12-02 19:50:15 +03:00
|
|
|
{[]pb.Entry{}, 0},
|
|
|
|
{[]pb.Entry{}, 0},
|
2014-10-26 04:49:49 +03:00
|
|
|
// no conflict
|
2014-12-02 19:50:15 +03:00
|
|
|
{[]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}, 0},
|
|
|
|
{[]pb.Entry{{Index: 2, Term: 2}, {Index: 3, Term: 3}}, 0},
|
|
|
|
{[]pb.Entry{{Index: 3, Term: 3}}, 0},
|
2014-10-26 04:49:49 +03:00
|
|
|
// no conflict, but has new entries
|
2014-12-02 19:50:15 +03:00
|
|
|
{[]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 4}}, 4},
|
|
|
|
{[]pb.Entry{{Index: 2, Term: 2}, {Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 4}}, 4},
|
|
|
|
{[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 4}}, 4},
|
|
|
|
{[]pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 4}}, 4},
|
2014-10-26 04:49:49 +03:00
|
|
|
// conflicts with existing entries
|
2014-12-02 19:50:15 +03:00
|
|
|
{[]pb.Entry{{Index: 1, Term: 4}, {Index: 2, Term: 4}}, 1},
|
|
|
|
{[]pb.Entry{{Index: 2, Term: 1}, {Index: 3, Term: 4}, {Index: 4, Term: 4}}, 2},
|
|
|
|
{[]pb.Entry{{Index: 3, Term: 1}, {Index: 4, Term: 2}, {Index: 5, Term: 4}, {Index: 6, Term: 4}}, 3},
|
2014-10-26 04:49:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(NewMemoryStorage(), raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(previousEnts...)
|
2014-10-26 04:49:49 +03:00
|
|
|
|
2014-12-02 19:50:15 +03:00
|
|
|
gconflict := raftLog.findConflict(tt.ents)
|
2014-10-26 04:49:49 +03:00
|
|
|
if gconflict != tt.wconflict {
|
|
|
|
t.Errorf("#%d: conflict = %d, want %d", i, gconflict, tt.wconflict)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 06:34:14 +03:00
|
|
|
func TestIsUpToDate(t *testing.T) {
|
2014-11-25 04:13:47 +03:00
|
|
|
previousEnts := []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(NewMemoryStorage(), raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(previousEnts...)
|
2014-10-26 06:34:14 +03:00
|
|
|
tests := []struct {
|
|
|
|
lastIndex uint64
|
|
|
|
term uint64
|
|
|
|
wUpToDate bool
|
|
|
|
}{
|
|
|
|
// greater term, ignore lastIndex
|
|
|
|
{raftLog.lastIndex() - 1, 4, true},
|
|
|
|
{raftLog.lastIndex(), 4, true},
|
|
|
|
{raftLog.lastIndex() + 1, 4, true},
|
|
|
|
// smaller term, ignore lastIndex
|
|
|
|
{raftLog.lastIndex() - 1, 2, false},
|
|
|
|
{raftLog.lastIndex(), 2, false},
|
|
|
|
{raftLog.lastIndex() + 1, 2, false},
|
|
|
|
// equal term, lager lastIndex wins
|
|
|
|
{raftLog.lastIndex() - 1, 3, false},
|
|
|
|
{raftLog.lastIndex(), 3, true},
|
|
|
|
{raftLog.lastIndex() + 1, 3, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
gUpToDate := raftLog.isUpToDate(tt.lastIndex, tt.term)
|
|
|
|
if gUpToDate != tt.wUpToDate {
|
|
|
|
t.Errorf("#%d: uptodate = %v, want %v", i, gUpToDate, tt.wUpToDate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 21:58:41 +04:00
|
|
|
func TestAppend(t *testing.T) {
|
2014-11-25 04:13:47 +03:00
|
|
|
previousEnts := []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}}
|
2014-07-14 21:58:41 +04:00
|
|
|
tests := []struct {
|
2014-08-28 05:53:18 +04:00
|
|
|
ents []pb.Entry
|
2014-10-08 14:29:53 +04:00
|
|
|
windex uint64
|
2014-08-28 05:53:18 +04:00
|
|
|
wents []pb.Entry
|
2014-10-08 14:29:53 +04:00
|
|
|
wunstable uint64
|
2014-07-14 21:58:41 +04:00
|
|
|
}{
|
|
|
|
{
|
2014-08-28 05:53:18 +04:00
|
|
|
[]pb.Entry{},
|
2014-07-14 21:58:41 +04:00
|
|
|
2,
|
2014-11-25 04:13:47 +03:00
|
|
|
[]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}},
|
2014-07-24 08:45:57 +04:00
|
|
|
3,
|
2014-07-14 21:58:41 +04:00
|
|
|
},
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
[]pb.Entry{{Index: 3, Term: 2}},
|
2014-07-14 21:58:41 +04:00
|
|
|
3,
|
2014-11-25 04:13:47 +03:00
|
|
|
[]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 2}},
|
2014-07-24 08:45:57 +04:00
|
|
|
3,
|
2014-07-14 21:58:41 +04:00
|
|
|
},
|
|
|
|
// conflicts with index 1
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
[]pb.Entry{{Index: 1, Term: 2}},
|
2014-07-14 21:58:41 +04:00
|
|
|
1,
|
2014-11-25 04:13:47 +03:00
|
|
|
[]pb.Entry{{Index: 1, Term: 2}},
|
2014-07-24 08:45:57 +04:00
|
|
|
1,
|
2014-07-14 21:58:41 +04:00
|
|
|
},
|
|
|
|
// conflicts with index 2
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
[]pb.Entry{{Index: 2, Term: 3}, {Index: 3, Term: 3}},
|
2014-07-14 21:58:41 +04:00
|
|
|
3,
|
2014-11-25 04:13:47 +03:00
|
|
|
[]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 3}, {Index: 3, Term: 3}},
|
2014-07-24 08:45:57 +04:00
|
|
|
2,
|
2014-07-14 21:58:41 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-11-03 23:04:24 +03:00
|
|
|
storage := NewMemoryStorage()
|
|
|
|
storage.Append(previousEnts)
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(storage, raftLogger)
|
2014-11-03 23:04:24 +03:00
|
|
|
|
2014-12-02 19:50:15 +03:00
|
|
|
index := raftLog.append(tt.ents...)
|
2014-07-14 21:58:41 +04:00
|
|
|
if index != tt.windex {
|
|
|
|
t.Errorf("#%d: lastIndex = %d, want %d", i, index, tt.windex)
|
|
|
|
}
|
2015-06-13 08:24:40 +03:00
|
|
|
g, err := raftLog.entries(1, noLimit)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("#%d: unexpected error %v", i, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(g, tt.wents) {
|
2014-07-14 21:58:41 +04:00
|
|
|
t.Errorf("#%d: logEnts = %+v, want %+v", i, g, tt.wents)
|
|
|
|
}
|
2015-06-13 08:24:40 +03:00
|
|
|
if goff := raftLog.unstable.offset; goff != tt.wunstable {
|
|
|
|
t.Errorf("#%d: unstable = %d, want %d", i, goff, tt.wunstable)
|
2014-07-24 08:45:57 +04:00
|
|
|
}
|
2014-07-14 21:58:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:06:21 +03:00
|
|
|
// TestLogMaybeAppend ensures:
|
|
|
|
// If the given (index, term) matches with the existing log:
|
|
|
|
// 1. If an existing entry conflicts with a new one (same index
|
|
|
|
// but different terms), delete the existing entry and all that
|
|
|
|
// follow it
|
|
|
|
// 2.Append any new entries not already in the log
|
|
|
|
// If the given (index, term) does not match with the existing log:
|
|
|
|
// return false
|
|
|
|
func TestLogMaybeAppend(t *testing.T) {
|
2014-11-25 04:13:47 +03:00
|
|
|
previousEnts := []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}
|
2014-10-29 01:06:21 +03:00
|
|
|
lastindex := uint64(3)
|
|
|
|
lastterm := uint64(3)
|
2014-10-30 00:04:30 +03:00
|
|
|
commit := uint64(1)
|
2014-10-29 01:06:21 +03:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
logTerm uint64
|
|
|
|
index uint64
|
|
|
|
committed uint64
|
|
|
|
ents []pb.Entry
|
|
|
|
|
|
|
|
wlasti uint64
|
|
|
|
wappend bool
|
|
|
|
wcommit uint64
|
2014-10-30 00:04:30 +03:00
|
|
|
wpanic bool
|
2014-10-29 01:06:21 +03:00
|
|
|
}{
|
|
|
|
// not match: term is different
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm - 1, lastindex, lastindex, []pb.Entry{{Index: lastindex + 1, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
0, false, commit, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
// not match: index out of bound
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm, lastindex + 1, lastindex, []pb.Entry{{Index: lastindex + 2, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
0, false, commit, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
// match with the last existing entry
|
|
|
|
{
|
|
|
|
lastterm, lastindex, lastindex, nil,
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex, true, lastindex, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
lastterm, lastindex, lastindex + 1, nil,
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex, true, lastindex, false, // do not increase commit higher than lastnewi
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
2014-10-29 21:44:18 +03:00
|
|
|
{
|
|
|
|
lastterm, lastindex, lastindex - 1, nil,
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex, true, lastindex - 1, false, // commit up to the commit in the message
|
|
|
|
},
|
|
|
|
{
|
|
|
|
lastterm, lastindex, 0, nil,
|
|
|
|
lastindex, true, commit, false, // commit do not decrease
|
|
|
|
},
|
|
|
|
{
|
2014-10-30 00:57:39 +03:00
|
|
|
0, 0, lastindex, nil,
|
|
|
|
0, true, commit, false, // commit do not decrease
|
2014-10-29 21:44:18 +03:00
|
|
|
},
|
2014-10-29 01:06:21 +03:00
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm, lastindex, lastindex, []pb.Entry{{Index: lastindex + 1, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex + 1, true, lastindex, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm, lastindex, lastindex + 1, []pb.Entry{{Index: lastindex + 1, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex + 1, true, lastindex + 1, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm, lastindex, lastindex + 2, []pb.Entry{{Index: lastindex + 1, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex + 1, true, lastindex + 1, false, // do not increase commit higher than lastnewi
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm, lastindex, lastindex + 2, []pb.Entry{{Index: lastindex + 1, Term: 4}, {Index: lastindex + 2, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex + 2, true, lastindex + 2, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
// match with the the entry in the middle
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm - 1, lastindex - 1, lastindex, []pb.Entry{{Index: lastindex, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex, true, lastindex, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm - 2, lastindex - 2, lastindex, []pb.Entry{{Index: lastindex - 1, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex - 1, true, lastindex - 1, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm - 3, lastindex - 3, lastindex, []pb.Entry{{Index: lastindex - 2, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex - 2, true, lastindex - 2, true, // conflict with existing committed entry
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
{
|
2014-11-25 04:13:47 +03:00
|
|
|
lastterm - 2, lastindex - 2, lastindex, []pb.Entry{{Index: lastindex - 1, Term: 4}, {Index: lastindex, Term: 4}},
|
2014-10-30 00:04:30 +03:00
|
|
|
lastindex, true, lastindex, false,
|
2014-10-29 01:06:21 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(NewMemoryStorage(), raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(previousEnts...)
|
2014-10-30 00:04:30 +03:00
|
|
|
raftLog.committed = commit
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2016-04-03 04:27:54 +03:00
|
|
|
if !tt.wpanic {
|
2014-10-30 00:04:30 +03:00
|
|
|
t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
glasti, gappend := raftLog.maybeAppend(tt.index, tt.logTerm, tt.committed, tt.ents...)
|
|
|
|
gcommit := raftLog.committed
|
2014-10-29 01:06:21 +03:00
|
|
|
|
2014-10-30 00:04:30 +03:00
|
|
|
if glasti != tt.wlasti {
|
|
|
|
t.Errorf("#%d: lastindex = %d, want %d", i, glasti, tt.wlasti)
|
2014-10-29 01:06:21 +03:00
|
|
|
}
|
2014-10-30 00:04:30 +03:00
|
|
|
if gappend != tt.wappend {
|
|
|
|
t.Errorf("#%d: append = %v, want %v", i, gappend, tt.wappend)
|
|
|
|
}
|
|
|
|
if gcommit != tt.wcommit {
|
|
|
|
t.Errorf("#%d: committed = %d, want %d", i, gcommit, tt.wcommit)
|
|
|
|
}
|
2014-12-03 03:19:37 +03:00
|
|
|
if gappend && len(tt.ents) != 0 {
|
2015-06-13 08:24:40 +03:00
|
|
|
gents, err := raftLog.slice(raftLog.lastIndex()-uint64(len(tt.ents))+1, raftLog.lastIndex()+1, noLimit)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
|
|
|
}
|
2014-10-30 00:04:30 +03:00
|
|
|
if !reflect.DeepEqual(tt.ents, gents) {
|
|
|
|
t.Errorf("%d: appended entries = %v, want %v", i, gents, tt.ents)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-10-29 01:06:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// TestCompactionSideEffects ensures that all the log related functionality works correctly after
|
2014-07-01 02:15:51 +04:00
|
|
|
// a compaction.
|
|
|
|
func TestCompactionSideEffects(t *testing.T) {
|
2014-10-08 14:29:53 +04:00
|
|
|
var i uint64
|
2014-11-03 23:04:24 +03:00
|
|
|
// Populate the log with 1000 entries; 750 in stable storage and 250 in unstable.
|
2014-10-08 14:29:53 +04:00
|
|
|
lastIndex := uint64(1000)
|
2014-11-03 23:04:24 +03:00
|
|
|
unstableIndex := uint64(750)
|
2014-09-10 22:32:01 +04:00
|
|
|
lastTerm := lastIndex
|
2014-11-03 23:04:24 +03:00
|
|
|
storage := NewMemoryStorage()
|
2014-11-18 00:37:46 +03:00
|
|
|
for i = 1; i <= unstableIndex; i++ {
|
2014-11-03 23:04:24 +03:00
|
|
|
storage.Append([]pb.Entry{{Term: uint64(i), Index: uint64(i)}})
|
|
|
|
}
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(storage, raftLogger)
|
2014-11-03 23:04:24 +03:00
|
|
|
for i = unstableIndex; i < lastIndex; i++ {
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(pb.Entry{Term: uint64(i + 1), Index: uint64(i + 1)})
|
2014-11-03 23:04:24 +03:00
|
|
|
}
|
2014-07-01 02:15:51 +04:00
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
ok := raftLog.maybeCommit(lastIndex, lastTerm)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("maybeCommit returned false")
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
2014-11-07 01:13:35 +03:00
|
|
|
raftLog.appliedTo(raftLog.committed)
|
2014-07-01 02:15:51 +04:00
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
offset := uint64(500)
|
2015-01-23 21:50:43 +03:00
|
|
|
storage.Compact(offset)
|
2014-07-01 02:15:51 +04:00
|
|
|
|
2014-07-24 03:15:25 +04:00
|
|
|
if raftLog.lastIndex() != lastIndex {
|
|
|
|
t.Errorf("lastIndex = %d, want %d", raftLog.lastIndex(), lastIndex)
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
for j := offset; j <= raftLog.lastIndex(); j++ {
|
2015-06-13 08:24:40 +03:00
|
|
|
if mustTerm(raftLog.term(j)) != j {
|
|
|
|
t.Errorf("term(%d) = %d, want %d", j, mustTerm(raftLog.term(j)), j)
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
for j := offset; j <= raftLog.lastIndex(); j++ {
|
|
|
|
if !raftLog.matchTerm(j, j) {
|
|
|
|
t.Errorf("matchTerm(%d) = false, want true", j)
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
unstableEnts := raftLog.unstableEntries()
|
|
|
|
if g := len(unstableEnts); g != 250 {
|
|
|
|
t.Errorf("len(unstableEntries) = %d, want = %d", g, 250)
|
2014-07-24 08:45:57 +04:00
|
|
|
}
|
2014-11-03 23:04:24 +03:00
|
|
|
if unstableEnts[0].Index != 751 {
|
|
|
|
t.Errorf("Index = %d, want = %d", unstableEnts[0].Index, 751)
|
2014-08-02 08:43:08 +04:00
|
|
|
}
|
2014-07-24 08:45:57 +04:00
|
|
|
|
2014-07-24 03:15:25 +04:00
|
|
|
prev := raftLog.lastIndex()
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(pb.Entry{Index: raftLog.lastIndex() + 1, Term: raftLog.lastIndex() + 1})
|
2014-07-24 03:15:25 +04:00
|
|
|
if raftLog.lastIndex() != prev+1 {
|
|
|
|
t.Errorf("lastIndex = %d, want = %d", raftLog.lastIndex(), prev+1)
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
|
|
|
|
2015-06-13 08:24:40 +03:00
|
|
|
ents, err := raftLog.entries(raftLog.lastIndex(), noLimit)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
|
|
|
}
|
2014-07-01 02:15:51 +04:00
|
|
|
if len(ents) != 1 {
|
|
|
|
t.Errorf("len(entries) = %d, want = %d", len(ents), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 10:42:59 +03:00
|
|
|
func TestHasNextEnts(t *testing.T) {
|
|
|
|
snap := pb.Snapshot{
|
|
|
|
Metadata: pb.SnapshotMetadata{Term: 1, Index: 3},
|
|
|
|
}
|
|
|
|
ents := []pb.Entry{
|
|
|
|
{Term: 1, Index: 4},
|
|
|
|
{Term: 1, Index: 5},
|
|
|
|
{Term: 1, Index: 6},
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
applied uint64
|
|
|
|
hasNext bool
|
|
|
|
}{
|
|
|
|
{0, true},
|
|
|
|
{3, true},
|
|
|
|
{4, true},
|
|
|
|
{5, false},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
storage := NewMemoryStorage()
|
|
|
|
storage.ApplySnapshot(snap)
|
|
|
|
raftLog := newLog(storage, raftLogger)
|
|
|
|
raftLog.append(ents...)
|
|
|
|
raftLog.maybeCommit(5, 1)
|
|
|
|
raftLog.appliedTo(tt.applied)
|
|
|
|
|
|
|
|
hasNext := raftLog.hasNextEnts()
|
|
|
|
if hasNext != tt.hasNext {
|
|
|
|
t.Errorf("#%d: hasNext = %v, want %v", i, hasNext, tt.hasNext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 11:09:54 +03:00
|
|
|
func TestNextEnts(t *testing.T) {
|
2014-11-20 00:17:50 +03:00
|
|
|
snap := pb.Snapshot{
|
|
|
|
Metadata: pb.SnapshotMetadata{Term: 1, Index: 3},
|
|
|
|
}
|
2014-11-14 11:09:54 +03:00
|
|
|
ents := []pb.Entry{
|
|
|
|
{Term: 1, Index: 4},
|
|
|
|
{Term: 1, Index: 5},
|
|
|
|
{Term: 1, Index: 6},
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
applied uint64
|
|
|
|
wents []pb.Entry
|
|
|
|
}{
|
2014-11-18 02:28:24 +03:00
|
|
|
{0, ents[:2]},
|
|
|
|
{3, ents[:2]},
|
|
|
|
{4, ents[1:2]},
|
2014-11-14 11:09:54 +03:00
|
|
|
{5, nil},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2014-11-20 00:17:50 +03:00
|
|
|
storage := NewMemoryStorage()
|
|
|
|
storage.ApplySnapshot(snap)
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(storage, raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(ents...)
|
2014-11-14 11:09:54 +03:00
|
|
|
raftLog.maybeCommit(5, 1)
|
|
|
|
raftLog.appliedTo(tt.applied)
|
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
nents := raftLog.nextEnts()
|
|
|
|
if !reflect.DeepEqual(nents, tt.wents) {
|
|
|
|
t.Errorf("#%d: nents = %+v, want %+v", i, nents, tt.wents)
|
2014-11-14 11:09:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 21:40:38 +03:00
|
|
|
// TestUnstableEnts ensures unstableEntries returns the unstable part of the
|
|
|
|
// entries correctly.
|
2014-07-24 08:45:57 +04:00
|
|
|
func TestUnstableEnts(t *testing.T) {
|
2014-11-18 00:37:46 +03:00
|
|
|
previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
|
2014-07-24 08:45:57 +04:00
|
|
|
tests := []struct {
|
2014-11-24 21:40:38 +03:00
|
|
|
unstable uint64
|
|
|
|
wents []pb.Entry
|
2014-07-24 08:45:57 +04:00
|
|
|
}{
|
2014-11-24 21:40:38 +03:00
|
|
|
{3, nil},
|
|
|
|
{1, previousEnts},
|
2014-07-24 08:45:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-11-24 21:40:38 +03:00
|
|
|
// append stable entries to storage
|
2014-11-03 23:04:24 +03:00
|
|
|
storage := NewMemoryStorage()
|
2014-11-24 21:40:38 +03:00
|
|
|
storage.Append(previousEnts[:tt.unstable-1])
|
|
|
|
|
|
|
|
// append unstable entries to raftlog
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(storage, raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(previousEnts[tt.unstable-1:]...)
|
2014-11-24 21:40:38 +03:00
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
ents := raftLog.unstableEntries()
|
2014-11-12 03:10:03 +03:00
|
|
|
if l := len(ents); l > 0 {
|
2014-11-29 01:13:07 +03:00
|
|
|
raftLog.stableTo(ents[l-1].Index, ents[l-i].Term)
|
2014-11-12 03:10:03 +03:00
|
|
|
}
|
2014-07-24 08:45:57 +04:00
|
|
|
if !reflect.DeepEqual(ents, tt.wents) {
|
|
|
|
t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
|
|
|
|
}
|
2014-11-24 21:40:38 +03:00
|
|
|
w := previousEnts[len(previousEnts)-1].Index + 1
|
2014-11-27 00:36:17 +03:00
|
|
|
if g := raftLog.unstable.offset; g != w {
|
2014-11-24 21:40:38 +03:00
|
|
|
t.Errorf("#%d: unstable = %d, want %d", i, g, w)
|
2014-07-24 08:45:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 03:58:49 +03:00
|
|
|
func TestCommitTo(t *testing.T) {
|
|
|
|
previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}, {Term: 3, Index: 3}}
|
|
|
|
commit := uint64(2)
|
|
|
|
tests := []struct {
|
|
|
|
commit uint64
|
|
|
|
wcommit uint64
|
|
|
|
wpanic bool
|
|
|
|
}{
|
|
|
|
{3, 3, false},
|
|
|
|
{1, 2, false}, // never decrease
|
|
|
|
{4, 0, true}, // commit out of range -> panic
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2016-04-03 04:27:54 +03:00
|
|
|
if !tt.wpanic {
|
2014-11-18 03:58:49 +03:00
|
|
|
t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(NewMemoryStorage(), raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(previousEnts...)
|
2014-11-18 03:58:49 +03:00
|
|
|
raftLog.committed = commit
|
|
|
|
raftLog.commitTo(tt.commit)
|
|
|
|
if raftLog.committed != tt.wcommit {
|
|
|
|
t.Errorf("#%d: committed = %d, want %d", i, raftLog.committed, tt.wcommit)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:10:03 +03:00
|
|
|
func TestStableTo(t *testing.T) {
|
|
|
|
tests := []struct {
|
2014-11-29 02:14:22 +03:00
|
|
|
stablei uint64
|
|
|
|
stablet uint64
|
2014-11-12 03:10:03 +03:00
|
|
|
wunstable uint64
|
|
|
|
}{
|
2014-11-29 02:14:22 +03:00
|
|
|
{1, 1, 2},
|
|
|
|
{2, 2, 3},
|
|
|
|
{2, 1, 1}, // bad term
|
|
|
|
{3, 1, 1}, // bad index
|
2014-11-12 03:10:03 +03:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(NewMemoryStorage(), raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}}...)
|
2014-11-29 02:14:22 +03:00
|
|
|
raftLog.stableTo(tt.stablei, tt.stablet)
|
2014-11-27 00:36:17 +03:00
|
|
|
if raftLog.unstable.offset != tt.wunstable {
|
2014-11-29 02:14:22 +03:00
|
|
|
t.Errorf("#%d: unstable = %d, want %d", i, raftLog.unstable.offset, tt.wunstable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStableToWithSnap(t *testing.T) {
|
|
|
|
snapi, snapt := uint64(5), uint64(2)
|
|
|
|
tests := []struct {
|
|
|
|
stablei uint64
|
|
|
|
stablet uint64
|
|
|
|
newEnts []pb.Entry
|
|
|
|
|
|
|
|
wunstable uint64
|
|
|
|
}{
|
|
|
|
{snapi + 1, snapt, nil, snapi + 1},
|
|
|
|
{snapi, snapt, nil, snapi + 1},
|
|
|
|
{snapi - 1, snapt, nil, snapi + 1},
|
|
|
|
|
|
|
|
{snapi + 1, snapt + 1, nil, snapi + 1},
|
|
|
|
{snapi, snapt + 1, nil, snapi + 1},
|
|
|
|
{snapi - 1, snapt + 1, nil, snapi + 1},
|
|
|
|
|
|
|
|
{snapi + 1, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 2},
|
|
|
|
{snapi, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
|
|
|
|
{snapi - 1, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
|
|
|
|
|
|
|
|
{snapi + 1, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
|
|
|
|
{snapi, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
|
|
|
|
{snapi - 1, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
s := NewMemoryStorage()
|
|
|
|
s.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: snapi, Term: snapt}})
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(s, raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
raftLog.append(tt.newEnts...)
|
2014-11-29 02:14:22 +03:00
|
|
|
raftLog.stableTo(tt.stablei, tt.stablet)
|
|
|
|
if raftLog.unstable.offset != tt.wunstable {
|
|
|
|
t.Errorf("#%d: unstable = %d, want %d", i, raftLog.unstable.offset, tt.wunstable)
|
2014-11-12 03:10:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 02:21:30 +03:00
|
|
|
//TestCompaction ensures that the number of log entries is correct after compactions.
|
2014-07-01 02:15:51 +04:00
|
|
|
func TestCompaction(t *testing.T) {
|
|
|
|
tests := []struct {
|
2014-10-08 14:29:53 +04:00
|
|
|
lastIndex uint64
|
|
|
|
compact []uint64
|
2014-09-10 22:32:01 +04:00
|
|
|
wleft []int
|
|
|
|
wallow bool
|
2014-07-01 02:15:51 +04:00
|
|
|
}{
|
|
|
|
// out of upper bound
|
2014-11-20 00:17:50 +03:00
|
|
|
{1000, []uint64{1001}, []int{-1}, false},
|
|
|
|
{1000, []uint64{300, 500, 800, 900}, []int{700, 500, 200, 100}, true},
|
2014-07-01 02:15:51 +04:00
|
|
|
// out of lower bound
|
2014-11-20 00:17:50 +03:00
|
|
|
{1000, []uint64{300, 299}, []int{700, -1}, false},
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2016-04-03 04:27:54 +03:00
|
|
|
if tt.wallow {
|
2014-11-03 23:04:24 +03:00
|
|
|
t.Errorf("%d: allow = %v, want %v: %v", i, false, true, r)
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
storage := NewMemoryStorage()
|
2014-11-18 00:37:46 +03:00
|
|
|
for i := uint64(1); i <= tt.lastIndex; i++ {
|
2014-11-25 04:13:47 +03:00
|
|
|
storage.Append([]pb.Entry{{Index: i}})
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(storage, raftLogger)
|
2014-11-20 00:17:50 +03:00
|
|
|
raftLog.maybeCommit(tt.lastIndex, 0)
|
2014-11-07 01:13:35 +03:00
|
|
|
raftLog.appliedTo(raftLog.committed)
|
2014-07-01 02:15:51 +04:00
|
|
|
|
|
|
|
for j := 0; j < len(tt.compact); j++ {
|
2015-01-23 21:50:43 +03:00
|
|
|
err := storage.Compact(tt.compact[j])
|
2014-11-26 02:13:07 +03:00
|
|
|
if err != nil {
|
|
|
|
if tt.wallow {
|
|
|
|
t.Errorf("#%d.%d allow = %t, want %t", i, j, false, tt.wallow)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2014-11-03 23:04:24 +03:00
|
|
|
if len(raftLog.allEntries()) != tt.wleft[j] {
|
|
|
|
t.Errorf("#%d.%d len = %d, want %d", i, j, len(raftLog.allEntries()), tt.wleft[j])
|
2014-07-01 02:15:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 23:10:43 +04:00
|
|
|
func TestLogRestore(t *testing.T) {
|
2014-10-08 14:29:53 +04:00
|
|
|
index := uint64(1000)
|
|
|
|
term := uint64(1000)
|
2014-11-20 00:17:50 +03:00
|
|
|
snap := pb.SnapshotMetadata{Index: index, Term: term}
|
|
|
|
storage := NewMemoryStorage()
|
|
|
|
storage.ApplySnapshot(pb.Snapshot{Metadata: snap})
|
2015-08-10 18:04:38 +03:00
|
|
|
raftLog := newLog(storage, raftLogger)
|
2014-07-01 23:10:43 +04:00
|
|
|
|
2014-11-18 00:37:46 +03:00
|
|
|
if len(raftLog.allEntries()) != 0 {
|
2014-11-24 22:10:02 +03:00
|
|
|
t.Errorf("len = %d, want 0", len(raftLog.allEntries()))
|
2014-07-01 23:10:43 +04:00
|
|
|
}
|
2014-11-18 00:37:46 +03:00
|
|
|
if raftLog.firstIndex() != index+1 {
|
|
|
|
t.Errorf("firstIndex = %d, want %d", raftLog.firstIndex(), index+1)
|
2014-07-01 23:10:43 +04:00
|
|
|
}
|
2014-07-24 03:15:25 +04:00
|
|
|
if raftLog.committed != index {
|
2014-12-05 01:12:55 +03:00
|
|
|
t.Errorf("committed = %d, want %d", raftLog.committed, index)
|
2014-07-01 23:10:43 +04:00
|
|
|
}
|
2014-11-27 00:36:17 +03:00
|
|
|
if raftLog.unstable.offset != index+1 {
|
2015-08-10 18:04:38 +03:00
|
|
|
t.Errorf("unstable = %d, want %d", raftLog.unstable.offset, index+1)
|
2014-07-24 08:45:57 +04:00
|
|
|
}
|
2015-06-13 08:24:40 +03:00
|
|
|
if mustTerm(raftLog.term(index)) != term {
|
|
|
|
t.Errorf("term = %d, want %d", mustTerm(raftLog.term(index)), term)
|
2014-07-01 23:10:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 03:15:04 +04:00
|
|
|
func TestIsOutOfBounds(t *testing.T) {
|
2014-10-08 14:29:53 +04:00
|
|
|
offset := uint64(100)
|
|
|
|
num := uint64(100)
|
2014-11-20 00:17:50 +03:00
|
|
|
storage := NewMemoryStorage()
|
|
|
|
storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}})
|
2015-08-10 18:04:38 +03:00
|
|
|
l := newLog(storage, raftLogger)
|
2014-12-02 19:50:15 +03:00
|
|
|
for i := uint64(1); i <= num; i++ {
|
|
|
|
l.append(pb.Entry{Index: i + offset})
|
|
|
|
}
|
2014-06-28 03:15:04 +04:00
|
|
|
|
2014-12-03 03:19:37 +03:00
|
|
|
first := offset + 1
|
2014-06-28 03:15:04 +04:00
|
|
|
tests := []struct {
|
2015-06-13 08:24:40 +03:00
|
|
|
lo, hi uint64
|
|
|
|
wpanic bool
|
|
|
|
wErrCompacted bool
|
2014-06-28 03:15:04 +04:00
|
|
|
}{
|
2014-12-03 03:19:37 +03:00
|
|
|
{
|
|
|
|
first - 2, first + 1,
|
2015-06-13 08:24:40 +03:00
|
|
|
false,
|
2014-12-03 03:19:37 +03:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
first - 1, first + 1,
|
2015-06-13 08:24:40 +03:00
|
|
|
false,
|
2014-12-03 03:19:37 +03:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
first, first,
|
|
|
|
false,
|
2015-06-13 08:24:40 +03:00
|
|
|
false,
|
2014-12-03 03:19:37 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
first + num/2, first + num/2,
|
|
|
|
false,
|
2015-06-13 08:24:40 +03:00
|
|
|
false,
|
2014-12-03 03:19:37 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
first + num - 1, first + num - 1,
|
|
|
|
false,
|
2015-06-13 08:24:40 +03:00
|
|
|
false,
|
2014-12-03 03:19:37 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
first + num, first + num,
|
|
|
|
false,
|
2015-06-13 08:24:40 +03:00
|
|
|
false,
|
2014-12-03 03:19:37 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
first + num, first + num + 1,
|
|
|
|
true,
|
2015-06-13 08:24:40 +03:00
|
|
|
false,
|
2014-12-03 03:19:37 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
first + num + 1, first + num + 1,
|
|
|
|
true,
|
2015-06-13 08:24:40 +03:00
|
|
|
false,
|
2014-12-03 03:19:37 +03:00
|
|
|
},
|
2014-06-28 03:15:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-12-03 03:19:37 +03:00
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2015-06-13 08:24:40 +03:00
|
|
|
if !tt.wpanic {
|
2014-12-03 03:19:37 +03:00
|
|
|
t.Errorf("%d: panic = %v, want %v: %v", i, true, false, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-06-13 08:24:40 +03:00
|
|
|
err := l.mustCheckOutOfBounds(tt.lo, tt.hi)
|
|
|
|
if tt.wpanic {
|
2014-12-03 03:19:37 +03:00
|
|
|
t.Errorf("%d: panic = %v, want %v", i, false, true)
|
|
|
|
}
|
2015-06-13 08:24:40 +03:00
|
|
|
if tt.wErrCompacted && err != ErrCompacted {
|
|
|
|
t.Errorf("%d: err = %v, want %v", i, err, ErrCompacted)
|
|
|
|
}
|
|
|
|
if !tt.wErrCompacted && err != nil {
|
|
|
|
t.Errorf("%d: unexpected err %v", i, err)
|
|
|
|
}
|
2014-12-03 03:19:37 +03:00
|
|
|
}()
|
2014-06-28 03:15:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 00:37:46 +03:00
|
|
|
func TestTerm(t *testing.T) {
|
|
|
|
var i uint64
|
|
|
|
offset := uint64(100)
|
|
|
|
num := uint64(100)
|
|
|
|
|
2014-11-20 00:17:50 +03:00
|
|
|
storage := NewMemoryStorage()
|
2014-12-03 10:57:02 +03:00
|
|
|
storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset, Term: 1}})
|
2015-08-10 18:04:38 +03:00
|
|
|
l := newLog(storage, raftLogger)
|
2014-11-18 00:37:46 +03:00
|
|
|
for i = 1; i < num; i++ {
|
2014-12-02 19:50:15 +03:00
|
|
|
l.append(pb.Entry{Index: offset + i, Term: i})
|
2014-11-18 00:37:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
index uint64
|
|
|
|
w uint64
|
|
|
|
}{
|
|
|
|
{offset - 1, 0},
|
2014-12-03 10:57:02 +03:00
|
|
|
{offset, 1},
|
2014-11-18 00:37:46 +03:00
|
|
|
{offset + num/2, num / 2},
|
|
|
|
{offset + num - 1, num - 1},
|
|
|
|
{offset + num, 0},
|
|
|
|
}
|
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
for j, tt := range tests {
|
2015-06-13 08:24:40 +03:00
|
|
|
term := mustTerm(l.term(tt.index))
|
2014-11-18 00:37:46 +03:00
|
|
|
if !reflect.DeepEqual(term, tt.w) {
|
2015-02-18 00:15:48 +03:00
|
|
|
t.Errorf("#%d: at = %d, want %d", j, term, tt.w)
|
2014-11-18 00:37:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 10:57:02 +03:00
|
|
|
func TestTermWithUnstableSnapshot(t *testing.T) {
|
|
|
|
storagesnapi := uint64(100)
|
|
|
|
unstablesnapi := storagesnapi + 5
|
|
|
|
|
|
|
|
storage := NewMemoryStorage()
|
|
|
|
storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: storagesnapi, Term: 1}})
|
2015-08-10 18:04:38 +03:00
|
|
|
l := newLog(storage, raftLogger)
|
2014-12-03 10:57:02 +03:00
|
|
|
l.restore(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: unstablesnapi, Term: 1}})
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
index uint64
|
|
|
|
w uint64
|
|
|
|
}{
|
|
|
|
// cannot get term from storage
|
|
|
|
{storagesnapi, 0},
|
|
|
|
// cannot get term from the gap between storage ents and unstable snapshot
|
|
|
|
{storagesnapi + 1, 0},
|
|
|
|
{unstablesnapi - 1, 0},
|
|
|
|
// get term from unstable snapshot index
|
|
|
|
{unstablesnapi, 1},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2015-06-13 08:24:40 +03:00
|
|
|
term := mustTerm(l.term(tt.index))
|
2014-12-03 10:57:02 +03:00
|
|
|
if !reflect.DeepEqual(term, tt.w) {
|
|
|
|
t.Errorf("#%d: at = %d, want %d", i, term, tt.w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 03:15:04 +04:00
|
|
|
func TestSlice(t *testing.T) {
|
2014-10-08 14:29:53 +04:00
|
|
|
var i uint64
|
|
|
|
offset := uint64(100)
|
|
|
|
num := uint64(100)
|
2015-03-18 22:36:29 +03:00
|
|
|
last := offset + num
|
|
|
|
half := offset + num/2
|
|
|
|
halfe := pb.Entry{Index: half, Term: half}
|
2014-06-28 03:15:04 +04:00
|
|
|
|
2014-11-20 00:17:50 +03:00
|
|
|
storage := NewMemoryStorage()
|
|
|
|
storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}})
|
2015-03-18 22:36:29 +03:00
|
|
|
for i = 1; i < num/2; i++ {
|
|
|
|
storage.Append([]pb.Entry{{Index: offset + i, Term: offset + i}})
|
|
|
|
}
|
2015-08-10 18:04:38 +03:00
|
|
|
l := newLog(storage, raftLogger)
|
2015-03-18 22:36:29 +03:00
|
|
|
for i = num / 2; i < num; i++ {
|
2014-12-02 19:50:15 +03:00
|
|
|
l.append(pb.Entry{Index: offset + i, Term: offset + i})
|
2014-06-28 03:15:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2015-03-18 22:36:29 +03:00
|
|
|
from uint64
|
|
|
|
to uint64
|
|
|
|
limit uint64
|
|
|
|
|
2014-12-03 03:19:37 +03:00
|
|
|
w []pb.Entry
|
|
|
|
wpanic bool
|
2014-06-28 03:15:04 +04:00
|
|
|
}{
|
2015-03-18 22:36:29 +03:00
|
|
|
// test no limit
|
2015-06-13 08:24:40 +03:00
|
|
|
{offset - 1, offset + 1, noLimit, nil, false},
|
|
|
|
{offset, offset + 1, noLimit, nil, false},
|
2015-03-18 22:36:29 +03:00
|
|
|
{half - 1, half + 1, noLimit, []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false},
|
|
|
|
{half, half + 1, noLimit, []pb.Entry{{Index: half, Term: half}}, false},
|
|
|
|
{last - 1, last, noLimit, []pb.Entry{{Index: last - 1, Term: last - 1}}, false},
|
|
|
|
{last, last + 1, noLimit, nil, true},
|
|
|
|
|
|
|
|
// test limit
|
|
|
|
{half - 1, half + 1, 0, []pb.Entry{{Index: half - 1, Term: half - 1}}, false},
|
|
|
|
{half - 1, half + 1, uint64(halfe.Size() + 1), []pb.Entry{{Index: half - 1, Term: half - 1}}, false},
|
|
|
|
{half - 1, half + 1, uint64(halfe.Size() * 2), []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false},
|
|
|
|
{half - 1, half + 2, uint64(halfe.Size() * 3), []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}, {Index: half + 1, Term: half + 1}}, false},
|
|
|
|
{half, half + 2, uint64(halfe.Size()), []pb.Entry{{Index: half, Term: half}}, false},
|
|
|
|
{half, half + 2, uint64(halfe.Size() * 2), []pb.Entry{{Index: half, Term: half}, {Index: half + 1, Term: half + 1}}, false},
|
2014-06-28 03:15:04 +04:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
for j, tt := range tests {
|
2014-12-03 03:19:37 +03:00
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if !tt.wpanic {
|
2015-02-18 00:15:48 +03:00
|
|
|
t.Errorf("%d: panic = %v, want %v: %v", j, true, false, r)
|
2014-12-03 03:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-06-13 08:24:40 +03:00
|
|
|
g, err := l.slice(tt.from, tt.to, tt.limit)
|
|
|
|
if tt.from <= offset && err != ErrCompacted {
|
|
|
|
t.Fatalf("#%d: err = %v, want %v", j, err, ErrCompacted)
|
|
|
|
}
|
|
|
|
if tt.from > offset && err != nil {
|
|
|
|
t.Fatalf("#%d: unexpected error %v", j, err)
|
|
|
|
}
|
2014-12-03 03:19:37 +03:00
|
|
|
if !reflect.DeepEqual(g, tt.w) {
|
2015-02-18 00:15:48 +03:00
|
|
|
t.Errorf("#%d: from %d to %d = %v, want %v", j, tt.from, tt.to, g, tt.w)
|
2014-12-03 03:19:37 +03:00
|
|
|
}
|
|
|
|
}()
|
2014-06-28 03:15:04 +04:00
|
|
|
}
|
|
|
|
}
|
2015-06-13 08:24:40 +03:00
|
|
|
|
|
|
|
func mustTerm(term uint64, err error) uint64 {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return term
|
|
|
|
}
|