Browse Source

raft: candidate should become leader directly if it is a one node cluster

Xiang Li 11 years ago
parent
commit
2066ff5acb
1 changed files with 4 additions and 1 deletions
  1. 4 1
      raft/raft.go

+ 4 - 1
raft/raft.go

@@ -204,7 +204,6 @@ func (sm *stateMachine) becomeCandidate() {
 	sm.term++
 	sm.vote = sm.addr
 	sm.state = stateCandidate
-	sm.poll(sm.addr, true)
 }
 
 func (sm *stateMachine) becomeLeader() {
@@ -228,6 +227,10 @@ func (sm *stateMachine) Step(m Message) {
 	switch m.Type {
 	case msgHup:
 		sm.becomeCandidate()
+		if sm.q() == sm.poll(sm.addr, true) {
+			sm.becomeLeader()
+			return
+		}
 		for i := 0; i < sm.k; i++ {
 			if i == sm.addr {
 				continue