mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
...deadlocked...
This commit is contained in:
parent
17e56a1c57
commit
f8be54b416
@ -88,7 +88,9 @@ func (s *Server) run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Stop() { close(s.done) }
|
func (s *Server) Stop() {
|
||||||
|
s.done <- struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) Do(ctx context.Context, r pb.Request) (Response, error) {
|
func (s *Server) Do(ctx context.Context, r pb.Request) (Response, error) {
|
||||||
if r.Id == 0 {
|
if r.Id == 0 {
|
||||||
|
@ -3,6 +3,7 @@ package etcdserver
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
"code.google.com/p/go.net/context"
|
"code.google.com/p/go.net/context"
|
||||||
|
|
||||||
pb "github.com/coreos/etcd/etcdserver2/etcdserverpb"
|
pb "github.com/coreos/etcd/etcdserver2/etcdserverpb"
|
||||||
@ -11,21 +12,51 @@ import (
|
|||||||
"github.com/coreos/etcd/store"
|
"github.com/coreos/etcd/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestServer(t *testing.T) {
|
func TestClusterOf1(t *testing.T) { testServer(t, 1) }
|
||||||
|
func TestClusterOf3(t *testing.T) { testServer(t, 3) }
|
||||||
|
|
||||||
|
func testServer(t *testing.T, ns int64) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
n := raft.Start(ctx, 1, []int64{1})
|
ss := make([]*Server, ns)
|
||||||
n.Campaign(ctx)
|
|
||||||
|
|
||||||
srv := &Server{
|
send := func(msgs []raftpb.Message) {
|
||||||
Node: n,
|
var m raftpb.Message
|
||||||
Store: store.New(),
|
for len(msgs) > 0 {
|
||||||
Send: func(_ []raftpb.Message) {},
|
m, msgs = msgs[0], msgs[1:]
|
||||||
Save: func(_ raftpb.State, _ []raftpb.Entry) {},
|
t.Logf("sending: %+v", m)
|
||||||
|
if err := ss[m.To].Node.Step(ctx, m); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rd := raft.RecvReadyNow(ss[m.To].Node)
|
||||||
|
msgs = append(msgs, rd.Messages...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
peers := make([]int64, ns)
|
||||||
|
for i := int64(0); i < ns; i++ {
|
||||||
|
peers[i] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
var srv *Server
|
||||||
|
for i := int64(0); i < ns; i++ {
|
||||||
|
n := raft.Start(ctx, i, peers)
|
||||||
|
|
||||||
|
srv = &Server{
|
||||||
|
Node: n,
|
||||||
|
Store: store.New(),
|
||||||
|
Send: send,
|
||||||
|
Save: func(_ raftpb.State, _ []raftpb.Entry) {},
|
||||||
|
}
|
||||||
|
Start(srv)
|
||||||
|
|
||||||
|
ss[i] = srv
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := srv.Node.Campaign(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
Start(srv)
|
|
||||||
defer srv.Stop()
|
|
||||||
|
|
||||||
r := pb.Request{
|
r := pb.Request{
|
||||||
Method: "PUT",
|
Method: "PUT",
|
||||||
@ -49,6 +80,18 @@ func TestServer(t *testing.T) {
|
|||||||
t.Error("value:", *g.Value)
|
t.Error("value:", *g.Value)
|
||||||
t.Errorf("g = %+v, w %+v", g, w)
|
t.Errorf("g = %+v, w %+v", g, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
|
||||||
|
var last interface{}
|
||||||
|
for i, sv := range ss {
|
||||||
|
sv.Stop()
|
||||||
|
g := store.Root(sv.Store)
|
||||||
|
if last != nil && !reflect.DeepEqual(last, g) {
|
||||||
|
t.Errorf("server %d: Root = %#v, want %#v", i, g, last)
|
||||||
|
}
|
||||||
|
last = g
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringp(s string) *string { return &s }
|
func stringp(s string) *string { return &s }
|
||||||
|
34
raft/node.go
34
raft/node.go
@ -33,20 +33,22 @@ func (rd Ready) containsUpdates(prev Ready) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Node struct {
|
type Node struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
propc chan pb.Message
|
propc chan pb.Message
|
||||||
recvc chan pb.Message
|
recvc chan pb.Message
|
||||||
readyc chan Ready
|
readyc chan Ready
|
||||||
tickc chan struct{}
|
tickc chan struct{}
|
||||||
|
alwaysreadyc chan Ready
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(ctx context.Context, id int64, peers []int64) Node {
|
func Start(ctx context.Context, id int64, peers []int64) Node {
|
||||||
n := Node{
|
n := Node{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
propc: make(chan pb.Message),
|
propc: make(chan pb.Message),
|
||||||
recvc: make(chan pb.Message),
|
recvc: make(chan pb.Message),
|
||||||
readyc: make(chan Ready),
|
readyc: make(chan Ready),
|
||||||
tickc: make(chan struct{}),
|
tickc: make(chan struct{}),
|
||||||
|
alwaysreadyc: make(chan Ready),
|
||||||
}
|
}
|
||||||
r := newRaft(id, peers)
|
r := newRaft(id, peers)
|
||||||
go n.run(r)
|
go n.run(r)
|
||||||
@ -94,6 +96,8 @@ func (n *Node) run(r *raft) {
|
|||||||
r.raftLog.resetNextEnts()
|
r.raftLog.resetNextEnts()
|
||||||
r.raftLog.resetUnstable()
|
r.raftLog.resetUnstable()
|
||||||
r.msgs = nil
|
r.msgs = nil
|
||||||
|
case n.alwaysreadyc <- rd:
|
||||||
|
// this is for testing only
|
||||||
case <-n.ctx.Done():
|
case <-n.ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -141,8 +145,8 @@ func (n *Node) Ready() <-chan Ready {
|
|||||||
return n.readyc
|
return n.readyc
|
||||||
}
|
}
|
||||||
|
|
||||||
type byMsgType []pb.Message
|
// RecvReadyNow returns the state of n without blocking. It is primarly for
|
||||||
|
// testing purposes only.
|
||||||
func (msgs byMsgType) Len() int { return len(msgs) }
|
func RecvReadyNow(n Node) Ready {
|
||||||
func (msgs byMsgType) Less(i, j int) bool { return msgs[i].Type == msgProp }
|
return <-n.alwaysreadyc
|
||||||
func (msgs byMsgType) Swap(i, j int) { msgs[i], msgs[j] = msgs[i], msgs[j] }
|
}
|
||||||
|
@ -75,6 +75,11 @@ func New() Store {
|
|||||||
return newStore()
|
return newStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Root returns the root of a Store and is for testing only.
|
||||||
|
func Root(st Store) interface{} {
|
||||||
|
return st.(*store).Root
|
||||||
|
}
|
||||||
|
|
||||||
func newStore() *store {
|
func newStore() *store {
|
||||||
s := new(store)
|
s := new(store)
|
||||||
s.CurrentVersion = defaultVersion
|
s.CurrentVersion = defaultVersion
|
||||||
|
Loading…
x
Reference in New Issue
Block a user