MIT 6.5840:Lecture 09 ZooKeeper Case Study
本篇逐行译注 MIT 6.5840 Spring 2026 Lecture 9,覆盖 ZooKeeper 的数据模型、session、ephemeral znode、watch、fencing、性能取舍,以及它与 Raft、linearizability 和 fault-tolerant applications 的关系。
MIT 6.5840:Lecture 09 ZooKeeper Case Study
TL;DR
- ZooKeeper 的定位:把 fault-tolerant state storage 作为基础设施,让普通的 straight-line application code 可以从 ZK 恢复状态,而不必直接编写 replicated state machine。
- 核心 API:
create、exists、getData、setData、getChildren,配合 exclusive creation、version check、session、ephemeral znode、sequential znode 和 watch。 - Coordinator election:用 ephemeral znode 进行竞争,用 watch 等待失败者释放位置;session termination 会原子地删除 ephemeral nodes 并拒绝该 session 的后续 operations。
- Fencing:failure detector 可能判断错误,但所有组件必须服从同一判断;被判定为 dead 的旧 client 必须被 fence,不能继续修改 ZK state。
- 性能取舍:ZK 主要优化 read/watch;reads 可由 followers 本地执行,但可能看不到最新 completed writes,只提供 client FIFO 等较弱保证。
名词解释
| 名词 | 含义 | 本讲对应物 |
|---|---|---|
| Znode | ZooKeeper tree 中类似文件的 state node | 有 name、content、children、path 和 version |
| Session | client 与 ZK 的存活关系 | keep-alive 失败后被终止 |
| Ephemeral znode | session 终止时自动删除的 znode | coordinator election 的 lock-like marker |
| Watch | 对 znode 变化的一次通知注册 | 避免高成本 polling |
| Fencing | 忽略已被宣告 dead 的 client 请求 | 防止旧 coordinator 继续写 state |
| ZXID | ZK 为写操作分配的顺序标识 | 保证 clients 看到 writes 的统一顺序 |
| Client FIFO order | 同一 client 的 reads 只向前观察 ZXID | ZK read 的重要保证 |
目录
| 章节 | 内容 |
|---|---|
| 版本与来源 | 固定 Lecture 9 源文件与论文 |
| 原文逐行翻译 | 按 l-zookeeper.txt 顺序译注 |
| 机制小结 | ZooKeeper 的 API、failure handling 与性能边界 |
| 本篇验收 | 源行、图示和原始术语记录 |
版本与来源
- 课程版本:MIT 6.5840 Spring 2026。
- Lecture 源文件:l-zookeeper.txt,287 行,非空源行 251 行,SHA-256:
405de0b1739dbb67e433649bd3764c9342706e6447d36f6e6e3aa4623f2a60fe。 - 配套论文:ZooKeeper: Wait-free coordination for Internet-scale systems。
- 课程日程:MIT 6.5840 Spring 2026 Schedule。
- 翻译边界:英文源行按原顺序保留,随后给出中文译文;ZooKeeper API、znode、session、watch、ZXID、ZAB、Raft、GFS、Lab 和论文 Section 原样保留。
- 图片结论:本 Lecture 源文件没有独立图片文件;
[diagram: ...]、[MR coord, workers, ZK black box]、ASCII API/时序示意和 Figure 引用按原位置保留。
原文逐行翻译
ZooKeeper 的定位
6.5840 2026 Lecture 9: Zookeeper Case Study
6.5840 2026 年 Lecture 9:Zookeeper Case Study。
Reading: "ZooKeeper: Wait-free coordination for Internet-scale
阅读材料:“ZooKeeper: Wait-free coordination for Internet-scale
systems", Patrick Hunt, Mahadev Konar, Flavio P. Junqueira, Benjamin
systems”,作者为 Patrick Hunt、Mahadev Konar、Flavio P. Junqueira、Benjamin
Reed. 2010 USENIX Annual Technical Conference.
Reed。发表于 2010 USENIX Annual Technical Conference。
today's lecture considers ZooKeeper from two angles:
今天从两个角度考察 ZooKeeper:
- a simpler foundation for fault-tolerant applications.
- 为 fault-tolerant applications 提供更简单的 foundation。
- an example use of Raft-like replication
- 一个使用 Raft-like replication 的例子。
ZooKeeper is very widely used, so worth paying attention
ZooKeeper 被广泛使用,因此值得关注。
Etcd, another popular coordination service, is influenced by ZooKeeper's design
另一个流行的 coordination service——Etcd——也受 ZooKeeper 设计的影响。
if we wanted to make a fault-tolerant service like MR coordinator,
如果我们想构建一个类似 MR coordinator 的 fault-tolerant service,
we could replicate with Raft, and that would be OK!
我们可以使用 Raft 做 replication,这当然是可行的!
[diagram: Raft-replicated MR coordinator, workers]
[diagram:Raft-replicated MR coordinator、workers]。
but building directly on Raft is hard
但直接构建在 Raft 之上很困难。
a replicated state machine is awkward to program
replicated state machine 很难编程。
everything framed as events, commit them, then execute them
所有事情都要被表达成 events,先 commit,再 execute。
is there a simpler way?
有没有更简单的方法?
you can think of state machine replication (Raft) as replicating
可以把 state machine replication(Raft)理解为复制
the computation; the state is replicated as a side-effect.
computation;state 则作为 side-effect 被复制。
can we have fault-tolerance without replicating computation?
能不能在不复制 computation 的情况下实现 fault-tolerance?
yes!
可以!
ordinary non-replicated server
普通的 non-replicated server。
server maintains state in fault-tolerant storage service
server 把 state 保存在 fault-tolerant storage service 中。
server crash -> new server, reads state from storage
server crash → new server 从 storage 读取 state。
ZooKeeper is designed to be the required fault-tolerant storage
ZooKeeper 的设计目标就是提供所需的 fault-tolerant storage。
[MR coord, workers, ZK black box]
[MR coord、workers、ZK black box]。
MR coordinator can be written in ordinary straight-line code
MR coordinator 可以用普通的 straight-line code 编写。
write state updates to ZK
把 state updates 写入 ZK。
much like saving in a file
这很像保存到一个 file 中。
MR coordinator 的 state
what might MR coord store in ZK?
MR coord 可能把什么存入 ZK?
coord's IP addr, set of jobs, status of tasks, set of workers, assignments
coord 的 IP addr、jobs 集合、tasks 状态、workers 集合和 assignments。
update data in ZK on each change
每次变化都更新 ZK 中的数据。
(but big data itself in GFS, not ZK)
(但 big data 本身放在 GFS 中,而不是 ZK 中。)
workers can read coord's IP address, maybe even task assignments, from ZK
workers 可以从 ZK 读取 coord 的 IP address,甚至可能读取 task assignments。
MR using ZK for "configuration management"
MR 使用 ZK 进行“configuration management”。
keep track of a collection of servers
跟踪一组 servers。
help servers find each other
帮助 servers 互相发现。
what if MR coord fails?
如果 MR coord failure 了怎么办?
we weren't replicating it on a backup coord server
我们没有在 backup coord server 上复制它。
but we don't need one!
但我们不需要 backup coord server!
just pick any computer, start MR coord s/w on it,
只需选择任意一台 computer,在上面启动 MR coord s/w,
have it read state from ZK.
让它从 ZK 读取 state。
new coord can pick up where failed one left off.
new coord 可以从 failed coord 停下的位置继续工作。
makes a lot of sense in a big cloud
这在 big cloud 中很合理。
easy to allocate a replacement server
因为很容易分配 replacement server。
challenges
挑战:
- failure detection (of MR coord)
- failure detection(检测 MR coord)。
- election (just one MR coord at a time -- no split brain!)
- election(同一时间只能有一个 MR coord——不能 split brain!)。
- recover/repair state from ZK (old MR coord might
- 从 ZK recover/repair state(old MR coord 可能
have been in the middle of updating it)
正在更新 state 的过程中)。
- deal with possibility old MR coord is still alive and active!
- 处理 old MR coord 仍然 alive 且 active 的可能性!
- performance
- performance。
ZK helps with all of these
ZK 能够帮助解决所有这些问题。
ZooKeeper server arrangement
ZooKeeper server arrangement
ZooKeeper server arrangement。
[ZK leader, ZK followers, clients, writes, ZXIDs, reads, watches]
[ZK leader、ZK followers、clients、writes、ZXIDs、reads、watches]。
Raft-like leader
Raft-like leader。
Raft-like log, commit, replicated execution and state
Raft-like log、commit、replicated execution 和 state。
ZK leader chooses order for incoming writes,
ZK leader 为 incoming writes 选择 order,
assigns ZXIDs,
分配 ZXIDs,
all followers execute writes in the same order
所有 followers 按相同顺序执行 writes。
ZK followers execute client reads (reads are not sent to ZK leader)
ZK followers 执行 client reads(reads 不会发送给 ZK leader)。
for now I'll treat ZK as a black box
现在先把 ZK 当作 black box。
Zookeeper data model 与 API
Zookeeper data model (Figure 1)
Zookeeper data model(Figure 1)。
a file-system-like tree of znodes
一个 file-system-like 的 znode tree。
znode names, znode content, children, path names
znode names、znode content、children、path names。
names and hierarchy help different apps avoid interfering
names 和 hierarchy 帮助不同 apps 避免互相干扰。
each znode has a version number
每个 znode 都有一个 version number。
types of znodes:
znodes 的类型:
regular
regular。
ephemeral
ephemeral。
sequential: name + seqno
sequential:name + seqno。
Operations (Section 2.2)
Operations(Section 2.2)。
s = openSession()
s = openSession()。
create(s, path, data, flags)
create(s, path, data, flags)。
exclusive -- fails if path already exists
exclusive——如果 path 已经存在,就失败。
exists(s, path, watch)
exists(s, path, watch)。
watch=true asks for notification if path is later created/deleted
watch=true 请求在 path 之后被 created/deleted 时收到 notification。
getData(s, path, watch) -> data, version
getData(s, path, watch) -> data, version。
setData(s, path, data, version)
setData(s, path, data, version)。
if znode.version = version, then update
如果 znode.version = version,就执行 update。
(same version scheme as Lab 2)
(与 Lab 2 相同的 version scheme。)
getChildren(s, path, watch)
getChildren(s, path, watch)。
exception if the ZK server says it has terminated the session
如果 ZK server 表示它已经 terminated session,就抛出 exception。
so that application won't continue
这样 application 就不会继续运行。
ZooKeeper API designed for synchronization and concurrent access:
ZooKeeper API 是为 synchronization 和 concurrent access 设计的:
- exclusive znode creation; exactly one concurrent create returns success
- exclusive znode creation;并发的 creates 中恰好一个返回 success。
- getData()/setData(x, version) supports mini-transactions
getData()/setData(x, version)支持 mini-transactions。
- sessions/ephemeral help cope with client failure
- sessions/ephemeral 帮助应对 client failure。
- sequential znodes create order among multiple clients
- sequential znodes 在多个 clients 之间创建 order。
- watches avoid costly polling
- watches 避免高成本的 polling。
MapReduce coordinator election
Example: MapReduce coordinator election
例子:MapReduce coordinator election。
this is the paper's Simple Lock in Section 2.4
这是论文 Section 2.4 中的 Simple Lock。
s = openSession()
s = openSession()。
while true:
while true:
if create(s, "/mr/c", ephemeral=true)
if create(s, "/mr/c", ephemeral=true)。
// we won this election and are now coordinator
// we won this election and are now coordinator。
setData(s, "/mr/ip", ...)
setData(s, "/mr/ip", ...)。
setData(s, "/mr/...", ...)
setData(s, "/mr/...", ...)。
...
...。
break
break。
else if exists(s, "/mr/c", watch=true)
else if exists(s, "/mr/c", watch=true)。
// we lost this election
// we lost this election。
wait for watch event
等待 watch event。
note:
注意:
exclusive create
exclusive create。
if multiple clients concurrently attempt, only one will succeed
如果多个 clients 并发尝试,只有一个会成功。
ephemeral znode
ephemeral znode。
coordinator failure automatically lets new coordinator be elected
coordinator failure 会自动允许选出 new coordinator。
watch
watch。
potential replacement coordinators can wait w/o polling
潜在的 replacement coordinators 可以等待,而不需要 polling。
Coordinator failure 与 crash recovery
what do we want if the elected coordinator fails?
如果 elected coordinator failure 了,我们希望什么?
- elect a replacement
- elect 一个 replacement。
- cope with crash in the middle of updating state in ZK
- 应对在更新 ZK 中 state 的过程中发生 crash。
- cope with possibility that the coordinator didn't fail!
- 应对 coordinator 其实没有 failure 的可能性!
even though /mr/c looks like a lock,
尽管 /mr/c 看起来像一个 lock,
the possibility of coordinator failure makes the situation
coordinator failure 的可能性使情况
different from e.g. Go sync.Mutex
不同于例如 Go sync.Mutex 的情况。
what does ZK do on failure of client (e.g. MR coordinator)?
client failure 时 ZK 会做什么(例如 MR coordinator)?
client failure -> client stops sending keep-alive messages to ZK
client failure → client 停止向 ZK 发送 keep-alive messages。
no keep-alives -> ZK leader times out and terminates the session
没有 keep-alives → ZK leader 超时并终止 session。
session termination -> ZK leader deletes session's ephemeral znodes
session termination → ZK leader 删除 session 的 ephemeral znodes。
*and* ignores further requests from that session
*并且* 忽略来自该 session 的后续 requests。
(ephemeral deletions are A-linearizable ZK ops)
(ephemeral deletions 是 A-linearizable ZK ops。)
now a new MR coordinator can elect itself
现在新的 MR coordinator 可以自行当选。
what if the MR coordinator crashed while updating state in ZK?
如果 MR coordinator 在更新 ZK 中的 state 时 crash 了怎么办?
this is crash recovery, related to DB logging; requires care.
这是 crash recovery,与 DB logging 相关;需要谨慎处理。
simplest: MR coord stores all info in a single ZK znode
最简单的方法:MR coord 把所有 info 存在单个 ZK znode 中。
individual setData() calls are atomic (all or nothing vs failure)
单个 setData() calls 是 atomic 的(相对于 failure 来说要么全部成功,要么全部失败)。
what if MR coord stores state in multiple znodes?
如果 MR coord 把 state 存在多个 znodes 中呢?
use paper's "ready" znode scheme (Section 2.3)
使用论文 Section 2.3 的“ready” znode scheme。
delete "ready"; update znodes; create "ready"
先 delete “ready”;update znodes;再 create “ready”。
leader: worker:
leader: worker:
delete(s, "ready")
`delete(s, "ready")`
setData(s, z1)
\t setData(s, z1)。
setData(s, z2) if exits("ready", watch=ready):
\t setData(s, z2) if exits("ready", watch=ready):。
create(s, "ready) read z1
\t create(s, "ready) read z1。
read z2
\t \t read z2。
newly elected MR coord can then tell if update was partial
新当选的 MR coord 随后可以判断 update 是否是 partial 的。
better: write entirely new set of znodes, then update
更好的方法:写入一整套新的 znodes,然后更新
a znode that indicates which set is current
一个表示当前使用哪一套的 znode。
all three end with "single committing write"
三种方法最终都以“single committing write”结束。
Fencing 与 failure detector
what if the old coordinator is alive and thinks it is still coordinator?
如果 old coordinator 仍然 alive,并认为自己仍是 coordinator,怎么办?
but ZK has decided it is dead and deleted its ephemeral /mr/c znode?
但 ZK 已经认定它 dead,并删除了它的 ephemeral /mr/c znode?
and a new coordinator is elected?
同时又选出了新的 coordinator?
will two computers think they are the coordinator?
会不会有两台 computers 都认为自己是 coordinator?
this could happen.
这有可能发生。
can the old coordinator modify state in ZK?
old coordinator 能修改 ZK 中的 state 吗?
this cannnot happen!
这不可能发生!
when ZK times out a client's session, two things happen atomically:
当 ZK 将一个 client 的 session 判定为 timeout 时,会原子地发生两件事:
ZK deletes the client's ephemeral nodes.
ZK 删除 client 的 ephemeral nodes。
ZK stops listening to the session -- will reject all operations.
ZK 停止监听该 session——会拒绝所有 operations。
so old coordinator can no longer modify or read data in ZK!
因此 old coordinator 再也不能修改或读取 ZK 中的数据!
if it tries, its client ZK library will raise an exception
如果它尝试这样做,client ZK library 会抛出 exception,
forcing the client to realize it is no longer coordinator
迫使 client 意识到自己已经不再是 coordinator。
"Fencing" is a term for ignoring requests from a client declared dead
“Fencing”指忽略来自一个已被宣告 dead 的 client 的 requests,
even if it is actually alive
即使它实际上仍然 alive。
an important pattern in distributed systems:
分布式系统中有一个重要 pattern:
a single entity (e.g. ZK) decides which computers are alive or dead
一个 entity(例如 ZK)决定哪些 computers 是 alive 或 dead。
"failure detector"
“failure detector”。
it may not be correct, e.g. if the network drops messages
它可能并不正确,例如 network 丢失了 messages。
but everyone obeys its decisions
但所有人都服从它的 decisions。
agreement is more important than being right, to avoid split brain
为了避免 split brain,agreement 比判断正确更重要。
but possibility of being wrong => need to fence
但判断错误的可能性意味着需要 fence。
thus ZK's session termination
这就是 ZK session termination 的作用。
ZK 的性能设计
how is ZK designed for good performance?
ZK 如何为 good performance 进行设计?
optimized primarily for read/watch performance
主要针对 read/watch performance 优化。
write performance is secondary
write performance 是次要的。
[diagram: leader, lots of followers, clients talk to followers]
[diagram:leader、lots of followers、clients talk to followers]。
- many ZK follower servers; clients are spread over them for parallelism
1)许多 ZK follower servers;clients 分散到它们上面以获得 parallelism。
client sends all operations to its ZK follower
client 把所有 operations 发送给自己的 ZK follower。
ZK follower executes reads locally, from its replica of ZK data
ZK follower 从自己的 ZK data replica 中在本地执行 reads。
to avoid loading the ZK leader
这样可以避免给 ZK leader 增加负载。
ZK follower forwards only writes to ZK leader
ZK follower 只把 writes 转发给 ZK leader。
- watch, not poll
2)使用 watch,而不是 poll。
the ZK follower (not the ZK leader) keeps watch info
ZK follower(而不是 ZK leader)保存 watch info。
- clients of ZK can launch async operations
3)ZK clients 可以发起 async operations。
i.e. send request; completion notification delivered to code separately
也就是发送 request;completion notification 单独交付给 code。
unlike RPC
这不同于 RPC。
a client can launch many ops without waiting
client 可以在不等待的情况下发起许多 ops。
ZK processes async ops efficiently in a batch; fewer msgs, disk writes
ZK 可以在 batch 中高效处理 async ops;更少的 msgs 和 disk writes。
client library numbers them, ZK executes them in that order
client library 为它们编号,ZK 按该顺序执行。
e.g. to update a bunch of znodes then create "ready" znode
例如先更新一批 znodes,再 create “ready” znode。
ZK reads 的一致性边界
a ZK read may not see latest completed writes!
一次 ZK read 可能看不到最新的 completed writes!
since client's follower may be behind (not in write's majority)
因为 client 所连接的 follower 可能落后(不在 write 的 majority 中)。
when is it ok for reads not to see recent writes?
什么时候 read 看不到 recent writes 是可以接受的?
data merely displayed to humans
data 只是展示给 humans。
read-only data
read-only data。
data that can be checked e.g. GFS chunk-server
可以被检查的数据,例如 GFS chunk-server。
when is it not ok?
什么时候不可以?
read-modify-write e.g. to increase a counter
read-modify-write,例如增加 counter。
when a group of items needs to be consistent
一组 items 需要保持 consistent 时。
ZK does provide some guarantees for reads
ZK 确实为 reads 提供了一些 guarantees。
every client sees writes appear in the same order (ZXID)
每个 client 都以相同 order(ZXID)看到 writes。
a client read sees all of its own previous writes
client 的 read 能看到它自己之前的所有 writes。
so follower may have to delay a read
因此 follower 可能必须 delay 一个 read。
a client's reads move only forward in time (by ZXID)
client 的 reads 只能沿时间向前移动(由 ZXID 定义)。
even if client switches ZK followers!
即使 client 切换了 ZK followers 也是如此!
"client FIFO order"
“client FIFO order”。
good enough the examples above
对于前面的 examples,这已经足够了。
实现细节与性能问题
Some implementation details related to performance:
一些与 performance 相关的 implementation details:
Data must fit in memory, so reads are fast (no need to read disk).
Data 必须能够放入 memory,因此 reads 很快(不需要读 disk)。
So you can't store huge data in ZooKeeper.
所以不能把 huge data 存入 ZooKeeper。
ZK logs writes to disk.
ZK 把 writes log 到 disk。
So committed updates aren't lost in a crash or power failure.
因此 committed updates 不会在 crash 或 power failure 中丢失。
Hurts performance; batching can help throughput.
这会损害 performance;batching 可以提高 throughput。
Periodically, ZK writes complete snapshots to disk.
ZK 会周期性地把完整 snapshots 写入 disk。
So it can truncate on-disk log.
这样它就可以 truncate on-disk log。
Fuzzy technique allows snapshotting concurrently with write operations.
Fuzzy technique 允许在 write operations 并发进行时创建 snapshot。
How is the performance?
性能如何?
Figure 5 -- throughput.
Figure 5——throughput。
Overall, can handle 10s of thousands of operations / second.
总体上,每秒可以处理 tens of thousands of operations。
Is this a lot? Enough?
这很多吗?够用吗?
Why do the lines go up as they move to the right?
为什么 lines 向右移动时会向上?
Why does the x=0 performance go down as the number of servers increases?
为什么 server 数量增加时,x=0 的 performance 会下降?
Why does the "3 servers" line change to be worst at 100% reads?
为什么“3 servers”这条 line 在 100% reads 时变得最差?
What might limit it at x=0 to 20,000?
在 x=0 时,什么因素可能把它限制在 20,000?
Each op is a 1000-byte write...
每个 op 都是一个 1000-byte write……
What about latency?
latency 呢?
Why might we care about latency? What's a good target?
为什么要关注 latency?什么是好的 target?
Table 2 / Section 5.2 implies 1.3 milliseconds (1 / 776).
Table 2 / Section 5.2 暗示 latency 是 1.3 milliseconds(1 / 776)。
For a single worker (client) waiting after each write request.
这是针对每次 write request 后都等待的单个 worker(client)。
Where might the 1.3 milliseconds come from?
这 1.3 milliseconds 可能来自哪里?
Disk writes? Communication? Computation?
Disk writes?Communication?Computation?
(How can it be this fast, given mechanical disk rotation times?)
(考虑到 mechanical disk rotation times,它怎么可能这么快?)
Why only ~2000 req/s rather than Figure 5's 20,000?
为什么只有约 2000 req/s,而不是 Figure 5 中的 20,000?
How long to recover from a ZK server failure?
从 ZK server failure 中恢复需要多久?
Is this an important consideration?
这是一个重要考虑因素吗?
Figure 8
Figure 8。
Leader failure -> a pause for timeout and election.
Leader failure → 因 timeout 和 election 而暂停。
Visually, on the order of a few seconds.
从直观上看,大约是几秒的量级。
Follower failure -> brief decrease in total throughput.
Follower failure → total throughput 短暂下降。
Why not a long pause for timeout?
为什么不会因为 timeout 而长时间暂停?
What are the leader recovery time tradeoffs/obstacles likely to be?
leader recovery time 可能有哪些 tradeoffs/obstacles?
ZooKeeper 的成功与局限
ZooKeeper has been very successful
ZooKeeper 非常成功。
good foundation for building fault-tolerant applications
它是构建 fault-tolerant applications 的良好 foundation。
see ZooKeeper's Wikipedia page for a list of projects that use it
可以查看 ZooKeeper 的 Wikipedia page,那里有使用它的 projects 列表。
Areas it could be improved?
它有哪些可以改进的地方?
too bad reads aren't linearizable
遗憾的是 reads 不是 linearizable 的。
sessions are pretty blunt; maybe better to have per-znode leases
sessions 相当粗粒度;也许 per-znode leases 会更好。
not easy to shard:
它不容易 shard:
znode tree has no obvious places to slice
znode tree 没有明显的切分位置。
sessions are global
sessions 是 global 的。
multi-znode transactions would be nice
multi-znode transactions 会很有用。
see etcd and consul for other design decisions
可以参考 etcd 和 consul 的其他 design decisions。
Next week:
下周:
distributed transactions
distributed transactions。
References:
参考资料:
https://zookeeper.apache.org/doc/r3.4.8/api/org/apache/zookeeper/ZooKeeper.html
https://zookeeper.apache.org/doc/r3.4.8/api/org/apache/zookeeper/ZooKeeper.html
ZAB: http://dl.acm.org/citation.cfm?id=2056409
ZAB:http://dl.acm.org/citation.cfm?id=2056409
https://zookeeper.apache.org/
https://zookeeper.apache.org/
https://cs.brown.edu/~mph/Herlihy91/p124-herlihy.pdf (wait free, universal
https://cs.brown.edu/~mph/Herlihy91/p124-herlihy.pdf(wait free、universal
objects, etc.)
objects 等。)
机制小结
1. ZooKeeper 与 Raft 的关系
Raft 复制的是 computation 的执行过程;ZooKeeper 把 fault-tolerant state storage 抽出来,让 application server 保持普通的 sequential code。ZK 内部仍然使用 Raft-like leader、log、commit 和 replicated state,但 client 主要通过 znode API 与它交互。
2. Coordinator election 的完整链路
candidate 用 exclusive create 竞争 ephemeral znode;成功者成为 coordinator,失败者注册 watch 并等待。client failure 会导致 session termination,ZK 随后原子删除 ephemeral nodes 并拒绝该 session 的后续 requests,于是 replacement coordinator 可以重新 election。
3. Fencing 解决“旧 coordinator 还活着”
failure detector 可能因为 network partition 把仍然运行的 client 判为 dead。分布式系统更重视所有组件对同一判断达成 agreement,而不是每次判断都正确;但这要求被判 dead 的 client 失去继续修改 state 的能力。ZK 通过 session termination 实现 fencing。
4. ZK 的一致性与性能边界
写入由 leader 排序并持久化,读取通常由 follower 本地完成,所以读性能好但可能看不到最新 completed writes。ZK 仍保证 client 看到 writes 的统一 ZXID 顺序、看到自己的 previous writes,并让 reads 只向前移动;这不是完整的 linearizability。
本篇验收
- [x] 官方
l-zookeeper.txt已固定:287 行、251 个非空源行,SHA-256 与来源记录一致。 - [x] 源文本的非空行按原顺序逐行保留,中文译文紧随对应英文源行。
- [x] ZooKeeper API、MapReduce election、ready znode scheme、session termination、fencing 和性能讨论均已覆盖。
- [x] 所有
[diagram: ...]、Figure 引用、ASCII API/时序示意和参考资料 URL 均按原位置保留;源文件没有独立图片。 - [x] ZK、znode、session、watch、ZXID、ZAB、Raft、GFS 和 API 名称保留原样。
- [ ] Lecture 9 的中文译文仍建议进行一次人工术语审校;自动审计只验证源文覆盖与顺序,不证明翻译语义正确。
评论 (0)