-
Notifications
You must be signed in to change notification settings - Fork 36
/
raft_fsm_candidate.go
147 lines (131 loc) · 3.62 KB
/
raft_fsm_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015 The etcd Authors
// Modified work copyright 2018 The tiglabs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package raft
import (
"fmt"
"github.com/tiglabs/raft/logger"
"github.com/tiglabs/raft/proto"
)
func (r *raftFsm) becomeCandidate() {
if r.state == stateLeader {
panic(AppPanicError(fmt.Sprintf("[raft->becomeCandidate][%v] invalid transition [leader -> candidate].", r.id)))
}
r.step = stepCandidate
r.reset(r.term+1, 0, false)
r.tick = r.tickElection
r.vote = r.config.NodeID
r.state = stateCandidate
if logger.IsEnableDebug() {
logger.Debug("raft[%v] became candidate at term %d.", r.id, r.term)
}
}
func stepCandidate(r *raftFsm, m *proto.Message) {
switch m.Type {
case proto.LocalMsgProp:
if logger.IsEnableDebug() {
logger.Debug("raft[%v] no leader at term %d; dropping proposal.", r.id, r.term)
}
proto.ReturnMessage(m)
return
case proto.ReqMsgAppend:
r.becomeFollower(r.term, m.From)
r.handleAppendEntries(m)
proto.ReturnMessage(m)
return
case proto.ReqMsgHeartBeat:
r.becomeFollower(r.term, m.From)
return
case proto.ReqMsgElectAck:
r.becomeFollower(r.term, m.From)
nmsg := proto.GetMessage()
nmsg.Type = proto.RespMsgElectAck
nmsg.To = m.From
r.send(nmsg)
proto.ReturnMessage(m)
return
case proto.ReqMsgVote:
if logger.IsEnableDebug() {
logger.Debug("raft[%v] [logterm: %d, index: %d, vote: %v] rejected vote from %v [logterm: %d, index: %d] at term %d.", r.id, r.raftLog.lastTerm(), r.raftLog.lastIndex(), r.vote, m.From, m.LogTerm, m.Index, r.term)
}
nmsg := proto.GetMessage()
nmsg.Type = proto.RespMsgVote
nmsg.To = m.From
nmsg.Reject = true
r.send(nmsg)
proto.ReturnMessage(m)
return
case proto.RespMsgVote:
gr := r.poll(m.From, !m.Reject)
if logger.IsEnableDebug() {
logger.Debug("raft[%v] [q:%d] has received %d votes and %d vote rejections.", r.id, r.quorum(), gr, len(r.votes)-gr)
}
switch r.quorum() {
case gr:
if r.config.LeaseCheck {
r.becomeElectionAck()
} else {
r.becomeLeader()
r.bcastAppend()
}
case len(r.votes) - gr:
r.becomeFollower(r.term, NoLeader)
}
}
}
func (r *raftFsm) campaign(force bool) {
r.becomeCandidate()
if r.quorum() == r.poll(r.config.NodeID, true) {
if r.config.LeaseCheck {
r.becomeElectionAck()
} else {
r.becomeLeader()
}
return
}
for id := range r.replicas {
if id == r.config.NodeID {
continue
}
li, lt := r.raftLog.lastIndexAndTerm()
if logger.IsEnableDebug() {
logger.Debug("[raft->campaign][%v logterm: %d, index: %d] sent vote request to %v at term %d.", r.id, lt, li, id, r.term)
}
m := proto.GetMessage()
m.To = id
m.Type = proto.ReqMsgVote
m.ForceVote = force
m.Index = li
m.LogTerm = lt
r.send(m)
}
}
func (r *raftFsm) poll(id uint64, v bool) (granted int) {
if logger.IsEnableDebug() {
if v {
logger.Debug("raft[%v] received vote from %v at term %d.", r.id, id, r.term)
} else {
logger.Debug("raft[%v] received vote rejection from %v at term %d.", r.id, id, r.term)
}
}
if _, ok := r.votes[id]; !ok {
r.votes[id] = v
}
for _, vv := range r.votes {
if vv {
granted++
}
}
return granted
}