论文:ZooKeeper:Wait-free Coordination for Internet-scale Systems
本篇整理 ZooKeeper 论文。每一页保留原始 PDF 页面图;页面图是原文排版、Figure、性能曲线、统计表、脚注和参考文献的原始副本,下面按原页顺序给出中文译文与必要术语说明。
论文:ZooKeeper:Wait-free Coordination for Internet-scale Systems
相关文章:6.5840:Lecture 09 ZooKeeper · 论文:The Chubby Lock Service for Loosely-Coupled Distributed Systems · 论文:Paxos Made Simple · 论文:ZooKeeper:Wait-free Coordination for Internet-scale Systems
TL;DR
- 解决的问题:为 distributed application 提供一个可复用的 coordination kernel,让 client 自己构造 configuration management、group membership、leader election、lock 和 barrier 等 primitive。
- 核心判断:ZooKeeper 不在 server 端内置 lock 等 blocking primitive,而是提供 hierarchical znode、ephemeral/sequential node、watch、conditional update 和 session。
- 一致性取舍:write 通过 leader 和 Zab 获得 A-linearizability;同一 client 的 request 保证 FIFO;read 在本地 replica 执行,因此可以返回 stale value,但吞吐量很高。
- 容错结构:ensemble 通过 majority quorum 复制 data tree;write-ahead log、idempotent transaction 和 fuzzy snapshot 支持恢复;client session 可在 server 间迁移。
- 性能边界:read-dominant workload 可以达到数十万 operation/s;更多 replica 提高容错能力,但会增加 atomic broadcast 成本;需要更高 write throughput 时可以 partition 到多个 ensemble。
名词解释
| 名词 | 含义 | 本文对应物 |
|---|---|---|
| Coordination kernel | 只提供通用状态与顺序保证,由 client 组合高层 primitive 的服务核心 | ZooKeeper server + client API |
| Znode | ZooKeeper data tree 中的内存 data node | /app1/p_1、/lock-000000001 |
| Ephemeral znode | 与创建它的 session 绑定,session 结束后自动删除的 znode | group membership、锁请求 |
| Sequential znode | 名称后自动追加单调递增 counter 的 znode | 排队锁、唯一成员名 |
| Watch | 一次性的 change notification,不直接携带最新 data | exists(path, true)、getData(path, true) |
| A-linearizability | 支持一个 client 同时有多个 outstanding operation 的异步线性化语义 | ZooKeeper 的 write ordering |
| Zab | ZooKeeper 使用的 leader-based atomic broadcast protocol | write ordering、recovery 和 leader 变更 |
| zxid | 标记 server 已看到的事务顺序的 identifier | read response、session migration |
| Fuzzy snapshot | 不锁 data tree、可能不对应某个瞬时全局状态的 snapshot | 依靠 ordered idempotent replay 恢复 |
目录
| 章节 | 内容 |
|---|---|
| 版本与来源 | USENIX ATC ’10 版本、PDF 固定信息与翻译边界 |
| 原文页面与逐段翻译 | 14 个 PDF 页面按顺序保留并翻译 |
| 机制索引 | API、consistency、recipes、Zab、snapshot 与性能 |
| 本篇验收 | 页数、图片、Figure/Table 和覆盖范围 |
版本与来源
- 论文标题:ZooKeeper: Wait-free Coordination for Internet-scale Systems。
- 作者与机构:Patrick Hunt、Mahadev Konar(Yahoo! Grid);Flavio P. Junqueira、Benjamin Reed(Yahoo! Research)。
- 发表信息:2010 USENIX Annual Technical Conference(USENIX ATC ’10),2010。
- 官方来源:USENIX 论文页面;USENIX PDF。
- 本地固定文件:
source/zookeeper.pdf。 - PDF 固定信息:14 页,每页 612×792 points;SHA-256:
743d56888f7b698cf4d0a86a4538793bbbf3c94c8e21d5c27dc34369d56102a0。 - 附件处理:每页渲染为 1224×1584 PNG,存放于
assets/41 ZooKeeper/,正文以600宽度嵌入。Figure 1–8、Table 1–3、性能曲线、故障时间线和 References 由原页图保留。 - 翻译边界:以原文段落、API、protocol steps、Figure/Table caption 和 References 为最小翻译单元;ZooKeeper、znode、watch、session、Zab、zxid、ensemble、leader、follower 等术语保留 English 或中英并列。
原文页面与逐段翻译
第 1 页:Abstract 与 Introduction

标题与作者
ZooKeeper:面向 Internet-scale system 的 wait-free coordination
作者:Patrick Hunt、Mahadev Konar、Flavio P. Junqueira、Benjamin Reed。
机构:Yahoo! Grid、Yahoo! Research。
Abstract
本文描述 ZooKeeper:一个用于协调 distributed application 中 process 的 service。由于 ZooKeeper 是 critical infrastructure 的一部分,它的目标是提供一个简单且高性能的 kernel,让 client 可以在其上构建更复杂的 coordination primitive。它把 group messaging、shared register 和 distributed lock service 的元素组合到一个 replicated、centralized service 中。
ZooKeeper 暴露的 interface 具有 shared register 的 wait-free 特征,同时具有类似 distributed file system cache invalidation 的 event-driven mechanism,从而提供简单但强大的 coordination service。
ZooKeeper interface 支持高性能的 service implementation。除了 wait-free property 外,ZooKeeper 还保证每个 client 的 request 按 FIFO 执行,并且所有改变 ZooKeeper state 的 request 都具有 linearizability。这些 design decision 使我们可以实现高性能 processing pipeline,并由 local server 满足 read request。
对于目标 workload(read/write ratio 从 2:1 到 100:1),我们展示 ZooKeeper 可以处理每秒数万到数十万 transaction。这样的 performance 使 client application 可以广泛使用 ZooKeeper。
1 Introduction
Large-scale distributed application 需要不同形式的 coordination。Configuration 是最基本的 coordination 形式之一。最简单时,configuration 只是 system process 的一组 operational parameter;更复杂的 system 则拥有 dynamic configuration parameter。Group membership 和 leader election 也是 distributed system 中的常见需求:process 往往需要知道哪些其他 process 存活,以及它们负责什么。Lock 是一种强大的 coordination primitive,可以实现对 critical resource 的 mutually exclusive access。
一种 coordination 方法,是针对不同 coordination need 分别开发 service。例如,Amazon Simple Queue Service [3] 专门处理 queue;其他 service 专门实现 leader election [25] 和 configuration [27]。能够实现更强 primitive 的 service,也可以用来实现更弱 primitive。例如,Chubby [6] 是一个带有 strong synchronization guarantee 的 locking service;随后可以使用 lock 实现 leader election、group membership 等。
在设计 coordination service 时,我们不再把 specific primitive 实现在 server side,而是选择暴露一个 API,让 application developer 自己实现所需 primitive。这样得到的是一个 coordination kernel:不需要修改 service core,就能增加新的 primitive。这种方法可以提供适合 application requirement 的多种 coordination form,而不是把 developer 限制在一组固定 primitive 上。
在设计 ZooKeeper API 时,我们也不再采用 lock 等 blocking primitive。Coordination service 中的 blocking primitive 会带来多种问题,其中包括让 slow 或 faulty client 对 faster client 的 performance 产生负面影响。如果 request processing 依赖其他 client 的 response 和 failure detection,service 本身的 implementation 也会变得更复杂。
因此,我们的 system ZooKeeper 实现的是一种操作简单 wait-free data object 的 API;这些 object 像 file system 一样按 hierarchy 组织。事实上,单看 API signature,ZooKeeper API 很像一个去掉 lock method、open 和 close 的 Chubby。但 wait-free data object 的实现,使 ZooKeeper 与基于 lock 等 blocking primitive 的 system 有了显著区别。
第 2 页:Consistency、Service Overview 与术语

虽然 wait-free property 对 performance 和 fault tolerance 很重要,但它本身不足以支持 coordination。我们还必须为 operation 提供 order guarantee。特别是,我们发现,同时保证所有 operation 的 FIFO client ordering 和 linearizable write,就能高效实现 service,并且足以实现 application 所需的 coordination primitive。
实际上,我们可以用 API 为任意数量的 process 实现 consensus;按照 Herlihy 的 hierarchy,ZooKeeper 实现了一个 universal object [14]。
ZooKeeper service 由一组 server 组成,通过 replication 获得 high availability 和 performance。它的高性能让包含大量 process 的 application 可以使用这个 coordination kernel 管理各个方面的 coordination。我们通过简单的 pipelined architecture 实现 ZooKeeper,使数百或数千个 request 可以处于 outstanding 状态,同时仍保持低 latency。
这样的 pipeline 自然支持单个 client 的 request 按 FIFO order 执行。保证 FIFO client order 使 client 能够异步提交 operation;client 可以同时拥有多个 outstanding operation。这一 feature 很有用,例如,新 client 成为 leader 后必须操作并更新 metadata。如果不能有多个 outstanding operation,初始化时间可能是秒级,而不是亚秒级。
为了保证 update operation 满足 linearizability,我们实现了一个 leader-based atomic broadcast protocol [23],称为 Zab [24]。
然而,ZooKeeper application 的典型 workload 以 read operation 为主,因此需要扩展 read throughput。在 ZooKeeper 中,server 在本地处理 read operation,而不使用 Zab 对 read 做 total order。
Client-side data caching 是提升 read performance 的重要技术。例如,process 可以 cache 当前 leader 的 identifier,而不必每次需要知道 leader 时都 probe ZooKeeper。ZooKeeper 使用 watch mechanism,让 client 在不直接管理 cache 的情况下 cache data:client 可以 watch 某个 data object 的 update,并在 update 发生时收到 notification。
Chubby 直接管理 client cache。它会阻塞 update,以 invalidation 所有 cache 了被修改 data 的 client。在这种设计下,如果某个 client slow 或 faulty,update 就会延迟。Chubby 使用 lease 防止 faulty client 永久阻塞 system。但 lease 只能限制 slow 或 faulty client 的影响,而 ZooKeeper watch 完全避免了这个问题。
本文讨论 ZooKeeper 的 design 和 implementation。通过 ZooKeeper,我们能够实现 application 所需的所有 coordination primitive,尽管只有 write 是 linearizable。为验证这种方法,我们展示如何使用 ZooKeeper 实现几种 coordination primitive。
本文的主要贡献可以总结为:
- Coordination kernel:提出一个带有 relaxed consistency guarantee 的 wait-free coordination service,用于 distributed system;具体描述一个 coordination kernel 的 design 和 implementation,并说明它已经被许多 critical application 用于实现不同 coordination technique。
- Coordination recipes:展示如何使用 ZooKeeper 构建更高层 coordination primitive,包括 application 中经常使用的 blocking 和 strongly consistent primitive。
- Experience with Coordination:分享我们使用 ZooKeeper 的方式并评估它的 performance。
2 The ZooKeeper service
Client 通过 client API 使用 ZooKeeper client library 提交 request。除了通过 client API 暴露 ZooKeeper service interface 外,client library 还管理 client 与 ZooKeeper server 之间的 network connection。
本节先给出 ZooKeeper service 的 high-level view,然后讨论 client 用来与 ZooKeeper 交互的 API。
Terminology
本文中,client 表示 ZooKeeper service 的 user;server 表示提供 ZooKeeper service 的 process;znode 表示 ZooKeeper data 中的 in-memory data node,所有 znode 按 hierarchical namespace 组织,这个 namespace 称为 data tree。我们还使用 update 和 write 指代任何修改 data tree state 的 operation。Client 连接 ZooKeeper 时会建立 session,并获得一个 session handle,通过它发出 request。
2.1 Service overview
ZooKeeper 向 client 提供一组 data node(znode)的 abstraction;这些 znode 按 hierarchical namespace 组织。Znode 是 client 通过 ZooKeeper API 操作的 data object。Hierarchical namespace 在 file system 中很常见,是组织 data object 的理想方式:user 熟悉这种 abstraction,而且它能更好地组织 application metadata。
第 3 页:Znode、Watch、Data Model、Sessions 与 API

要引用某个 znode,我们使用标准 UNIX file-system path notation。例如,/A/B/C 表示 znode C 的 path,其中 C 的 parent 是 B,B 的 parent 是 A。所有 znode 都存储 data;除 ephemeral znode 外,所有 znode 都可以拥有 child。
Figure 1:ZooKeeper hierarchical namespace
原图展示 root / 下的 /app1 和 /app2,以及 /app1/p_1、/app1/p_2、/app1/p_3 三个 child znode。
Client 可以创建两种 znode:
- Regular:Client 显式创建和删除 regular znode。
- Ephemeral:Client 创建 ephemeral znode;client 可以显式删除,也可以让 system 在创建该 znode 的 session 终止时自动删除(无论 session 是主动结束还是因 failure 结束)。
此外,创建 znode 时,client 可以设置 sequential flag。设置 sequential flag 创建的 node,会在名称后追加一个 monotonically increasing counter。如果 n 是新 znode,p 是 parent znode,那么 n 的 sequence value 永远不会小于 p 下曾创建过的任何其他 sequential znode 名称中的 value。
ZooKeeper 实现了 watch,让 client 可以及时收到 change notification,而不需要 polling。当 client 发出带有 watch flag 的 read operation 时,operation 正常完成;除此之外,server 承诺在返回信息发生改变时通知 client。Watch 是与 session 关联的一次性 trigger:触发后或 session 关闭时就会 unregister。
Watch 表示发生了 change,但不提供 change 的具体内容。例如,client 在 /foo 被修改两次之前执行 getData("/foo", true),client 会收到一个 watch event,告知 /foo 的 data 已改变。Session event,例如 connection loss event,也会发送给 watch callback,使 client 知道 watch event 可能延迟。
Data model
ZooKeeper 的 data model 本质上是带有简化 API、只支持完整 data read/write 的 file system,或者说是带有 hierarchical key 的 key/value table。Hierarchical namespace 可用于为不同 application 分配 namespace subtree,以及设置对这些 subtree 的 access right。我们还利用 client-side directory 概念构建更高层 primitive,详见第 2.4 节。
与 file system 中的 file 不同,znode 不是为通用 data storage 设计的。相反,znode 映射到 client application 的 abstraction,通常对应 coordination 所需的 metadata。Figure 1 中有两个 subtree:一个属于 Application 1(/app1),另一个属于 Application 2(/app2)。Application 1 的 subtree 实现了一个简单 group-membership protocol:每个 client process p_i 在 /app1 下创建一个 p_i znode,只要 process 运行,该 znode 就持续存在。
尽管 znode 不是为 general data storage 设计的,ZooKeeper 仍允许 client 存储一些可用于 distributed computation 中 metadata 或 configuration 的 information。例如,在 leader-based application 中,一个刚启动的 application server 需要知道当前哪个 server 是 leader。可以让当前 leader 把这条信息写到 znode space 中一个已知 location。Znode 还带有包含 timestamp 和 version counter 的 metadata,使 client 能跟踪 znode change,并基于 znode version 执行 conditional update。
Sessions
Client 连接 ZooKeeper 并启动 session。Session 带有 timeout。如果超过该 timeout 没有从 session 收到任何东西,ZooKeeper 就认为 client faulty。Client 显式关闭 session handle,或 ZooKeeper 检测到 client faulty 时,session 结束。
在一个 session 内,client 观察到一连串反映其 operation 执行情况的 state change。Session 允许 client 在同一个 ZooKeeper ensemble 内从一个 server 透明迁移到另一个 server,因此 session 可以跨 ZooKeeper server 持续存在。
2.2 Client API
下面给出 ZooKeeper API 的相关子集,并讨论各 request 的语义:
create(path, data, flags):创建 path name 为path的 znode,把data[]存入其中,并返回新 znode 的 name。flags允许 client 选择 znode type:regular、ephemeral,以及设置 sequential flag。delete(path, version):当 znode 当前 version 是 expected version 时,删除 path 对应的 znode。exists(path, watch):如果 path name 对应的 znode 存在,返回 true,否则返回 false。Watch flag 允许 client 在该 znode 上设置 watch。
第 4 页:Client API、A-linearizability 与 Ready Znode

getData(path, watch):返回 znode 的 data 和 metadata,例如 version information。Watch flag 的作用与exists()相同,但如果 znode 不存在,ZooKeeper 不会设置 watch。setData(path, data, version):当 version number 是 znode 当前 version 时,把data[]写入 path 对应的 znode。getChildren(path, watch):返回 znode child 的 name 集合。sync(path):等待 operation 开始时 pending 的所有 update 传播到 client 所连接的 server。当前会忽略 path。
所有 method 都通过 API 提供 synchronous 和 asynchronous 两种版本。Application 只需要执行单个 ZooKeeper operation,且没有并发 task 时,使用 synchronous API:发出必要的 ZooKeeper call 并阻塞。Asynchronous API 则允许 application 同时拥有多个 outstanding ZooKeeper operation,并并行执行其他 task。ZooKeeper client 保证每个 operation 对应的 callback 按顺序调用。
注意,ZooKeeper 不使用 handle 访问 znode。每个 request 都包含被操作 znode 的完整 path。这不仅简化了 API(不需要 open() 或 close() method),也消除了 server 需要维护的额外 state。
每个 update method 都接收一个 expected version number,因此能够实现 conditional update。如果 znode 的 actual version number 不等于 expected version number,update 会失败并返回 unexpected-version error。如果 version number 为 −1,则不执行 version check。
2.3 ZooKeeper guarantees
ZooKeeper 有两个基本 ordering guarantee:
- Linearizable writes:所有更新 ZooKeeper state 的 request 都是 serializable,并遵守 precedence。
- FIFO client order:来自同一个 client 的所有 request,都按 client 发出的顺序执行。
请注意,我们对 linearizability 的定义与 Herlihy [15] 最初提出的定义不同,因此称作 A-linearizability(asynchronous linearizability)。Herlihy 的原始 linearizability 定义中,一个 client 同一时刻只能有一个 outstanding operation(client 是一个 thread)。我们的定义允许一个 client 同时拥有多个 outstanding operation,因此可以选择不保证同一个 client 的 outstanding operation 之间的特定 order,也可以保证 FIFO order。我们选择后者。
所有适用于 linearizable object 的结果,也适用于 A-linearizable object,因为满足 A-linearizability 的 system 同时满足 linearizability。由于只有 update request 是 A-linearizable,ZooKeeper 可以在每个 replica 本地处理 read request。这使 service 可以随着 server 增加而线性扩展。
为了理解两个 guarantee 如何交互,考虑这样一个场景:由若干 process 组成的 system 选出一个 leader,由它指挥 worker process。当新 leader 接管 system 时,必须修改大量 configuration parameter,并在修改完成后通知其他 process。此时有两个重要要求:
- 新 leader 开始修改时,不希望其他 process 开始使用正在修改的 configuration。
- 如果新 leader 在 configuration 完整更新前死亡,不希望 process 使用 partial configuration。
Distributed lock(例如 Chubby 提供的 lock)可以帮助满足第一个要求,但不足以满足第二个要求。
使用 ZooKeeper,新 leader 可以指定一个 path 作为 ready znode;只有当这个 znode 存在时,其他 process 才使用 configuration。新 leader 通过删除 ready、更新各个 configuration znode、再创建 ready 来进行 configuration change。所有 change 都可以 pipeline,并以 asynchronous 方式发出,从而快速更新 configuration state。
虽然一次 change operation 的 latency 大约是 2ms,但如果新 leader 必须逐个更新 5000 个 znode,按顺序发 request 就需要 10 秒;使用 asynchronous request 则不到一秒。由于 ordering guarantee,如果一个 process 看到了 ready znode,它也一定能看到新 leader 做出的所有 configuration change。如果 new leader 在创建 ready 之前死亡,其他 process 会知道 configuration 尚未 finalise,因此不会使用它。
上述 scheme 仍然存在一个问题:如果某个 process 在 new leader 开始修改之前看到 ready 存在,然后在 change 进行中开始读取 configuration,该怎么办?这个问题通过 notification 的 ordering guarantee 解决:如果 client 正在 watch change,那么它会先看到 notification event,再看到 change 后 system 的新 state。因此,读取 ready znode 的 process 如果同时请求 watch,就会在读取任何新 configuration 之前先收到 change notification。
第 5 页:Sync、Liveness 与 Configuration/Rendezvous/Group Membership

另一个问题出现在 client 除了 ZooKeeper 外还有自己的 communication channel 时。例如,client A 和 B 在 ZooKeeper 中共享 configuration,并通过共享 communication channel 通信。A 修改 ZooKeeper 中的 shared configuration,然后通过共享 channel 告知 B 发生了 change;B 会预期重新读取 configuration 时看到这个 change。但如果 B 所连接的 ZooKeeper replica 比 A 略微落后,B 可能看不到新 configuration。
使用上述 guarantee,B 可以先执行一次 write,再重新读取 configuration,从而确保自己看到最新信息。为了更高效地处理这个场景,ZooKeeper 提供 sync request:当它后面跟随一个 read 时,构成一次 slow read。sync 让 server 在处理 read 之前应用所有 pending write,而不需要一次完整 write 的 overhead。这个 primitive 与 ISIS [5] 的 flush primitive 思想类似。
ZooKeeper 还有两个 liveness 和 durability guarantee:如果多数 ZooKeeper server active 且互相通信,service 就可用;如果 ZooKeeper service 成功响应了一个 change request,那么只要最终有一个 server quorum 能够恢复,该 change 就能跨越任意数量的 failure 持久存在。
2.4 Examples of primitives
本节展示如何使用 ZooKeeper API 实现更强的 primitive。ZooKeeper service 并不知道这些更强的 primitive,因为它们完全由 client 使用 ZooKeeper client API 实现。一些常见 primitive(例如 group membership 和 configuration management)本身也是 wait-free。另一些 primitive(例如 rendezvous)需要 client 等待 event。尽管 ZooKeeper 是 wait-free 的,我们仍然可以用它高效实现 blocking primitive。ZooKeeper 的 ordering guarantee 使 system state 易于高效推理,watch 则使等待高效。
Configuration Management
ZooKeeper 可用于在 distributed application 中实现 dynamic configuration。最简单时,configuration 存储在 znode z_c 中。Process 启动时获得 z_c 的完整 pathname。Starting process 读取 z_c,并把 watch flag 设置为 true,以获取 configuration。如果 z_c 被 update,process 会收到 notification,然后重新读取 configuration,并再次把 watch 设置为 true。
注意,在这个以及大多数使用 watch 的 scheme 中,watch 的作用是确保 process 拥有最新信息。例如,正在 watch z_c 的 process 收到 z_c change notification,但在发出 read 之前 z_c 又发生了三次 change,此时 process 不会再收到三条 notification。这不影响 process behavior,因为那三次 event 只是通知 process 某些它已经知道的事情:process 当前关于 z_c 的 information 已经 stale。
Rendezvous
在 distributed system 中,最终 system configuration 有时无法预先确定。例如,一个 client 想启动一个 master process 和若干 worker process,但启动 process 的工作由 scheduler 执行,因此 client 事先不知道可以提供给 worker、用于连接 master 的 address 和 port。
我们使用 ZooKeeper 的 rendezvous znode z_r 处理这个场景。Client 把 z_r 的完整 pathname 作为 master 和 worker process 的 startup parameter。Master 启动时把自己使用的 address 和 port 等信息填入 z_r。Worker 启动时以 watch=true 读取 z_r。如果 z_r 尚未填充,worker 就等待 notification,直到收到 z_r update。如果 z_r 是 ephemeral node,master 和 worker process 可以 watch z_r 被删除,并在 client 结束时清理自己。
Group Membership
我们利用 ephemeral node 实现 group membership。具体来说,使用 ephemeral node 可以观察创建该 node 的 session state。首先指定 znode z_g 表示 group。当一个 process member 启动时,它在 z_g 下创建一个 ephemeral child znode。如果每个 process 有 unique name 或 identifier,就使用该 name 作为 child znode name;否则 process 使用 SEQUENTIAL flag 创建 znode,以获得 unique name assignment。Process 可以把自身信息(例如使用的 address 和 port)放在 child znode 的 data 中。
Child znode 在 z_g 下创建后,process 就正常启动,不需要再做其他事情。如果 process failure 或结束,z_g 下代表它的 znode 会自动删除。
Process 只需列出 z_g 的 child,就能获取 group information。如果 process 想监控 group membership change,可以把 watch flag 设置为 true;收到 change notification 后刷新 group information,并始终再次设置 watch=true。
第 6 页:Lock、Read/Write Lock 与 Double Barrier

Simple Locks
虽然 ZooKeeper 不是 lock service,但可以用它实现 lock。使用 ZooKeeper 的 application 通常使用针对自身需求定制的 synchronization primitive,例如前面展示的 primitive。这里展示如何实现 lock,是为了说明 ZooKeeper 可以实现多种通用 synchronization primitive。
最简单的 lock implementation 使用“lock file”。Lock 由一个 znode 表示。获取 lock 时,client 尝试用 EPHEMERAL flag 创建指定 znode。如果 create 成功,client 持有 lock;否则,client 可以以 watch=true 读取该 znode,在当前 leader 死亡时收到 notification。Client death 或显式删除 znode 时释放 lock。其他等待 lock 的 client 观察到该 znode 被删除后,再次尝试获得 lock。
这个简单 lock protocol 虽然可用,但有两个问题。第一,它有 herd effect:如果很多 client 等待 lock,那么 lock 释放时它们都会争抢,即使最终只有一个 client 能获得 lock。第二,它只实现 exclusive lock。下面两个 primitive 分别解决这两个问题。
Simple Locks without Herd Effect
定义 lock znode l 来实现这种 lock。直观上,我们把请求 lock 的所有 client 排成队,client 按 request arrival order 依次获得 lock。Client 获取 lock 时执行:
Lock
1 n = create(l + "/lock-", EPHEMERAL|SEQUENTIAL)
2 C = getChildren(l, false)
3 if n is lowest znode in C, exit
4 p = znode in C ordered just before n
5 if exists(p, true) wait for watch event
6 goto 2
Unlock
1 delete(n)
第 1 行使用 SEQUENTIAL flag,使 client 获取 lock 的尝试相对于其他尝试排序。如果 client 的 znode 拥有最低 sequence number,那么该 client 持有 lock。否则,它等待排在自己 znode 前面的 znode 被删除;那个 znode 要么当前持有 lock,要么会先获得 lock。
Client 只 watch 紧邻自己之前的 znode,从而避免 herd effect:lock release 或 lock request abandon 时只唤醒一个 process。被 watch 的 znode 消失后,client 必须重新检查自己是否持有 lock,因为之前的 lock request 可能 abandoned,而仍有更低 sequence number 的 znode 在等待或持有 lock。
释放 lock 只需删除表示 lock request 的 znode n。创建时使用 EPHEMERAL flag 后,crash 的 process 会自动清理自身的 lock request,或者自动释放它持有的 lock。
这个 lock scheme 的优点包括:
- 删除一个 znode 只会唤醒一个 client,因为每个 znode 恰好只被另一个 client watch,不会产生 herd effect。
- 不需要 polling 或 timeout。
- 由于 lock 的实现方式,可以通过浏览 ZooKeeper data 看到 lock contention 数量、break lock,并 debug locking problem。
Read/Write Locks
要实现 read/write lock,我们略微修改 lock procedure,并分别定义 read-lock 和 write-lock procedure。Unlock procedure 与 global lock case 相同。
Write Lock
1 n = create(l + "/write-", EPHEMERAL|SEQUENTIAL)
2 C = getChildren(l, false)
3 if n is lowest znode in C, exit
4 p = znode in C ordered just before n
5 if exists(p, true) wait for event
6 goto 2
Read Lock
1 n = create(l + "/read-", EPHEMERAL|SEQUENTIAL)
2 C = getChildren(l, false)
3 if no write znodes lower than n in C, exit
4 p = write znode in C ordered just before n
5 if exists(p, true) wait for event
6 goto 3
Write lock 只在命名上不同。由于 read lock 可以共享,第 3、4 行略有变化:只有更早的 write-lock znode 会阻止 client 获得 read lock。当多个等待 read lock 的 client 因 sequence number 最低的 write- znode 被删除而收到 notification 时,看起来可能像发生了 herd effect;但这是期望行为,因为这些 read client 此时都可能获得 lock。
Double Barrier
Double barrier 让 client 能同步 computation 的开始和结束。当加入 barrier 的 process 数量达到 barrier threshold 时,process 开始 computation;完成后离开 barrier。
我们在 ZooKeeper 中用一个 znode(记作 b)表示 barrier。每个 process 进入时都在 b 下创建一个 child znode 注册自己;准备离开时删除该 child 进行 unregister。当 b 的 child znode 数量超过 barrier threshold 时,process 可以进入 barrier;当所有 process 都删除自己的 child 时,process 可以离开 barrier。
我们使用 watch 高效等待 enter 和 exit condition。为了进入,process watch b 下一个 ready child 的 existence;当 child 数量超过 threshold 的 process 会创建这个 ready child。为了离开,process watch 某个 child 消失,并且只在该 znode 被删除后检查 exit condition。
第 7 页:Applications 与 Fetching Service、Katta、YMB

3 ZooKeeper Applications
下面描述几个使用 ZooKeeper 的 application,并简要解释它们如何使用 ZooKeeper。每个例子使用的 primitive 以粗体表示。
The Fetching Service
Crawling 是 search engine 的重要组成部分,Yahoo! 会抓取数十亿个 Web document。Fetching Service(FS)是 Yahoo! crawler 的一部分,目前已经投入 production。它本质上拥有指挥 page-fetching process 的 master process。Master 向 fetcher 提供 configuration,fetcher 写回自身 status 和 health。
使用 ZooKeeper 对 FS 的主要好处是:从 master failure 中恢复;在 failure 存在时保证 availability;以及把 client 与 server 解耦,让 client 只需从 ZooKeeper 读取 server status,就能把 request 指向 healthy server。因此,FS 主要用 ZooKeeper 管理 configuration metadata,同时也用它选出 master(leader election)。
Figure 2:Fetching Service workload
原图展示一个由 FS 使用的 ZooKeeper server 在三天内的 read/write traffic。每个点代表一秒样本;纵轴为 operation 数量,横轴为时间。Read traffic 明显高于 write traffic。在速率高于每秒 1000 个 operation 的时期,read:write ratio 在 10:1 到 100:1 之间。该 workload 中的 read operation 是 getData()、getChildren() 和 exists(),使用频率依次增加。
Katta
Katta [17] 是一个使用 ZooKeeper 做 coordination 的 distributed indexer,也是一个非 Yahoo! application 的例子。Katta 使用 shard 分配 indexing work。Master server 把 shard 分配给 slave,并跟踪 progress。Slave 可能 failure,因此 master 必须随着 slave 加入或离开重新分配 load。Master 自己也可能 failure,因此其他 server 必须准备好接管。
Katta 使用 ZooKeeper 跟踪 slave server 和 master 的 status(group membership),处理 master failover(leader election),并跟踪和传播 shard 到 slave 的 assignment(configuration management)。
Yahoo! Message Broker
Yahoo! Message Broker(YMB)是一个 distributed publish-subscribe system。它管理数千个 topic,client 可以向 topic publish message 或接收 message。Topic 被分布到一组 server 上,以提供 scalability。每个 topic 使用 primary-backup scheme replication,确保 message 被复制到两台 machine,以保证可靠 delivery。
组成 YMB 的 server 使用 shared-nothing distributed architecture,因此 coordination 对正确运行至关重要。YMB 使用 ZooKeeper 管理 topic distribution(configuration metadata),处理 machine failure(failure detection 和 group membership),并控制 system operation。
Figure 3:YMB 在 ZooKeeper 中的结构
原图展示一个 broker domain 下的 znode layout,包括 broker_disabled、shutdown、migration_prohibited、nodes、load、topics,以及每个 topic 的 primary、backup 和 hostname。每个 broker domain 有一个 nodes znode;每个 active server 在其中创建 ephemeral znode,并写入 load 和 status information,同时提供 group membership 与 status。所有 YMB server 都监视 shutdown 和 migration_prohibited,以实现集中式控制。topics directory 为每个 topic 保存一个 child znode;topic znode 下的 child 表示该 topic 的 primary、backup server 和 subscriber。
Primary 和 backup server znode 不仅让 server 找到负责某个 topic 的 server,也处理 leader election 和 server crash。
第 8 页:ZooKeeper Implementation、Replicated Database 与 Zab

Figure 4:ZooKeeper service components
原图展示 ZooKeeper service 的高层 component:client 的 Write Request 进入 Request Processor,随后进入 Atomic Broadcast,再写入 Replicated Database;Response 返回 client。Read Request 可以由本地 Replicated Database 直接处理。
4 ZooKeeper Implementation
ZooKeeper 通过在组成 service 的每个 server 上复制 ZooKeeper data,提供 high availability。我们假设 server 会以 crash 方式 failure,faulty server 之后可以恢复。
收到 request 后,server 先由 request processor 准备执行。如果 request 需要 server 之间协调(即 write request),server 使用 agreement protocol(atomic broadcast 的一种 implementation),最后把 change commit 到 ensemble 中所有 server 完整复制的 ZooKeeper database。对于 read request,server 只读取 local database state,并生成 response。
Replicated database 是一个 in-memory database,包含完整 data tree。默认每个 znode 最多存储 1MB data,但这个上限是 configuration parameter,在特定场景可以调整。为支持 recoverability,我们高效地把 update log 到 disk,并在 update 应用到 in-memory database 之前,强制 write 到 disk media。
和 Chubby [8] 一样,我们保存 committed operation 的 replay log(在这里是 write-ahead log),并周期性地对 in-memory database 生成 snapshot。
每个 ZooKeeper server 都服务 client。Client 只连接一个 server 来提交 request。正如前文所述,read request 由每个 server 的 local database replica 服务。改变 service state 的 write request 则由 agreement protocol 处理。
作为 agreement protocol 的一部分,write request 会被转发给一个 server,称为 leader。其余 ZooKeeper server 称为 follower;它们接收由 leader 提出的 state-change proposal,并对 state change 达成一致。
4.1 Request Processor
由于 messaging layer 是 atomic 的,我们保证 local replica 永远不会 diverge,尽管在任意时刻,某些 server 可能已经比其他 server 应用了更多 transaction。与 client 发送的 request 不同,transaction 是 idempotent 的。
Leader 收到 write request 后,会计算应用 write 后 system 的 state,并把 request 转换成描述这个新 state 的 transaction。必须计算 future state,是因为可能有尚未应用到 database 的 outstanding transaction。
例如,如果 client 执行 conditional setData,并且 request 中的 version number 与待更新 znode 的 future version number 匹配,service 就生成一个 setDataTXN,其中包含 new data、new version number 和更新后的 timestamp。如果发生 error,例如 version number 不匹配,或者待更新 znode 不存在,则生成 errorTXN。
4.2 Atomic Broadcast
所有更新 ZooKeeper state 的 request 都转发给 leader。Leader 执行 request,并通过 Zab [24](一种 atomic broadcast protocol)广播 ZooKeeper state 的 change。收到 client request 的 server,在 delivery 对应 state change 后向 client 返回 response。
Zab 默认使用 simple majority quorum 决定 proposal,因此 Zab 以及 ZooKeeper 只有在多数 server 正确时才能工作(即有 2f + 1 个 server 时可以容忍 f 个 failure)。
为了获得高 throughput,ZooKeeper 尽量保持 request-processing pipeline 饱和,可能同时有数千个 request 位于 pipeline 的不同阶段。由于 state change 依赖前序 state change 的 application,Zab 比 regular atomic broadcast 提供更强的 order guarantee:leader broadcast 的 change 按发送顺序 delivery;而且在 established leader broadcast 自己的 change 之前,前任 leader 的所有 change 都会 delivery。
一些 implementation detail 同时简化 implementation 并提供良好 performance。我们使用 TCP 作为 transport,network 会保持 message order,从而简化 implementation。我们使用 Zab 选出的 leader 作为 ZooKeeper leader,因此创建 transaction 和 proposal 的是同一个 process。我们使用 log 跟踪 proposal,把它作为 in-memory database 的 write-ahead log,这样不必把 message 写两次 disk。
第 9 页:Zab Recovery、Fuzzy Snapshot 与 Client-Server Interaction

正常运行时,Zab 会按顺序且 exactly once delivery 所有 message。但由于 Zab 没有持久记录每个已 delivery message 的 ID,恢复期间可能重新 delivery 某个 message。因为 transaction 是 idempotent 的,只要按顺序 delivery,多次 delivery 就可以接受。
事实上,ZooKeeper 要求 Zab 至少重新 delivery 从最近一次 snapshot 开始之后 delivery 的所有 message。
4.3 Replicated Database
每个 replica 都在 memory 中拥有一份 ZooKeeper state 的 copy。当 ZooKeeper server 从 crash 中恢复时,需要恢复这份 internal state。如果重新应用所有 delivery message,server 运行一段时间后 recovery 时间会长得无法接受,因此 ZooKeeper 使用 periodic snapshot,只要求重新 delivery snapshot 开始之后的 message。
我们称 ZooKeeper snapshot 为 fuzzy snapshot,因为生成 snapshot 时不会 lock ZooKeeper state;相反,system 对 data tree 做 depth-first scan,atomic 地读取每个 znode 的 data 和 metadata,并写入 disk。
由于 fuzzy snapshot 可能在生成过程中应用了某些 state change 的 subset,因此它可能不对应 ZooKeeper 在任何时刻的真实 state。然而,只要按顺序应用 state change,就可以把 state change 应用两次,因为 transaction 是 idempotent 的。
例如,假设 fuzzy snapshot 开始时,ZooKeeper data tree 中 /foo 和 /goo 的 value 分别是 f1 和 g1,二者 version 都是 1。随后到达以下形式的 state-change stream:
⟨SetDataTXN, /foo, f2, 2⟩
⟨SetDataTXN, /goo, g2, 2⟩
⟨SetDataTXN, /foo, f3, 3⟩
应用这些 state change 后,/foo 和 /goo 的 value 分别是 f3 和 g2,version 分别是 3 和 2。但 fuzzy snapshot 可能记录 /foo = f3, version = 3,同时记录 /goo = g1, version = 1;这并不是 ZooKeeper data tree 曾经处于的有效 state。如果 server 使用这个 snapshot crash 并恢复,随后 Zab 重新 delivery state change,得到的 state 会对应 crash 前 service 的 state。
4.4 Client-Server Interactions
Server 处理 write request 时,也会发送并清除与该 update 对应的 watch notification。Server 按顺序处理 write,不会与其他 write 或 read 并发处理,因此保证 notification 严格 succession。注意,server 在本地处理 notification:只有 client 所连接的 server 跟踪并 trigger 该 client 的 notification。
Read request 在每个 server 本地处理。每个 read request 都被处理,并标记一个对应 server 最近看到的 transaction 的 zxid。这个 zxid 定义 read request 相对于 write request 的 partial order。
本地处理 read 可以获得 excellent read performance,因为它只是 local server 上的 in-memory operation,不需要 disk activity 或 agreement protocol。这一 design choice 是 read-dominant workload 获得 excellent performance 的关键。
快速 read 的一个缺点是不能保证 read operation 的 precedence order:即使对同一个 znode 的较新 update 已 commit,read operation 仍可能返回 stale value。并非所有 application 都需要 precedence order;对于需要它的 application,我们实现了 sync。
sync asynchronous 执行,由 leader 在其 local replica 上所有 pending write 之后排序。为了保证某个 read operation 返回最新 update 的 value,client 调用 sync,然后调用 read。Client operation 的 FIFO order guarantee,加上 sync 的 global guarantee,使 read result 能反映 sync 发出之前发生的所有 change。
在我们的 implementation 中,由于使用 leader-based algorithm,不需要对 sync 做 atomic broadcast。我们只需把 sync operation 放到 leader 与执行 sync call 的 server 之间 request queue 的末尾。
要使这一点成立,follower 必须确认 leader 仍然是 leader。如果存在即将 commit 的 pending transaction,server 不会怀疑 leader;如果 pending queue 为空,leader 必须发出一个 null transaction 来 commit,并把 sync 排在这个 transaction 后面。这样 leader 负载很高时不会产生额外 broadcast traffic。在我们的 implementation 中,timeout 被设置成让 leader 在 follower 放弃它之前意识到自己已经不是 leader,因此不需要发 null transaction。
ZooKeeper server 按 FIFO order 处理 client request。Response 包含其相对的 zxid。即使没有 activity,heartbeat message 也包含 client 所连接 server 最近看到的 zxid。如果 client 连接到新 server,新 server 会把自己的 ZooKeeper data view 与 client 的 last zxid 比较,确保自己的 view 至少同样新。如果 client 的 view 更新,新 server 不会重新建立 client session,直到自己追上为止。
第 10 页:Session Failure、Throughput Evaluation 与 Figure 5

由于 client 只看到已经复制到多数 ZooKeeper server 的 change,因此 client 一定能找到拥有 recent view 的另一个 server。这个行为对保证 durability 很重要。
为了检测 client session failure,ZooKeeper 使用 timeout。如果在 session timeout 内没有任何其他 server 收到来自某个 client session 的内容,leader 就判断发生 failure。如果 client 足够频繁地发送 request,就不需要发送其他 message;否则 client 在低 activity 期间发送 heartbeat message。
如果 client 无法与 server 通信以发送 request 或 heartbeat,它会连接另一个 ZooKeeper server 来重新建立 session。为了防止 session timeout,ZooKeeper client library 在 session idle s/3 ms 后发送 heartbeat;如果 2s/3 ms 内没有收到 server response,就切换到新 server,其中 s 是以 milliseconds 表示的 session timeout。
5 Evaluation
所有 evaluation 都在一个 50-server cluster 上完成。每台 server 有一个 Xeon dual-core 2.1GHz processor、4GB RAM、gigabit Ethernet 和两块 SATA hard drive。以下 discussion 分为 request throughput 和 request latency 两部分。
5.1 Throughput
为了评估 system,我们在 system 饱和时测量 throughput,并测量注入不同 failure 后 throughput 的变化。我们改变组成 ZooKeeper service 的 server 数量,但始终保持 client 数量相同。为了模拟大量 client,使用 35 台 machine 模拟 250 个 simultaneous client。
我们有 Java 版 ZooKeeper server,以及 Java 和 C client。实验使用配置为写入一块 dedicated disk、并把 snapshot 写到另一块 disk 的 Java server。Benchmark client 使用 asynchronous Java client API,每个 client 至少有 100 个 outstanding request。每个 request 读或写 1KB data。
我们不展示其他 operation 的 benchmark,因为所有修改 state 的 operation performance 大致相同;除 sync 外,不修改 state 的 operation performance 也大致相同(sync 的 performance 接近 lightweight write,因为 request 必须到 leader,但不会被 broadcast)。Client 每 300ms 发送一次 completed-operation count,系统每 6s sample 一次。
为防止 memory overflow,server 会 throttle system 中 concurrent request 数量。ZooKeeper 使用 request throttling,避免 server 被压垮。本实验中,ZooKeeper server 的 in-process request 上限配置为 2,000。
Figure 5:Saturated system 的 throughput
原图横轴为 read request percentage,纵轴为每秒 operation 数量;曲线分别对应 3、5、7、9、13 个 server。读比例越高,本地 read 的优势越明显。
Table 1:Saturated system 两个极端的 throughput
| Server 数量 | 100% Reads | 0% Reads |
|---|---|---|
| 13 | 460k | 8k |
| 9 | 296k | 12k |
| 7 | 257k | 14k |
| 5 | 165k | 18k |
| 3 | 87k | 21k |
Figure 5 展示改变 read/write ratio 时的 throughput,每条 curve 对应不同数量的 ZooKeeper server;Table 1 展示 read load 的两个极端。
Read throughput 高于 write throughput,因为 read 不使用 atomic broadcast。Graph 还展示 server 数量会对 broadcast protocol performance 产生负面影响。因此,server 数量不仅影响 service 能够容忍的 failure 数量,也影响 service 能够处理的 workload。注意,3-server configuration 的 curve 在约 60% read 处与其他 curve 相交;由于 local read 带来的 parallelism,所有 configuration 都会出现这种情况,只是图中其他 configuration 的 y-axis throughput 上限被截断以便可读,所以看不出来。
Write request 比 read request 处理时间长有两个原因。首先,write 必须经过 atomic broadcast,会增加额外 processing 和 latency。其次,server 在向 leader 发送 acknowledgment 前,必须确保 transaction 已记录到 non-volatile store。原则上这个要求可能过于严格,但由于 ZooKeeper 是 production system 的 application ground truth,我们用 performance 换 reliability。我们通过增加 server 来容忍更多 fault;通过把 ZooKeeper data partition 到多个 ZooKeeper ensemble 来提高 write throughput。这种 replication 与 partitioning 之间的 performance trade-off,Gray 等人 [12] 也观察过。
第 11 页:Leader-only Throughput、Atomic Broadcast 与故障注入

Figure 6:所有 request 发往 leader 时的 throughput
原图比较 3、5、7、9、13 个 server,在所有 client 只连接 leader 时,read/write ratio 与 saturated throughput 的关系。
ZooKeeper 能获得如此高的 throughput,是因为把 load 分散到组成 service 的多个 server。由于 relaxed consistency guarantee,我们能够这样分散 load。Chubby client 则把所有 request 发给 leader。
Figure 6 展示如果不利用这种 relaxation、强制 client 只连接 leader,会发生什么。对于 read-dominant workload,throughput 如预期大幅降低;即使 write-dominant workload,throughput 也更低。服务 client 会带来额外 CPU 和 network load,影响 leader 协调 proposal broadcast 的能力,进而不利于整体 write performance。
Atomic broadcast protocol 承担 system 的大部分 work,因此比其他任何 component 更限制 ZooKeeper performance。Figure 7 展示 atomic broadcast component 的 throughput。为了 benchmark 其 performance,我们让 leader 直接生成 transaction 来模拟 client,从而没有 client connection 或 client request/response。
在最大 throughput 下,atomic broadcast component 受 CPU 限制。理论上,Figure 7 的 performance 应该等于 100% write 时 ZooKeeper 的 performance。但 ZooKeeper 的 client communication、ACL check,以及 request-to-transaction conversion 都需要 CPU。CPU contention 使 ZooKeeper throughput 明显低于 atomic broadcast component 单独运行时的 throughput。
由于 ZooKeeper 是 critical production component,到目前为止我们的 development focus 是 correctness 和 robustness。通过消除 extra copy、同一 object 的多次 serialization,以及使用更高效的 internal data structure 等方式,仍有许多显著改善 performance 的机会。
Figure 7:Atomic broadcast component 单独运行的平均 throughput
原图横轴为 ensemble size,纵轴为 requests per second;error bar 表示 minimum 和 maximum value。
Figure 8:Failure 注入期间的 throughput
为了展示 failure 被注入时 system 的行为,我们运行一个由 5 台 machine 组成的 ZooKeeper service。使用之前相同的 saturation benchmark,但把 write percentage 固定为 30%,这是对预期 workload 的 conservative ratio。我们周期性地 kill 某些 server process。Figure 8 展示 throughput 随时间变化;图中事件为:
- 一个 follower failure 并恢复。
- 另一个 follower failure 并恢复。
- leader failure。
- 两个 follower failure(前两个标记为 a、b),并在第三个标记 c 处恢复。
- leader failure。
第 12 页:故障观察、Latency 与 Barrier Performance

- leader recovery。
从 graph 可以得到几个重要观察。第一,如果 follower 很快 failure 并恢复,ZooKeeper 即使存在 failure 也能保持高 throughput。单个 follower failure 不会阻止 server 形成 quorum;throughput 只会大约减少该 server failure 前处理的 read request 所占的比例。
第二,leader election algorithm 恢复得足够快,不会让 throughput 大幅下降。我们的观察是,ZooKeeper 选出新 leader 所需时间少于 200ms。因此,尽管 server 会停止服务不到一秒,我们的 sampling period 以秒为量级,不会观察到 throughput 为零。
第三,即使 follower 需要更长时间恢复,ZooKeeper 也能在 follower 开始处理 request 后重新提高 throughput。事件 1、2、4 后没有恢复到完整 throughput 的一个原因是:client 只有在与 follower 的 connection 断开时才切换 follower。因此,event 4 后 client 不会重新分布,直到 event 3 和 5 的 leader failure 发生。实践中,随着 client 不断加入和离开,这些 imbalance 会随时间自行消失。
5.2 Latency of requests
为了评估 request latency,我们构造了一个以 Chubby benchmark [6] 为模型的 benchmark。创建一个 worker process:它发送一个 create,等待 create 完成;然后发送一个新 node 的 asynchronous delete,再开始下一个 create。我们改变 worker 数量;每次运行中,每个 worker 创建 50,000 个 node。通过用所有 worker 完成 create request 的总数除以它们完成所需的总时间,计算 throughput。
Table 2:每秒处理的 Create request
| Server 数量 | 1 worker | 10 workers | 20 workers |
|---|---|---|---|
| 3 | 776 | 2074 | 2740 |
| 5 | 748 | 1832 | 2336 |
| 7 | 758 | 1572 | 1934 |
| 9 | 711 | 1540 | 1890 |
Table 2 的 create request 包含 1KB data,而 Chubby benchmark 只有 5 bytes,因此更接近我们的预期 use case。即使 request 更大,ZooKeeper throughput 仍超过 Chubby 已公布 throughput 的 3 倍。单个 ZooKeeper worker benchmark 表明:3-server 时 average request latency 是 1.2ms,9-server 时是 1.4ms。
5.3 Performance of barriers
本实验连续执行若干 barrier,评估使用 ZooKeeper 实现的 primitive 的 performance。对于给定 barrier 数量 b,每个 client 先依次进入全部 b 个 barrier,再依次离开全部 barrier。由于使用第 2.4 节的 double-barrier algorithm,client 必须先等待所有其他 client 执行 enter(),才能进入下一次调用;leave() 也同理。
Table 3:Barrier experiment(时间单位为 seconds)
| Barrier 数量 | 50 clients | 100 clients | 200 clients |
|---|---|---|---|
| 200 | 9.4 | 19.8 | 41.0 |
| 400 | 16.4 | 34.1 | 62.0 |
| 800 | 28.9 | 55.9 | 112.1 |
| 1600 | 54.0 | 102.7 | 234.4 |
实验中有 50、100、200 个 client 依次进入数量为 b 的 barrier,b ∈ {200, 400, 800, 1600}。尽管 application 可以拥有数千个 ZooKeeper client,但通常每次 coordination operation 只由更小的 subset 参与,因为 client 往往按 application specification 分组。
有两个有趣观察:处理全部 barrier 的时间随 barrier 数量大致线性增长,说明对 data tree 同一部分的 concurrent access 没有产生意外 delay;latency 与 client 数量成比例增加,这是没有 saturate ZooKeeper service 的结果。
事实上,即使 client lock-step 前进,barrier operation(enter 和 leave)的 throughput 在所有情况下也处于 1,950–3,100 operation/s。折算到 ZooKeeper operation,相当于 10,700–17,000 operation/s。由于我们的 implementation 中 read/write ratio 为 4:1(80% read),benchmark code 使用的 throughput 远低于 ZooKeeper 可以达到的 raw throughput(Figure 5 中超过 40,000),原因是 client 在等待其他 client。
第 13 页:Related Work 与 Conclusions 上半部分

6 Related work
ZooKeeper 并不是第一个提出用于协调 distributed application 的 system。早期 system 为 transactional application [13] 和 computer cluster 中的信息共享 [19] 提出 distributed lock service。更近期的 Chubby [6] 提出为 distributed application 管理 advisory lock 的 system。
Chubby 与 ZooKeeper 有若干共同目标。它也拥有 file-system-like interface,并使用 agreement protocol 保证 replica consistency。但 ZooKeeper 不是 lock service:client 可以用它实现 lock,但其 API 没有 lock operation。与 Chubby 不同,ZooKeeper 允许 client 连接任意 ZooKeeper server,而不只是 leader。
由于 ZooKeeper 的 consistency model 比 Chubby relaxed 得多,ZooKeeper client 可以使用 local replica 提供 data 并管理 watch。这使 ZooKeeper 能比 Chubby 提供更高 performance,让 application 更广泛地使用 ZooKeeper。
文献中还有一些 fault-tolerant system,目标是降低构建 fault-tolerant distributed application 的难度。早期 system 之一是 ISIS [5]:它把 abstract type specification 转换为 fault-tolerant distributed object,使 fault-tolerance mechanism 对 user 透明。Horus [30] 和 Ensemble [31] 都从 ISIS 演化而来。ZooKeeper 采用了 ISIS 的 virtual synchrony 概念。
Totem [22] 在利用 local-area network hardware broadcast 的 architecture 中保证 message total order。ZooKeeper 面向多种 network topology,因此使用 server process 之间的 TCP connection,而不假设特殊 topology 或 hardware feature。我们也不向 user 暴露 ZooKeeper 内部使用的 ensemble communication。
构建 fault-tolerant service 的一个重要技术是 state-machine replication [26];Paxos [20] 是一种允许在 asynchronous system 中高效实现 replicated state machine 的 algorithm。我们使用的 algorithm 与 Paxos 共享一些特征,但把 consensus 所需的 transaction logging 与 data-tree recovery 所需的 write-ahead logging 结合起来,以获得高效 implementation。
文献中还提出了 practical Byzantine-tolerant replicated state machine protocol [7, 10, 18, 1, 28]。ZooKeeper 不假设 server 会 Byzantine,但使用 checksum 和 sanity check 等机制捕获 non-malicious Byzantine fault。Clement 等人 [9] 讨论了不修改当前 server code base、使 ZooKeeper 完全 Byzantine fault-tolerant 的方法。到目前为止,我们没有观察到生产环境中的 fault 会被 fully Byzantine fault-tolerant protocol 所避免 [29]。
Boxwood [21] 是使用 distributed lock server 的 system。它向 application 提供更高层 abstraction,并依赖一个基于 Paxos 的 distributed lock service。和 Boxwood 一样,ZooKeeper 是构建 distributed system 的 component。但 ZooKeeper 有更高的 performance requirement,并且在 client application 中使用得更广泛。ZooKeeper 暴露的是由 application 用来实现更高层 primitive 的 lower-level primitive。
ZooKeeper 看起来像一个小型 file system,但它只提供少数 file-system operation,并加入大多数 file system 没有的 functionality,例如 ordering guarantee 和 conditional write。不过,ZooKeeper watch 在思想上类似 AFS [16] 的 cache callback。
Sinfonia [2] 引入 mini-transaction,一种构建 scalable distributed system 的新 paradigm。Sinfonia 设计用于存储 application data,而 ZooKeeper 存储 application metadata。ZooKeeper 为了获得高 performance 和 consistent latency,把 state 完整复制并放入 memory。我们对 file-system-like operation 和 ordering 的使用,提供了与 mini-transaction 相近的功能。Znode 是一种方便的 abstraction,我们在其上增加 watch;Sinfonia 缺少这项功能。
Dynamo [11] 允许 client 在 distributed key-value store 中 get/put 相对较小(小于 1MB)的 data。与 ZooKeeper 不同,Dynamo 的 key space 不是 hierarchical。Dynamo 也不为 write 提供 strong durability 和 consistency guarantee,而是在 read 时解决 conflict。
DepSpace [4] 使用 tuple space 提供 Byzantine fault-tolerant service。和 ZooKeeper 一样,DepSpace 使用简单 server interface,让 client 实现 strong synchronization primitive。DepSpace 的 performance 远低于 ZooKeeper,但提供更强的 fault tolerance 和 confidentiality guarantee。
7 Conclusions
ZooKeeper 通过向 client 暴露 wait-free object,采用 wait-free 方法解决 distributed system 中的 process coordination 问题。我们发现 ZooKeeper 对 Yahoo! 内部和外部的多个 application 都很有用。
通过使用 fast read 和 watch(两者都由 local replica 服务),ZooKeeper 在 read-dominant workload 下达到了每秒数十万 operation 的 throughput。尽管我们对 read 和 watch 提供的 consistency guarantee 看起来很弱,但通过 use case 我们展示了这种组合如何让 client 实现高效而复杂的 coordination protocol,即使 read 没有 precedence order,data object 的 implementation 也是 wait-free 的。
Wait-free property 已经证明对 high performance 至关重要。
第 14 页:Conclusions、Acknowledgements 与 References

虽然本文只描述了少数 application,但还有许多其他 application 使用 ZooKeeper。我们认为这种成功来自简单的 interface,以及可以通过该 interface 实现的强大 abstraction。此外,由于 ZooKeeper 具有高 throughput,application 不仅可以使用它做 coarse-grained locking,还可以广泛使用它。
Acknowledgements
感谢 Andrew Kornev 和 Runping Qi 对 ZooKeeper 的贡献;感谢 Zeke Huang 和 Mark Marchukov 提供有价值的 feedback;感谢 Brian Cooper 和 Laurence Ramontianu 对 ZooKeeper 的早期贡献;Brian Bershad 和 Geoff Voelker 对 presentation 提出了重要意见。
References
- M. Abd-El-Malek, G. R. Ganger, G. R. Goodson, M. K. Reiter, and J. J. Wylie. Fault-scalable Byzantine fault-tolerant services. SOSP ’05, pp. 59–74, 2005。
- M. Aguilera, A. Merchant, M. Shah, A. Veitch, and C. Karamanolis. Sinfonia: A new paradigm for building scalable distributed systems. SOSP ’07, 2007。
- Amazon. Amazon Simple Queue Service. 2008。
- A. N. Bessani, E. P. Alchieri, M. Correia, and J. da Silva Fraga. DepSpace: A Byzantine fault-tolerant coordination service. EuroSys 2008。
- K. P. Birman. Replication and fault-tolerance in the ISIS system. SOSP ’85, 1985。
- M. Burrows. The Chubby lock service for loosely-coupled distributed systems. OSDI, 2006。
- M. Castro and B. Liskov. Practical Byzantine fault tolerance and proactive recovery. ACM TOCS 20(4), 2002。
- T. Chandra, R. Griesemer, and J. Redstone. Paxos made live: An engineering perspective. PODC, 2007。
- A. Clement, M. Kapritsos, S. Lee, Y. Wang, L. Alvisi, M. Dahlin, and T. Riche. UpRight cluster services. SOSP, 2009。
- J. Cowling, D. Myers, B. Liskov, R. Rodrigues, and L. Shira. HQ replication: A hybrid quorum protocol for Byzantine fault tolerance. SOSP ’07, 2007。
- G. DeCandia et al. Dynamo: Amazon’s highly available key-value store. SOSP ’07, 2007。
- J. Gray, P. Helland, P. O’Neil, and D. Shasha. The dangers of replication and a solution. SIGMOD ’96, pp. 173–182, 1996。
- A. Hastings. Distributed lock management in a transaction processing environment. IEEE SRDS, 1990。
- M. Herlihy. Wait-free synchronization. ACM TOPLAS 13(1), 1991。
- M. Herlihy and J. Wing. Linearizability: A correctness condition for concurrent objects. ACM TOPLAS 12(3), 1990。
- J. H. Howard et al. Scale and performance in a distributed file system. ACM TOCS 6(1), 1988。
- Katta. Katta — distribute Lucene indexes in a grid. 2008。
- R. Kotla et al. Zyzzyva: Speculative Byzantine fault tolerance. SIGOPS Operating Systems Review 41(6), 2007。
- N. P. Kronenberg, H. M. Levy, and W. D. Strecker. VAXclusters: A closely-coupled distributed system. SIGOPS Operating Systems Review 19(5), 1985。
- L. Lamport. The part-time parliament. ACM TOCS 16(2), 1998。
- J. MacCormick et al. Boxwood: Abstractions as the foundation for storage infrastructure. OSDI, 2004。
- L. Moser et al. The Totem system. International Symposium on Fault-Tolerant Computing, 1995。
- S. Mullender, ed. Distributed Systems, 2nd edition. ACM Press, 1993。
- B. Reed and F. P. Junqueira. A simple totally ordered broadcast protocol. LADIS ’08, 2008。
- N. Schiper and S. Toueg. A robust and lightweight stable leader election service for dynamic systems. DSN, 2008。
- F. B. Schneider. Implementing fault-tolerant services using the state machine approach: A tutorial. ACM Computing Surveys 22(4), 1990。
- A. Sherman, P. A. Lisiecki, A. Berkheimer, and J. Wein. ACMS: The Akamai configuration management system. NSDI, 2005。
- A. Singh et al. Zeno: Eventually consistent Byzantine-fault tolerance. NSDI ’09, pp. 169–184, 2009。
- Y. J. Song, F. Junqueira, and B. Reed. BFT for the skeptics。
- R. van Renesse and K. Birman. Horus, a flexible group communication system. Communications of the ACM 39(16), 1996。
- R. van Renesse et al. Building adaptive systems using Ensemble. Software—Practice and Experience 28(5), 1998。
机制索引
| 机制 | 本文位置 | 关键判断 |
|---|---|---|
| Znode data tree | 第 2–3 页 | 用 file-system-like hierarchy 存储 application metadata,而非通用 data |
| Ephemeral/sequential node | 第 3、5–6 页 | 让 session liveness、唯一排队顺序和自动清理成为 client recipe 的基础 |
| Watch | 第 3–5 页 | 一次性 notification 替代 polling,不阻塞 server 等待 client cache invalidation |
| A-linearizable write | 第 4 页 | 支持异步 outstanding operation,同时保持 update 的全局顺序 |
| Local read | 第 4、9 页 | read 不走 Zab,可扩展 read throughput,但可能 stale |
| Ready znode | 第 4 页 | 用删除/更新/重建 marker 防止 client 使用 partial configuration |
| Recipes | 第 5–7 页 | configuration、rendezvous、membership、lock、read/write lock、barrier 均在 client 实现 |
| Zab | 第 8–9 页 | leader-based atomic broadcast,提供 write order、quorum 和 recovery 基础 |
| Fuzzy snapshot | 第 9 页 | 不暂停 tree 的 snapshot + ordered idempotent replay,降低恢复成本 |
| Sync | 第 5、9 页 | 在 local read 前建立“看到此前 pending write”的边界 |
| Session migration | 第 9–10 页 | 通过 zxid 确保新 server 的 view 不落后于 client |
| Ensemble scaling | 第 10–12 页 | 更多 replica 提高 fault tolerance 但降低 broadcast performance;可 partition ensemble |
本篇验收
- 页面覆盖:14/14 个 PDF 页面均已保留原图并按原顺序翻译。
- 图表覆盖:保留 Figure 1–8、Table 1–3、lock/read-write-lock protocol、fuzzy snapshot stream、API、性能曲线和故障时间线。
- 机制覆盖:service overview、znode/data tree、ephemeral/sequential node、watch、session、API、A-linearizability、recipes、Zab、replicated database、fuzzy snapshot、client-server interaction、session migration、performance、failure injection、barrier benchmark、related work、conclusions 和 31 条 References 均已覆盖。
- 翻译策略:遵循逐页、逐段顺序;ZooKeeper 生态中的约定俗成术语保留 English,不强行直译;原文页面图作为版式与图片位置的最终依据。
参考资料
评论 (0)