From a5efbf826d7e4ef87cda194b4aa4b60405263809 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 8 Dec 2014 21:17:04 -0800 Subject: [PATCH] raft: drop nodes in softState --- etcdserver/server.go | 8 ++++---- etcdserver/server_test.go | 23 +++++++---------------- raft/node.go | 4 +--- raft/node_test.go | 3 +-- raft/raft.go | 4 +--- 5 files changed, 14 insertions(+), 28 deletions(-) diff --git a/etcdserver/server.go b/etcdserver/server.go index b8ab8f72f..e9dd4bf33 100644 --- a/etcdserver/server.go +++ b/etcdserver/server.go @@ -395,7 +395,7 @@ func (s *EtcdServer) run() { // snapi indicates the index of the last submitted snapshot request snapi := snap.Metadata.Index appliedi := snap.Metadata.Index - confState := &snap.Metadata.ConfState + confState := snap.Metadata.ConfState defer func() { s.node.Stop() @@ -458,7 +458,7 @@ func (s *EtcdServer) run() { ents = rd.CommittedEntries[appliedi+1-firsti:] } if len(ents) > 0 { - if appliedi, shouldstop = s.apply(ents, confState); shouldstop { + if appliedi, shouldstop = s.apply(ents, &confState); shouldstop { return } } @@ -468,7 +468,7 @@ func (s *EtcdServer) run() { if appliedi-snapi > s.snapCount { log.Printf("etcdserver: start to snapshot (applied: %d, lastsnap: %d)", appliedi, snapi) - s.snapshot(appliedi, confState) + s.snapshot(appliedi, &confState) snapi = appliedi } case <-syncC: @@ -784,7 +784,7 @@ func (s *EtcdServer) applyConfChange(cc raftpb.ConfChange, confState *raftpb.Con s.node.ApplyConfChange(cc) return false, err } - confState = s.node.ApplyConfChange(cc) + *confState = *s.node.ApplyConfChange(cc) switch cc.Type { case raftpb.ConfChangeAddNode: m := new(Member) diff --git a/etcdserver/server_test.go b/etcdserver/server_test.go index 60846a0cb..5163386c0 100644 --- a/etcdserver/server_test.go +++ b/etcdserver/server_test.go @@ -509,7 +509,7 @@ func TestApplyConfChangeShouldStop(t *testing.T) { NodeID: 2, } // remove non-local member - shouldStop, err := srv.applyConfChange(cc, nil) + shouldStop, err := srv.applyConfChange(cc, &raftpb.ConfState{}) if err != nil { t.Fatalf("unexpected error %v", err) } @@ -519,7 +519,7 @@ func TestApplyConfChangeShouldStop(t *testing.T) { // remove local member cc.NodeID = 1 - shouldStop, err = srv.applyConfChange(cc, nil) + shouldStop, err = srv.applyConfChange(cc, &raftpb.ConfState{}) if err != nil { t.Fatalf("unexpected error %v", err) } @@ -1095,10 +1095,7 @@ func TestApplySnapshotAndCommittedEntries(t *testing.T) { func TestAddMember(t *testing.T) { n := newNodeConfChangeCommitterRecorder() n.readyc <- raft.Ready{ - SoftState: &raft.SoftState{ - RaftState: raft.StateLeader, - Nodes: []uint64{2345, 3456}, - }, + SoftState: &raft.SoftState{RaftState: raft.StateLeader}, } cl := newTestCluster(nil) cl.SetStore(store.New()) @@ -1132,10 +1129,7 @@ func TestAddMember(t *testing.T) { func TestRemoveMember(t *testing.T) { n := newNodeConfChangeCommitterRecorder() n.readyc <- raft.Ready{ - SoftState: &raft.SoftState{ - RaftState: raft.StateLeader, - Nodes: []uint64{1234, 2345, 3456}, - }, + SoftState: &raft.SoftState{RaftState: raft.StateLeader}, } cl := newTestCluster(nil) cl.SetStore(store.New()) @@ -1169,10 +1163,7 @@ func TestRemoveMember(t *testing.T) { func TestUpdateMember(t *testing.T) { n := newNodeConfChangeCommitterRecorder() n.readyc <- raft.Ready{ - SoftState: &raft.SoftState{ - RaftState: raft.StateLeader, - Nodes: []uint64{1234, 2345, 3456}, - }, + SoftState: &raft.SoftState{RaftState: raft.StateLeader}, } cl := newTestCluster(nil) cl.SetStore(store.New()) @@ -1579,7 +1570,7 @@ func (n *nodeRecorder) Ready() <-chan raft.Ready { return nil } func (n *nodeRecorder) Advance() {} func (n *nodeRecorder) ApplyConfChange(conf raftpb.ConfChange) *raftpb.ConfState { n.record(action{name: "ApplyConfChange", params: []interface{}{conf}}) - return nil + return &raftpb.ConfState{} } func (n *nodeRecorder) Stop() { n.record(action{name: "Stop"}) @@ -1643,7 +1634,7 @@ func (n *nodeConfChangeCommitterRecorder) Ready() <-chan raft.Ready { } func (n *nodeConfChangeCommitterRecorder) ApplyConfChange(conf raftpb.ConfChange) *raftpb.ConfState { n.record(action{name: "ApplyConfChange:" + conf.Type.String()}) - return nil + return &raftpb.ConfState{} } type waitWithResponse struct { diff --git a/raft/node.go b/raft/node.go index 33994922e..b75d3e863 100644 --- a/raft/node.go +++ b/raft/node.go @@ -19,7 +19,6 @@ package raft import ( "errors" "log" - "reflect" "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context" pb "github.com/coreos/etcd/raft/raftpb" @@ -37,11 +36,10 @@ var ( type SoftState struct { Lead uint64 RaftState StateType - Nodes []uint64 } func (a *SoftState) equal(b *SoftState) bool { - return reflect.DeepEqual(a, b) + return a.Lead == b.Lead && a.RaftState == b.RaftState } // Ready encapsulates the entries and messages that are ready to read, diff --git a/raft/node_test.go b/raft/node_test.go index d37649359..f148e7e06 100644 --- a/raft/node_test.go +++ b/raft/node_test.go @@ -305,7 +305,7 @@ func TestNodeStart(t *testing.T) { } wants := []Ready{ { - SoftState: &SoftState{Lead: 1, Nodes: []uint64{1}, RaftState: StateLeader}, + SoftState: &SoftState{Lead: 1, RaftState: StateLeader}, HardState: raftpb.HardState{Term: 2, Commit: 2}, Entries: []raftpb.Entry{ {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata}, @@ -446,7 +446,6 @@ func TestSoftStateEqual(t *testing.T) { {&SoftState{}, true}, {&SoftState{Lead: 1}, false}, {&SoftState{RaftState: StateLeader}, false}, - {&SoftState{Nodes: []uint64{1, 2}}, false}, } for i, tt := range tests { if g := tt.st.equal(&SoftState{}); g != tt.we { diff --git a/raft/raft.go b/raft/raft.go index 9283b3f8c..c62858369 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -175,9 +175,7 @@ func (r *raft) hasLeader() bool { return r.lead != None } func (r *raft) leader() uint64 { return r.lead } -func (r *raft) softState() *SoftState { - return &SoftState{Lead: r.lead, RaftState: r.state, Nodes: r.nodes()} -} +func (r *raft) softState() *SoftState { return &SoftState{Lead: r.lead, RaftState: r.state} } func (r *raft) q() int { return len(r.prs)/2 + 1 }