Skip to content

Commit

Permalink
Merge pull request etcd-io#7830 from aaronlehmann/new-nodes-start-active
Browse files Browse the repository at this point in the history
raft: Set the RecentActive flag for newly added nodes
  • Loading branch information
xiang90 committed May 5, 2017
2 parents 1f8de1a + 9451fa1 commit db6f45e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,10 @@ func (r *raft) addNode(id uint64) {
}

r.setProgress(id, 0, r.raftLog.lastIndex()+1)
// When a node is first added, we should mark it as recently active.
// Otherwise, CheckQuorum may cause us to step down if it is invoked
// before the added node has a chance to communicate with us.
r.prs[id].RecentActive = true
}

func (r *raft) removeNode(id uint64) {
Expand Down
35 changes: 35 additions & 0 deletions raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,41 @@ func TestAddNode(t *testing.T) {
}
}

// TestAddNodeCheckQuorum tests that addNode does not trigger a leader election
// immediately when checkQuorum is set.
func TestAddNodeCheckQuorum(t *testing.T) {
r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage())
r.pendingConf = true
r.checkQuorum = true

r.becomeCandidate()
r.becomeLeader()

for i := 0; i < r.electionTimeout-1; i++ {
r.tick()
}

r.addNode(2)

// This tick will reach electionTimeout, which triggers a quorum check.
r.tick()

// Node 1 should still be the leader after a single tick.
if r.state != StateLeader {
t.Errorf("state = %v, want %v", r.state, StateLeader)
}

// After another electionTimeout ticks without hearing from node 2,
// node 1 should step down.
for i := 0; i < r.electionTimeout; i++ {
r.tick()
}

if r.state != StateFollower {
t.Errorf("state = %v, want %v", r.state, StateFollower)
}
}

// TestRemoveNode tests that removeNode could update pendingConf, nodes and
// and removed list correctly.
func TestRemoveNode(t *testing.T) {
Expand Down

0 comments on commit db6f45e

Please sign in to comment.