6.5840-lab2-raft记录

urlyy

lab2a主要是完成日志选举和心跳机制
只需要修改/src/raft/raft.go中的内容

考虑测试中有一些不确定因素,可以考虑使用课程助教提供的实现反复测试的脚本,链接在此
使用方法:100表示执行100次,4表示4个tester子进程测试

1
./test-many.sh 100 4 2A

代码如下

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package raft

import (
"math/rand"
"sync"
"sync/atomic"
"time"
// "6.5840/labgob"
"6.5840/labrpc"
)

type state int

// 自定义常量
const (
FOLLOWER = iota
CANDIDATE
LEADER
)
const (
TimeOut = iota
VoteDone
VoteSuccess
)
const (
INIT_TERM = 0 //初始的任期
NULL = -1
VOTE_NOBODY = -1
HEARTBEAT_GAP = 130 //心跳间隔(ms)
ELECTION_TIMEOUT_FLOOR = 700 //选举超时时间下限(ms)
ELECTION_TIMEOUT_CEIL = 1000 //选举超时时间上限(ms)
RETRY_NUM = 1 //3次超时重试
ELECTION_DURATION = 3000
)

type ApplyMsg struct {
CommandValid bool
Command interface{}
CommandIndex int

// For 2D:
SnapshotValid bool
Snapshot []byte
SnapshotTerm int
SnapshotIndex int
}

type Entry struct {
}

// Raft
// A Go object implementing a single Raft peer.
type Raft struct {
mu sync.Mutex // Lock to protect shared access to this peer's state
peers []*labrpc.ClientEnd // RPC end points of all peers
persister *Persister // Object to hold this peer's persisted state
me int // this peer's index into peers[]
dead int32 // set by Kill()

// Your data here (2A, 2B, 2C).
// Look at the paper's Figure 2 for a description of what
// state a Raft server must maintain.F
curTerm int //当前结点任期
votedFor int //给谁投了票
curState state //当前状态
msgHandler chan ApplyMsg //处理传入的请求
electionTimeout int64 //本结点的选举超时时间
logs []Entry //日志

ticketGot int //选举阶段获得的票数

timeLastReceiveHeartBeat time.Time //上一次收到心跳的时间
timeLastSendHeartBeat time.Time //作为leader上一次发送心跳的时间
timeStartElection time.Time //选举开始时间
}

// GetState
// return currentTerm and whether this server
// believes it is the leader.
func (rf *Raft) GetState() (int, bool) {
// Your code here (2A).
rf.mu.Lock()
defer rf.mu.Unlock()
return rf.curTerm, rf.curState == LEADER
}

func (rf *Raft) persist() {
// Your code here (2C).
// Example:
// w := new(bytes.Buffer)
// e := labgob.NewEncoder(w)
// e.Encode(rf.xxx)
// e.Encode(rf.yyy)
// raftstate := w.Bytes()
// rf.persister.Save(raftstate, nil)
}

// restore previously persisted state.
func (rf *Raft) readPersist(data []byte) {
if data == nil || len(data) < 1 { // bootstrap without any state?
return
}
// Your code here (2C).
// Example:
// r := bytes.NewBuffer(data)
// d := labgob.NewDecoder(r)
// var xxx
// var yyy
// if d.Decode(&xxx) != nil ||
// d.Decode(&yyy) != nil {
// error...
// } else {
// rf.xxx = xxx
// rf.yyy = yyy
// }
}

// Snapshot
// the service says it has created a snapshot that has
// all info up to and including index. this means the
// service no longer needs the log through (and including)
// that index. Raft should now trim its log as much as possible.
func (rf *Raft) Snapshot(index int, snapshot []byte) {
// Your code here (2D).

}

type ReqVoteArgs struct {
// Your data here (2A, 2B).
CandidateID int
CandidateTerm int
}

type ReqVoteReply struct {
// Your data here (2A).
VoteGranted bool
FollowerTerm int
}

type ReqAppendEntriesArgs struct {
LeaderTerm int
LeaderID int
}
type ReqAppendEntriesReply struct {
Success bool
FollowerTerm int
}

func (rf *Raft) beFollower(newTerm int) {
//fmt.Printf("结点 %d 之前的身份是%d\n", rf.me, rf.curState)
rf.curTerm = newTerm
rf.curState = FOLLOWER
rf.votedFor = VOTE_NOBODY
rf.refreshElectionTimer()
rf.refreshSendHeartBeatTimer()
}

func (rf *Raft) beCandidate() {
// 可能会变多次
if rf.curState != CANDIDATE {
// 任期+1
//fmt.Printf("结点 %d 任期+1\n", rf.me)
rf.curTerm += 1
//变状态
rf.curState = CANDIDATE
}
// 先给自己投票,防止待会给别人投了
rf.votedFor = rf.me
// 初始化自己的票数
rf.ticketGot = 1
}

func (rf *Raft) beLeader() {
rf.curState = LEADER
rf.votedFor = VOTE_NOBODY
// 这里不用重置,让马上发心跳
// rf.timeLastSendHeartBeat = time.Now()
}

// 异步发消息
func (rf *Raft) sendAndHandleRequestVote(server int) {
rf.mu.Lock()
tmpTerm := rf.curTerm
rf.mu.Unlock()
reqArgs := ReqVoteArgs{
CandidateID: rf.me,
CandidateTerm: tmpTerm,
}
reply := ReqVoteReply{}
// 重试发请求
ok := false
tmpRetryNum := 0
for ok == false && tmpRetryNum < RETRY_NUM {
ok = rf.peers[server].Call("Raft.HandleVote", &reqArgs, &reply)
tmpRetryNum += 1
}
rf.mu.Lock()
if ok == false {
//fmt.Printf("结点 %d 发请求给 %d 失败\n", reqArgs.CandidateID, server)
// 对方结点宕机也算没同意投票
} else {
if rf.curState == CANDIDATE {
// 别人同意投票
if reply.VoteGranted {
// 可能会有很久以前的请求,需要丢弃他们
if reply.FollowerTerm < rf.curTerm {
//不处理
} else if reply.FollowerTerm == rf.curTerm {
rf.ticketGot += 1
// 查看投票结果,超过结点数一半就算赢
//log.Printf("结点 %d 当前票数:%d/%d\n", rf.me, rf.ticketGot, len(rf.peers))
if rf.ticketGot*2 > len(rf.peers) {
//log.Printf("------结点 %d(%d) : 变成leader------\n", rf.me, rf.curTerm)
rf.beLeader()
}
}
} else {
// 我out了
if reply.FollowerTerm > rf.curTerm {
rf.beFollower(reply.FollowerTerm)
rf.mu.Unlock()
} else {
//不处理
}
}
}
}
rf.mu.Unlock()
}

func (rf *Raft) HandleVote(args *ReqVoteArgs, reply *ReqVoteReply) {
// Your code here (2A, 2B).
//log.Printf("结点(id:%d,term:%d)收到(id:%d,term:%d)的投票请求\n", rf.me, rf.curTerm, args.CandidateID, args.CandidateTerm)
rf.mu.Lock()
if rf.curTerm > args.CandidateTerm {
//不投票
reply.VoteGranted = false
reply.FollowerTerm = rf.curTerm
//log.Printf("拒绝")
} else if rf.curTerm < args.CandidateTerm {
// 投票
reply.VoteGranted = true
reply.FollowerTerm = args.CandidateTerm
//log.Printf("同意")
rf.beFollower(args.CandidateTerm)
rf.votedFor = args.CandidateID
} else {
// 先投票
if rf.votedFor == VOTE_NOBODY || rf.votedFor == args.CandidateID {
reply.VoteGranted = true
reply.FollowerTerm = args.CandidateTerm
//log.Printf("同意\n")
rf.beFollower(args.CandidateTerm)
rf.votedFor = args.CandidateID
}
}
rf.mu.Unlock()
}

// 选举超过一段时间后的操作
func (rf *Raft) electionTimer() {
time.Sleep(time.Duration(ELECTION_DURATION) * time.Millisecond)
rf.mu.Lock()
if rf.curState == CANDIDATE {
rf.beFollower(rf.curTerm - 1)
}
rf.mu.Unlock()
}

// 异步发心跳
func (rf *Raft) sendAppendEntries(server int) {
rf.mu.Lock()
tmpTerm := rf.curTerm
rf.mu.Unlock()
reqArgs := ReqAppendEntriesArgs{
LeaderTerm: tmpTerm,
LeaderID: rf.me,
}
reply := ReqAppendEntriesReply{}
// 重试发请求
ok := false
tmpRetryNum := 0
for ok == false && tmpRetryNum < RETRY_NUM {
ok = rf.peers[server].Call("Raft.HandleAppendEntries", &reqArgs, &reply)
tmpRetryNum += 1
}
if ok {
rf.mu.Lock()
if reply.Success {

} else {
rf.beFollower(reply.FollowerTerm)
}
rf.mu.Unlock()
}
}

func (rf *Raft) HandleAppendEntries(args *ReqAppendEntriesArgs, reply *ReqAppendEntriesReply) {
// Your code here (2A, 2B).
rf.mu.Lock()
//fmt.Printf("结点 (%d,%d) 收到结点 (%d,%d) 的心跳\n", rf.me, rf.curTerm, args.LeaderID, args.LeaderTerm)
if rf.curTerm > args.LeaderTerm {
reply.FollowerTerm = rf.curTerm
reply.Success = false
} else {
rf.beFollower(args.LeaderTerm)
reply.FollowerTerm = args.LeaderTerm
reply.Success = true
//log.Printf("结点 %d 重置心跳\n", rf.me)
}
rf.mu.Unlock()
}

func (rf *Raft) Start(command interface{}) (int, int, bool) {
index := -1
term := -1
isLeader := true
//if isLeader ==false{
// return NULL,NULL,false
//}
// Your code here (2B).

return index, term, isLeader
}

func (rf *Raft) Kill() {
atomic.StoreInt32(&rf.dead, 1)
// Your code here, if desired.
}

func (rf *Raft) killed() bool {
z := atomic.LoadInt32(&rf.dead)
return z == 1
}

// 选举和发送心跳计时器
func (rf *Raft) ticker() {
for rf.killed() == false {
// pause for a random amount of time between 50 and 350 milliseconds.
// 稍微卡一下时间
ms := 50 + (rand.Int63() % 50)
time.Sleep(time.Duration(ms) * time.Millisecond)
// Your code here (2A)
// Check if a leader election should be started.
rf.mu.Lock()
// 只有follower才会想抢
if rf.curState == FOLLOWER {
// 心跳超时,开始选举
//fmt.Printf("节点%d距上一次心跳%d,预设超时为%d\n", rf.me, time.Now().Sub(rf.timeLastReceiveHeartBeat).Milliseconds(), rf.electionTimeout)
if time.Now().Sub(rf.timeLastReceiveHeartBeat).Milliseconds() > rf.electionTimeout {
rf.beCandidate()
//log.Printf("结点%d开始选举,term:%d\n", rf.me, rf.curTerm)
// 广播投票请求
for serverID := range rf.peers {
if serverID == rf.me {
continue
}
go rf.sendAndHandleRequestVote(serverID)
}
go rf.electionTimer()
}
} else if rf.curState == LEADER {
//leader发心跳
//fmt.Printf("leader结点%d发心跳\n", rf.me)
//fmt.Printf("leader距上一次心跳%v,阈值%v\n", time.Now().Sub(rf.timeLastSendHeartBeat).Milliseconds(), HEARTBEAT_GAP)
if time.Now().Sub(rf.timeLastSendHeartBeat).Milliseconds() > HEARTBEAT_GAP {
for idx := range rf.peers {
if idx == rf.me {
continue
}
go rf.sendAppendEntries(idx)
}
//重置计时
rf.refreshSendHeartBeatTimer()
}
}
rf.mu.Unlock()
}
}

func (rf *Raft) refreshElectionTimer() {
rf.timeLastReceiveHeartBeat = time.Now()
}

func (rf *Raft) refreshSendHeartBeatTimer() {
rf.timeLastSendHeartBeat = time.Now()
}

func Make(peers []*labrpc.ClientEnd, me int,
persister *Persister, applyCh chan ApplyMsg) *Raft {
rf := &Raft{}
rf.peers = peers
rf.persister = persister
rf.me = me
// Your initialization code here (2A, 2B, 2C).
rf.logs = make([]Entry, 1024)
rf.msgHandler = applyCh
rf.electionTimeout = int64(ELECTION_TIMEOUT_FLOOR + rand.Intn(ELECTION_TIMEOUT_CEIL-ELECTION_TIMEOUT_FLOOR))
//fmt.Printf("超时时间为%d\n", rf.electionTimeout)
rf.beFollower(INIT_TERM)

// initialize from state persisted before a crash
rf.readPersist(persister.ReadRaftState())
// start ticker goroutine to start elections
go rf.ticker()
return rf
}

内容

  • 标题: 6.5840-lab2-raft记录
  • 作者: urlyy
  • 创建于 : 2023-02-25 22:45:13
  • 更新于 : 2023-07-29 19:50:39
  • 链接: https://urlyy.github.io/2023/02/25/6-5840-raft-lab2记录/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
6.5840-lab2-raft记录