mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #1149 from unihorn/137
raft: stop tickElection when the node is not in peer list
This commit is contained in:
11
raft/raft.go
11
raft/raft.go
@@ -265,6 +265,10 @@ func (r *raft) appendEntry(e pb.Entry) {
|
||||
|
||||
// tickElection is ran by followers and candidates after r.electionTimeout.
|
||||
func (r *raft) tickElection() {
|
||||
if !r.promotable() {
|
||||
r.elapsed = 0
|
||||
return
|
||||
}
|
||||
r.elapsed++
|
||||
// TODO (xiangli): elctionTimeout should be randomized.
|
||||
if r.elapsed > r.electionTimeout {
|
||||
@@ -530,6 +534,13 @@ func (r *raft) delProgress(id int64) {
|
||||
delete(r.prs, id)
|
||||
}
|
||||
|
||||
// promotable indicates whether state machine can be promoted to leader,
|
||||
// which is true when its own id is in progress list.
|
||||
func (r *raft) promotable() bool {
|
||||
_, ok := r.prs[r.id]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (r *raft) loadEnts(ents []pb.Entry) {
|
||||
r.raftLog.load(ents)
|
||||
}
|
||||
|
||||
@@ -1053,6 +1053,28 @@ func TestRemoveNode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromotable(t *testing.T) {
|
||||
id := int64(1)
|
||||
tests := []struct {
|
||||
peers []int64
|
||||
wp bool
|
||||
}{
|
||||
{[]int64{1}, true},
|
||||
{[]int64{1, 2, 3}, true},
|
||||
{[]int64{}, false},
|
||||
{[]int64{2, 3}, false},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
r := &raft{id: id, prs: make(map[int64]*progress)}
|
||||
for _, id := range tt.peers {
|
||||
r.prs[id] = &progress{}
|
||||
}
|
||||
if g := r.promotable(); g != tt.wp {
|
||||
t.Errorf("#%d: promotable = %v, want %v", i, g, tt.wp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ents(terms ...int64) *raft {
|
||||
ents := []pb.Entry{{}}
|
||||
for _, term := range terms {
|
||||
|
||||
Reference in New Issue
Block a user