mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #1257 from bdarnell/cleanups
Raft: assorted cleanups (golint and go vet)
This commit is contained in:
commit
c67fd14fe8
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@
|
||||
etcd
|
||||
*.swp
|
||||
/hack/insta-discovery/.env
|
||||
*.test
|
||||
|
@ -11,6 +11,8 @@ import (
|
||||
|
||||
var (
|
||||
emptyState = pb.HardState{}
|
||||
|
||||
// ErrStopped is returned by methods on Nodes that have been 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
|
||||
}
|
||||
|
||||
// IsEmptyHardState returns true if the given HardState is empty.
|
||||
func IsEmptyHardState(st pb.HardState) bool {
|
||||
return isHardStateEqual(st, emptyState)
|
||||
}
|
||||
|
||||
// IsEmptySnap returns true if the given Snapshot is empty.
|
||||
func IsEmptySnap(sp pb.Snapshot) bool {
|
||||
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
|
||||
}
|
||||
|
||||
// Node represents a node in a raft cluster.
|
||||
type Node interface {
|
||||
// Tick increments the internal logical clock for the Node by a single tick. Election
|
||||
// timeouts and heartbeat timeouts are in units of ticks.
|
||||
|
@ -31,7 +31,7 @@ func TestNodeStep(t *testing.T) {
|
||||
if msgt == msgBeat || msgt == msgHup {
|
||||
select {
|
||||
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:
|
||||
}
|
||||
} else {
|
||||
@ -74,7 +74,7 @@ func TestNodeStepUnblock(t *testing.T) {
|
||||
select {
|
||||
case err := <-errc:
|
||||
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
|
||||
if ctx.Err() != nil {
|
||||
|
27
raft/raft.go
27
raft/raft.go
@ -9,6 +9,7 @@ import (
|
||||
pb "github.com/coreos/etcd/raft/raftpb"
|
||||
)
|
||||
|
||||
// None is a placeholder node ID used when there is no leader.
|
||||
const None int64 = 0
|
||||
|
||||
type messageType int64
|
||||
@ -26,15 +27,15 @@ const (
|
||||
)
|
||||
|
||||
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 {
|
||||
@ -43,18 +44,20 @@ func (mt messageType) String() string {
|
||||
|
||||
var errNoLeader = errors.New("no leader")
|
||||
|
||||
// Possible values for StateType.
|
||||
const (
|
||||
StateFollower StateType = iota
|
||||
StateCandidate
|
||||
StateLeader
|
||||
)
|
||||
|
||||
// StateType represents the role of a node in a cluster.
|
||||
type StateType int64
|
||||
|
||||
var stmap = [...]string{
|
||||
StateFollower: "StateFollower",
|
||||
StateCandidate: "StateCandidate",
|
||||
StateLeader: "StateLeader",
|
||||
"StateFollower",
|
||||
"StateCandidate",
|
||||
"StateLeader",
|
||||
}
|
||||
|
||||
func (st StateType) String() string {
|
||||
|
@ -92,13 +92,13 @@ func TestLogReplication(t *testing.T) {
|
||||
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) {
|
||||
if e.Data != nil {
|
||||
ents = append(ents, e)
|
||||
}
|
||||
}
|
||||
props := make([]pb.Message, 0)
|
||||
props := []pb.Message{}
|
||||
for _, m := range tt.msgs {
|
||||
if m.Type == msgProp {
|
||||
props = append(props, m)
|
||||
@ -651,7 +651,7 @@ func TestRecvMsgVote(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
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 {
|
||||
mm := make([]pb.Message, 0)
|
||||
mm := []pb.Message{}
|
||||
for _, m := range msgs {
|
||||
if nw.ignorem[m.Type] {
|
||||
continue
|
||||
|
Loading…
x
Reference in New Issue
Block a user