引言
lab2B的实验要求如下:
Implement the leader and follower code to append new log entries, so that the go test -run 2B tests pass.
- Hint: Run git pull to get the latest lab software.
- Hint: Your first goal should be to pass TestBasicAgree2B(). Start by implementing Start(), then write the code to send and receive new log entries via AppendEntries RPCs, following Figure 2. Send each newly committed entry on applyCh on each peer.
- Hint: You will need to implement the election restriction (section 5.4.1 in the paper).
- Hint: One way to fail to reach agreement in the early Lab 2B tests is to hold repeated elections even though the leader is alive. Look for bugs in election timer management, or not sending out heartbeats immediately after winning an election.
- Hint: Your code may have loops that repeatedly check for certain events. Don’t have these loops execute continuously without pausing, since that will slow your implementation enough that it fails tests. Use Go’s condition variables, or insert a time.Sleep(10 * time.Millisecond) in each loop iteration.
- Hint: Do yourself a favor for future labs and write (or re-write) code that’s clean and clear. For ideas, re-visit our the Guidance page with tips on how to develop and debug your code.
- Hint: If you fail a test, look over the code for the test in config.go and test_test.go to get a better understanding what the test is testing. config.go also illustrates how the tester uses the Raft API.
主要的要求就是在lab2A完成领导者选举的基础上实现日志的复制,代码可以在https://github.com/slipegg/MIT6.824中得到。
测试环境
由于整个lab是在模拟环境中进行的,所以我们需要先简单连接一下实验是如何测试和运行的。
查看测试脚本可知,每次客户端提交用户请求都是通过调用leader的Start函数来实现的,故Start函数在接收到了日志后就需要主动开始日志复制的过程。
而当leader将日志复制到大多数节点后,除了各个节点自己需要标定这个日志已经提交了外,还需要将这个日志已经提交了的信息返回给客户端,这个信息的结构为ApplyMsg,它通过applyCh这个channel来实现的,实际代码中我们可能需要启用一个go协程来在需要时进行异步执行,如下所示。
type ApplyMsg struct {
<


749





