【MIT6.824】lab2B-log replication 实现笔记

引言

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 {
   <
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值