-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_candidate.go
56 lines (47 loc) · 1.59 KB
/
state_candidate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package raft
type candidateState struct {
node *node
}
func newCandidateState(n *node) state {
//logger.Tracef("%s newCandidateState",node.address)
s := &candidateState{
node: n,
}
s.Start()
return s
}
func (s *candidateState) Start() {
s.node.ready = false
s.node.leader.Store("")
s.node.currentTerm.Add(1)
s.node.votedFor.Store(s.node.address)
s.node.election.Random(true)
s.node.election.Reset()
s.node.requestVotes()
s.node.logger.Tracef("%s candidateState.Start Term :%d", s.node.address, s.node.currentTerm.Load())
}
func (s *candidateState) Update() bool {
return false
}
func (s *candidateState) FixedUpdate() {
if s.node.election.Timeout() {
s.node.logger.Tracef("%s candidateState.FixedUpdate ElectionTimeout Votes %d Quorum %d Term %d", s.node.address, s.node.votes.Count(), s.node.Quorum(), s.node.currentTerm.Load())
s.node.stay()
} else if s.node.votes.Count() >= s.node.Quorum() {
s.node.logger.Tracef("%s candidateState.FixedUpdate request Enough Votes %d Quorum %d Term %d", s.node.address, s.node.votes.Count(), s.node.Quorum(), s.node.currentTerm.Load())
s.node.nextState()
}
}
func (s *candidateState) String() string {
return candidate
}
func (s *candidateState) StepDown() state {
s.node.logger.Tracef("%s candidateState.StepDown", s.node.address)
return newFollowerState(s.node)
}
func (s *candidateState) NextState() state {
s.node.logger.Tracef("%s candidateState.NextState", s.node.address)
return newLeaderState(s.node)
}