mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: raft learners should be returned after applyConfChange
This commit is contained in:
@@ -732,3 +732,55 @@ func TestIsHardStateEqual(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeProposeAddLearnerNode(t *testing.T) {
|
||||
ticker := time.NewTicker(time.Millisecond * 100)
|
||||
defer ticker.Stop()
|
||||
n := newNode()
|
||||
s := NewMemoryStorage()
|
||||
r := newTestRaft(1, []uint64{1}, 10, 1, s)
|
||||
go n.run(r)
|
||||
n.Campaign(context.TODO())
|
||||
stop := make(chan struct{})
|
||||
done := make(chan struct{})
|
||||
applyConfChan := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
case <-ticker.C:
|
||||
n.Tick()
|
||||
case rd := <-n.Ready():
|
||||
s.Append(rd.Entries)
|
||||
t.Logf("raft: %v", rd.Entries)
|
||||
for _, ent := range rd.Entries {
|
||||
if ent.Type != raftpb.EntryConfChange {
|
||||
continue
|
||||
}
|
||||
var cc raftpb.ConfChange
|
||||
cc.Unmarshal(ent.Data)
|
||||
state := n.ApplyConfChange(cc)
|
||||
if len(state.Learners) == 0 ||
|
||||
state.Learners[0] != cc.NodeID ||
|
||||
cc.NodeID != 2 {
|
||||
t.Errorf("apply conf change should return new added learner: %v", state.String())
|
||||
}
|
||||
|
||||
if len(state.Nodes) != 1 {
|
||||
t.Errorf("add learner should not change the nodes: %v", state.String())
|
||||
}
|
||||
t.Logf("apply raft conf %v changed to: %v", cc, state.String())
|
||||
applyConfChan <- struct{}{}
|
||||
}
|
||||
n.Advance()
|
||||
}
|
||||
}
|
||||
}()
|
||||
cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddLearnerNode, NodeID: 2}
|
||||
n.ProposeConfChange(context.TODO(), cc)
|
||||
<-applyConfChan
|
||||
close(stop)
|
||||
<-done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user