Browse Source

Merge pull request #1257 from bdarnell/cleanups

Raft: assorted cleanups (golint and go vet)
Xiang Li 11 years ago
parent
commit
c67fd14fe8
5 changed files with 27 additions and 18 deletions
  1. 1 0
      .gitignore
  2. 5 0
      raft/node.go
  3. 2 2
      raft/node_test.go
  4. 15 12
      raft/raft.go
  5. 4 4
      raft/raft_test.go

+ 1 - 0
.gitignore

@@ -8,3 +8,4 @@
 etcd
 etcd
 *.swp
 *.swp
 /hack/insta-discovery/.env
 /hack/insta-discovery/.env
+*.test

+ 5 - 0
raft/node.go

@@ -11,6 +11,8 @@ import (
 
 
 var (
 var (
 	emptyState = pb.HardState{}
 	emptyState = pb.HardState{}
+
+	// ErrStopped is returned by methods on Nodes that have been stopped.
 	ErrStopped = errors.New("raft: stopped")
 	ErrStopped = errors.New("raft: stopped")
 )
 )
 
 
@@ -68,10 +70,12 @@ func isHardStateEqual(a, b pb.HardState) bool {
 	return a.Term == b.Term && a.Vote == b.Vote && a.Commit == b.Commit
 	return a.Term == b.Term && a.Vote == b.Vote && a.Commit == b.Commit
 }
 }
 
 
+// IsEmptyHardState returns true if the given HardState is empty.
 func IsEmptyHardState(st pb.HardState) bool {
 func IsEmptyHardState(st pb.HardState) bool {
 	return isHardStateEqual(st, emptyState)
 	return isHardStateEqual(st, emptyState)
 }
 }
 
 
+// IsEmptySnap returns true if the given Snapshot is empty.
 func IsEmptySnap(sp pb.Snapshot) bool {
 func IsEmptySnap(sp pb.Snapshot) bool {
 	return sp.Index == 0
 	return sp.Index == 0
 }
 }
@@ -81,6 +85,7 @@ func (rd Ready) containsUpdates() bool {
 		len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0
 		len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0
 }
 }
 
 
+// Node represents a node in a raft cluster.
 type Node interface {
 type Node interface {
 	// Tick increments the internal logical clock for the Node by a single tick. Election
 	// Tick increments the internal logical clock for the Node by a single tick. Election
 	// timeouts and heartbeat timeouts are in units of ticks.
 	// timeouts and heartbeat timeouts are in units of ticks.

+ 2 - 2
raft/node_test.go

@@ -31,7 +31,7 @@ func TestNodeStep(t *testing.T) {
 			if msgt == msgBeat || msgt == msgHup {
 			if msgt == msgBeat || msgt == msgHup {
 				select {
 				select {
 				case <-n.recvc:
 				case <-n.recvc:
-					t.Errorf("%d: step should ignore msgHub/msgBeat", i, mtmap[i])
+					t.Errorf("%d: step should ignore %s", i, mtmap[i])
 				default:
 				default:
 				}
 				}
 			} else {
 			} else {
@@ -74,7 +74,7 @@ func TestNodeStepUnblock(t *testing.T) {
 		select {
 		select {
 		case err := <-errc:
 		case err := <-errc:
 			if err != tt.werr {
 			if err != tt.werr {
-				t.Errorf("#%d: err = %v, want %v", err, tt.werr)
+				t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
 			}
 			}
 			//clean up side-effect
 			//clean up side-effect
 			if ctx.Err() != nil {
 			if ctx.Err() != nil {

+ 15 - 12
raft/raft.go

@@ -9,6 +9,7 @@ import (
 	pb "github.com/coreos/etcd/raft/raftpb"
 	pb "github.com/coreos/etcd/raft/raftpb"
 )
 )
 
 
+// None is a placeholder node ID used when there is no leader.
 const None int64 = 0
 const None int64 = 0
 
 
 type messageType int64
 type messageType int64
@@ -26,15 +27,15 @@ const (
 )
 )
 
 
 var mtmap = [...]string{
 var mtmap = [...]string{
-	msgHup:      "msgHup",
-	msgBeat:     "msgBeat",
-	msgProp:     "msgProp",
-	msgApp:      "msgApp",
-	msgAppResp:  "msgAppResp",
-	msgVote:     "msgVote",
-	msgVoteResp: "msgVoteResp",
-	msgSnap:     "msgSnap",
-	msgDenied:   "msgDenied",
+	"msgHup",
+	"msgBeat",
+	"msgProp",
+	"msgApp",
+	"msgAppResp",
+	"msgVote",
+	"msgVoteResp",
+	"msgSnap",
+	"msgDenied",
 }
 }
 
 
 func (mt messageType) String() string {
 func (mt messageType) String() string {
@@ -43,18 +44,20 @@ func (mt messageType) String() string {
 
 
 var errNoLeader = errors.New("no leader")
 var errNoLeader = errors.New("no leader")
 
 
+// Possible values for StateType.
 const (
 const (
 	StateFollower StateType = iota
 	StateFollower StateType = iota
 	StateCandidate
 	StateCandidate
 	StateLeader
 	StateLeader
 )
 )
 
 
+// StateType represents the role of a node in a cluster.
 type StateType int64
 type StateType int64
 
 
 var stmap = [...]string{
 var stmap = [...]string{
-	StateFollower:  "StateFollower",
-	StateCandidate: "StateCandidate",
-	StateLeader:    "StateLeader",
+	"StateFollower",
+	"StateCandidate",
+	"StateLeader",
 }
 }
 
 
 func (st StateType) String() string {
 func (st StateType) String() string {

+ 4 - 4
raft/raft_test.go

@@ -92,13 +92,13 @@ func TestLogReplication(t *testing.T) {
 				t.Errorf("#%d.%d: committed = %d, want %d", i, j, sm.raftLog.committed, tt.wcommitted)
 				t.Errorf("#%d.%d: committed = %d, want %d", i, j, sm.raftLog.committed, tt.wcommitted)
 			}
 			}
 
 
-			ents := make([]pb.Entry, 0)
+			ents := []pb.Entry{}
 			for _, e := range nextEnts(sm) {
 			for _, e := range nextEnts(sm) {
 				if e.Data != nil {
 				if e.Data != nil {
 					ents = append(ents, e)
 					ents = append(ents, e)
 				}
 				}
 			}
 			}
-			props := make([]pb.Message, 0)
+			props := []pb.Message{}
 			for _, m := range tt.msgs {
 			for _, m := range tt.msgs {
 				if m.Type == msgProp {
 				if m.Type == msgProp {
 					props = append(props, m)
 					props = append(props, m)
@@ -651,7 +651,7 @@ func TestRecvMsgVote(t *testing.T) {
 			continue
 			continue
 		}
 		}
 		if g := msgs[0].Reject; g != tt.wreject {
 		if g := msgs[0].Reject; g != tt.wreject {
-			t.Errorf("#%d, m.Reject = %d, want %d", i, g, tt.wreject)
+			t.Errorf("#%d, m.Reject = %v, want %v", i, g, tt.wreject)
 		}
 		}
 	}
 	}
 }
 }
@@ -1270,7 +1270,7 @@ func (nw *network) recover() {
 }
 }
 
 
 func (nw *network) filter(msgs []pb.Message) []pb.Message {
 func (nw *network) filter(msgs []pb.Message) []pb.Message {
-	mm := make([]pb.Message, 0)
+	mm := []pb.Message{}
 	for _, m := range msgs {
 	for _, m := range msgs {
 		if nw.ignorem[m.Type] {
 		if nw.ignorem[m.Type] {
 			continue
 			continue