2016-05-13 06:49:40 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// 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-08-26 05:39:02 +04:00
|
|
|
package etcdserver
|
|
|
|
|
2014-08-29 03:41:42 +04:00
|
|
|
import (
|
2014-09-27 01:18:59 +04:00
|
|
|
"encoding/json"
|
2014-09-11 04:12:58 +04:00
|
|
|
"fmt"
|
2015-12-30 02:27:20 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-10-22 04:54:21 +04:00
|
|
|
"path"
|
2014-08-29 03:41:42 +04:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-08-30 02:32:41 +04:00
|
|
|
"time"
|
2014-09-03 08:36:14 +04:00
|
|
|
|
|
|
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
2016-04-07 22:02:37 +03:00
|
|
|
"github.com/coreos/etcd/etcdserver/membership"
|
2016-01-08 21:06:33 +03:00
|
|
|
"github.com/coreos/etcd/lease"
|
2016-04-25 22:32:58 +03:00
|
|
|
"github.com/coreos/etcd/mvcc"
|
|
|
|
"github.com/coreos/etcd/mvcc/backend"
|
2015-01-13 22:19:27 +03:00
|
|
|
"github.com/coreos/etcd/pkg/idutil"
|
2016-03-16 01:07:18 +03:00
|
|
|
"github.com/coreos/etcd/pkg/mock/mockstorage"
|
|
|
|
"github.com/coreos/etcd/pkg/mock/mockstore"
|
|
|
|
"github.com/coreos/etcd/pkg/mock/mockwait"
|
2014-11-14 11:10:46 +03:00
|
|
|
"github.com/coreos/etcd/pkg/pbutil"
|
2014-10-27 19:46:37 +03:00
|
|
|
"github.com/coreos/etcd/pkg/testutil"
|
2014-10-31 03:31:14 +03:00
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2015-12-24 02:51:00 +03:00
|
|
|
"github.com/coreos/etcd/pkg/wait"
|
2014-08-29 03:41:42 +04:00
|
|
|
"github.com/coreos/etcd/raft"
|
|
|
|
"github.com/coreos/etcd/raft/raftpb"
|
2016-01-04 22:24:49 +03:00
|
|
|
"github.com/coreos/etcd/rafthttp"
|
2014-08-29 03:41:42 +04:00
|
|
|
"github.com/coreos/etcd/store"
|
2016-03-23 03:10:28 +03:00
|
|
|
"golang.org/x/net/context"
|
2014-08-29 03:41:42 +04:00
|
|
|
)
|
|
|
|
|
2014-09-11 04:12:58 +04:00
|
|
|
// TestDoLocalAction tests requests which do not need to go through raft to be applied,
|
|
|
|
// and are served through local data.
|
|
|
|
func TestDoLocalAction(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
req pb.Request
|
|
|
|
|
2014-09-24 21:46:22 +04:00
|
|
|
wresp Response
|
|
|
|
werr error
|
2015-01-02 22:21:23 +03:00
|
|
|
wactions []testutil.Action
|
2014-09-11 04:12:58 +04:00
|
|
|
}{
|
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "GET", ID: 1, Wait: true},
|
2016-01-04 22:24:49 +03:00
|
|
|
Response{Watcher: store.NewNopWatcher()}, nil, []testutil.Action{{Name: "Watch"}},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "GET", ID: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}}, nil,
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Get",
|
|
|
|
Params: []interface{}{"", false, false},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-11-04 09:01:07 +03:00
|
|
|
{
|
|
|
|
pb.Request{Method: "HEAD", ID: 1},
|
|
|
|
Response{Event: &store.Event{}}, nil,
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Get",
|
|
|
|
Params: []interface{}{"", false, false},
|
2014-11-04 09:01:07 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "BADMETHOD", ID: 1},
|
2015-01-02 22:21:23 +03:00
|
|
|
Response{}, ErrUnknownMethod, []testutil.Action{},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2016-03-16 01:07:18 +03:00
|
|
|
st := mockstore.NewRecorder()
|
2014-12-16 03:07:47 +03:00
|
|
|
srv := &EtcdServer{
|
|
|
|
store: st,
|
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
|
|
|
}
|
2014-09-11 04:12:58 +04:00
|
|
|
resp, err := srv.Do(context.TODO(), tt.req)
|
|
|
|
|
|
|
|
if err != tt.werr {
|
|
|
|
t.Fatalf("#%d: err = %+v, want %+v", i, err, tt.werr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp, tt.wresp) {
|
|
|
|
t.Errorf("#%d: resp = %+v, want %+v", i, resp, tt.wresp)
|
|
|
|
}
|
2014-09-24 21:46:22 +04:00
|
|
|
gaction := st.Action()
|
|
|
|
if !reflect.DeepEqual(gaction, tt.wactions) {
|
|
|
|
t.Errorf("#%d: action = %+v, want %+v", i, gaction, tt.wactions)
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 13:04:16 +04:00
|
|
|
// TestDoBadLocalAction tests server requests which do not need to go through consensus,
|
|
|
|
// and return errors when they fetch from local data.
|
|
|
|
func TestDoBadLocalAction(t *testing.T) {
|
|
|
|
storeErr := fmt.Errorf("bah")
|
|
|
|
tests := []struct {
|
|
|
|
req pb.Request
|
|
|
|
|
2015-01-02 22:21:23 +03:00
|
|
|
wactions []testutil.Action
|
2014-09-18 13:04:16 +04:00
|
|
|
}{
|
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "GET", ID: 1, Wait: true},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{{Name: "Watch"}},
|
2014-09-18 13:04:16 +04:00
|
|
|
},
|
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "GET", ID: 1},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Get",
|
|
|
|
Params: []interface{}{"", false, false},
|
|
|
|
},
|
|
|
|
},
|
2014-09-18 13:04:16 +04:00
|
|
|
},
|
2014-11-04 09:01:07 +03:00
|
|
|
{
|
|
|
|
pb.Request{Method: "HEAD", ID: 1},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Get",
|
|
|
|
Params: []interface{}{"", false, false},
|
|
|
|
},
|
|
|
|
},
|
2014-11-04 09:01:07 +03:00
|
|
|
},
|
2014-09-18 13:04:16 +04:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2016-03-16 01:07:18 +03:00
|
|
|
st := mockstore.NewErrRecorder(storeErr)
|
2014-12-16 03:07:47 +03:00
|
|
|
srv := &EtcdServer{
|
|
|
|
store: st,
|
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
|
|
|
}
|
2014-09-18 13:04:16 +04:00
|
|
|
resp, err := srv.Do(context.Background(), tt.req)
|
|
|
|
|
|
|
|
if err != storeErr {
|
|
|
|
t.Fatalf("#%d: err = %+v, want %+v", i, err, storeErr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(resp, Response{}) {
|
|
|
|
t.Errorf("#%d: resp = %+v, want %+v", i, resp, Response{})
|
|
|
|
}
|
2014-09-24 21:46:22 +04:00
|
|
|
gaction := st.Action()
|
|
|
|
if !reflect.DeepEqual(gaction, tt.wactions) {
|
|
|
|
t.Errorf("#%d: action = %+v, want %+v", i, gaction, tt.wactions)
|
2014-09-18 13:04:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:19:39 +03:00
|
|
|
// TestApplyRepeat tests that server handles repeat raft messages gracefully
|
|
|
|
func TestApplyRepeat(t *testing.T) {
|
2016-01-29 03:29:39 +03:00
|
|
|
n := newNodeConfChangeCommitterStream()
|
2016-01-26 21:19:39 +03:00
|
|
|
n.readyc <- raft.Ready{
|
|
|
|
SoftState: &raft.SoftState{RaftState: raft.StateLeader},
|
|
|
|
}
|
|
|
|
cl := newTestCluster(nil)
|
|
|
|
st := store.New()
|
|
|
|
cl.SetStore(store.New())
|
2016-04-07 22:02:37 +03:00
|
|
|
cl.AddMember(&membership.Member{ID: 1234})
|
2016-01-26 21:19:39 +03:00
|
|
|
s := &EtcdServer{
|
|
|
|
r: raftNode{
|
|
|
|
Node: n,
|
|
|
|
raftStorage: raft.NewMemoryStorage(),
|
2016-03-16 01:07:18 +03:00
|
|
|
storage: mockstorage.NewStorageRecorder(""),
|
2016-01-26 21:19:39 +03:00
|
|
|
transport: rafthttp.NewNopTransporter(),
|
|
|
|
},
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{},
|
2016-01-26 21:19:39 +03:00
|
|
|
store: st,
|
|
|
|
cluster: cl,
|
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
s.applyV2 = &applierV2store{store: s.store, cluster: s.cluster}
|
2016-01-26 21:19:39 +03:00
|
|
|
s.start()
|
|
|
|
req := &pb.Request{Method: "QGET", ID: uint64(1)}
|
|
|
|
ents := []raftpb.Entry{{Index: 1, Data: pbutil.MustMarshal(req)}}
|
|
|
|
n.readyc <- raft.Ready{CommittedEntries: ents}
|
|
|
|
// dup msg
|
|
|
|
n.readyc <- raft.Ready{CommittedEntries: ents}
|
|
|
|
|
|
|
|
// use a conf change to block until dup msgs are all processed
|
|
|
|
cc := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
|
|
|
|
ents = []raftpb.Entry{{
|
|
|
|
Index: 2,
|
|
|
|
Type: raftpb.EntryConfChange,
|
|
|
|
Data: pbutil.MustMarshal(cc),
|
|
|
|
}}
|
|
|
|
n.readyc <- raft.Ready{CommittedEntries: ents}
|
2016-01-29 03:29:39 +03:00
|
|
|
// wait for conf change message
|
2016-01-26 21:19:39 +03:00
|
|
|
act, err := n.Wait(1)
|
2016-01-29 03:29:39 +03:00
|
|
|
// wait for stop message (async to avoid deadlock)
|
|
|
|
stopc := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, werr := n.Wait(1)
|
|
|
|
stopc <- werr
|
|
|
|
}()
|
2016-01-26 21:19:39 +03:00
|
|
|
s.Stop()
|
|
|
|
|
|
|
|
// only want to confirm etcdserver won't panic; no data to check
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(act) == 0 {
|
|
|
|
t.Fatalf("expected len(act)=0, got %d", len(act))
|
|
|
|
}
|
2016-01-29 03:29:39 +03:00
|
|
|
|
|
|
|
if err = <-stopc; err != nil {
|
|
|
|
t.Fatalf("error on stop (%v)", err)
|
|
|
|
}
|
2016-01-26 21:19:39 +03:00
|
|
|
}
|
|
|
|
|
2014-10-01 02:14:44 +04:00
|
|
|
func TestApplyRequest(t *testing.T) {
|
2014-09-11 04:12:58 +04:00
|
|
|
tests := []struct {
|
|
|
|
req pb.Request
|
|
|
|
|
2014-09-24 21:46:22 +04:00
|
|
|
wresp Response
|
2015-01-02 22:21:23 +03:00
|
|
|
wactions []testutil.Action
|
2014-09-11 04:12:58 +04:00
|
|
|
}{
|
2014-09-24 21:46:22 +04:00
|
|
|
// POST ==> Create
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "POST", ID: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Create",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", false, "", true, store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-25 02:46:40 +04:00
|
|
|
// POST ==> Create, with expiration
|
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "POST", ID: 1, Expiration: 1337},
|
2014-09-25 02:46:40 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Create",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", false, "", true, store.TTLOptionSet{ExpireTime: time.Unix(0, 1337)}},
|
2014-09-25 02:46:40 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-09-25 02:57:06 +04:00
|
|
|
// POST ==> Create, with dir
|
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "POST", ID: 1, Dir: true},
|
2014-09-25 02:57:06 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Create",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", true, "", true, store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-25 02:57:06 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// PUT ==> Set
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "PUT", ID: 1},
|
2014-09-25 02:57:06 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Set",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", false, "", store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-25 02:57:06 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// PUT ==> Set, with dir
|
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "PUT", ID: 1, Dir: true},
|
2014-09-25 02:57:06 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Set",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", true, "", store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-25 02:57:06 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// PUT with PrevExist=true ==> Update
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-12-31 01:51:26 +03:00
|
|
|
pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(true)},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Update",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", "", store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// PUT with PrevExist=false ==> Create
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-12-31 01:51:26 +03:00
|
|
|
pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(false)},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Create",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", false, "", false, store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2015-04-15 08:18:04 +03:00
|
|
|
// PUT with PrevExist=true *and* PrevIndex set ==> CompareAndSwap
|
2014-09-24 21:46:22 +04:00
|
|
|
{
|
2014-12-31 01:51:26 +03:00
|
|
|
pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(true), PrevIndex: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
2015-04-15 08:18:04 +03:00
|
|
|
Name: "CompareAndSwap",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", "", uint64(1), "", store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// PUT with PrevExist=false *and* PrevIndex set ==> Create
|
|
|
|
{
|
2014-12-31 01:51:26 +03:00
|
|
|
pb.Request{Method: "PUT", ID: 1, PrevExist: pbutil.Boolp(false), PrevIndex: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Create",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", false, "", false, store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// PUT with PrevIndex set ==> CompareAndSwap
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "PUT", ID: 1, PrevIndex: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "CompareAndSwap",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", "", uint64(1), "", store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// PUT with PrevValue set ==> CompareAndSwap
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "PUT", ID: 1, PrevValue: "bar"},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "CompareAndSwap",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", "bar", uint64(0), "", store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// PUT with PrevIndex and PrevValue set ==> CompareAndSwap
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "PUT", ID: 1, PrevIndex: 1, PrevValue: "bar"},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "CompareAndSwap",
|
2016-01-07 23:20:44 +03:00
|
|
|
Params: []interface{}{"", "bar", uint64(1), "", store.TTLOptionSet{ExpireTime: time.Time{}}},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// DELETE ==> Delete
|
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "DELETE", ID: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Delete",
|
|
|
|
Params: []interface{}{"", false, false},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// DELETE with PrevIndex set ==> CompareAndDelete
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "DELETE", ID: 1, PrevIndex: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "CompareAndDelete",
|
|
|
|
Params: []interface{}{"", "", uint64(1)},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// DELETE with PrevValue set ==> CompareAndDelete
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "DELETE", ID: 1, PrevValue: "bar"},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "CompareAndDelete",
|
|
|
|
Params: []interface{}{"", "bar", uint64(0)},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// DELETE with PrevIndex *and* PrevValue set ==> CompareAndDelete
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "DELETE", ID: 1, PrevIndex: 5, PrevValue: "bar"},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "CompareAndDelete",
|
|
|
|
Params: []interface{}{"", "bar", uint64(5)},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// QGET ==> Get
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "QGET", ID: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{Event: &store.Event{}},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "Get",
|
|
|
|
Params: []interface{}{"", false, false},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
// SYNC ==> DeleteExpiredKeys
|
2014-09-16 04:35:02 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "SYNC", ID: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "DeleteExpiredKeys",
|
|
|
|
Params: []interface{}{time.Unix(0, 0)},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
2014-09-16 04:35:02 +04:00
|
|
|
},
|
2014-09-24 21:46:22 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "SYNC", ID: 1, Time: 12345},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{
|
|
|
|
{
|
|
|
|
Name: "DeleteExpiredKeys",
|
|
|
|
Params: []interface{}{time.Unix(0, 12345)},
|
2014-09-24 21:46:22 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Unknown method - error
|
2014-09-11 04:12:58 +04:00
|
|
|
{
|
2014-09-23 21:17:53 +04:00
|
|
|
pb.Request{Method: "BADMETHOD", ID: 1},
|
2014-09-24 21:46:22 +04:00
|
|
|
Response{err: ErrUnknownMethod},
|
2015-01-02 22:21:23 +03:00
|
|
|
[]testutil.Action{},
|
2014-09-11 04:12:58 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2016-03-16 01:07:18 +03:00
|
|
|
st := mockstore.NewRecorder()
|
2014-10-03 22:05:56 +04:00
|
|
|
srv := &EtcdServer{store: st}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
resp := srv.applyV2Request(&tt.req)
|
2014-09-11 04:12:58 +04:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(resp, tt.wresp) {
|
|
|
|
t.Errorf("#%d: resp = %+v, want %+v", i, resp, tt.wresp)
|
|
|
|
}
|
2014-09-24 21:46:22 +04:00
|
|
|
gaction := st.Action()
|
|
|
|
if !reflect.DeepEqual(gaction, tt.wactions) {
|
|
|
|
t.Errorf("#%d: action = %#v, want %#v", i, gaction, tt.wactions)
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 12:13:47 +04:00
|
|
|
func TestApplyRequestOnAdminMemberAttributes(t *testing.T) {
|
2016-04-07 22:02:37 +03:00
|
|
|
cl := newTestCluster([]*membership.Member{{ID: 1}})
|
2014-10-24 12:13:47 +04:00
|
|
|
srv := &EtcdServer{
|
2016-03-16 01:07:18 +03:00
|
|
|
store: mockstore.NewRecorder(),
|
2015-05-13 03:22:06 +03:00
|
|
|
cluster: cl,
|
2014-10-24 12:13:47 +04:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2014-10-24 12:13:47 +04:00
|
|
|
req := pb.Request{
|
|
|
|
Method: "PUT",
|
|
|
|
ID: 1,
|
2016-04-08 05:12:00 +03:00
|
|
|
Path: membership.MemberAttributesStorePath(1),
|
2015-04-06 18:38:13 +03:00
|
|
|
Val: `{"Name":"abc","ClientURLs":["http://127.0.0.1:2379"]}`,
|
2014-10-24 12:13:47 +04:00
|
|
|
}
|
2016-04-20 10:31:58 +03:00
|
|
|
srv.applyV2Request(&req)
|
2016-04-07 22:02:37 +03:00
|
|
|
w := membership.Attributes{Name: "abc", ClientURLs: []string{"http://127.0.0.1:2379"}}
|
2014-10-24 12:13:47 +04:00
|
|
|
if g := cl.Member(1).Attributes; !reflect.DeepEqual(g, w) {
|
|
|
|
t.Errorf("attributes = %v, want %v", g, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 20:49:26 +04:00
|
|
|
func TestApplyConfChangeError(t *testing.T) {
|
2016-04-07 22:02:37 +03:00
|
|
|
cl := membership.NewCluster("")
|
2014-11-06 04:16:42 +03:00
|
|
|
cl.SetStore(store.New())
|
|
|
|
for i := 1; i <= 4; i++ {
|
2016-04-07 22:02:37 +03:00
|
|
|
cl.AddMember(&membership.Member{ID: types.ID(i)})
|
2014-11-06 04:16:42 +03:00
|
|
|
}
|
2015-04-19 21:08:03 +03:00
|
|
|
cl.RemoveMember(4)
|
2014-11-06 04:16:42 +03:00
|
|
|
|
2014-10-16 20:49:26 +04:00
|
|
|
tests := []struct {
|
|
|
|
cc raftpb.ConfChange
|
|
|
|
werr error
|
|
|
|
}{
|
2014-10-21 22:04:42 +04:00
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeAddNode,
|
|
|
|
NodeID: 4,
|
|
|
|
},
|
2016-04-07 22:02:37 +03:00
|
|
|
membership.ErrIDRemoved,
|
2014-10-21 22:04:42 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
2014-11-11 22:46:49 +03:00
|
|
|
Type: raftpb.ConfChangeUpdateNode,
|
2014-10-21 22:04:42 +04:00
|
|
|
NodeID: 4,
|
|
|
|
},
|
2016-04-07 22:02:37 +03:00
|
|
|
membership.ErrIDRemoved,
|
2014-10-21 22:04:42 +04:00
|
|
|
},
|
2014-10-16 20:49:26 +04:00
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeAddNode,
|
|
|
|
NodeID: 1,
|
|
|
|
},
|
2016-04-07 22:02:37 +03:00
|
|
|
membership.ErrIDExists,
|
2014-10-16 20:49:26 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeRemoveNode,
|
|
|
|
NodeID: 5,
|
|
|
|
},
|
2016-04-07 22:02:37 +03:00
|
|
|
membership.ErrIDNotFound,
|
2014-10-16 20:49:26 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2016-01-04 22:24:49 +03:00
|
|
|
n := newNodeRecorder()
|
2014-10-16 20:49:26 +04:00
|
|
|
srv := &EtcdServer{
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{Node: n},
|
2015-05-13 03:22:06 +03:00
|
|
|
cluster: cl,
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{},
|
2014-10-16 20:49:26 +04:00
|
|
|
}
|
2015-04-19 21:08:03 +03:00
|
|
|
_, err := srv.applyConfChange(tt.cc, nil)
|
2014-10-16 20:49:26 +04:00
|
|
|
if err != tt.werr {
|
|
|
|
t.Errorf("#%d: applyConfChange error = %v, want %v", i, err, tt.werr)
|
|
|
|
}
|
|
|
|
cc := raftpb.ConfChange{Type: tt.cc.Type, NodeID: raft.None}
|
2015-01-02 22:21:23 +03:00
|
|
|
w := []testutil.Action{
|
2014-10-16 20:49:26 +04:00
|
|
|
{
|
2015-01-02 22:21:23 +03:00
|
|
|
Name: "ApplyConfChange",
|
|
|
|
Params: []interface{}{cc},
|
2014-10-16 20:49:26 +04:00
|
|
|
},
|
|
|
|
}
|
2016-01-04 22:39:02 +03:00
|
|
|
if g, _ := n.Wait(1); !reflect.DeepEqual(g, w) {
|
2014-10-16 20:49:26 +04:00
|
|
|
t.Errorf("#%d: action = %+v, want %+v", i, g, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:03:34 +03:00
|
|
|
func TestApplyConfChangeShouldStop(t *testing.T) {
|
2016-04-07 22:02:37 +03:00
|
|
|
cl := membership.NewCluster("")
|
2014-11-14 10:03:34 +03:00
|
|
|
cl.SetStore(store.New())
|
|
|
|
for i := 1; i <= 3; i++ {
|
2016-04-07 22:02:37 +03:00
|
|
|
cl.AddMember(&membership.Member{ID: types.ID(i)})
|
2014-11-14 10:03:34 +03:00
|
|
|
}
|
|
|
|
srv := &EtcdServer{
|
2015-01-16 01:39:30 +03:00
|
|
|
id: 1,
|
|
|
|
r: raftNode{
|
2016-01-04 22:24:49 +03:00
|
|
|
Node: newNodeNop(),
|
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2015-01-16 01:39:30 +03:00
|
|
|
},
|
2015-05-13 03:22:06 +03:00
|
|
|
cluster: cl,
|
2014-11-14 10:03:34 +03:00
|
|
|
}
|
|
|
|
cc := raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeRemoveNode,
|
|
|
|
NodeID: 2,
|
|
|
|
}
|
|
|
|
// remove non-local member
|
2015-04-19 21:08:03 +03:00
|
|
|
shouldStop, err := srv.applyConfChange(cc, &raftpb.ConfState{})
|
2014-11-14 10:03:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
|
|
|
}
|
2016-04-02 22:55:11 +03:00
|
|
|
if shouldStop {
|
2014-11-14 10:03:34 +03:00
|
|
|
t.Errorf("shouldStop = %t, want %t", shouldStop, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove local member
|
|
|
|
cc.NodeID = 1
|
2015-04-19 21:08:03 +03:00
|
|
|
shouldStop, err = srv.applyConfChange(cc, &raftpb.ConfState{})
|
2014-11-14 10:03:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error %v", err)
|
|
|
|
}
|
2016-04-02 22:55:11 +03:00
|
|
|
if !shouldStop {
|
2014-11-14 10:03:34 +03:00
|
|
|
t.Errorf("shouldStop = %t, want %t", shouldStop, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 02:51:00 +03:00
|
|
|
// TestApplyMultiConfChangeShouldStop ensures that apply will return shouldStop
|
|
|
|
// if the local member is removed along with other conf updates.
|
|
|
|
func TestApplyMultiConfChangeShouldStop(t *testing.T) {
|
2016-04-07 22:02:37 +03:00
|
|
|
cl := membership.NewCluster("")
|
2015-12-24 02:51:00 +03:00
|
|
|
cl.SetStore(store.New())
|
|
|
|
for i := 1; i <= 5; i++ {
|
2016-04-07 22:02:37 +03:00
|
|
|
cl.AddMember(&membership.Member{ID: types.ID(i)})
|
2015-12-24 02:51:00 +03:00
|
|
|
}
|
|
|
|
srv := &EtcdServer{
|
|
|
|
id: 2,
|
|
|
|
r: raftNode{
|
2016-01-04 22:24:49 +03:00
|
|
|
Node: newNodeNop(),
|
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2015-12-24 02:51:00 +03:00
|
|
|
},
|
|
|
|
cluster: cl,
|
|
|
|
w: wait.New(),
|
|
|
|
}
|
|
|
|
ents := []raftpb.Entry{}
|
|
|
|
for i := 1; i <= 4; i++ {
|
|
|
|
ent := raftpb.Entry{
|
|
|
|
Term: 1,
|
|
|
|
Index: uint64(i),
|
|
|
|
Type: raftpb.EntryConfChange,
|
|
|
|
Data: pbutil.MustMarshal(
|
|
|
|
&raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeRemoveNode,
|
|
|
|
NodeID: uint64(i)}),
|
|
|
|
}
|
|
|
|
ents = append(ents, ent)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, shouldStop := srv.apply(ents, &raftpb.ConfState{})
|
2016-04-02 22:55:11 +03:00
|
|
|
if !shouldStop {
|
2015-12-24 02:51:00 +03:00
|
|
|
t.Errorf("shouldStop = %t, want %t", shouldStop, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 04:12:58 +04:00
|
|
|
func TestDoProposal(t *testing.T) {
|
|
|
|
tests := []pb.Request{
|
2015-08-22 04:52:16 +03:00
|
|
|
{Method: "POST", ID: 1},
|
|
|
|
{Method: "PUT", ID: 1},
|
|
|
|
{Method: "DELETE", ID: 1},
|
|
|
|
{Method: "GET", ID: 1, Quorum: true},
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2016-03-16 01:07:18 +03:00
|
|
|
st := mockstore.NewRecorder()
|
2014-09-12 04:14:41 +04:00
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{
|
|
|
|
Node: newNodeCommitter(),
|
2016-03-16 01:07:18 +03:00
|
|
|
storage: mockstorage.NewStorageRecorder(""),
|
2015-12-08 18:52:54 +03:00
|
|
|
raftStorage: raft.NewMemoryStorage(),
|
2016-01-04 22:24:49 +03:00
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2015-01-16 01:39:30 +03:00
|
|
|
},
|
|
|
|
store: st,
|
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2014-09-29 22:52:36 +04:00
|
|
|
srv.start()
|
2014-12-30 11:16:31 +03:00
|
|
|
resp, err := srv.Do(context.Background(), tt)
|
2014-09-11 04:12:58 +04:00
|
|
|
srv.Stop()
|
|
|
|
|
2014-09-18 00:12:56 +04:00
|
|
|
action := st.Action()
|
|
|
|
if len(action) != 1 {
|
|
|
|
t.Errorf("#%d: len(action) = %d, want 1", i, len(action))
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("#%d: err = %v, want nil", i, err)
|
|
|
|
}
|
|
|
|
wresp := Response{Event: &store.Event{}}
|
|
|
|
if !reflect.DeepEqual(resp, wresp) {
|
|
|
|
t.Errorf("#%d: resp = %v, want %v", i, resp, wresp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoProposalCancelled(t *testing.T) {
|
2016-04-15 09:56:26 +03:00
|
|
|
wt := mockwait.NewRecorder()
|
2014-09-12 04:14:41 +04:00
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2016-01-04 22:24:49 +03:00
|
|
|
r: raftNode{Node: newNodeNop()},
|
2016-04-15 09:56:26 +03:00
|
|
|
w: wt,
|
2014-12-30 11:16:31 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2014-12-30 11:16:31 +03:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2014-09-11 04:12:58 +04:00
|
|
|
cancel()
|
2014-12-30 11:16:31 +03:00
|
|
|
_, err := srv.Do(ctx, pb.Request{Method: "PUT"})
|
2014-09-11 04:12:58 +04:00
|
|
|
|
2014-10-26 21:35:59 +03:00
|
|
|
if err != ErrCanceled {
|
|
|
|
t.Fatalf("err = %v, want %v", err, ErrCanceled)
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
2015-01-02 22:21:23 +03:00
|
|
|
w := []testutil.Action{{Name: "Register"}, {Name: "Trigger"}}
|
2016-04-15 09:56:26 +03:00
|
|
|
if !reflect.DeepEqual(wt.Action(), w) {
|
|
|
|
t.Errorf("wt.action = %+v, want %+v", wt.Action(), w)
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 21:35:59 +03:00
|
|
|
func TestDoProposalTimeout(t *testing.T) {
|
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2016-01-04 22:24:49 +03:00
|
|
|
r: raftNode{Node: newNodeNop()},
|
2016-03-16 01:07:18 +03:00
|
|
|
w: mockwait.NewNop(),
|
2014-12-16 03:07:47 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-10-26 21:35:59 +03:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2014-12-30 11:16:31 +03:00
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 0)
|
|
|
|
_, err := srv.Do(ctx, pb.Request{Method: "PUT"})
|
2014-10-26 21:35:59 +03:00
|
|
|
if err != ErrTimeout {
|
|
|
|
t.Fatalf("err = %v, want %v", err, ErrTimeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 04:12:58 +04:00
|
|
|
func TestDoProposalStopped(t *testing.T) {
|
2014-09-12 04:14:41 +04:00
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2016-01-04 22:24:49 +03:00
|
|
|
r: raftNode{Node: newNodeNop()},
|
2016-03-16 01:07:18 +03:00
|
|
|
w: mockwait.NewNop(),
|
2014-12-30 11:16:31 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-11 04:12:58 +04:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2016-09-14 01:46:05 +03:00
|
|
|
srv.stopping = make(chan struct{})
|
|
|
|
close(srv.stopping)
|
2014-12-30 11:16:31 +03:00
|
|
|
_, err := srv.Do(context.Background(), pb.Request{Method: "PUT", ID: 1})
|
2014-09-11 04:12:58 +04:00
|
|
|
if err != ErrStopped {
|
|
|
|
t.Errorf("err = %v, want %v", err, ErrStopped)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 11:16:31 +03:00
|
|
|
// TestSync tests sync 1. is nonblocking 2. proposes SYNC request.
|
2014-09-16 04:35:02 +04:00
|
|
|
func TestSync(t *testing.T) {
|
2016-01-04 22:24:49 +03:00
|
|
|
n := newNodeRecorder()
|
2014-09-16 04:35:02 +04:00
|
|
|
srv := &EtcdServer{
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{Node: n},
|
2014-12-16 03:07:47 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2014-09-16 04:35:02 +04:00
|
|
|
// check that sync is non-blocking
|
2015-10-06 05:37:08 +03:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
srv.sync(10 * time.Second)
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("sync should be non-blocking but did not return after 1s!")
|
|
|
|
}
|
|
|
|
|
2016-01-04 22:39:02 +03:00
|
|
|
action, _ := n.Wait(1)
|
2014-12-30 11:16:31 +03:00
|
|
|
if len(action) != 1 {
|
|
|
|
t.Fatalf("len(action) = %d, want 1", len(action))
|
|
|
|
}
|
2015-01-02 22:21:23 +03:00
|
|
|
if action[0].Name != "Propose" {
|
|
|
|
t.Fatalf("action = %s, want Propose", action[0].Name)
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
2015-01-02 22:21:23 +03:00
|
|
|
data := action[0].Params[0].([]byte)
|
2014-09-18 07:54:14 +04:00
|
|
|
var r pb.Request
|
2014-12-30 11:16:31 +03:00
|
|
|
if err := r.Unmarshal(data); err != nil {
|
2014-09-18 07:54:14 +04:00
|
|
|
t.Fatalf("unmarshal request error: %v", err)
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
2014-09-18 07:54:14 +04:00
|
|
|
if r.Method != "SYNC" {
|
|
|
|
t.Errorf("method = %s, want SYNC", r.Method)
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 07:54:14 +04:00
|
|
|
// TestSyncTimeout tests the case that sync 1. is non-blocking 2. cancel request
|
|
|
|
// after timeout
|
|
|
|
func TestSyncTimeout(t *testing.T) {
|
2016-01-04 22:24:49 +03:00
|
|
|
n := newProposalBlockerRecorder()
|
2014-09-16 04:35:02 +04:00
|
|
|
srv := &EtcdServer{
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{Node: n},
|
2014-12-16 03:07:47 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2014-09-16 04:35:02 +04:00
|
|
|
// check that sync is non-blocking
|
2015-10-06 05:37:08 +03:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
srv.sync(0)
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("sync should be non-blocking but did not return after 1s!")
|
|
|
|
}
|
2014-09-16 04:35:02 +04:00
|
|
|
|
2015-01-02 22:21:23 +03:00
|
|
|
w := []testutil.Action{{Name: "Propose blocked"}}
|
2016-01-04 22:39:02 +03:00
|
|
|
if g, _ := n.Wait(1); !reflect.DeepEqual(g, w) {
|
2014-09-18 07:54:14 +04:00
|
|
|
t.Errorf("action = %v, want %v", g, w)
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-18 07:54:14 +04:00
|
|
|
// TODO: TestNoSyncWhenNoLeader
|
|
|
|
|
2014-09-23 05:30:51 +04:00
|
|
|
// TestSyncTrigger tests that the server proposes a SYNC request when its sync timer ticks
|
|
|
|
func TestSyncTrigger(t *testing.T) {
|
2014-12-30 11:16:31 +03:00
|
|
|
n := newReadyNode()
|
2014-09-23 05:30:51 +04:00
|
|
|
st := make(chan time.Time, 1)
|
2014-09-16 04:35:02 +04:00
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{
|
|
|
|
Node: n,
|
2015-12-08 18:52:54 +03:00
|
|
|
raftStorage: raft.NewMemoryStorage(),
|
2016-01-04 22:24:49 +03:00
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2016-03-16 01:07:18 +03:00
|
|
|
storage: mockstorage.NewStorageRecorder(""),
|
2015-01-16 01:39:30 +03:00
|
|
|
},
|
2016-03-16 01:07:18 +03:00
|
|
|
store: mockstore.NewNop(),
|
2015-01-16 01:39:30 +03:00
|
|
|
SyncTicker: st,
|
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
2016-01-04 22:39:02 +03:00
|
|
|
|
2014-09-23 05:30:51 +04:00
|
|
|
// trigger the server to become a leader and accept sync requests
|
2016-01-04 22:39:02 +03:00
|
|
|
go func() {
|
|
|
|
srv.start()
|
|
|
|
n.readyc <- raft.Ready{
|
|
|
|
SoftState: &raft.SoftState{
|
|
|
|
RaftState: raft.StateLeader,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// trigger a sync request
|
|
|
|
st <- time.Time{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
action, _ := n.Wait(1)
|
|
|
|
go srv.Stop()
|
2014-12-30 11:16:31 +03:00
|
|
|
|
|
|
|
if len(action) != 1 {
|
|
|
|
t.Fatalf("len(action) = %d, want 1", len(action))
|
2014-09-23 05:30:51 +04:00
|
|
|
}
|
2015-01-02 22:21:23 +03:00
|
|
|
if action[0].Name != "Propose" {
|
|
|
|
t.Fatalf("action = %s, want Propose", action[0].Name)
|
2014-12-30 11:16:31 +03:00
|
|
|
}
|
2015-01-02 22:21:23 +03:00
|
|
|
data := action[0].Params[0].([]byte)
|
2014-09-23 05:30:51 +04:00
|
|
|
var req pb.Request
|
|
|
|
if err := req.Unmarshal(data); err != nil {
|
|
|
|
t.Fatalf("error unmarshalling data: %v", err)
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
2014-09-23 05:30:51 +04:00
|
|
|
if req.Method != "SYNC" {
|
|
|
|
t.Fatalf("unexpected proposed request: %#v", req.Method)
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
2016-01-04 22:39:02 +03:00
|
|
|
|
|
|
|
// wait on stop message
|
|
|
|
<-n.Chan()
|
2014-09-16 04:35:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 05:18:45 +04:00
|
|
|
// snapshot should snapshot the store and cut the persistent
|
|
|
|
func TestSnapshot(t *testing.T) {
|
2016-03-24 02:39:15 +03:00
|
|
|
be, tmpPath := backend.NewDefaultTmpBackend()
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(tmpPath)
|
|
|
|
}()
|
|
|
|
|
2015-12-08 18:52:54 +03:00
|
|
|
s := raft.NewMemoryStorage()
|
2014-12-30 11:16:31 +03:00
|
|
|
s.Append([]raftpb.Entry{{Index: 1}})
|
2016-07-28 01:37:27 +03:00
|
|
|
st := mockstore.NewRecorderStream()
|
2016-07-06 19:56:20 +03:00
|
|
|
p := mockstorage.NewStorageRecorderStream("")
|
2014-11-03 23:04:24 +03:00
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{},
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{
|
2016-01-04 22:24:49 +03:00
|
|
|
Node: newNodeNop(),
|
2015-01-16 01:39:30 +03:00
|
|
|
raftStorage: s,
|
|
|
|
storage: p,
|
|
|
|
},
|
|
|
|
store: st,
|
2014-09-17 05:18:45 +04:00
|
|
|
}
|
2016-04-25 22:32:58 +03:00
|
|
|
srv.kv = mvcc.New(be, &lease.FakeLessor{}, &srv.consistIndex)
|
2016-03-24 02:39:15 +03:00
|
|
|
srv.be = be
|
|
|
|
|
2016-07-28 01:37:27 +03:00
|
|
|
ch := make(chan struct{}, 2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
gaction, _ := p.Wait(1)
|
2016-11-04 00:44:08 +03:00
|
|
|
defer func() { ch <- struct{}{} }()
|
|
|
|
|
2016-07-28 01:37:27 +03:00
|
|
|
if len(gaction) != 1 {
|
|
|
|
t.Fatalf("len(action) = %d, want 1", len(gaction))
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gaction[0], testutil.Action{Name: "SaveSnap"}) {
|
|
|
|
t.Errorf("action = %s, want SaveSnap", gaction[0])
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
gaction, _ := st.Wait(2)
|
2016-11-04 00:44:08 +03:00
|
|
|
defer func() { ch <- struct{}{} }()
|
|
|
|
|
2016-07-28 01:37:27 +03:00
|
|
|
if len(gaction) != 2 {
|
|
|
|
t.Fatalf("len(action) = %d, want 2", len(gaction))
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gaction[0], testutil.Action{Name: "Clone"}) {
|
|
|
|
t.Errorf("action = %s, want Clone", gaction[0])
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gaction[1], testutil.Action{Name: "SaveNoCopy"}) {
|
|
|
|
t.Errorf("action = %s, want SaveNoCopy", gaction[1])
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-02-26 23:39:50 +03:00
|
|
|
srv.snapshot(1, raftpb.ConfState{Nodes: []uint64{1}})
|
2016-07-28 01:37:27 +03:00
|
|
|
<-ch
|
|
|
|
<-ch
|
2014-09-17 05:18:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Applied > SnapCount should trigger a SaveSnap event
|
|
|
|
func TestTriggerSnap(t *testing.T) {
|
2016-03-24 02:39:15 +03:00
|
|
|
be, tmpPath := backend.NewDefaultTmpBackend()
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(tmpPath)
|
|
|
|
}()
|
|
|
|
|
2014-12-30 11:16:31 +03:00
|
|
|
snapc := 10
|
2016-03-16 01:07:18 +03:00
|
|
|
st := mockstore.NewRecorder()
|
|
|
|
p := mockstorage.NewStorageRecorderStream("")
|
2014-11-03 23:04:24 +03:00
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2015-03-06 00:03:04 +03:00
|
|
|
snapCount: uint64(snapc),
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{
|
|
|
|
Node: newNodeCommitter(),
|
2015-12-08 18:52:54 +03:00
|
|
|
raftStorage: raft.NewMemoryStorage(),
|
2015-01-16 01:39:30 +03:00
|
|
|
storage: p,
|
2016-01-04 22:24:49 +03:00
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2015-01-16 01:39:30 +03:00
|
|
|
},
|
|
|
|
store: st,
|
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-17 05:18:45 +04:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
srv.applyV2 = &applierV2store{store: srv.store, cluster: srv.cluster}
|
2016-04-20 10:31:58 +03:00
|
|
|
|
2016-04-25 22:32:58 +03:00
|
|
|
srv.kv = mvcc.New(be, &lease.FakeLessor{}, &srv.consistIndex)
|
2016-03-24 02:39:15 +03:00
|
|
|
srv.be = be
|
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
srv.start()
|
2016-02-22 09:01:51 +03:00
|
|
|
|
|
|
|
donec := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wcnt := 2 + snapc
|
|
|
|
gaction, _ := p.Wait(wcnt)
|
|
|
|
|
|
|
|
// each operation is recorded as a Save
|
|
|
|
// (SnapCount+1) * Puts + SaveSnap = (SnapCount+1) * Save + SaveSnap
|
|
|
|
if len(gaction) != wcnt {
|
|
|
|
t.Fatalf("len(action) = %d, want %d", len(gaction), wcnt)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gaction[wcnt-1], testutil.Action{Name: "SaveSnap"}) {
|
|
|
|
t.Errorf("action = %s, want SaveSnap", gaction[wcnt-1])
|
|
|
|
}
|
|
|
|
close(donec)
|
|
|
|
}()
|
|
|
|
|
2014-12-30 11:16:31 +03:00
|
|
|
for i := 0; i < snapc+1; i++ {
|
|
|
|
srv.Do(context.Background(), pb.Request{Method: "PUT"})
|
2014-09-17 05:18:45 +04:00
|
|
|
}
|
2016-01-04 22:39:02 +03:00
|
|
|
|
2016-02-22 09:01:51 +03:00
|
|
|
<-donec
|
2016-10-12 21:51:48 +03:00
|
|
|
srv.Stop()
|
2014-09-17 05:18:45 +04:00
|
|
|
}
|
|
|
|
|
2015-12-30 02:27:20 +03:00
|
|
|
// TestConcurrentApplyAndSnapshotV3 will send out snapshots concurrently with
|
|
|
|
// proposals.
|
|
|
|
func TestConcurrentApplyAndSnapshotV3(t *testing.T) {
|
|
|
|
const (
|
|
|
|
// snapshots that may queue up at once without dropping
|
|
|
|
maxInFlightMsgSnap = 16
|
|
|
|
)
|
2016-01-04 22:39:02 +03:00
|
|
|
n := newNopReadyNode()
|
2016-04-07 22:02:37 +03:00
|
|
|
st := store.New()
|
|
|
|
cl := membership.NewCluster("abc")
|
|
|
|
cl.SetStore(st)
|
2015-12-30 02:27:20 +03:00
|
|
|
|
|
|
|
testdir, err := ioutil.TempDir(os.TempDir(), "testsnapdir")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't open tempdir (%v)", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(testdir)
|
|
|
|
if err := os.MkdirAll(testdir+"/member/snap", 0755); err != nil {
|
|
|
|
t.Fatalf("Couldn't make snap dir (%v)", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rs := raft.NewMemoryStorage()
|
2016-01-04 22:24:49 +03:00
|
|
|
tr, snapDoneC := rafthttp.NewSnapTransporter(testdir)
|
2015-12-30 02:27:20 +03:00
|
|
|
s := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{
|
2015-12-30 02:27:20 +03:00
|
|
|
DataDir: testdir,
|
|
|
|
},
|
|
|
|
r: raftNode{
|
2016-10-27 02:26:00 +03:00
|
|
|
isIDRemoved: func(id uint64) bool { return cl.IsIDRemoved(types.ID(id)) },
|
2015-12-30 02:27:20 +03:00
|
|
|
Node: n,
|
|
|
|
transport: tr,
|
2016-03-16 01:07:18 +03:00
|
|
|
storage: mockstorage.NewStorageRecorder(testdir),
|
2015-12-30 02:27:20 +03:00
|
|
|
raftStorage: rs,
|
2016-10-27 02:26:00 +03:00
|
|
|
msgSnapC: make(chan raftpb.Message, maxInFlightMsgSnap),
|
2015-12-30 02:27:20 +03:00
|
|
|
},
|
2016-10-27 02:26:00 +03:00
|
|
|
store: st,
|
|
|
|
cluster: cl,
|
2015-12-30 02:27:20 +03:00
|
|
|
}
|
2016-05-17 00:40:52 +03:00
|
|
|
s.applyV2 = &applierV2store{store: s.store, cluster: s.cluster}
|
2015-12-30 02:27:20 +03:00
|
|
|
|
2016-01-06 06:45:18 +03:00
|
|
|
be, tmpPath := backend.NewDefaultTmpBackend()
|
|
|
|
defer func() {
|
|
|
|
os.RemoveAll(tmpPath)
|
|
|
|
}()
|
2016-04-25 22:32:58 +03:00
|
|
|
s.kv = mvcc.New(be, &lease.FakeLessor{}, &s.consistIndex)
|
2016-01-07 03:25:25 +03:00
|
|
|
s.be = be
|
2015-12-30 02:27:20 +03:00
|
|
|
|
|
|
|
s.start()
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// submit applied entries and snap entries
|
|
|
|
idx := uint64(0)
|
|
|
|
outdated := 0
|
|
|
|
accepted := 0
|
|
|
|
for k := 1; k <= 101; k++ {
|
|
|
|
idx++
|
|
|
|
ch := s.w.Register(uint64(idx))
|
|
|
|
req := &pb.Request{Method: "QGET", ID: uint64(idx)}
|
|
|
|
ent := raftpb.Entry{Index: uint64(idx), Data: pbutil.MustMarshal(req)}
|
|
|
|
ready := raft.Ready{Entries: []raftpb.Entry{ent}}
|
|
|
|
n.readyc <- ready
|
|
|
|
|
|
|
|
ready = raft.Ready{CommittedEntries: []raftpb.Entry{ent}}
|
|
|
|
n.readyc <- ready
|
|
|
|
|
|
|
|
// "idx" applied
|
|
|
|
<-ch
|
|
|
|
|
|
|
|
// one snapshot for every two messages
|
|
|
|
if k%2 != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
n.readyc <- raft.Ready{Messages: []raftpb.Message{{Type: raftpb.MsgSnap}}}
|
|
|
|
// get the snapshot sent by the transport
|
2016-01-04 22:24:49 +03:00
|
|
|
snapMsg := <-snapDoneC
|
2015-12-30 02:27:20 +03:00
|
|
|
// If the snapshot trails applied records, recovery will panic
|
|
|
|
// since there's no allocated snapshot at the place of the
|
|
|
|
// snapshot record. This only happens when the applier and the
|
|
|
|
// snapshot sender get out of sync.
|
|
|
|
if snapMsg.Snapshot.Metadata.Index == idx {
|
|
|
|
idx++
|
|
|
|
snapMsg.Snapshot.Metadata.Index = idx
|
|
|
|
ready = raft.Ready{Snapshot: snapMsg.Snapshot}
|
|
|
|
n.readyc <- ready
|
|
|
|
accepted++
|
|
|
|
} else {
|
|
|
|
outdated++
|
|
|
|
}
|
|
|
|
// don't wait for the snapshot to complete, move to next message
|
|
|
|
}
|
|
|
|
if accepted != 50 {
|
|
|
|
t.Errorf("accepted=%v, want 50", accepted)
|
|
|
|
}
|
|
|
|
if outdated != 0 {
|
|
|
|
t.Errorf("outdated=%v, want 0", outdated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 02:14:44 +04:00
|
|
|
// TestAddMember tests AddMember can propose and perform node addition.
|
|
|
|
func TestAddMember(t *testing.T) {
|
2014-09-23 23:02:44 +04:00
|
|
|
n := newNodeConfChangeCommitterRecorder()
|
2014-10-16 20:49:26 +04:00
|
|
|
n.readyc <- raft.Ready{
|
2014-12-09 08:17:04 +03:00
|
|
|
SoftState: &raft.SoftState{RaftState: raft.StateLeader},
|
2014-10-16 20:49:26 +04:00
|
|
|
}
|
2014-10-23 03:15:37 +04:00
|
|
|
cl := newTestCluster(nil)
|
2015-01-02 23:22:01 +03:00
|
|
|
st := store.New()
|
|
|
|
cl.SetStore(st)
|
2014-09-19 23:36:19 +04:00
|
|
|
s := &EtcdServer{
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{
|
|
|
|
Node: n,
|
2015-12-08 18:52:54 +03:00
|
|
|
raftStorage: raft.NewMemoryStorage(),
|
2016-03-16 01:07:18 +03:00
|
|
|
storage: mockstorage.NewStorageRecorder(""),
|
2016-01-04 22:24:49 +03:00
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2015-01-16 01:39:30 +03:00
|
|
|
},
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{},
|
2015-01-16 01:39:30 +03:00
|
|
|
store: st,
|
2015-05-13 03:22:06 +03:00
|
|
|
cluster: cl,
|
2015-01-16 01:39:30 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
2014-09-29 22:52:36 +04:00
|
|
|
s.start()
|
2016-04-07 22:02:37 +03:00
|
|
|
m := membership.Member{ID: 1234, RaftAttributes: membership.RaftAttributes{PeerURLs: []string{"foo"}}}
|
2014-10-16 20:49:26 +04:00
|
|
|
err := s.AddMember(context.TODO(), m)
|
2014-09-24 21:46:22 +04:00
|
|
|
gaction := n.Action()
|
2014-09-19 23:36:19 +04:00
|
|
|
s.Stop()
|
|
|
|
|
2014-10-16 20:49:26 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("AddMember error: %v", err)
|
|
|
|
}
|
2015-01-02 22:21:23 +03:00
|
|
|
wactions := []testutil.Action{{Name: "ProposeConfChange:ConfChangeAddNode"}, {Name: "ApplyConfChange:ConfChangeAddNode"}}
|
2014-09-24 21:46:22 +04:00
|
|
|
if !reflect.DeepEqual(gaction, wactions) {
|
|
|
|
t.Errorf("action = %v, want %v", gaction, wactions)
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
2014-10-23 03:15:37 +04:00
|
|
|
if cl.Member(1234) == nil {
|
|
|
|
t.Errorf("member with id 1234 is not added")
|
2014-10-01 02:14:44 +04:00
|
|
|
}
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
|
|
|
|
2014-10-01 02:14:44 +04:00
|
|
|
// TestRemoveMember tests RemoveMember can propose and perform node removal.
|
|
|
|
func TestRemoveMember(t *testing.T) {
|
2014-09-23 23:02:44 +04:00
|
|
|
n := newNodeConfChangeCommitterRecorder()
|
2014-10-16 20:49:26 +04:00
|
|
|
n.readyc <- raft.Ready{
|
2014-12-09 08:17:04 +03:00
|
|
|
SoftState: &raft.SoftState{RaftState: raft.StateLeader},
|
2014-10-16 20:49:26 +04:00
|
|
|
}
|
2014-11-06 03:40:15 +03:00
|
|
|
cl := newTestCluster(nil)
|
2015-01-02 23:22:01 +03:00
|
|
|
st := store.New()
|
2014-11-06 03:40:15 +03:00
|
|
|
cl.SetStore(store.New())
|
2016-04-07 22:02:37 +03:00
|
|
|
cl.AddMember(&membership.Member{ID: 1234})
|
2014-09-19 23:36:19 +04:00
|
|
|
s := &EtcdServer{
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{
|
|
|
|
Node: n,
|
2015-12-08 18:52:54 +03:00
|
|
|
raftStorage: raft.NewMemoryStorage(),
|
2016-03-16 01:07:18 +03:00
|
|
|
storage: mockstorage.NewStorageRecorder(""),
|
2016-01-04 22:24:49 +03:00
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2015-01-16 01:39:30 +03:00
|
|
|
},
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{},
|
2015-01-16 01:39:30 +03:00
|
|
|
store: st,
|
2015-05-13 03:22:06 +03:00
|
|
|
cluster: cl,
|
2015-01-16 01:39:30 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
2014-09-29 22:52:36 +04:00
|
|
|
s.start()
|
2014-10-23 03:15:37 +04:00
|
|
|
err := s.RemoveMember(context.TODO(), 1234)
|
2014-09-24 21:46:22 +04:00
|
|
|
gaction := n.Action()
|
2014-09-19 23:36:19 +04:00
|
|
|
s.Stop()
|
|
|
|
|
2014-10-16 20:49:26 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("RemoveMember error: %v", err)
|
|
|
|
}
|
2015-04-19 21:08:03 +03:00
|
|
|
wactions := []testutil.Action{{Name: "ProposeConfChange:ConfChangeRemoveNode"}, {Name: "ApplyConfChange:ConfChangeRemoveNode"}}
|
2014-09-24 21:46:22 +04:00
|
|
|
if !reflect.DeepEqual(gaction, wactions) {
|
|
|
|
t.Errorf("action = %v, want %v", gaction, wactions)
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
2014-10-23 03:15:37 +04:00
|
|
|
if cl.Member(1234) != nil {
|
|
|
|
t.Errorf("member with id 1234 is not removed")
|
2014-10-01 02:14:44 +04:00
|
|
|
}
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
|
|
|
|
2014-11-11 22:46:49 +03:00
|
|
|
// TestUpdateMember tests RemoveMember can propose and perform node update.
|
|
|
|
func TestUpdateMember(t *testing.T) {
|
|
|
|
n := newNodeConfChangeCommitterRecorder()
|
|
|
|
n.readyc <- raft.Ready{
|
2014-12-09 08:17:04 +03:00
|
|
|
SoftState: &raft.SoftState{RaftState: raft.StateLeader},
|
2014-11-11 22:46:49 +03:00
|
|
|
}
|
2014-11-06 03:40:15 +03:00
|
|
|
cl := newTestCluster(nil)
|
2015-01-02 23:22:01 +03:00
|
|
|
st := store.New()
|
|
|
|
cl.SetStore(st)
|
2016-04-07 22:02:37 +03:00
|
|
|
cl.AddMember(&membership.Member{ID: 1234})
|
2014-11-11 22:46:49 +03:00
|
|
|
s := &EtcdServer{
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{
|
|
|
|
Node: n,
|
2015-12-08 18:52:54 +03:00
|
|
|
raftStorage: raft.NewMemoryStorage(),
|
2016-03-16 01:07:18 +03:00
|
|
|
storage: mockstorage.NewStorageRecorder(""),
|
2016-01-04 22:24:49 +03:00
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2015-01-16 01:39:30 +03:00
|
|
|
},
|
|
|
|
store: st,
|
2015-05-13 03:22:06 +03:00
|
|
|
cluster: cl,
|
2015-01-16 01:39:30 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-11-11 22:46:49 +03:00
|
|
|
}
|
|
|
|
s.start()
|
2016-04-07 22:02:37 +03:00
|
|
|
wm := membership.Member{ID: 1234, RaftAttributes: membership.RaftAttributes{PeerURLs: []string{"http://127.0.0.1:1"}}}
|
2014-11-11 22:46:49 +03:00
|
|
|
err := s.UpdateMember(context.TODO(), wm)
|
|
|
|
gaction := n.Action()
|
|
|
|
s.Stop()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("UpdateMember error: %v", err)
|
|
|
|
}
|
2015-04-19 21:08:03 +03:00
|
|
|
wactions := []testutil.Action{{Name: "ProposeConfChange:ConfChangeUpdateNode"}, {Name: "ApplyConfChange:ConfChangeUpdateNode"}}
|
2014-11-11 22:46:49 +03:00
|
|
|
if !reflect.DeepEqual(gaction, wactions) {
|
|
|
|
t.Errorf("action = %v, want %v", gaction, wactions)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(cl.Member(1234), &wm) {
|
|
|
|
t.Errorf("member = %v, want %v", cl.Member(1234), &wm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 02:07:20 +04:00
|
|
|
// TODO: test server could stop itself when being removed
|
2014-09-24 23:26:19 +04:00
|
|
|
|
2014-09-27 01:18:59 +04:00
|
|
|
func TestPublish(t *testing.T) {
|
2016-01-04 22:24:49 +03:00
|
|
|
n := newNodeRecorder()
|
2014-09-27 01:18:59 +04:00
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
// simulate that request has gone through consensus
|
|
|
|
ch <- Response{}
|
2016-01-04 22:24:49 +03:00
|
|
|
w := wait.NewWithResponse(ch)
|
2014-09-27 01:18:59 +04:00
|
|
|
srv := &EtcdServer{
|
2016-04-23 05:57:33 +03:00
|
|
|
readych: make(chan struct{}),
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2014-10-03 02:48:58 +04:00
|
|
|
id: 1,
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{Node: n},
|
2016-04-07 22:02:37 +03:00
|
|
|
attributes: membership.Attributes{Name: "node1", ClientURLs: []string{"http://a", "http://b"}},
|
|
|
|
cluster: &membership.RaftCluster{},
|
2014-10-03 02:48:58 +04:00
|
|
|
w: w,
|
2014-12-16 03:07:47 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
2014-10-01 20:33:55 +04:00
|
|
|
srv.publish(time.Hour)
|
2014-09-27 01:18:59 +04:00
|
|
|
|
2014-12-30 11:16:31 +03:00
|
|
|
action := n.Action()
|
|
|
|
if len(action) != 1 {
|
|
|
|
t.Fatalf("len(action) = %d, want 1", len(action))
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
2015-01-02 22:21:23 +03:00
|
|
|
if action[0].Name != "Propose" {
|
|
|
|
t.Fatalf("action = %s, want Propose", action[0].Name)
|
2014-12-30 11:16:31 +03:00
|
|
|
}
|
2015-01-02 22:21:23 +03:00
|
|
|
data := action[0].Params[0].([]byte)
|
2015-08-13 23:41:52 +03:00
|
|
|
var r pb.Request
|
|
|
|
if err := r.Unmarshal(data); err != nil {
|
2014-09-27 01:18:59 +04:00
|
|
|
t.Fatalf("unmarshal request error: %v", err)
|
|
|
|
}
|
|
|
|
if r.Method != "PUT" {
|
|
|
|
t.Errorf("method = %s, want PUT", r.Method)
|
|
|
|
}
|
2016-04-07 22:02:37 +03:00
|
|
|
wm := membership.Member{ID: 1, Attributes: membership.Attributes{Name: "node1", ClientURLs: []string{"http://a", "http://b"}}}
|
|
|
|
if wpath := membership.MemberAttributesStorePath(wm.ID); r.Path != wpath {
|
2015-02-18 00:15:48 +03:00
|
|
|
t.Errorf("path = %s, want %s", r.Path, wpath)
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
2016-04-07 22:02:37 +03:00
|
|
|
var gattr membership.Attributes
|
2014-10-03 02:48:58 +04:00
|
|
|
if err := json.Unmarshal([]byte(r.Val), &gattr); err != nil {
|
2014-09-27 01:18:59 +04:00
|
|
|
t.Fatalf("unmarshal val error: %v", err)
|
|
|
|
}
|
2014-10-03 02:48:58 +04:00
|
|
|
if !reflect.DeepEqual(gattr, wm.Attributes) {
|
|
|
|
t.Errorf("member = %v, want %v", gattr, wm.Attributes)
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPublishStopped tests that publish will be stopped if server is stopped.
|
|
|
|
func TestPublishStopped(t *testing.T) {
|
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{
|
2016-01-04 22:24:49 +03:00
|
|
|
Node: newNodeNop(),
|
|
|
|
transport: rafthttp.NewNopTransporter(),
|
2015-01-16 01:39:30 +03:00
|
|
|
},
|
2016-04-07 22:02:37 +03:00
|
|
|
cluster: &membership.RaftCluster{},
|
2016-03-16 01:07:18 +03:00
|
|
|
w: mockwait.NewNop(),
|
2015-01-16 01:39:30 +03:00
|
|
|
done: make(chan struct{}),
|
2016-09-14 01:46:05 +03:00
|
|
|
stopping: make(chan struct{}),
|
2015-01-16 01:39:30 +03:00
|
|
|
stop: make(chan struct{}),
|
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
2016-09-14 01:46:05 +03:00
|
|
|
close(srv.stopping)
|
2014-10-01 20:33:55 +04:00
|
|
|
srv.publish(time.Hour)
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestPublishRetry tests that publish will keep retry until success.
|
|
|
|
func TestPublishRetry(t *testing.T) {
|
2016-10-01 01:42:19 +03:00
|
|
|
n := newNodeRecorderStream()
|
2014-09-27 01:18:59 +04:00
|
|
|
srv := &EtcdServer{
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2015-01-16 01:39:30 +03:00
|
|
|
r: raftNode{Node: n},
|
2016-03-16 01:07:18 +03:00
|
|
|
w: mockwait.NewNop(),
|
2016-09-14 01:46:05 +03:00
|
|
|
stopping: make(chan struct{}),
|
2014-12-16 03:07:47 +03:00
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
2016-10-01 01:42:19 +03:00
|
|
|
// expect multiple proposals from retrying
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
if action, err := n.Wait(2); err != nil {
|
|
|
|
t.Errorf("len(action) = %d, want >= 2 (%v)", len(action), err)
|
|
|
|
}
|
|
|
|
close(srv.stopping)
|
2016-10-31 19:47:15 +03:00
|
|
|
// drain remaining actions, if any, so publish can terminate
|
2016-10-01 01:42:19 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
n.Action()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-10-01 20:33:55 +04:00
|
|
|
srv.publish(10 * time.Nanosecond)
|
2016-10-01 01:42:19 +03:00
|
|
|
ch <- struct{}{}
|
|
|
|
<-ch
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
|
|
|
|
2015-05-08 21:10:12 +03:00
|
|
|
func TestUpdateVersion(t *testing.T) {
|
2016-01-04 22:24:49 +03:00
|
|
|
n := newNodeRecorder()
|
2015-05-08 21:10:12 +03:00
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
// simulate that request has gone through consensus
|
|
|
|
ch <- Response{}
|
2016-01-04 22:24:49 +03:00
|
|
|
w := wait.NewWithResponse(ch)
|
2015-05-08 21:10:12 +03:00
|
|
|
srv := &EtcdServer{
|
|
|
|
id: 1,
|
2016-05-12 22:34:03 +03:00
|
|
|
Cfg: &ServerConfig{TickMs: 1},
|
2015-05-08 21:10:12 +03:00
|
|
|
r: raftNode{Node: n},
|
2016-04-07 22:02:37 +03:00
|
|
|
attributes: membership.Attributes{Name: "node1", ClientURLs: []string{"http://node1.com"}},
|
|
|
|
cluster: &membership.RaftCluster{},
|
2015-05-08 21:10:12 +03:00
|
|
|
w: w,
|
|
|
|
reqIDGen: idutil.NewGenerator(0, time.Time{}),
|
|
|
|
}
|
|
|
|
srv.updateClusterVersion("2.0.0")
|
|
|
|
|
|
|
|
action := n.Action()
|
|
|
|
if len(action) != 1 {
|
|
|
|
t.Fatalf("len(action) = %d, want 1", len(action))
|
|
|
|
}
|
|
|
|
if action[0].Name != "Propose" {
|
|
|
|
t.Fatalf("action = %s, want Propose", action[0].Name)
|
|
|
|
}
|
|
|
|
data := action[0].Params[0].([]byte)
|
2015-08-13 23:41:52 +03:00
|
|
|
var r pb.Request
|
|
|
|
if err := r.Unmarshal(data); err != nil {
|
2015-05-08 21:10:12 +03:00
|
|
|
t.Fatalf("unmarshal request error: %v", err)
|
|
|
|
}
|
|
|
|
if r.Method != "PUT" {
|
|
|
|
t.Errorf("method = %s, want PUT", r.Method)
|
|
|
|
}
|
|
|
|
if wpath := path.Join(StoreClusterPrefix, "version"); r.Path != wpath {
|
|
|
|
t.Errorf("path = %s, want %s", r.Path, wpath)
|
|
|
|
}
|
|
|
|
if r.Val != "2.0.0" {
|
|
|
|
t.Errorf("val = %s, want %s", r.Val, "2.0.0")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:16:48 +03:00
|
|
|
func TestStopNotify(t *testing.T) {
|
|
|
|
s := &EtcdServer{
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
<-s.stop
|
|
|
|
close(s.done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
notifier := s.StopNotify()
|
|
|
|
select {
|
|
|
|
case <-notifier:
|
|
|
|
t.Fatalf("received unexpected stop notification")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
s.Stop()
|
|
|
|
select {
|
|
|
|
case <-notifier:
|
|
|
|
default:
|
|
|
|
t.Fatalf("cannot receive stop notification")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-08 23:07:23 +03:00
|
|
|
func TestGetOtherPeerURLs(t *testing.T) {
|
|
|
|
tests := []struct {
|
2016-04-07 22:02:37 +03:00
|
|
|
membs []*membership.Member
|
2014-11-08 23:07:23 +03:00
|
|
|
wurls []string
|
|
|
|
}{
|
|
|
|
{
|
2016-04-07 22:02:37 +03:00
|
|
|
[]*membership.Member{
|
|
|
|
membership.NewMember("1", types.MustNewURLs([]string{"http://10.0.0.1:1"}), "a", nil),
|
2014-11-08 23:07:23 +03:00
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
},
|
|
|
|
{
|
2016-04-07 22:02:37 +03:00
|
|
|
[]*membership.Member{
|
|
|
|
membership.NewMember("1", types.MustNewURLs([]string{"http://10.0.0.1:1"}), "a", nil),
|
|
|
|
membership.NewMember("2", types.MustNewURLs([]string{"http://10.0.0.2:2"}), "a", nil),
|
|
|
|
membership.NewMember("3", types.MustNewURLs([]string{"http://10.0.0.3:3"}), "a", nil),
|
2014-11-08 23:07:23 +03:00
|
|
|
},
|
2016-04-07 22:02:37 +03:00
|
|
|
[]string{"http://10.0.0.2:2", "http://10.0.0.3:3"},
|
2014-11-08 23:07:23 +03:00
|
|
|
},
|
|
|
|
{
|
2016-04-07 22:02:37 +03:00
|
|
|
[]*membership.Member{
|
|
|
|
membership.NewMember("1", types.MustNewURLs([]string{"http://10.0.0.1:1"}), "a", nil),
|
|
|
|
membership.NewMember("3", types.MustNewURLs([]string{"http://10.0.0.3:3"}), "a", nil),
|
|
|
|
membership.NewMember("2", types.MustNewURLs([]string{"http://10.0.0.2:2"}), "a", nil),
|
2014-11-08 23:07:23 +03:00
|
|
|
},
|
2016-04-07 22:02:37 +03:00
|
|
|
[]string{"http://10.0.0.2:2", "http://10.0.0.3:3"},
|
2014-11-08 23:07:23 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2016-04-07 22:02:37 +03:00
|
|
|
cl := membership.NewClusterFromMembers("", types.ID(0), tt.membs)
|
|
|
|
self := "1"
|
|
|
|
urls := getRemotePeerURLs(cl, self)
|
2014-11-08 23:07:23 +03:00
|
|
|
if !reflect.DeepEqual(urls, tt.wurls) {
|
|
|
|
t.Errorf("#%d: urls = %+v, want %+v", i, urls, tt.wurls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 22:21:23 +03:00
|
|
|
type nodeRecorder struct{ testutil.Recorder }
|
2014-09-18 07:54:14 +04:00
|
|
|
|
2016-10-01 01:42:19 +03:00
|
|
|
func newNodeRecorder() *nodeRecorder { return &nodeRecorder{&testutil.RecorderBuffered{}} }
|
|
|
|
func newNodeRecorderStream() *nodeRecorder { return &nodeRecorder{testutil.NewRecorderStream()} }
|
|
|
|
func newNodeNop() raft.Node { return newNodeRecorder() }
|
2016-01-04 22:24:49 +03:00
|
|
|
|
2015-01-02 22:21:23 +03:00
|
|
|
func (n *nodeRecorder) Tick() { n.Record(testutil.Action{Name: "Tick"}) }
|
2014-09-18 07:54:14 +04:00
|
|
|
func (n *nodeRecorder) Campaign(ctx context.Context) error {
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "Campaign"})
|
2014-09-18 07:54:14 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (n *nodeRecorder) Propose(ctx context.Context, data []byte) error {
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "Propose", Params: []interface{}{data}})
|
2014-09-18 07:54:14 +04:00
|
|
|
return nil
|
|
|
|
}
|
2014-09-23 23:02:44 +04:00
|
|
|
func (n *nodeRecorder) ProposeConfChange(ctx context.Context, conf raftpb.ConfChange) error {
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "ProposeConfChange"})
|
2014-09-18 07:54:14 +04:00
|
|
|
return nil
|
|
|
|
}
|
2014-09-19 23:36:19 +04:00
|
|
|
func (n *nodeRecorder) Step(ctx context.Context, msg raftpb.Message) error {
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "Step"})
|
2014-09-18 07:54:14 +04:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-11 02:26:11 +03:00
|
|
|
func (n *nodeRecorder) Status() raft.Status { return raft.Status{} }
|
|
|
|
func (n *nodeRecorder) Ready() <-chan raft.Ready { return nil }
|
|
|
|
func (n *nodeRecorder) TransferLeadership(ctx context.Context, lead, transferee uint64) {}
|
|
|
|
func (n *nodeRecorder) ReadIndex(ctx context.Context, rctx []byte) error { return nil }
|
|
|
|
func (n *nodeRecorder) Advance() {}
|
2014-11-20 00:17:50 +03:00
|
|
|
func (n *nodeRecorder) ApplyConfChange(conf raftpb.ConfChange) *raftpb.ConfState {
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "ApplyConfChange", Params: []interface{}{conf}})
|
2014-12-09 08:17:04 +03:00
|
|
|
return &raftpb.ConfState{}
|
2014-09-23 08:59:13 +04:00
|
|
|
}
|
2014-12-26 04:20:49 +03:00
|
|
|
|
2014-09-18 07:54:14 +04:00
|
|
|
func (n *nodeRecorder) Stop() {
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "Stop"})
|
2014-09-18 07:54:14 +04:00
|
|
|
}
|
2014-12-26 04:20:49 +03:00
|
|
|
|
2015-02-05 01:41:14 +03:00
|
|
|
func (n *nodeRecorder) ReportUnreachable(id uint64) {}
|
|
|
|
|
2015-02-25 08:38:18 +03:00
|
|
|
func (n *nodeRecorder) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
|
|
|
|
|
2014-10-08 15:58:53 +04:00
|
|
|
func (n *nodeRecorder) Compact(index uint64, nodes []uint64, d []byte) {
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "Compact"})
|
2014-09-18 07:54:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type nodeProposalBlockerRecorder struct {
|
|
|
|
nodeRecorder
|
|
|
|
}
|
|
|
|
|
2016-01-04 22:24:49 +03:00
|
|
|
func newProposalBlockerRecorder() *nodeProposalBlockerRecorder {
|
|
|
|
return &nodeProposalBlockerRecorder{*newNodeRecorder()}
|
|
|
|
}
|
|
|
|
|
2014-09-18 07:54:14 +04:00
|
|
|
func (n *nodeProposalBlockerRecorder) Propose(ctx context.Context, data []byte) error {
|
|
|
|
<-ctx.Done()
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "Propose blocked"})
|
2014-09-18 07:54:14 +04:00
|
|
|
return nil
|
2014-09-18 01:38:10 +04:00
|
|
|
}
|
2014-09-19 23:36:19 +04:00
|
|
|
|
2016-01-04 22:24:49 +03:00
|
|
|
// readyNode is a nodeRecorder with a user-writeable ready channel
|
|
|
|
type readyNode struct {
|
2014-09-19 23:36:19 +04:00
|
|
|
nodeRecorder
|
|
|
|
readyc chan raft.Ready
|
2016-01-04 22:24:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newReadyNode() *readyNode {
|
2016-01-04 22:39:02 +03:00
|
|
|
return &readyNode{
|
|
|
|
nodeRecorder{testutil.NewRecorderStream()},
|
|
|
|
make(chan raft.Ready, 1)}
|
|
|
|
}
|
|
|
|
func newNopReadyNode() *readyNode {
|
2016-01-04 22:24:49 +03:00
|
|
|
return &readyNode{*newNodeRecorder(), make(chan raft.Ready, 1)}
|
|
|
|
}
|
2016-01-04 22:39:02 +03:00
|
|
|
|
2016-01-04 22:24:49 +03:00
|
|
|
func (n *readyNode) Ready() <-chan raft.Ready { return n.readyc }
|
|
|
|
|
|
|
|
type nodeConfChangeCommitterRecorder struct {
|
|
|
|
readyNode
|
|
|
|
index uint64
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
|
|
|
|
2014-09-23 23:02:44 +04:00
|
|
|
func newNodeConfChangeCommitterRecorder() *nodeConfChangeCommitterRecorder {
|
2016-01-04 22:39:02 +03:00
|
|
|
return &nodeConfChangeCommitterRecorder{*newNopReadyNode(), 0}
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
2016-01-04 22:39:02 +03:00
|
|
|
|
2016-01-29 03:29:39 +03:00
|
|
|
func newNodeConfChangeCommitterStream() *nodeConfChangeCommitterRecorder {
|
|
|
|
return &nodeConfChangeCommitterRecorder{*newReadyNode(), 0}
|
|
|
|
}
|
|
|
|
|
2014-09-23 23:02:44 +04:00
|
|
|
func (n *nodeConfChangeCommitterRecorder) ProposeConfChange(ctx context.Context, conf raftpb.ConfChange) error {
|
2014-09-23 01:38:21 +04:00
|
|
|
data, err := conf.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-09 03:13:16 +03:00
|
|
|
n.index++
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "ProposeConfChange:" + conf.Type.String()})
|
2015-06-11 19:40:53 +03:00
|
|
|
n.readyc <- raft.Ready{CommittedEntries: []raftpb.Entry{{Index: n.index, Type: raftpb.EntryConfChange, Data: data}}}
|
2014-09-23 08:59:13 +04:00
|
|
|
return nil
|
2014-09-19 23:36:19 +04:00
|
|
|
}
|
2014-09-23 23:02:44 +04:00
|
|
|
func (n *nodeConfChangeCommitterRecorder) Ready() <-chan raft.Ready {
|
2014-09-19 23:36:19 +04:00
|
|
|
return n.readyc
|
|
|
|
}
|
2014-11-20 00:17:50 +03:00
|
|
|
func (n *nodeConfChangeCommitterRecorder) ApplyConfChange(conf raftpb.ConfChange) *raftpb.ConfState {
|
2015-01-02 22:21:23 +03:00
|
|
|
n.Record(testutil.Action{Name: "ApplyConfChange:" + conf.Type.String()})
|
2014-12-09 08:17:04 +03:00
|
|
|
return &raftpb.ConfState{}
|
2014-09-23 08:59:13 +04:00
|
|
|
}
|
2014-09-27 01:18:59 +04:00
|
|
|
|
2014-12-30 11:16:31 +03:00
|
|
|
// nodeCommitter commits proposed data immediately.
|
|
|
|
type nodeCommitter struct {
|
2016-01-04 22:24:49 +03:00
|
|
|
readyNode
|
|
|
|
index uint64
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
|
|
|
|
2016-01-04 22:24:49 +03:00
|
|
|
func newNodeCommitter() raft.Node {
|
2016-01-04 22:39:02 +03:00
|
|
|
return &nodeCommitter{*newNopReadyNode(), 0}
|
2014-09-27 01:18:59 +04:00
|
|
|
}
|
2014-12-30 11:16:31 +03:00
|
|
|
func (n *nodeCommitter) Propose(ctx context.Context, data []byte) error {
|
|
|
|
n.index++
|
|
|
|
ents := []raftpb.Entry{{Index: n.index, Data: data}}
|
|
|
|
n.readyc <- raft.Ready{
|
|
|
|
Entries: ents,
|
|
|
|
CommittedEntries: ents,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-07 22:02:37 +03:00
|
|
|
|
|
|
|
func newTestCluster(membs []*membership.Member) *membership.RaftCluster {
|
|
|
|
c := membership.NewCluster("")
|
|
|
|
for _, m := range membs {
|
|
|
|
c.AddMember(m)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|