Skip to content

Commit

Permalink
raftpb: clean up naming in ConfChange
Browse files Browse the repository at this point in the history
  • Loading branch information
tbg committed Jul 23, 2019
1 parent b67303c commit b9c051e
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 173 deletions.
5 changes: 2 additions & 3 deletions clientv3/snapshot/v3_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"strings"
"time"

bolt "go.etcd.io/bbolt"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/etcdserver"
"go.etcd.io/etcd/etcdserver/api/membership"
Expand All @@ -43,8 +44,6 @@ import (
"go.etcd.io/etcd/raft/raftpb"
"go.etcd.io/etcd/wal"
"go.etcd.io/etcd/wal/walpb"

bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -482,7 +481,7 @@ func (s *v3Manager) saveWALAndSnap() error {
Index: commit,
Term: term,
ConfState: raftpb.ConfState{
Nodes: nodeIDs,
Voters: nodeIDs,
},
},
}
Expand Down
3 changes: 1 addition & 2 deletions etcdserver/api/snap/snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ import (
"testing"

"go.etcd.io/etcd/raft/raftpb"

"go.uber.org/zap"
)

var testSnap = &raftpb.Snapshot{
Data: []byte("some snapshot"),
Metadata: raftpb.SnapshotMetadata{
ConfState: raftpb.ConfState{
Nodes: []uint64{1, 2, 3},
Voters: []uint64{1, 2, 3},
},
Index: 1,
Term: 1,
Expand Down
2 changes: 1 addition & 1 deletion etcdserver/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func restartAsStandaloneNode(cfg ServerConfig, snapshot *raftpb.Snapshot) (types
func getIDs(lg *zap.Logger, snap *raftpb.Snapshot, ents []raftpb.Entry) []uint64 {
ids := make(map[uint64]bool)
if snap != nil {
for _, id := range snap.Metadata.ConfState.Nodes {
for _, id := range snap.Metadata.ConfState.Voters {
ids[id] = true
}
}
Expand Down
12 changes: 6 additions & 6 deletions etcdserver/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ func TestGetIDs(t *testing.T) {
widSet []uint64
}{
{nil, []raftpb.Entry{}, []uint64{}},
{&raftpb.ConfState{Nodes: []uint64{1}},
{&raftpb.ConfState{Voters: []uint64{1}},
[]raftpb.Entry{}, []uint64{1}},
{&raftpb.ConfState{Nodes: []uint64{1}},
{&raftpb.ConfState{Voters: []uint64{1}},
[]raftpb.Entry{addEntry}, []uint64{1, 2}},
{&raftpb.ConfState{Nodes: []uint64{1}},
{&raftpb.ConfState{Voters: []uint64{1}},
[]raftpb.Entry{addEntry, removeEntry}, []uint64{1}},
{&raftpb.ConfState{Nodes: []uint64{1}},
{&raftpb.ConfState{Voters: []uint64{1}},
[]raftpb.Entry{addEntry, normalEntry}, []uint64{1, 2}},
{&raftpb.ConfState{Nodes: []uint64{1}},
{&raftpb.ConfState{Voters: []uint64{1}},
[]raftpb.Entry{addEntry, normalEntry, updateEntry}, []uint64{1, 2}},
{&raftpb.ConfState{Nodes: []uint64{1}},
{&raftpb.ConfState{Voters: []uint64{1}},
[]raftpb.Entry{addEntry, removeEntry, normalEntry}, []uint64{1}},
}

Expand Down
2 changes: 1 addition & 1 deletion etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ func TestSnapshot(t *testing.T) {
}
}()

srv.snapshot(1, raftpb.ConfState{Nodes: []uint64{1}})
srv.snapshot(1, raftpb.ConfState{Voters: []uint64{1}})
<-ch
<-ch
}
Expand Down
2 changes: 1 addition & 1 deletion raft/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (n *node) run(rn *RawNode) {
// very sound and likely has bugs.
if _, okAfter := r.prs.Progress[r.id]; okBefore && !okAfter {
var found bool
for _, sl := range [][]uint64{cs.Nodes, cs.NodesJoint} {
for _, sl := range [][]uint64{cs.Voters, cs.VotersOutgoing} {
for _, id := range sl {
if id == r.id {
found = true
Expand Down
4 changes: 2 additions & 2 deletions raft/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func TestNodeRestart(t *testing.T) {
func TestNodeRestartFromSnapshot(t *testing.T) {
snap := raftpb.Snapshot{
Metadata: raftpb.SnapshotMetadata{
ConfState: raftpb.ConfState{Nodes: []uint64{1, 2}},
ConfState: raftpb.ConfState{Voters: []uint64{1, 2}},
Index: 2,
Term: 1,
},
Expand Down Expand Up @@ -845,7 +845,7 @@ func TestNodeProposeAddLearnerNode(t *testing.T) {
t.Errorf("apply conf change should return new added learner: %v", state.String())
}

if len(state.Nodes) != 1 {
if len(state.Voters) != 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())
Expand Down
22 changes: 11 additions & 11 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,14 @@ func newRaft(c *Config) *raft {
}
peers := c.peers
learners := c.learners
if len(cs.Nodes) > 0 || len(cs.Learners) > 0 {
if len(cs.Voters) > 0 || len(cs.Learners) > 0 {
if len(peers) > 0 || len(learners) > 0 {
// TODO(bdarnell): the peers argument is always nil except in
// tests; the argument should be removed and these tests should be
// updated to specify their nodes through a snapshot.
panic("cannot specify both newRaft(peers, learners) and ConfState.(Nodes, Learners)")
panic("cannot specify both newRaft(peers, learners) and ConfState.(Voters, Learners)")
}
peers = cs.Nodes
peers = cs.Voters
learners = cs.Learners
}
r := &raft{
Expand Down Expand Up @@ -1384,7 +1384,7 @@ func (r *raft) restore(s pb.Snapshot) bool {
found := false
cs := s.Metadata.ConfState
for _, set := range [][]uint64{
cs.Nodes,
cs.Voters,
cs.Learners,
} {
for _, id := range set {
Expand Down Expand Up @@ -1415,7 +1415,7 @@ func (r *raft) restore(s pb.Snapshot) bool {

// Reset the configuration and add the (potentially updated) peers in anew.
r.prs = tracker.MakeProgressTracker(r.prs.MaxInflight)
for _, id := range s.Metadata.ConfState.Nodes {
for _, id := range s.Metadata.ConfState.Voters {
r.applyConfChange(pb.ConfChange{NodeID: id, Type: pb.ConfChangeAddNode}.AsV2())
}
for _, id := range s.Metadata.ConfState.Learners {
Expand Down Expand Up @@ -1463,11 +1463,11 @@ func (r *raft) applyConfChange(cc pb.ConfChangeV2) pb.ConfState {
// Now that the configuration is updated, handle any side effects.

cs := pb.ConfState{
Nodes: r.prs.Voters[0].Slice(),
NodesJoint: r.prs.Voters[1].Slice(),
Learners: quorum.MajorityConfig(r.prs.Learners).Slice(),
LearnersNext: quorum.MajorityConfig(r.prs.LearnersNext).Slice(),
AutoLeave: r.prs.AutoLeave,
Voters: r.prs.Voters[0].Slice(),
VotersOutgoing: r.prs.Voters[1].Slice(),
Learners: quorum.MajorityConfig(r.prs.Learners).Slice(),
LearnersNext: quorum.MajorityConfig(r.prs.LearnersNext).Slice(),
AutoLeave: r.prs.AutoLeave,
}
pr, ok := r.prs.Progress[r.id]

Expand All @@ -1490,7 +1490,7 @@ func (r *raft) applyConfChange(cc pb.ConfChangeV2) pb.ConfState {

// The remaining steps only make sense if this node is the leader and there
// are other nodes.
if r.state != StateLeader || len(cs.Nodes) == 0 {
if r.state != StateLeader || len(cs.Voters) == 0 {
return cs
}
if r.maybeCommit() {
Expand Down
4 changes: 2 additions & 2 deletions raft/raft_snap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1, 2}},
ConfState: pb.ConfState{Voters: []uint64{1, 2}},
},
}
)
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestSnapshotSucceedViaAppResp(t *testing.T) {
n1.applyConfChange(pb.ConfChange{NodeID: 2, Type: pb.ConfChangeAddNode}.AsV2())
s1.snapshot = pb.Snapshot{
Metadata: pb.SnapshotMetadata{
ConfState: pb.ConfState{Nodes: []uint64{1, 2}},
ConfState: pb.ConfState{Voters: []uint64{1, 2}},
Index: s1.lastIndex(),
Term: s1.ents[len(s1.ents)-1].Term,
},
Expand Down
34 changes: 17 additions & 17 deletions raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2458,7 +2458,7 @@ func TestBcastBeat(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: offset,
Term: 1,
ConfState: pb.ConfState{Nodes: []uint64{1, 2, 3}},
ConfState: pb.ConfState{Voters: []uint64{1, 2, 3}},
},
}
storage := NewMemoryStorage()
Expand Down Expand Up @@ -2709,7 +2709,7 @@ func TestRestore(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1, 2, 3}},
ConfState: pb.ConfState{Voters: []uint64{1, 2, 3}},
},
}

Expand All @@ -2726,8 +2726,8 @@ func TestRestore(t *testing.T) {
t.Errorf("log.lastTerm = %d, want %d", mustTerm(sm.raftLog.term(s.Metadata.Index)), s.Metadata.Term)
}
sg := sm.prs.VoterNodes()
if !reflect.DeepEqual(sg, s.Metadata.ConfState.Nodes) {
t.Errorf("sm.Nodes = %+v, want %+v", sg, s.Metadata.ConfState.Nodes)
if !reflect.DeepEqual(sg, s.Metadata.ConfState.Voters) {
t.Errorf("sm.Voters = %+v, want %+v", sg, s.Metadata.ConfState.Voters)
}

if ok := sm.restore(s); ok {
Expand All @@ -2741,7 +2741,7 @@ func TestRestoreWithLearner(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1, 2}, Learners: []uint64{3}},
ConfState: pb.ConfState{Voters: []uint64{1, 2}, Learners: []uint64{3}},
},
}

Expand All @@ -2758,14 +2758,14 @@ func TestRestoreWithLearner(t *testing.T) {
t.Errorf("log.lastTerm = %d, want %d", mustTerm(sm.raftLog.term(s.Metadata.Index)), s.Metadata.Term)
}
sg := sm.prs.VoterNodes()
if len(sg) != len(s.Metadata.ConfState.Nodes) {
t.Errorf("sm.Nodes = %+v, length not equal with %+v", sg, s.Metadata.ConfState.Nodes)
if len(sg) != len(s.Metadata.ConfState.Voters) {
t.Errorf("sm.Voters = %+v, length not equal with %+v", sg, s.Metadata.ConfState.Voters)
}
lns := sm.prs.LearnerNodes()
if len(lns) != len(s.Metadata.ConfState.Learners) {
t.Errorf("sm.LearnerNodes = %+v, length not equal with %+v", sg, s.Metadata.ConfState.Learners)
}
for _, n := range s.Metadata.ConfState.Nodes {
for _, n := range s.Metadata.ConfState.Voters {
if sm.prs.Progress[n].IsLearner {
t.Errorf("sm.Node %x isLearner = %s, want %t", n, sm.prs.Progress[n], false)
}
Expand Down Expand Up @@ -2794,7 +2794,7 @@ func TestRestoreVoterToLearner(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1, 2}, Learners: []uint64{3}},
ConfState: pb.ConfState{Voters: []uint64{1, 2}, Learners: []uint64{3}},
},
}

Expand All @@ -2816,7 +2816,7 @@ func TestRestoreLearnerPromotion(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1, 2, 3}},
ConfState: pb.ConfState{Voters: []uint64{1, 2, 3}},
},
}

Expand All @@ -2843,7 +2843,7 @@ func TestLearnerReceiveSnapshot(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1}, Learners: []uint64{2}},
ConfState: pb.ConfState{Voters: []uint64{1}, Learners: []uint64{2}},
},
}

Expand Down Expand Up @@ -2881,7 +2881,7 @@ func TestRestoreIgnoreSnapshot(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: commit,
Term: 1,
ConfState: pb.ConfState{Nodes: []uint64{1, 2}},
ConfState: pb.ConfState{Voters: []uint64{1, 2}},
},
}

Expand Down Expand Up @@ -2909,7 +2909,7 @@ func TestProvideSnap(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1, 2}},
ConfState: pb.ConfState{Voters: []uint64{1, 2}},
},
}
storage := NewMemoryStorage()
Expand Down Expand Up @@ -2939,7 +2939,7 @@ func TestIgnoreProvidingSnap(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1, 2}},
ConfState: pb.ConfState{Voters: []uint64{1, 2}},
},
}
storage := NewMemoryStorage()
Expand Down Expand Up @@ -2967,7 +2967,7 @@ func TestRestoreFromSnapMsg(t *testing.T) {
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Nodes: []uint64{1, 2}},
ConfState: pb.ConfState{Voters: []uint64{1, 2}},
},
}
m := pb.Message{Type: pb.MsgSnap, From: 1, Term: 2, Snapshot: s}
Expand All @@ -2992,7 +2992,7 @@ func TestSlowNodeRestore(t *testing.T) {
}
lead := nt.peers[1].(*raft)
nextEnts(lead, nt.storage[1])
nt.storage[1].CreateSnapshot(lead.raftLog.applied, &pb.ConfState{Nodes: lead.prs.VoterNodes()}, nil)
nt.storage[1].CreateSnapshot(lead.raftLog.applied, &pb.ConfState{Voters: lead.prs.VoterNodes()}, nil)
nt.storage[1].Compact(lead.raftLog.applied)

nt.recover()
Expand Down Expand Up @@ -3469,7 +3469,7 @@ func TestLeaderTransferAfterSnapshot(t *testing.T) {
nt.send(pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{}}})
lead := nt.peers[1].(*raft)
nextEnts(lead, nt.storage[1])
nt.storage[1].CreateSnapshot(lead.raftLog.applied, &pb.ConfState{Nodes: lead.prs.VoterNodes()}, nil)
nt.storage[1].CreateSnapshot(lead.raftLog.applied, &pb.ConfState{Voters: lead.prs.VoterNodes()}, nil)
nt.storage[1].Compact(lead.raftLog.applied)

nt.recover()
Expand Down
Loading

0 comments on commit b9c051e

Please sign in to comment.