jie

参考: https://github.com/shishujuan/mit6.824-2017-raft.git

raft.go @Make

  1. 初始化 rf
1
2
3
state=Follower 

rf.heartbeatInterval = time.Duration(HeartbeatInterval) * time.Millisecond // 50 ms
  1. 启动 bot
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
electionTimeout := getRandomElectionTimeout() // 范围 {300 + [0,100)}ms

switch state {
case Follower:
select {
case <-rf.appendEntryCh: // 不用去处理接收的日志吗?
case <-rf.grantVoteCh: // ??
case <-time.After(electionTimeout): // 选举超时, Follower -> Candidate
rf.mu.Lock()
rf.convertToCandidate()
rf.mu.Unlock()
}
case Candidate:
go rf.leaderElection() // 1. 发起选举
select {
case <-rf.appendEntryCh:
case <-rf.grantVoteCh:
case <-rf.leaderCh:
case <-time.After(electionTimeout):
rf.mu.Lock()
rf.convertToCandidate() // 选举超时, 重新开始选举
rf.mu.Unlock()
}
case Leader:
rf.startAppendEntries() // 开始去追加日志
time.Sleep(rf.heartbeatInterval) // 心跳间隙
}