MIT 6.5840:Lecture 10 Lab 3A+B Q&A
本篇逐行译注 MIT 6.5840 Lecture 10:Lab 3A+B Q&A,覆盖 Raft library 的两种组织方式、safety/liveness、实验起步、debugging、locking 和 code tour。
MIT 6.5840:Lecture 10 Lab 3A+B Q&A
TL;DR
- Raft library 的两种结构:multi-threaded with locks 与 single-threaded state-machine 都可行,没有严格意义上的优劣之分。
- Correctness 的两面:safety 是永远不返回错误结果;liveness 是最终返回结果。
- 实验起步:先让第一个 test case 工作,再逐步扩展;用 race detector、可搜索日志和假设驱动的重复调试定位问题。
- 锁的边界:RPC handler 可以持有 Raft lock 保证 atomicity,但不能在 RPC 调用期间持有锁,否则可能 deadlock。
- Single-threaded 方案:一个 thread 处理 Raft protocol input events 并收集 output events;先 persist,再发送 RPC requests。
名词解释
| 名词 | 含义 | 本讲对应物 |
|---|---|---|
| Safety | 系统永远不返回 incorrect result | 分布式系统 correctness property |
| Liveness | 系统最终返回 result | 与 safety 相对的 progress property |
| Race detector | 检测并发 data race 的工具 | 调试 Lab 实现的第一步 |
| Condvar | condition variable | 通知负责向 applych 写入的 thread |
| Applier | 把 committed log entries 应用到 service 的 thread | 需要和 Raft state 分离 |
| Input/output event | state-machine Raft 的事件模型 | tick、RPC request/reply、persist、send RPC |
目录
| 章节 | 内容 |
|---|---|
| 版本与来源 | 固定 Lecture 10 源文件 |
| 原文逐行翻译 | 按 l-raft-QA.txt 顺序译注 |
| 机制小结 | 两种实现结构与调试路径 |
| 本篇验收 | 源行覆盖和图件边界 |
版本与来源
- 课程版本:MIT 6.5840 Spring 2026。
- Lecture 源文件:l-raft-QA.txt,75 行,非空源行 67 行,SHA-256:
c4301e66a85554d6680c0dc1dbd2b6f7e494ad61109f788991c924bcbf653b4e。 - 课程日程:MIT 6.5840 Spring 2026 Schedule。
- 翻译边界:英文源行按原顺序保留,随后给出中文译文;
VoteRequest、RPC、Raft、applych、condvar、AppendEntries和函数名原样保留。 - 图片结论:本 Lecture 源文件没有图片、ASCII 图示或独立图件。
原文逐行翻译
Raft library 的组织方式
6.5840 Lecture 10: lab 3 A+B Q&A
6.5840 Lecture 10:Lab 3 A+B Q&A。
Two different structures of Raft library
Raft library 有两种不同的组织结构。
multi-threaded with locks
使用 locks 的 multi-threaded 结构。
single-threaded state-machine
single-threaded state-machine 结构。
neither one is strictly better than the other
两者都没有严格优于对方。
other plans are possible
也可以采用其他方案。
Distributed systems 中的 correctness
Correctness in distributed systems:
distributed systems 中的 correctness:
safety: never returning an incorrect result
safety:永远不返回 incorrect result。
liveness: eventually returning a result
liveness:最终返回 result。
Q: give an example of a safety and liveness bug in your solution
问题:分别举出你的 solution 中一个 safety bug 和一个 liveness bug。
开始一个 lab
Getting started on a lab
开始一个 lab。
make first test case work
先让第一个 test case 工作。
fill out VoteRequest structs
填好 VoteRequest structs。
do an RPC
发起一个 RPC。
then much easier to get the lay of the land
然后就更容易了解整体情况。
other strategy: read all guides, read test code, etc.
另一种 strategy:阅读所有 guides、test code 等。
Debugging
Debugging
Debugging。
Run with race detector
使用 race detector 运行。
Log all action/messages in easy searchable way
以便于搜索的方式记录所有 actions/messages。
standard format: src, dst, opcode, raft state,..
标准格式:src、dst、opcode、raft state 等。
Run test case
运行 test case。
if ok:
如果成功:
next test case
进入下一个 test case。
if fail:
如果失败:
repeat:
重复:
study test case
研究 test case。
formulate hypothesis about what might be wrong
提出关于可能出错位置的 hypothesis。
study log and figure 2, run with race detector
研究 log 和 Figure 2,并使用 race detector 运行。
modify code and try test again
修改 code,再次尝试 test。
Multi-threaded Raft library
Multi-threaded Raft library
Multi-threaded Raft library。
many threads (Start() thread, thread reading from applych)
许多 threads(Start() thread、从 applych 读取的 thread)。
many RPC threads to talk to peers in parallel
许多 RPC threads,用于并行地与 peers 通信。
many RPC handler threads (started by RPC package)
许多 RPC handler threads(由 RPC package 启动)。
raft state with lock
带有 lock 的 raft state。
one thread writing to applych
一个向 applych 写入的 thread。
use condvar to signal it
使用 condvar 向它发 signal。
Locking
Locking
Locking。
Raft lock serializes operations
Raft lock 将 operations 串行化。
RPC handlers hold Raft lock so are atomic
RPC handlers 持有 Raft lock,因此是 atomic 的。
little parallelism
parallelism 很少。
Threads don't hold lock during RPC
Threads 在 RPC 期间不持有 lock。
risk: deadlock if locks are held during RPC
风险:如果在 RPC 期间持有 locks,可能发生 deadlock。
Single-threaded state-machine
Single-threaded state-machine
Single-threaded state-machine。
observation: little parallelism anyway
观察:无论如何,parallelism 都很少。
main use: sending RPCs in parallel
主要用途是并行发送 RPCs。
one thread runs Raft protocol
一个 thread 运行 Raft protocol。
receives input events (e.g., tick, RPC request, reply)
它接收 input events(例如 tick、RPC request、reply)。
process event, updating raft structure, without locks
不使用 locks 处理 event,并更新 raft structure。
collecting output events (persist, send RPCs)
同时收集 output events(persist、send RPCs)。
performs output events
执行 output events。
first persist, then RPC requests
先 persist,再发送 RPC requests。
one thread for the applier
为 applier 配置一个 thread。
need to separate state between applier and state-machine
需要分离 applier 与 state-machine 之间的 state。
protect shared state with lock (e.g., the log)
使用 lock 保护 shared state(例如 log)。
Code tour
Code tour
Code tour。
Raft struct
Raft struct。
Ticker
Ticker。
Election timeout
Election timeout。
Start election
Start election。
VoteRequest handling
VoteRequest handling。
becomeLeader
becomeLeader。
send appends
send appends。
AppendEntries request handling
AppendEntries request handling。
AppendEntries reply handling
AppendEntries reply handling。
commit
commit。
applier
applier。
Start()
Start()。
机制小结
1. 两种 Raft library 结构
multi-threaded 结构把并发直接交给 threads 和 locks:RPC handlers 可以在持锁状态下原子地修改 Raft state,但会牺牲 parallelism。single-threaded state-machine 结构让一个 thread 串行处理 protocol events,把并发主要留给 RPC 发送和独立的 applier。
2. Safety 与 liveness
调试时要分别问两个问题:系统是否可能返回 incorrect result(safety),以及系统是否可能永远不返回 result(liveness)。一个看似“没有错误结果”的实现仍可能因为锁、等待条件或消息处理路径而失去 liveness。
3. RPC 与 lock 的边界
Raft lock 可以让 RPC handler 内的状态转换 atomic,但 RPC 本身可能阻塞或回调其他 threads;在 RPC 调用期间持有 lock 容易形成 deadlock。更安全的边界是读取并保存必要 state、释放 lock、执行 RPC,再重新获取 lock 处理 reply。
4. 实验调试循环
先完成最小 test case,再按 test case 逐个推进;失败时形成可检验的 hypothesis,结合日志、Figure 2 和 race detector 验证,修改后重跑同一个 test。可搜索的统一日志格式是定位 distributed interleaving 的关键工具。
本篇验收
- [x] 官方
l-raft-QA.txt已固定:75 行、67 个非空源行,SHA-256 与来源记录一致。 - [x] 源文本的非空行按原顺序逐行保留,中文译文紧随对应英文源行。
- [x] multi-threaded、single-threaded、locking、debugging 和 code tour 全部覆盖。
- [x]
VoteRequest、AppendEntries、applych、condvar、Raft和函数名保留原样。 - [x] 源文件没有图片、ASCII 图示或独立图件。
- [ ] Lecture 10 的中文译文仍建议进行一次人工术语审校;自动审计只验证源文覆盖与顺序,不证明翻译语义正确。
评论 (0)