| 1 |
// Copyright 2015 The etcd Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package raft |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"math" |
| 22 |
"math/rand" |
| 23 |
"sort" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"time" |
| 27 |
|
| 28 |
pb "github.com/coreos/etcd/raft/raftpb" |
| 29 |
) |
| 30 |
|
| 31 |
// None is a placeholder node ID used when there is no leader. |
| 32 |
const None uint64 = 0 |
| 33 |
const noLimit = math.MaxUint64 |
| 34 |
|
| 35 |
// Possible values for StateType. |
| 36 |
const ( |
| 37 |
StateFollower StateType = iota |
| 38 |
StateCandidate |
| 39 |
StateLeader |
| 40 |
StatePreCandidate |
| 41 |
numStates |
| 42 |
) |
| 43 |
|
| 44 |
type ReadOnlyOption int |
| 45 |
|
| 46 |
const ( |
| 47 |
// ReadOnlySafe guarantees the linearizability of the read only request by |
| 48 |
// communicating with the quorum. It is the default and suggested option. |
| 49 |
ReadOnlySafe ReadOnlyOption = iota |
| 50 |
// ReadOnlyLeaseBased ensures linearizability of the read only request by |
| 51 |
// relying on the leader lease. It can be affected by clock drift. |
| 52 |
// If the clock drift is unbounded, leader might keep the lease longer than it |
| 53 |
// should (clock can move backward/pause without any bound). ReadIndex is not safe |
| 54 |
// in that case. |
| 55 |
ReadOnlyLeaseBased |
| 56 |
) |
| 57 |
|
| 58 |
// Possible values for CampaignType |
| 59 |
const ( |
| 60 |
// campaignPreElection represents the first phase of a normal election when |
| 61 |
// Config.PreVote is true. |
| 62 |
campaignPreElection CampaignType = "CampaignPreElection" |
| 63 |
// campaignElection represents a normal (time-based) election (the second phase |
| 64 |
// of the election when Config.PreVote is true). |
| 65 |
campaignElection CampaignType = "CampaignElection" |
| 66 |
// campaignTransfer represents the type of leader transfer |
| 67 |
campaignTransfer CampaignType = "CampaignTransfer" |
| 68 |
) |
| 69 |
|
| 70 |
// lockedRand is a small wrapper around rand.Rand to provide |
| 71 |
// synchronization. Only the methods needed by the code are exposed |
| 72 |
// (e.g. Intn). |
| 73 |
type lockedRand struct { |
| 74 |
mu sync.Mutex |
| 75 |
rand *rand.Rand |
| 76 |
} |
| 77 |
|
| 78 |
func (r *lockedRand) Intn(n int) int { |
| 79 |
r.mu.Lock() |
| 80 |
v := r.rand.Intn(n) |
| 81 |
r.mu.Unlock() |
| 82 |
return v |
| 83 |
} |
| 84 |
|
| 85 |
var globalRand = &lockedRand{ |
| 86 |
rand: rand.New(rand.NewSource(time.Now().UnixNano())), |
| 87 |
} |
| 88 |
|
| 89 |
// CampaignType represents the type of campaigning |
| 90 |
// the reason we use the type of string instead of uint64 |
| 91 |
// is because it's simpler to compare and fill in raft entries |
| 92 |
type CampaignType string |
| 93 |
|
| 94 |
// StateType represents the role of a node in a cluster. |
| 95 |
type StateType uint64 |
| 96 |
|
| 97 |
var stmap = [...]string{ |
| 98 |
"StateFollower", |
| 99 |
"StateCandidate", |
| 100 |
"StateLeader", |
| 101 |
"StatePreCandidate", |
| 102 |
} |
| 103 |
|
| 104 |
func (st StateType) String() string { |
| 105 |
return stmap[uint64(st)] |
| 106 |
} |
| 107 |
|
| 108 |
// Config contains the parameters to start a raft. |
| 109 |
type Config struct { |
| 110 |
// ID is the identity of the local raft. ID cannot be 0. |
| 111 |
ID uint64 |
| 112 |
|
| 113 |
// peers contains the IDs of all nodes (including self) in the raft cluster. It |
| 114 |
// should only be set when starting a new raft cluster. Restarting raft from |
| 115 |
// previous configuration will panic if peers is set. peer is private and only |
| 116 |
// used for testing right now. |
| 117 |
peers []uint64 |
| 118 |
|
| 119 |
// learners contains the IDs of all leaner nodes (including self if the local node is a leaner) in the raft cluster. |
| 120 |
// learners only receives entries from the leader node. It does not vote or promote itself. |
| 121 |
learners []uint64 |
| 122 |
|
| 123 |
// ElectionTick is the number of Node.Tick invocations that must pass between |
| 124 |
// elections. That is, if a follower does not receive any message from the |
| 125 |
// leader of current term before ElectionTick has elapsed, it will become |
| 126 |
// candidate and start an election. ElectionTick must be greater than |
| 127 |
// HeartbeatTick. We suggest ElectionTick = 10 * HeartbeatTick to avoid |
| 128 |
// unnecessary leader switching. |
| 129 |
ElectionTick int |
| 130 |
// HeartbeatTick is the number of Node.Tick invocations that must pass between |
| 131 |
// heartbeats. That is, a leader sends heartbeat messages to maintain its |
| 132 |
// leadership every HeartbeatTick ticks. |
| 133 |
HeartbeatTick int |
| 134 |
|
| 135 |
// Storage is the storage for raft. raft generates entries and states to be |
| 136 |
// stored in storage. raft reads the persisted entries and states out of |
| 137 |
// Storage when it needs. raft reads out the previous state and configuration |
| 138 |
// out of storage when restarting. |
| 139 |
Storage Storage |
| 140 |
// Applied is the last applied index. It should only be set when restarting |
| 141 |
// raft. raft will not return entries to the application smaller or equal to |
| 142 |
// Applied. If Applied is unset when restarting, raft might return previous |
| 143 |
// applied entries. This is a very application dependent configuration. |
| 144 |
Applied uint64 |
| 145 |
|
| 146 |
// MaxSizePerMsg limits the max size of each append message. Smaller value |
| 147 |
// lowers the raft recovery cost(initial probing and message lost during normal |
| 148 |
// operation). On the other side, it might affect the throughput during normal |
| 149 |
// replication. Note: math.MaxUint64 for unlimited, 0 for at most one entry per |
| 150 |
// message. |
| 151 |
MaxSizePerMsg uint64 |
| 152 |
// MaxInflightMsgs limits the max number of in-flight append messages during |
| 153 |
// optimistic replication phase. The application transportation layer usually |
| 154 |
// has its own sending buffer over TCP/UDP. Setting MaxInflightMsgs to avoid |
| 155 |
// overflowing that sending buffer. TODO (xiangli): feedback to application to |
| 156 |
// limit the proposal rate? |
| 157 |
MaxInflightMsgs int |
| 158 |
|
| 159 |
// CheckQuorum specifies if the leader should check quorum activity. Leader |
| 160 |
// steps down when quorum is not active for an electionTimeout. |
| 161 |
CheckQuorum bool |
| 162 |
|
| 163 |
// PreVote enables the Pre-Vote algorithm described in raft thesis section |
| 164 |
// 9.6. This prevents disruption when a node that has been partitioned away |
| 165 |
// rejoins the cluster. |
| 166 |
PreVote bool |
| 167 |
|
| 168 |
// ReadOnlyOption specifies how the read only request is processed. |
| 169 |
// |
| 170 |
// ReadOnlySafe guarantees the linearizability of the read only request by |
| 171 |
// communicating with the quorum. It is the default and suggested option. |
| 172 |
// |
| 173 |
// ReadOnlyLeaseBased ensures linearizability of the read only request by |
| 174 |
// relying on the leader lease. It can be affected by clock drift. |
| 175 |
// If the clock drift is unbounded, leader might keep the lease longer than it |
| 176 |
// should (clock can move backward/pause without any bound). ReadIndex is not safe |
| 177 |
// in that case. |
| 178 |
// CheckQuorum MUST be enabled if ReadOnlyOption is ReadOnlyLeaseBased. |
| 179 |
ReadOnlyOption ReadOnlyOption |
| 180 |
|
| 181 |
// Logger is the logger used for raft log. For multinode which can host |
| 182 |
// multiple raft group, each raft group can have its own logger |
| 183 |
Logger Logger |
| 184 |
|
| 185 |
// DisableProposalForwarding set to true means that followers will drop |
| 186 |
// proposals, rather than forwarding them to the leader. One use case for |
| 187 |
// this feature would be in a situation where the Raft leader is used to |
| 188 |
// compute the data of a proposal, for example, adding a timestamp from a |
| 189 |
// hybrid logical clock to data in a monotonically increasing way. Forwarding |
| 190 |
// should be disabled to prevent a follower with an innaccurate hybrid |
| 191 |
// logical clock from assigning the timestamp and then forwarding the data |
| 192 |
// to the leader. |
| 193 |
DisableProposalForwarding bool |
| 194 |
} |
| 195 |
|
| 196 |
func (c *Config) validate() error { |
| 197 |
if c.ID == None { |
| 198 |
return errors.New("cannot use none as id") |
| 199 |
} |
| 200 |
|
| 201 |
if c.HeartbeatTick <= 0 { |
| 202 |
return errors.New("heartbeat tick must be greater than 0") |
| 203 |
} |
| 204 |
|
| 205 |
if c.ElectionTick <= c.HeartbeatTick { |
| 206 |
return errors.New("election tick must be greater than heartbeat tick") |
| 207 |
} |
| 208 |
|
| 209 |
if c.Storage == nil { |
| 210 |
return errors.New("storage cannot be nil") |
| 211 |
} |
| 212 |
|
| 213 |
if c.MaxInflightMsgs <= 0 { |
| 214 |
return errors.New("max inflight messages must be greater than 0") |
| 215 |
} |
| 216 |
|
| 217 |
if c.Logger == nil { |
| 218 |
c.Logger = raftLogger |
| 219 |
} |
| 220 |
|
| 221 |
if c.ReadOnlyOption == ReadOnlyLeaseBased && !c.CheckQuorum { |
| 222 |
return errors.New("CheckQuorum must be enabled when ReadOnlyOption is ReadOnlyLeaseBased") |
| 223 |
} |
| 224 |
|
| 225 |
return nil |
| 226 |
} |
| 227 |
|
| 228 |
type raft struct { |
| 229 |
id uint64 |
| 230 |
|
| 231 |
Term uint64 |
| 232 |
Vote uint64 |
| 233 |
|
| 234 |
readStates []ReadState |
| 235 |
|
| 236 |
// the log |
| 237 |
raftLog *raftLog |
| 238 |
|
| 239 |
maxInflight int |
| 240 |
maxMsgSize uint64 |
| 241 |
prs map[uint64]*Progress |
| 242 |
learnerPrs map[uint64]*Progress |
| 243 |
|
| 244 |
state StateType |
| 245 |
|
| 246 |
// isLearner is true if the local raft node is a learner. |
| 247 |
isLearner bool |
| 248 |
|
| 249 |
votes map[uint64]bool |
| 250 |
|
| 251 |
msgs []pb.Message |
| 252 |
|
| 253 |
// the leader id |
| 254 |
lead uint64 |
| 255 |
// leadTransferee is id of the leader transfer target when its value is not zero. |
| 256 |
// Follow the procedure defined in raft thesis 3.10. |
| 257 |
leadTransferee uint64 |
| 258 |
// New configuration is ignored if there exists unapplied configuration. |
| 259 |
pendingConf bool |
| 260 |
|
| 261 |
readOnly *readOnly |
| 262 |
|
| 263 |
// number of ticks since it reached last electionTimeout when it is leader |
| 264 |
// or candidate. |
| 265 |
// number of ticks since it reached last electionTimeout or received a |
| 266 |
// valid message from current leader when it is a follower. |
| 267 |
electionElapsed int |
| 268 |
|
| 269 |
// number of ticks since it reached last heartbeatTimeout. |
| 270 |
// only leader keeps heartbeatElapsed. |
| 271 |
heartbeatElapsed int |
| 272 |
|
| 273 |
checkQuorum bool |
| 274 |
preVote bool |
| 275 |
|
| 276 |
heartbeatTimeout int |
| 277 |
electionTimeout int |
| 278 |
// randomizedElectionTimeout is a random number between |
| 279 |
// [electiontimeout, 2 * electiontimeout - 1]. It gets reset |
| 280 |
// when raft changes its state to follower or candidate. |
| 281 |
randomizedElectionTimeout int |
| 282 |
disableProposalForwarding bool |
| 283 |
|
| 284 |
tick func() |
| 285 |
step stepFunc |
| 286 |
|
| 287 |
logger Logger |
| 288 |
} |
| 289 |
|
| 290 |
func newRaft(c *Config) *raft { |
| 291 |
if err := c.validate(); err != nil { |
| 292 |
panic(err.Error()) |
| 293 |
} |
| 294 |
raftlog := newLog(c.Storage, c.Logger) |
| 295 |
hs, cs, err := c.Storage.InitialState() |
| 296 |
if err != nil { |
| 297 |
panic(err) // TODO(bdarnell) |
| 298 |
} |
| 299 |
peers := c.peers |
| 300 |
learners := c.learners |
| 301 |
if len(cs.Nodes) > 0 || len(cs.Learners) > 0 { |
| 302 |
if len(peers) > 0 || len(learners) > 0 { |
| 303 |
// TODO(bdarnell): the peers argument is always nil except in |
| 304 |
// tests; the argument should be removed and these tests should be |
| 305 |
// updated to specify their nodes through a snapshot. |
| 306 |
panic("cannot specify both newRaft(peers, learners) and ConfState.(Nodes, Learners)") |
| 307 |
} |
| 308 |
peers = cs.Nodes |
| 309 |
learners = cs.Learners |
| 310 |
} |
| 311 |
r := &raft{ |
| 312 |
id: c.ID, |
| 313 |
lead: None, |
| 314 |
isLearner: false, |
| 315 |
raftLog: raftlog, |
| 316 |
maxMsgSize: c.MaxSizePerMsg, |
| 317 |
maxInflight: c.MaxInflightMsgs, |
| 318 |
prs: make(map[uint64]*Progress), |
| 319 |
learnerPrs: make(map[uint64]*Progress), |
| 320 |
electionTimeout: c.ElectionTick, |
| 321 |
heartbeatTimeout: c.HeartbeatTick, |
| 322 |
logger: c.Logger, |
| 323 |
checkQuorum: c.CheckQuorum, |
| 324 |
preVote: c.PreVote, |
| 325 |
readOnly: newReadOnly(c.ReadOnlyOption), |
| 326 |
disableProposalForwarding: c.DisableProposalForwarding, |
| 327 |
} |
| 328 |
for _, p := range peers { |
| 329 |
r.prs[p] = &Progress{Next: 1, ins: newInflights(r.maxInflight)} |
| 330 |
} |
| 331 |
for _, p := range learners { |
| 332 |
if _, ok := r.prs[p]; ok { |
| 333 |
panic(fmt.Sprintf("node %x is in both learner and peer list", p)) |
| 334 |
} |
| 335 |
r.learnerPrs[p] = &Progress{Next: 1, ins: newInflights(r.maxInflight), IsLearner: true} |
| 336 |
if r.id == p { |
| 337 |
r.isLearner = true |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
if !isHardStateEqual(hs, emptyState) { |
| 342 |
r.loadState(hs) |
| 343 |
} |
| 344 |
if c.Applied > 0 { |
| 345 |
raftlog.appliedTo(c.Applied) |
| 346 |
} |
| 347 |
r.becomeFollower(r.Term, None) |
| 348 |
|
| 349 |
var nodesStrs []string |
| 350 |
for _, n := range r.nodes() { |
| 351 |
nodesStrs = append(nodesStrs, fmt.Sprintf("%x", n)) |
| 352 |
} |
| 353 |
|
| 354 |
r.logger.Infof("newRaft %x [peers: [%s], term: %d, commit: %d, applied: %d, lastindex: %d, lastterm: %d]", |
| 355 |
r.id, strings.Join(nodesStrs, ","), r.Term, r.raftLog.committed, r.raftLog.applied, r.raftLog.lastIndex(), r.raftLog.lastTerm()) |
| 356 |
return r |
| 357 |
} |
| 358 |
|
| 359 |
func (r *raft) hasLeader() bool { return r.lead != None } |
| 360 |
|
| 361 |
func (r *raft) softState() *SoftState { return &SoftState{Lead: r.lead, RaftState: r.state} } |
| 362 |
|
| 363 |
func (r *raft) hardState() pb.HardState { |
| 364 |
return pb.HardState{ |
| 365 |
Term: r.Term, |
| 366 |
Vote: r.Vote, |
| 367 |
Commit: r.raftLog.committed, |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
func (r *raft) quorum() int { return len(r.prs)/2 + 1 } |
| 372 |
|
| 373 |
func (r *raft) nodes() []uint64 { |
| 374 |
nodes := make([]uint64, 0, len(r.prs)+len(r.learnerPrs)) |
| 375 |
for id := range r.prs { |
| 376 |
nodes = append(nodes, id) |
| 377 |
} |
| 378 |
for id := range r.learnerPrs { |
| 379 |
nodes = append(nodes, id) |
| 380 |
} |
| 381 |
sort.Sort(uint64Slice(nodes)) |
| 382 |
return nodes |
| 383 |
} |
| 384 |
|
| 385 |
// send persists state to stable storage and then sends to its mailbox. |
| 386 |
func (r *raft) send(m pb.Message) { |
| 387 |
m.From = r.id |
| 388 |
if m.Type == pb.MsgVote || m.Type == pb.MsgVoteResp || m.Type == pb.MsgPreVote || m.Type == pb.MsgPreVoteResp { |
| 389 |
if m.Term == 0 { |
| 390 |
// All {pre-,}campaign messages need to have the term set when |
| 391 |
// sending. |
| 392 |
// - MsgVote: m.Term is the term the node is campaigning for, |
| 393 |
// non-zero as we increment the term when campaigning. |
| 394 |
// - MsgVoteResp: m.Term is the new r.Term if the MsgVote was |
| 395 |
// granted, non-zero for the same reason MsgVote is |
| 396 |
// - MsgPreVote: m.Term is the term the node will campaign, |
| 397 |
// non-zero as we use m.Term to indicate the next term we'll be |
| 398 |
// campaigning for |
| 399 |
// - MsgPreVoteResp: m.Term is the term received in the original |
| 400 |
// MsgPreVote if the pre-vote was granted, non-zero for the |
| 401 |
// same reasons MsgPreVote is |
| 402 |
panic(fmt.Sprintf("term should be set when sending %s", m.Type)) |
| 403 |
} |
| 404 |
} else { |
| 405 |
if m.Term != 0 { |
| 406 |
panic(fmt.Sprintf("term should not be set when sending %s (was %d)", m.Type, m.Term)) |
| 407 |
} |
| 408 |
// do not attach term to MsgProp, MsgReadIndex |
| 409 |
// proposals are a way to forward to the leader and |
| 410 |
// should be treated as local message. |
| 411 |
// MsgReadIndex is also forwarded to leader. |
| 412 |
if m.Type != pb.MsgProp && m.Type != pb.MsgReadIndex { |
| 413 |
m.Term = r.Term |
| 414 |
} |
| 415 |
} |
| 416 |
r.msgs = append(r.msgs, m) |
| 417 |
} |
| 418 |
|
| 419 |
func (r *raft) getProgress(id uint64) *Progress { |
| 420 |
if pr, ok := r.prs[id]; ok { |
| 421 |
return pr |
| 422 |
} |
| 423 |
|
| 424 |
return r.learnerPrs[id] |
| 425 |
} |
| 426 |
|
| 427 |
// sendAppend sends RPC, with entries to the given peer. |
| 428 |
func (r *raft) sendAppend(to uint64) { |
| 429 |
pr := r.getProgress(to) |
| 430 |
if pr.IsPaused() { |
| 431 |
return |
| 432 |
} |
| 433 |
m := pb.Message{} |
| 434 |
m.To = to |
| 435 |
|
| 436 |
term, errt := r.raftLog.term(pr.Next - 1) |
| 437 |
ents, erre := r.raftLog.entries(pr.Next, r.maxMsgSize) |
| 438 |
|
| 439 |
if errt != nil || erre != nil { // send snapshot if we failed to get term or entries |
| 440 |
if !pr.RecentActive { |
| 441 |
r.logger.Debugf("ignore sending snapshot to %x since it is not recently active", to) |
| 442 |
return |
| 443 |
} |
| 444 |
|
| 445 |
m.Type = pb.MsgSnap |
| 446 |
snapshot, err := r.raftLog.snapshot() |
| 447 |
if err != nil { |
| 448 |
if err == ErrSnapshotTemporarilyUnavailable { |
| 449 |
r.logger.Debugf("%x failed to send snapshot to %x because snapshot is temporarily unavailable", r.id, to) |
| 450 |
return |
| 451 |
} |
| 452 |
panic(err) // TODO(bdarnell) |
| 453 |
} |
| 454 |
if IsEmptySnap(snapshot) { |
| 455 |
panic("need non-empty snapshot") |
| 456 |
} |
| 457 |
m.Snapshot = snapshot |
| 458 |
sindex, sterm := snapshot.Metadata.Index, snapshot.Metadata.Term |
| 459 |
r.logger.Debugf("%x [firstindex: %d, commit: %d] sent snapshot[index: %d, term: %d] to %x [%s]", |
| 460 |
r.id, r.raftLog.firstIndex(), r.raftLog.committed, sindex, sterm, to, pr) |
| 461 |
pr.becomeSnapshot(sindex) |
| 462 |
r.logger.Debugf("%x paused sending replication messages to %x [%s]", r.id, to, pr) |
| 463 |
} else { |
| 464 |
m.Type = pb.MsgApp |
| 465 |
m.Index = pr.Next - 1 |
| 466 |
m.LogTerm = term |
| 467 |
m.Entries = ents |
| 468 |
m.Commit = r.raftLog.committed |
| 469 |
if n := len(m.Entries); n != 0 { |
| 470 |
switch pr.State { |
| 471 |
// optimistically increase the next when in ProgressStateReplicate |
| 472 |
case ProgressStateReplicate: |
| 473 |
last := m.Entries[n-1].Index |
| 474 |
pr.optimisticUpdate(last) |
| 475 |
pr.ins.add(last) |
| 476 |
case ProgressStateProbe: |
| 477 |
pr.pause() |
| 478 |
default: |
| 479 |
r.logger.Panicf("%x is sending append in unhandled state %s", r.id, pr.State) |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
r.send(m) |
| 484 |
} |
| 485 |
|
| 486 |
// sendHeartbeat sends an empty MsgApp |
| 487 |
func (r *raft) sendHeartbeat(to uint64, ctx []byte) { |
| 488 |
// Attach the commit as min(to.matched, r.committed). |
| 489 |
// When the leader sends out heartbeat message, |
| 490 |
// the receiver(follower) might not be matched with the leader |
| 491 |
// or it might not have all the committed entries. |
| 492 |
// The leader MUST NOT forward the follower's commit to |
| 493 |
// an unmatched index. |
| 494 |
commit := min(r.getProgress(to).Match, r.raftLog.committed) |
| 495 |
m := pb.Message{ |
| 496 |
To: to, |
| 497 |
Type: pb.MsgHeartbeat, |
| 498 |
Commit: commit, |
| 499 |
Context: ctx, |
| 500 |
} |
| 501 |
|
| 502 |
r.send(m) |
| 503 |
} |
| 504 |
|
| 505 |
func (r *raft) forEachProgress(f func(id uint64, pr *Progress)) { |
| 506 |
for id, pr := range r.prs { |
| 507 |
f(id, pr) |
| 508 |
} |
| 509 |
|
| 510 |
for id, pr := range r.learnerPrs { |
| 511 |
f(id, pr) |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
// bcastAppend sends RPC, with entries to all peers that are not up-to-date |
| 516 |
// according to the progress recorded in r.prs. |
| 517 |
func (r *raft) bcastAppend() { |
| 518 |
r.forEachProgress(func(id uint64, _ *Progress) { |
| 519 |
if id == r.id { |
| 520 |
return |
| 521 |
} |
| 522 |
|
| 523 |
r.sendAppend(id) |
| 524 |
}) |
| 525 |
} |
| 526 |
|
| 527 |
// bcastHeartbeat sends RPC, without entries to all the peers. |
| 528 |
func (r *raft) bcastHeartbeat() { |
| 529 |
lastCtx := r.readOnly.lastPendingRequestCtx() |
| 530 |
if len(lastCtx) == 0 { |
| 531 |
r.bcastHeartbeatWithCtx(nil) |
| 532 |
} else { |
| 533 |
r.bcastHeartbeatWithCtx([]byte(lastCtx)) |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
func (r *raft) bcastHeartbeatWithCtx(ctx []byte) { |
| 538 |
r.forEachProgress(func(id uint64, _ *Progress) { |
| 539 |
if id == r.id { |
| 540 |
return |
| 541 |
} |
| 542 |
r.sendHeartbeat(id, ctx) |
| 543 |
}) |
| 544 |
} |
| 545 |
|
| 546 |
// maybeCommit attempts to advance the commit index. Returns true if |
| 547 |
// the commit index changed (in which case the caller should call |
| 548 |
// r.bcastAppend). |
| 549 |
func (r *raft) maybeCommit() bool { |
| 550 |
// TODO(bmizerany): optimize.. Currently naive |
| 551 |
mis := make(uint64Slice, 0, len(r.prs)) |
| 552 |
for _, p := range r.prs { |
| 553 |
mis = append(mis, p.Match) |
| 554 |
} |
| 555 |
sort.Sort(sort.Reverse(mis)) |
| 556 |
mci := mis[r.quorum()-1] |
| 557 |
return r.raftLog.maybeCommit(mci, r.Term) |
| 558 |
} |
| 559 |
|
| 560 |
func (r *raft) reset(term uint64) { |
| 561 |
if r.Term != term { |
| 562 |
r.Term = term |
| 563 |
r.Vote = None |
| 564 |
} |
| 565 |
r.lead = None |
| 566 |
|
| 567 |
r.electionElapsed = 0 |
| 568 |
r.heartbeatElapsed = 0 |
| 569 |
r.resetRandomizedElectionTimeout() |
| 570 |
|
| 571 |
r.abortLeaderTransfer() |
| 572 |
|
| 573 |
r.votes = make(map[uint64]bool) |
| 574 |
r.forEachProgress(func(id uint64, pr *Progress) { |
| 575 |
*pr = Progress{Next: r.raftLog.lastIndex() + 1, ins: newInflights(r.maxInflight), IsLearner: pr.IsLearner} |
| 576 |
if id == r.id { |
| 577 |
pr.Match = r.raftLog.lastIndex() |
| 578 |
} |
| 579 |
}) |
| 580 |
|
| 581 |
r.pendingConf = false |
| 582 |
r.readOnly = newReadOnly(r.readOnly.option) |
| 583 |
} |
| 584 |
|
| 585 |
func (r *raft) appendEntry(es ...pb.Entry) { |
| 586 |
li := r.raftLog.lastIndex() |
| 587 |
for i := range es { |
| 588 |
es[i].Term = r.Term |
| 589 |
es[i].Index = li + 1 + uint64(i) |
| 590 |
} |
| 591 |
r.raftLog.append(es...) |
| 592 |
r.getProgress(r.id).maybeUpdate(r.raftLog.lastIndex()) |
| 593 |
// Regardless of maybeCommit's return, our caller will call bcastAppend. |
| 594 |
r.maybeCommit() |
| 595 |
} |
| 596 |
|
| 597 |
// tickElection is run by followers and candidates after r.electionTimeout. |
| 598 |
func (r *raft) tickElection() { |
| 599 |
r.electionElapsed++ |
| 600 |
|
| 601 |
if r.promotable() && r.pastElectionTimeout() { |
| 602 |
r.electionElapsed = 0 |
| 603 |
r.Step(pb.Message{From: r.id, Type: pb.MsgHup}) |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
// tickHeartbeat is run by leaders to send a MsgBeat after r.heartbeatTimeout. |
| 608 |
func (r *raft) tickHeartbeat() { |
| 609 |
r.heartbeatElapsed++ |
| 610 |
r.electionElapsed++ |
| 611 |
|
| 612 |
if r.electionElapsed >= r.electionTimeout { |
| 613 |
r.electionElapsed = 0 |
| 614 |
if r.checkQuorum { |
| 615 |
r.Step(pb.Message{From: r.id, Type: pb.MsgCheckQuorum}) |
| 616 |
} |
| 617 |
// If current leader cannot transfer leadership in electionTimeout, it becomes leader again. |
| 618 |
if r.state == StateLeader && r.leadTransferee != None { |
| 619 |
r.abortLeaderTransfer() |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
if r.state != StateLeader { |
| 624 |
return |
| 625 |
} |
| 626 |
|
| 627 |
if r.heartbeatElapsed >= r.heartbeatTimeout { |
| 628 |
r.heartbeatElapsed = 0 |
| 629 |
r.Step(pb.Message{From: r.id, Type: pb.MsgBeat}) |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
func (r *raft) becomeFollower(term uint64, lead uint64) { |
| 634 |
r.step = stepFollower |
| 635 |
r.reset(term) |
| 636 |
r.tick = r.tickElection |
| 637 |
r.lead = lead |
| 638 |
r.state = StateFollower |
| 639 |
r.logger.Infof("%x became follower at term %d", r.id, r.Term) |
| 640 |
} |
| 641 |
|
| 642 |
func (r *raft) becomeCandidate() { |
| 643 |
// TODO(xiangli) remove the panic when the raft implementation is stable |
| 644 |
if r.state == StateLeader { |
| 645 |
panic("invalid transition [leader -> candidate]") |
| 646 |
} |
| 647 |
r.step = stepCandidate |
| 648 |
r.reset(r.Term + 1) |
| 649 |
r.tick = r.tickElection |
| 650 |
r.Vote = r.id |
| 651 |
r.state = StateCandidate |
| 652 |
r.logger.Infof("%x became candidate at term %d", r.id, r.Term) |
| 653 |
} |
| 654 |
|
| 655 |
func (r *raft) becomePreCandidate() { |
| 656 |
// TODO(xiangli) remove the panic when the raft implementation is stable |
| 657 |
if r.state == StateLeader { |
| 658 |
panic("invalid transition [leader -> pre-candidate]") |
| 659 |
} |
| 660 |
// Becoming a pre-candidate changes our step functions and state, |
| 661 |
// but doesn't change anything else. In particular it does not increase |
| 662 |
// r.Term or change r.Vote. |
| 663 |
r.step = stepCandidate |
| 664 |
r.votes = make(map[uint64]bool) |
| 665 |
r.tick = r.tickElection |
| 666 |
r.state = StatePreCandidate |
| 667 |
r.logger.Infof("%x became pre-candidate at term %d", r.id, r.Term) |
| 668 |
} |
| 669 |
|
| 670 |
func (r *raft) becomeLeader() { |
| 671 |
// TODO(xiangli) remove the panic when the raft implementation is stable |
| 672 |
if r.state == StateFollower { |
| 673 |
panic("invalid transition [follower -> leader]") |
| 674 |
} |
| 675 |
r.step = stepLeader |
| 676 |
r.reset(r.Term) |
| 677 |
r.tick = r.tickHeartbeat |
| 678 |
r.lead = r.id |
| 679 |
r.state = StateLeader |
| 680 |
ents, err := r.raftLog.entries(r.raftLog.committed+1, noLimit) |
| 681 |
if err != nil { |
| 682 |
r.logger.Panicf("unexpected error getting uncommitted entries (%v)", err) |
| 683 |
} |
| 684 |
|
| 685 |
nconf := numOfPendingConf(ents) |
| 686 |
if nconf > 1 { |
| 687 |
panic("unexpected multiple uncommitted config entry") |
| 688 |
} |
| 689 |
if nconf == 1 { |
| 690 |
r.pendingConf = true |
| 691 |
} |
| 692 |
|
| 693 |
r.appendEntry(pb.Entry{Data: nil}) |
| 694 |
r.logger.Infof("%x became leader at term %d", r.id, r.Term) |
| 695 |
} |
| 696 |
|
| 697 |
func (r *raft) campaign(t CampaignType) { |
| 698 |
var term uint64 |
| 699 |
var voteMsg pb.MessageType |
| 700 |
if t == campaignPreElection { |
| 701 |
r.becomePreCandidate() |
| 702 |
voteMsg = pb.MsgPreVote |
| 703 |
// PreVote RPCs are sent for the next term before we've incremented r.Term. |
| 704 |
term = r.Term + 1 |
| 705 |
} else { |
| 706 |
r.becomeCandidate() |
| 707 |
voteMsg = pb.MsgVote |
| 708 |
term = r.Term |
| 709 |
} |
| 710 |
if r.quorum() == r.poll(r.id, voteRespMsgType(voteMsg), true) { |
| 711 |
// We won the election after voting for ourselves (which must mean that |
| 712 |
// this is a single-node cluster). Advance to the next state. |
| 713 |
if t == campaignPreElection { |
| 714 |
r.campaign(campaignElection) |
| 715 |
} else { |
| 716 |
r.becomeLeader() |
| 717 |
} |
| 718 |
return |
| 719 |
} |
| 720 |
for id := range r.prs { |
| 721 |
if id == r.id { |
| 722 |
continue |
| 723 |
} |
| 724 |
r.logger.Infof("%x [logterm: %d, index: %d] sent %s request to %x at term %d", |
| 725 |
r.id, r.raftLog.lastTerm(), r.raftLog.lastIndex(), voteMsg, id, r.Term) |
| 726 |
|
| 727 |
var ctx []byte |
| 728 |
if t == campaignTransfer { |
| 729 |
ctx = []byte(t) |
| 730 |
} |
| 731 |
r.send(pb.Message{Term: term, To: id, Type: voteMsg, Index: r.raftLog.lastIndex(), LogTerm: r.raftLog.lastTerm(), Context: ctx}) |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
func (r *raft) poll(id uint64, t pb.MessageType, v bool) (granted int) { |
| 736 |
if v { |
| 737 |
r.logger.Infof("%x received %s from %x at term %d", r.id, t, id, r.Term) |
| 738 |
} else { |
| 739 |
r.logger.Infof("%x received %s rejection from %x at term %d", r.id, t, id, r.Term) |
| 740 |
} |
| 741 |
if _, ok := r.votes[id]; !ok { |
| 742 |
r.votes[id] = v |
| 743 |
} |
| 744 |
for _, vv := range r.votes { |
| 745 |
if vv { |
| 746 |
granted++ |
| 747 |
} |
| 748 |
} |
| 749 |
return granted |
| 750 |
} |
| 751 |
|
| 752 |
func (r *raft) Step(m pb.Message) error { |
| 753 |
// Handle the message term, which may result in our stepping down to a follower. |
| 754 |
switch { |
| 755 |
case m.Term == 0: |
| 756 |
// local message |
| 757 |
case m.Term > r.Term: |
| 758 |
if m.Type == pb.MsgVote || m.Type == pb.MsgPreVote { |
| 759 |
force := bytes.Equal(m.Context, []byte(campaignTransfer)) |
| 760 |
inLease := r.checkQuorum && r.lead != None && r.electionElapsed < r.electionTimeout |
| 761 |
if !force && inLease { |
| 762 |
// If a server receives a RequestVote request within the minimum election timeout |
| 763 |
// of hearing from a current leader, it does not update its term or grant its vote |
| 764 |
r.logger.Infof("%x [logterm: %d, index: %d, vote: %x] ignored %s from %x [logterm: %d, index: %d] at term %d: lease is not expired (remaining ticks: %d)", |
| 765 |
r.id, r.raftLog.lastTerm(), r.raftLog.lastIndex(), r.Vote, m.Type, m.From, m.LogTerm, m.Index, r.Term, r.electionTimeout-r.electionElapsed) |
| 766 |
return nil |
| 767 |
} |
| 768 |
} |
| 769 |
switch { |
| 770 |
case m.Type == pb.MsgPreVote: |
| 771 |
// Never change our term in response to a PreVote |
| 772 |
case m.Type == pb.MsgPreVoteResp && !m.Reject: |
| 773 |
// We send pre-vote requests with a term in our future. If the |
| 774 |
// pre-vote is granted, we will increment our term when we get a |
| 775 |
// quorum. If it is not, the term comes from the node that |
| 776 |
// rejected our vote so we should become a follower at the new |
| 777 |
// term. |
| 778 |
default: |
| 779 |
r.logger.Infof("%x [term: %d] received a %s message with higher term from %x [term: %d]", |
| 780 |
r.id, r.Term, m.Type, m.From, m.Term) |
| 781 |
if m.Type == pb.MsgApp || m.Type == pb.MsgHeartbeat || m.Type == pb.MsgSnap { |
| 782 |
r.becomeFollower(m.Term, m.From) |
| 783 |
} else { |
| 784 |
r.becomeFollower(m.Term, None) |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
case m.Term < r.Term: |
| 789 |
if r.checkQuorum && (m.Type == pb.MsgHeartbeat || m.Type == pb.MsgApp) { |
| 790 |
// We have received messages from a leader at a lower term. It is possible |
| 791 |
// that these messages were simply delayed in the network, but this could |
| 792 |
// also mean that this node has advanced its term number during a network |
| 793 |
// partition, and it is now unable to either win an election or to rejoin |
| 794 |
// the majority on the old term. If checkQuorum is false, this will be |
| 795 |
// handled by incrementing term numbers in response to MsgVote with a |
| 796 |
// higher term, but if checkQuorum is true we may not advance the term on |
| 797 |
// MsgVote and must generate other messages to advance the term. The net |
| 798 |
// result of these two features is to minimize the disruption caused by |
| 799 |
// nodes that have been removed from the cluster's configuration: a |
| 800 |
// removed node will send MsgVotes (or MsgPreVotes) which will be ignored, |
| 801 |
// but it will not receive MsgApp or MsgHeartbeat, so it will not create |
| 802 |
// disruptive term increases |
| 803 |
r.send(pb.Message{To: m.From, Type: pb.MsgAppResp}) |
| 804 |
} else { |
| 805 |
// ignore other cases |
| 806 |
r.logger.Infof("%x [term: %d] ignored a %s message with lower term from %x [term: %d]", |
| 807 |
r.id, r.Term, m.Type, m.From, m.Term) |
| 808 |
} |
| 809 |
return nil |
| 810 |
} |
| 811 |
|
| 812 |
switch m.Type { |
| 813 |
case pb.MsgHup: |
| 814 |
if r.state != StateLeader { |
| 815 |
ents, err := r.raftLog.slice(r.raftLog.applied+1, r.raftLog.committed+1, noLimit) |
| 816 |
if err != nil { |
| 817 |
r.logger.Panicf("unexpected error getting unapplied entries (%v)", err) |
| 818 |
} |
| 819 |
if n := numOfPendingConf(ents); n != 0 && r.raftLog.committed > r.raftLog.applied { |
| 820 |
r.logger.Warningf("%x cannot campaign at term %d since there are still %d pending configuration changes to apply", r.id, r.Term, n) |
| 821 |
return nil |
| 822 |
} |
| 823 |
|
| 824 |
r.logger.Infof("%x is starting a new election at term %d", r.id, r.Term) |
| 825 |
if r.preVote { |
| 826 |
r.campaign(campaignPreElection) |
| 827 |
} else { |
| 828 |
r.campaign(campaignElection) |
| 829 |
} |
| 830 |
} else { |
| 831 |
r.logger.Debugf("%x ignoring MsgHup because already leader", r.id) |
| 832 |
} |
| 833 |
|
| 834 |
case pb.MsgVote, pb.MsgPreVote: |
| 835 |
if r.isLearner { |
| 836 |
// TODO: learner may need to vote, in case of node down when confchange. |
| 837 |
r.logger.Infof("%x [logterm: %d, index: %d, vote: %x] ignored %s from %x [logterm: %d, index: %d] at term %d: learner can not vote", |
| 838 |
r.id, r.raftLog.lastTerm(), r.raftLog.lastIndex(), r.Vote, m.Type, m.From, m.LogTerm, m.Index, r.Term) |
| 839 |
return nil |
| 840 |
} |
| 841 |
// The m.Term > r.Term clause is for MsgPreVote. For MsgVote m.Term should |
| 842 |
// always equal r.Term. |
| 843 |
if (r.Vote == None || m.Term > r.Term || r.Vote == m.From) && r.raftLog.isUpToDate(m.Index, m.LogTerm) { |
| 844 |
r.logger.Infof("%x [logterm: %d, index: %d, vote: %x] cast %s for %x [logterm: %d, index: %d] at term %d", |
| 845 |
r.id, r.raftLog.lastTerm(), r.raftLog.lastIndex(), r.Vote, m.Type, m.From, m.LogTerm, m.Index, r.Term) |
| 846 |
// When responding to Msg{Pre,}Vote messages we include the term |
| 847 |
// from the message, not the local term. To see why consider the |
| 848 |
// case where a single node was previously partitioned away and |
| 849 |
// it's local term is now of date. If we include the local term |
| 850 |
// (recall that for pre-votes we don't update the local term), the |
| 851 |
// (pre-)campaigning node on the other end will proceed to ignore |
| 852 |
// the message (it ignores all out of date messages). |
| 853 |
// The term in the original message and current local term are the |
| 854 |
// same in the case of regular votes, but different for pre-votes. |
| 855 |
r.send(pb.Message{To: m.From, Term: m.Term, Type: voteRespMsgType(m.Type)}) |
| 856 |
if m.Type == pb.MsgVote { |
| 857 |
// Only record real votes. |
| 858 |
r.electionElapsed = 0 |
| 859 |
r.Vote = m.From |
| 860 |
} |
| 861 |
} else { |
| 862 |
r.logger.Infof("%x [logterm: %d, index: %d, vote: %x] rejected %s from %x [logterm: %d, index: %d] at term %d", |
| 863 |
r.id, r.raftLog.lastTerm(), r.raftLog.lastIndex(), r.Vote, m.Type, m.From, m.LogTerm, m.Index, r.Term) |
| 864 |
r.send(pb.Message{To: m.From, Term: r.Term, Type: voteRespMsgType(m.Type), Reject: true}) |
| 865 |
} |
| 866 |
|
| 867 |
default: |
| 868 |
r.step(r, m) |
| 869 |
} |
| 870 |
return nil |
| 871 |
} |
| 872 |
|
| 873 |
type stepFunc func(r *raft, m pb.Message) |
| 874 |
|
| 875 |
func stepLeader(r *raft, m pb.Message) { |
| 876 |
// These message types do not require any progress for m.From. |
| 877 |
switch m.Type { |
| 878 |
case pb.MsgBeat: |
| 879 |
r.bcastHeartbeat() |
| 880 |
return |
| 881 |
case pb.MsgCheckQuorum: |
| 882 |
if !r.checkQuorumActive() { |
| 883 |
r.logger.Warningf("%x stepped down to follower since quorum is not active", r.id) |
| 884 |
r.becomeFollower(r.Term, None) |
| 885 |
} |
| 886 |
return |
| 887 |
case pb.MsgProp: |
| 888 |
if len(m.Entries) == 0 { |
| 889 |
r.logger.Panicf("%x stepped empty MsgProp", r.id) |
| 890 |
} |
| 891 |
if _, ok := r.prs[r.id]; !ok { |
| 892 |
// If we are not currently a member of the range (i.e. this node |
| 893 |
// was removed from the configuration while serving as leader), |
| 894 |
// drop any new proposals. |
| 895 |
return |
| 896 |
} |
| 897 |
if r.leadTransferee != None { |
| 898 |
r.logger.Debugf("%x [term %d] transfer leadership to %x is in progress; dropping proposal", r.id, r.Term, r.leadTransferee) |
| 899 |
return |
| 900 |
} |
| 901 |
|
| 902 |
for i, e := range m.Entries { |
| 903 |
if e.Type == pb.EntryConfChange { |
| 904 |
if r.pendingConf { |
| 905 |
r.logger.Infof("propose conf %s ignored since pending unapplied configuration", e.String()) |
| 906 |
m.Entries[i] = pb.Entry{Type: pb.EntryNormal} |
| 907 |
} |
| 908 |
r.pendingConf = true |
| 909 |
} |
| 910 |
} |
| 911 |
r.appendEntry(m.Entries...) |
| 912 |
r.bcastAppend() |
| 913 |
return |
| 914 |
case pb.MsgReadIndex: |
| 915 |
if r.quorum() > 1 { |
| 916 |
if r.raftLog.zeroTermOnErrCompacted(r.raftLog.term(r.raftLog.committed)) != r.Term { |
| 917 |
// Reject read only request when this leader has not committed any log entry at its term. |
| 918 |
return |
| 919 |
} |
| 920 |
|
| 921 |
// thinking: use an interally defined context instead of the user given context. |
| 922 |
// We can express this in terms of the term and index instead of a user-supplied value. |
| 923 |
// This would allow multiple reads to piggyback on the same message. |
| 924 |
switch r.readOnly.option { |
| 925 |
case ReadOnlySafe: |
| 926 |
r.readOnly.addRequest(r.raftLog.committed, m) |
| 927 |
r.bcastHeartbeatWithCtx(m.Entries[0].Data) |
| 928 |
case ReadOnlyLeaseBased: |
| 929 |
ri := r.raftLog.committed |
| 930 |
if m.From == None || m.From == r.id { // from local member |
| 931 |
r.readStates = append(r.readStates, ReadState{Index: r.raftLog.committed, RequestCtx: m.Entries[0].Data}) |
| 932 |
} else { |
| 933 |
r.send(pb.Message{To: m.From, Type: pb.MsgReadIndexResp, Index: ri, Entries: m.Entries}) |
| 934 |
} |
| 935 |
} |
| 936 |
} else { |
| 937 |
r.readStates = append(r.readStates, ReadState{Index: r.raftLog.committed, RequestCtx: m.Entries[0].Data}) |
| 938 |
} |
| 939 |
|
| 940 |
return |
| 941 |
} |
| 942 |
|
| 943 |
// All other message types require a progress for m.From (pr). |
| 944 |
pr := r.getProgress(m.From) |
| 945 |
if pr == nil { |
| 946 |
r.logger.Debugf("%x no progress available for %x", r.id, m.From) |
| 947 |
return |
| 948 |
} |
| 949 |
switch m.Type { |
| 950 |
case pb.MsgAppResp: |
| 951 |
pr.RecentActive = true |
| 952 |
|
| 953 |
if m.Reject { |
| 954 |
r.logger.Debugf("%x received msgApp rejection(lastindex: %d) from %x for index %d", |
| 955 |
r.id, m.RejectHint, m.From, m.Index) |
| 956 |
if pr.maybeDecrTo(m.Index, m.RejectHint) { |
| 957 |
r.logger.Debugf("%x decreased progress of %x to [%s]", r.id, m.From, pr) |
| 958 |
if pr.State == ProgressStateReplicate { |
| 959 |
pr.becomeProbe() |
| 960 |
} |
| 961 |
r.sendAppend(m.From) |
| 962 |
} |
| 963 |
} else { |
| 964 |
oldPaused := pr.IsPaused() |
| 965 |
if pr.maybeUpdate(m.Index) { |
| 966 |
switch { |
| 967 |
case pr.State == ProgressStateProbe: |
| 968 |
pr.becomeReplicate() |
| 969 |
case pr.State == ProgressStateSnapshot && pr.needSnapshotAbort(): |
| 970 |
r.logger.Debugf("%x snapshot aborted, resumed sending replication messages to %x [%s]", r.id, m.From, pr) |
| 971 |
pr.becomeProbe() |
| 972 |
case pr.State == ProgressStateReplicate: |
| 973 |
pr.ins.freeTo(m.Index) |
| 974 |
} |
| 975 |
|
| 976 |
if r.maybeCommit() { |
| 977 |
r.bcastAppend() |
| 978 |
} else if oldPaused { |
| 979 |
// update() reset the wait state on this node. If we had delayed sending |
| 980 |
// an update before, send it now. |
| 981 |
r.sendAppend(m.From) |
| 982 |
} |
| 983 |
// Transfer leadership is in progress. |
| 984 |
if m.From == r.leadTransferee && pr.Match == r.raftLog.lastIndex() { |
| 985 |
r.logger.Infof("%x sent MsgTimeoutNow to %x after received MsgAppResp", r.id, m.From) |
| 986 |
r.sendTimeoutNow(m.From) |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
case pb.MsgHeartbeatResp: |
| 991 |
pr.RecentActive = true |
| 992 |
pr.resume() |
| 993 |
|
| 994 |
// free one slot for the full inflights window to allow progress. |
| 995 |
if pr.State == ProgressStateReplicate && pr.ins.full() { |
| 996 |
pr.ins.freeFirstOne() |
| 997 |
} |
| 998 |
if pr.Match < r.raftLog.lastIndex() { |
| 999 |
r.sendAppend(m.From) |
| 1000 |
} |
| 1001 |
|
| 1002 |
if r.readOnly.option != ReadOnlySafe || len(m.Context) == 0 { |
| 1003 |
return |
| 1004 |
} |
| 1005 |
|
| 1006 |
ackCount := r.readOnly.recvAck(m) |
| 1007 |
if ackCount < r.quorum() { |
| 1008 |
return |
| 1009 |
} |
| 1010 |
|
| 1011 |
rss := r.readOnly.advance(m) |
| 1012 |
for _, rs := range rss { |
| 1013 |
req := rs.req |
| 1014 |
if req.From == None || req.From == r.id { // from local member |
| 1015 |
r.readStates = append(r.readStates, ReadState{Index: rs.index, RequestCtx: req.Entries[0].Data}) |
| 1016 |
} else { |
| 1017 |
r.send(pb.Message{To: req.From, Type: pb.MsgReadIndexResp, Index: rs.index, Entries: req.Entries}) |
| 1018 |
} |
| 1019 |
} |
| 1020 |
case pb.MsgSnapStatus: |
| 1021 |
if pr.State != ProgressStateSnapshot { |
| 1022 |
return |
| 1023 |
} |
| 1024 |
if !m.Reject { |
| 1025 |
pr.becomeProbe() |
| 1026 |
r.logger.Debugf("%x snapshot succeeded, resumed sending replication messages to %x [%s]", r.id, m.From, pr) |
| 1027 |
} else { |
| 1028 |
pr.snapshotFailure() |
| 1029 |
pr.becomeProbe() |
| 1030 |
r.logger.Debugf("%x snapshot failed, resumed sending replication messages to %x [%s]", r.id, m.From, pr) |
| 1031 |
} |
| 1032 |
// If snapshot finish, wait for the msgAppResp from the remote node before sending |
| 1033 |
// out the next msgApp. |
| 1034 |
// If snapshot failure, wait for a heartbeat interval before next try |
| 1035 |
pr.pause() |
| 1036 |
case pb.MsgUnreachable: |
| 1037 |
// During optimistic replication, if the remote becomes unreachable, |
| 1038 |
// there is huge probability that a MsgApp is lost. |
| 1039 |
if pr.State == ProgressStateReplicate { |
| 1040 |
pr.becomeProbe() |
| 1041 |
} |
| 1042 |
r.logger.Debugf("%x failed to send message to %x because it is unreachable [%s]", r.id, m.From, pr) |
| 1043 |
case pb.MsgTransferLeader: |
| 1044 |
if pr.IsLearner { |
| 1045 |
r.logger.Debugf("%x is learner. Ignored transferring leadership", r.id) |
| 1046 |
return |
| 1047 |
} |
| 1048 |
leadTransferee := m.From |
| 1049 |
lastLeadTransferee := r.leadTransferee |
| 1050 |
if lastLeadTransferee != None { |
| 1051 |
if lastLeadTransferee == leadTransferee { |
| 1052 |
r.logger.Infof("%x [term %d] transfer leadership to %x is in progress, ignores request to same node %x", |
| 1053 |
r.id, r.Term, leadTransferee, leadTransferee) |
| 1054 |
return |
| 1055 |
} |
| 1056 |
r.abortLeaderTransfer() |
| 1057 |
r.logger.Infof("%x [term %d] abort previous transferring leadership to %x", r.id, r.Term, lastLeadTransferee) |
| 1058 |
} |
| 1059 |
if leadTransferee == r.id { |
| 1060 |
r.logger.Debugf("%x is already leader. Ignored transferring leadership to self", r.id) |
| 1061 |
return |
| 1062 |
} |
| 1063 |
// Transfer leadership to third party. |
| 1064 |
r.logger.Infof("%x [term %d] starts to transfer leadership to %x", r.id, r.Term, leadTransferee) |
| 1065 |
// Transfer leadership should be finished in one electionTimeout, so reset r.electionElapsed. |
| 1066 |
r.electionElapsed = 0 |
| 1067 |
r.leadTransferee = leadTransferee |
| 1068 |
if pr.Match == r.raftLog.lastIndex() { |
| 1069 |
r.sendTimeoutNow(leadTransferee) |
| 1070 |
r.logger.Infof("%x sends MsgTimeoutNow to %x immediately as %x already has up-to-date log", r.id, leadTransferee, leadTransferee) |
| 1071 |
} else { |
| 1072 |
r.sendAppend(leadTransferee) |
| 1073 |
} |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
// stepCandidate is shared by StateCandidate and StatePreCandidate; the difference is |
| 1078 |
// whether they respond to MsgVoteResp or MsgPreVoteResp. |
| 1079 |
func stepCandidate(r *raft, m pb.Message) { |
| 1080 |
// Only handle vote responses corresponding to our candidacy (while in |
| 1081 |
// StateCandidate, we may get stale MsgPreVoteResp messages in this term from |
| 1082 |
// our pre-candidate state). |
| 1083 |
var myVoteRespType pb.MessageType |
| 1084 |
if r.state == StatePreCandidate { |
| 1085 |
myVoteRespType = pb.MsgPreVoteResp |
| 1086 |
} else { |
| 1087 |
myVoteRespType = pb.MsgVoteResp |
| 1088 |
} |
| 1089 |
switch m.Type { |
| 1090 |
case pb.MsgProp: |
| 1091 |
r.logger.Infof("%x no leader at term %d; dropping proposal", r.id, r.Term) |
| 1092 |
return |
| 1093 |
case pb.MsgApp: |
| 1094 |
r.becomeFollower(r.Term, m.From) |
| 1095 |
r.handleAppendEntries(m) |
| 1096 |
case pb.MsgHeartbeat: |
| 1097 |
r.becomeFollower(r.Term, m.From) |
| 1098 |
r.handleHeartbeat(m) |
| 1099 |
case pb.MsgSnap: |
| 1100 |
r.becomeFollower(m.Term, m.From) |
| 1101 |
r.handleSnapshot(m) |
| 1102 |
case myVoteRespType: |
| 1103 |
gr := r.poll(m.From, m.Type, !m.Reject) |
| 1104 |
r.logger.Infof("%x [quorum:%d] has received %d %s votes and %d vote rejections", r.id, r.quorum(), gr, m.Type, len(r.votes)-gr) |
| 1105 |
switch r.quorum() { |
| 1106 |
case gr: |
| 1107 |
if r.state == StatePreCandidate { |
| 1108 |
r.campaign(campaignElection) |
| 1109 |
} else { |
| 1110 |
r.becomeLeader() |
| 1111 |
r.bcastAppend() |
| 1112 |
} |
| 1113 |
case len(r.votes) - gr: |
| 1114 |
r.becomeFollower(r.Term, None) |
| 1115 |
} |
| 1116 |
case pb.MsgTimeoutNow: |
| 1117 |
r.logger.Debugf("%x [term %d state %v] ignored MsgTimeoutNow from %x", r.id, r.Term, r.state, m.From) |
| 1118 |
} |
| 1119 |
} |
| 1120 |
|
| 1121 |
func stepFollower(r *raft, m pb.Message) { |
| 1122 |
switch m.Type { |
| 1123 |
case pb.MsgProp: |
| 1124 |
if r.lead == None { |
| 1125 |
r.logger.Infof("%x no leader at term %d; dropping proposal", r.id, r.Term) |
| 1126 |
return |
| 1127 |
} else if r.disableProposalForwarding { |
| 1128 |
r.logger.Infof("%x not forwarding to leader %x at term %d; dropping proposal", r.id, r.lead, r.Term) |
| 1129 |
return |
| 1130 |
} |
| 1131 |
m.To = r.lead |
| 1132 |
r.send(m) |
| 1133 |
case pb.MsgApp: |
| 1134 |
r.electionElapsed = 0 |
| 1135 |
r.lead = m.From |
| 1136 |
r.handleAppendEntries(m) |
| 1137 |
case pb.MsgHeartbeat: |
| 1138 |
r.electionElapsed = 0 |
| 1139 |
r.lead = m.From |
| 1140 |
r.handleHeartbeat(m) |
| 1141 |
case pb.MsgSnap: |
| 1142 |
r.electionElapsed = 0 |
| 1143 |
r.lead = m.From |
| 1144 |
r.handleSnapshot(m) |
| 1145 |
case pb.MsgTransferLeader: |
| 1146 |
if r.lead == None { |
| 1147 |
r.logger.Infof("%x no leader at term %d; dropping leader transfer msg", r.id, r.Term) |
| 1148 |
return |
| 1149 |
} |
| 1150 |
m.To = r.lead |
| 1151 |
r.send(m) |
| 1152 |
case pb.MsgTimeoutNow: |
| 1153 |
if r.promotable() { |
| 1154 |
r.logger.Infof("%x [term %d] received MsgTimeoutNow from %x and starts an election to get leadership.", r.id, r.Term, m.From) |
| 1155 |
// Leadership transfers never use pre-vote even if r.preVote is true; we |
| 1156 |
// know we are not recovering from a partition so there is no need for the |
| 1157 |
// extra round trip. |
| 1158 |
r.campaign(campaignTransfer) |
| 1159 |
} else { |
| 1160 |
r.logger.Infof("%x received MsgTimeoutNow from %x but is not promotable", r.id, m.From) |
| 1161 |
} |
| 1162 |
case pb.MsgReadIndex: |
| 1163 |
if r.lead == None { |
| 1164 |
r.logger.Infof("%x no leader at term %d; dropping index reading msg", r.id, r.Term) |
| 1165 |
return |
| 1166 |
} |
| 1167 |
m.To = r.lead |
| 1168 |
r.send(m) |
| 1169 |
case pb.MsgReadIndexResp: |
| 1170 |
if len(m.Entries) != 1 { |
| 1171 |
r.logger.Errorf("%x invalid format of MsgReadIndexResp from %x, entries count: %d", r.id, m.From, len(m.Entries)) |
| 1172 |
return |
| 1173 |
} |
| 1174 |
r.readStates = append(r.readStates, ReadState{Index: m.Index, RequestCtx: m.Entries[0].Data}) |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
func (r *raft) handleAppendEntries(m pb.Message) { |
| 1179 |
if m.Index < r.raftLog.committed { |
| 1180 |
r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: r.raftLog.committed}) |
| 1181 |
return |
| 1182 |
} |
| 1183 |
|
| 1184 |
if mlastIndex, ok := r.raftLog.maybeAppend(m.Index, m.LogTerm, m.Commit, m.Entries...); ok { |
| 1185 |
r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: mlastIndex}) |
| 1186 |
} else { |
| 1187 |
r.logger.Debugf("%x [logterm: %d, index: %d] rejected msgApp [logterm: %d, index: %d] from %x", |
| 1188 |
r.id, r.raftLog.zeroTermOnErrCompacted(r.raftLog.term(m.Index)), m.Index, m.LogTerm, m.Index, m.From) |
| 1189 |
r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: m.Index, Reject: true, RejectHint: r.raftLog.lastIndex()}) |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
func (r *raft) handleHeartbeat(m pb.Message) { |
| 1194 |
r.raftLog.commitTo(m.Commit) |
| 1195 |
r.send(pb.Message{To: m.From, Type: pb.MsgHeartbeatResp, Context: m.Context}) |
| 1196 |
} |
| 1197 |
|
| 1198 |
func (r *raft) handleSnapshot(m pb.Message) { |
| 1199 |
sindex, sterm := m.Snapshot.Metadata.Index, m.Snapshot.Metadata.Term |
| 1200 |
if r.restore(m.Snapshot) { |
| 1201 |
r.logger.Infof("%x [commit: %d] restored snapshot [index: %d, term: %d]", |
| 1202 |
r.id, r.raftLog.committed, sindex, sterm) |
| 1203 |
r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: r.raftLog.lastIndex()}) |
| 1204 |
} else { |
| 1205 |
r.logger.Infof("%x [commit: %d] ignored snapshot [index: %d, term: %d]", |
| 1206 |
r.id, r.raftLog.committed, sindex, sterm) |
| 1207 |
r.send(pb.Message{To: m.From, Type: pb.MsgAppResp, Index: r.raftLog.committed}) |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
// restore recovers the state machine from a snapshot. It restores the log and the |
| 1212 |
// configuration of state machine. |
| 1213 |
func (r *raft) restore(s pb.Snapshot) bool { |
| 1214 |
if s.Metadata.Index <= r.raftLog.committed { |
| 1215 |
return false |
| 1216 |
} |
| 1217 |
if r.raftLog.matchTerm(s.Metadata.Index, s.Metadata.Term) { |
| 1218 |
r.logger.Infof("%x [commit: %d, lastindex: %d, lastterm: %d] fast-forwarded commit to snapshot [index: %d, term: %d]", |
| 1219 |
r.id, r.raftLog.committed, r.raftLog.lastIndex(), r.raftLog.lastTerm(), s.Metadata.Index, s.Metadata.Term) |
| 1220 |
r.raftLog.commitTo(s.Metadata.Index) |
| 1221 |
return false |
| 1222 |
} |
| 1223 |
|
| 1224 |
// The normal peer can't become learner. |
| 1225 |
if !r.isLearner { |
| 1226 |
for _, id := range s.Metadata.ConfState.Learners { |
| 1227 |
if id == r.id { |
| 1228 |
r.logger.Errorf("%x can't become learner when restores snapshot [index: %d, term: %d]", r.id, s.Metadata.Index, s.Metadata.Term) |
| 1229 |
return false |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
r.logger.Infof("%x [commit: %d, lastindex: %d, lastterm: %d] starts to restore snapshot [index: %d, term: %d]", |
| 1235 |
r.id, r.raftLog.committed, r.raftLog.lastIndex(), r.raftLog.lastTerm(), s.Metadata.Index, s.Metadata.Term) |
| 1236 |
|
| 1237 |
r.raftLog.restore(s) |
| 1238 |
r.prs = make(map[uint64]*Progress) |
| 1239 |
r.learnerPrs = make(map[uint64]*Progress) |
| 1240 |
r.restoreNode(s.Metadata.ConfState.Nodes, false) |
| 1241 |
r.restoreNode(s.Metadata.ConfState.Learners, true) |
| 1242 |
return true |
| 1243 |
} |
| 1244 |
|
| 1245 |
func (r *raft) restoreNode(nodes []uint64, isLearner bool) { |
| 1246 |
for _, n := range nodes { |
| 1247 |
match, next := uint64(0), r.raftLog.lastIndex()+1 |
| 1248 |
if n == r.id { |
| 1249 |
match = next - 1 |
| 1250 |
r.isLearner = isLearner |
| 1251 |
} |
| 1252 |
r.setProgress(n, match, next, isLearner) |
| 1253 |
r.logger.Infof("%x restored progress of %x [%s]", r.id, n, r.getProgress(n)) |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
// promotable indicates whether state machine can be promoted to leader, |
| 1258 |
// which is true when its own id is in progress list. |
| 1259 |
func (r *raft) promotable() bool { |
| 1260 |
_, ok := r.prs[r.id] |
| 1261 |
return ok |
| 1262 |
} |
| 1263 |
|
| 1264 |
func (r *raft) addNode(id uint64) { |
| 1265 |
r.addNodeOrLearnerNode(id, false) |
| 1266 |
} |
| 1267 |
|
| 1268 |
func (r *raft) addLearner(id uint64) { |
| 1269 |
r.addNodeOrLearnerNode(id, true) |
| 1270 |
} |
| 1271 |
|
| 1272 |
func (r *raft) addNodeOrLearnerNode(id uint64, isLearner bool) { |
| 1273 |
r.pendingConf = false |
| 1274 |
pr := r.getProgress(id) |
| 1275 |
if pr == nil { |
| 1276 |
r.setProgress(id, 0, r.raftLog.lastIndex()+1, isLearner) |
| 1277 |
} else { |
| 1278 |
if isLearner && !pr.IsLearner { |
| 1279 |
// can only change Learner to Voter |
| 1280 |
r.logger.Infof("%x ignored addLeaner: do not support changing %x from raft peer to learner.", r.id, id) |
| 1281 |
return |
| 1282 |
} |
| 1283 |
|
| 1284 |
if isLearner == pr.IsLearner { |
| 1285 |
// Ignore any redundant addNode calls (which can happen because the |
| 1286 |
// initial bootstrapping entries are applied twice). |
| 1287 |
return |
| 1288 |
} |
| 1289 |
|
| 1290 |
// change Learner to Voter, use origin Learner progress |
| 1291 |
delete(r.learnerPrs, id) |
| 1292 |
pr.IsLearner = false |
| 1293 |
r.prs[id] = pr |
| 1294 |
} |
| 1295 |
|
| 1296 |
if r.id == id { |
| 1297 |
r.isLearner = isLearner |
| 1298 |
} |
| 1299 |
|
| 1300 |
// When a node is first added, we should mark it as recently active. |
| 1301 |
// Otherwise, CheckQuorum may cause us to step down if it is invoked |
| 1302 |
// before the added node has a chance to communicate with us. |
| 1303 |
pr = r.getProgress(id) |
| 1304 |
pr.RecentActive = true |
| 1305 |
} |
| 1306 |
|
| 1307 |
func (r *raft) removeNode(id uint64) { |
| 1308 |
r.delProgress(id) |
| 1309 |
r.pendingConf = false |
| 1310 |
|
| 1311 |
// do not try to commit or abort transferring if there is no nodes in the cluster. |
| 1312 |
if len(r.prs) == 0 && len(r.learnerPrs) == 0 { |
| 1313 |
return |
| 1314 |
} |
| 1315 |
|
| 1316 |
// The quorum size is now smaller, so see if any pending entries can |
| 1317 |
// be committed. |
| 1318 |
if r.maybeCommit() { |
| 1319 |
r.bcastAppend() |
| 1320 |
} |
| 1321 |
// If the removed node is the leadTransferee, then abort the leadership transferring. |
| 1322 |
if r.state == StateLeader && r.leadTransferee == id { |
| 1323 |
r.abortLeaderTransfer() |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
func (r *raft) resetPendingConf() { r.pendingConf = false } |
| 1328 |
|
| 1329 |
func (r *raft) setProgress(id, match, next uint64, isLearner bool) { |
| 1330 |
if !isLearner { |
| 1331 |
delete(r.learnerPrs, id) |
| 1332 |
r.prs[id] = &Progress{Next: next, Match: match, ins: newInflights(r.maxInflight)} |
| 1333 |
return |
| 1334 |
} |
| 1335 |
|
| 1336 |
if _, ok := r.prs[id]; ok { |
| 1337 |
panic(fmt.Sprintf("%x unexpected changing from voter to learner for %x", r.id, id)) |
| 1338 |
} |
| 1339 |
r.learnerPrs[id] = &Progress{Next: next, Match: match, ins: newInflights(r.maxInflight), IsLearner: true} |
| 1340 |
} |
| 1341 |
|
| 1342 |
func (r *raft) delProgress(id uint64) { |
| 1343 |
delete(r.prs, id) |
| 1344 |
delete(r.learnerPrs, id) |
| 1345 |
} |
| 1346 |
|
| 1347 |
func (r *raft) loadState(state pb.HardState) { |
| 1348 |
if state.Commit < r.raftLog.committed || state.Commit > r.raftLog.lastIndex() { |
| 1349 |
r.logger.Panicf("%x state.commit %d is out of range [%d, %d]", r.id, state.Commit, r.raftLog.committed, r.raftLog.lastIndex()) |
| 1350 |
} |
| 1351 |
r.raftLog.committed = state.Commit |
| 1352 |
r.Term = state.Term |
| 1353 |
r.Vote = state.Vote |
| 1354 |
} |
| 1355 |
|
| 1356 |
// pastElectionTimeout returns true iff r.electionElapsed is greater |
| 1357 |
// than or equal to the randomized election timeout in |
| 1358 |
// [electiontimeout, 2 * electiontimeout - 1]. |
| 1359 |
func (r *raft) pastElectionTimeout() bool { |
| 1360 |
return r.electionElapsed >= r.randomizedElectionTimeout |
| 1361 |
} |
| 1362 |
|
| 1363 |
func (r *raft) resetRandomizedElectionTimeout() { |
| 1364 |
r.randomizedElectionTimeout = r.electionTimeout + globalRand.Intn(r.electionTimeout) |
| 1365 |
} |
| 1366 |
|
| 1367 |
// checkQuorumActive returns true if the quorum is active from |
| 1368 |
// the view of the local raft state machine. Otherwise, it returns |
| 1369 |
// false. |
| 1370 |
// checkQuorumActive also resets all RecentActive to false. |
| 1371 |
func (r *raft) checkQuorumActive() bool { |
| 1372 |
var act int |
| 1373 |
|
| 1374 |
r.forEachProgress(func(id uint64, pr *Progress) { |
| 1375 |
if id == r.id { // self is always active |
| 1376 |
act++ |
| 1377 |
return |
| 1378 |
} |
| 1379 |
|
| 1380 |
if pr.RecentActive && !pr.IsLearner { |
| 1381 |
act++ |
| 1382 |
} |
| 1383 |
|
| 1384 |
pr.RecentActive = false |
| 1385 |
}) |
| 1386 |
|
| 1387 |
return act >= r.quorum() |
| 1388 |
} |
| 1389 |
|
| 1390 |
func (r *raft) sendTimeoutNow(to uint64) { |
| 1391 |
r.send(pb.Message{To: to, Type: pb.MsgTimeoutNow}) |
| 1392 |
} |
| 1393 |
|
| 1394 |
func (r *raft) abortLeaderTransfer() { |
| 1395 |
r.leadTransferee = None |
| 1396 |
} |
| 1397 |
|
| 1398 |
func numOfPendingConf(ents []pb.Entry) int { |
| 1399 |
n := 0 |
| 1400 |
for i := range ents { |
| 1401 |
if ents[i].Type == pb.EntryConfChange { |
| 1402 |
n++ |
| 1403 |
} |
| 1404 |
} |
| 1405 |
return n |
| 1406 |
} |