mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00

Compaction is now treated as an implementation detail of Storage implementations; Node.Compact() and related functionality have been removed. Ready.Snapshot is now used only for incoming snapshots. A return value has been added to ApplyConfChange to allow applications to track the node information that must be stored in the snapshot. raftpb.Snapshot has been split into Snapshot and SnapshotMetadata, to allow the full snapshot data to be read from disk only when needed. raft.Storage has new methods Snapshot, ApplySnapshot, HardState, and SetHardState. The Snapshot and HardState parameters have been removed from RestartNode() and will now be loaded from Storage instead. The only remaining difference between StartNode and RestartNode is that the former bootstraps an initial list of Peers.
146 lines
4.0 KiB
Go
146 lines
4.0 KiB
Go
/*
|
|
Copyright 2014 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.
|
|
*/
|
|
|
|
package etcdserver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/coreos/etcd/pkg/pbutil"
|
|
"github.com/coreos/etcd/pkg/types"
|
|
"github.com/coreos/etcd/raft/raftpb"
|
|
)
|
|
|
|
func TestGetIDs(t *testing.T) {
|
|
addcc := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
|
|
addEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc)}
|
|
removecc := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
|
|
removeEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc)}
|
|
normalEntry := raftpb.Entry{Type: raftpb.EntryNormal}
|
|
|
|
tests := []struct {
|
|
confState *raftpb.ConfState
|
|
ents []raftpb.Entry
|
|
|
|
widSet []uint64
|
|
}{
|
|
{nil, []raftpb.Entry{}, []uint64{}},
|
|
{&raftpb.ConfState{Nodes: []uint64{1}},
|
|
[]raftpb.Entry{}, []uint64{1}},
|
|
{&raftpb.ConfState{Nodes: []uint64{1}},
|
|
[]raftpb.Entry{addEntry}, []uint64{1, 2}},
|
|
{&raftpb.ConfState{Nodes: []uint64{1}},
|
|
[]raftpb.Entry{addEntry, removeEntry}, []uint64{1}},
|
|
{&raftpb.ConfState{Nodes: []uint64{1}},
|
|
[]raftpb.Entry{addEntry, normalEntry}, []uint64{1, 2}},
|
|
{&raftpb.ConfState{Nodes: []uint64{1}},
|
|
[]raftpb.Entry{addEntry, removeEntry, normalEntry}, []uint64{1}},
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
var snap raftpb.Snapshot
|
|
if tt.confState != nil {
|
|
snap.Metadata.ConfState = *tt.confState
|
|
}
|
|
idSet := getIDs(&snap, tt.ents)
|
|
if !reflect.DeepEqual(idSet, tt.widSet) {
|
|
t.Errorf("#%d: idset = %#v, want %#v", i, idSet, tt.widSet)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCreateConfigChangeEnts(t *testing.T) {
|
|
m := Member{
|
|
ID: types.ID(1),
|
|
RaftAttributes: RaftAttributes{PeerURLs: []string{"http://localhost:7001", "http://localhost:2380"}},
|
|
}
|
|
ctx, err := json.Marshal(m)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
addcc1 := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1, Context: ctx}
|
|
removecc2 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
|
|
removecc3 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 3}
|
|
tests := []struct {
|
|
ids []uint64
|
|
self uint64
|
|
term, index uint64
|
|
|
|
wents []raftpb.Entry
|
|
}{
|
|
{
|
|
[]uint64{1},
|
|
1,
|
|
1, 1,
|
|
|
|
[]raftpb.Entry{},
|
|
},
|
|
{
|
|
[]uint64{1, 2},
|
|
1,
|
|
1, 1,
|
|
|
|
[]raftpb.Entry{{Term: 1, Index: 2, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
|
|
},
|
|
{
|
|
[]uint64{1, 2},
|
|
1,
|
|
2, 2,
|
|
|
|
[]raftpb.Entry{{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
|
|
},
|
|
{
|
|
[]uint64{1, 2, 3},
|
|
1,
|
|
2, 2,
|
|
|
|
[]raftpb.Entry{
|
|
{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
|
|
{Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
|
|
},
|
|
},
|
|
{
|
|
[]uint64{2, 3},
|
|
2,
|
|
2, 2,
|
|
|
|
[]raftpb.Entry{
|
|
{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
|
|
},
|
|
},
|
|
{
|
|
[]uint64{2, 3},
|
|
1,
|
|
2, 2,
|
|
|
|
[]raftpb.Entry{
|
|
{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
|
|
{Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
|
|
{Term: 2, Index: 5, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc1)},
|
|
},
|
|
},
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
gents := createConfigChangeEnts(tt.ids, tt.self, tt.term, tt.index)
|
|
if !reflect.DeepEqual(gents, tt.wents) {
|
|
t.Errorf("#%d: ents = %v, want %v", i, gents, tt.wents)
|
|
}
|
|
}
|
|
}
|