Lab 5:分片 KV 与分片迁移
Lab 5:分片 KV 与分片迁移
Lab 5 在 Lab 4 的复制状态机之上增加“按 shard 水平切分数据”和“动态迁移 shard”:控制器先冻结源 shard,再安装到目标组,最后更新配置,让客户端始终只访问当前拥有者。
TL;DR
- 分片(shard)把 key 空间切成多个子集,每个 shard group 只负责其中一部分,从而让多个组并行处理请求。
- 控制器把配置保存在 Lab 2 的
kvsrv中;客户端通过配置把 key 路由到对应的 shard group。 - 配置变更的安全顺序是
FreezeShard → InstallShard → DeleteShard → 发布新配置,避免新旧组同时接受写入。 - Part B 使用
current与next两份配置恢复中断迁移;Part C 使用 kvsrv 的版本号对next配置做 CAS/fencing,防止并发控制器互相覆盖。 - 当前实现位置:
implementation/6.5840/src/shardkv1、implementation/6.5840/src/kvsrv1和implementation/6.5840/src/kvraft1/rsm。这是本地教学实现;5A–5C 是实现范围,Part D 仅翻译。
目录
| 章节 | 内容 |
|---|---|
| 原文逐行翻译 | 按 source/lab-shard1.html 顺序翻译 |
| 实现映射 | 控制器、分片组、客户端与迁移协议 |
| 关键状态机 | 路由、迁移、配置 fencing |
| 测试验收 | 实现验证结果与已知压力项 |
| 下一步 | Lab 5 之后的扩展方向 |
原文逐行翻译
来源为本地
source/lab-shard1.html。正文按原文顺序逐句对照;以下代码块中的测试结果是官方讲义示例,不是本机验收输出。原文的拼写或命令错误保留,例如reponsible、configuation、TestStatiOneShardGroup5A;实际运行请以本地测试名为准。
标题与导航
6.5840 Lab 5: Sharded Key/Value Service
6.5840 实验 5:分片键值服务
Collaboration policy
协作政策
Submit lab
提交实验
Setup Go
配置 Go 环境
Guidance
指导说明
Piazza
Piazza 课程讨论区
Introduction
Introduction
简介
You can either do a final project based on your own ideas, or this lab.
你可以根据自己的想法完成期末项目,也可以完成本实验。
In this lab you'll build a key/value storage system that "shards," or partitions, the keys over a set of Raft-replicated key/value server groups (shardgrps).
在本实验中,你将构建一个键值存储系统,把 key“分片”(shard),也就是把 key 分区到一组由 Raft 复制的键值服务器组(shardgrp)上。
A shard is a subset of the key/value pairs; for example, all the keys starting with "a" might be one shard, all the keys starting with "b" another, etc.
一个 shard 是键值对的一个子集;例如,所有以 “a” 开头的 key 可以组成一个 shard,所有以 “b” 开头的 key 组成另一个 shard,依此类推。
The reason for sharding is performance.
进行分片的原因是性能。
Each shardgrp handles puts and gets for just a few of the shards, and the shardgrps operate in parallel; thus total system throughput (puts and gets per unit time) increases in proportion to the number of shardgrps.
每个 shardgrp 只处理少数几个 shard 的 put 和 get,并且各个 shardgrp 可以并行运行;因此,系统总吞吐量(单位时间内的 put 和 get 数量)会随 shardgrp 数量近似成比例增加。
原文架构图位于这里:

图中展示了 Controller、Clerk、kvsrv 和多个 ShardGrp 之间的请求与迁移关系。
The sharded key/value service has the components shown above.
分片键值服务由上图所示的组件组成。
Shardgrps (shown with blue squares) store shards with keys: shardgrp 1 holds a shard storing key "a", and shardgrp 2 holds a shard storing key "b".
Shardgrp(图中的蓝色方框)保存带有 key 的 shard:shardgrp 1 保存包含 key “a” 的 shard,shardgrp 2 保存包含 key “b” 的 shard。
Clients of the sharded key/value service interact with the service through a clerk (shown with a green circle), which implements
GetandPutmethods.
分片键值服务的客户端通过 Clerk(图中的绿色圆圈)与服务交互;Clerk 实现 Get 和 Put 方法。
To find the shardgrp for a key passed to
Put/Get, the clerk gets the configuration from the kvsrv (shown with a black square), which you implemented in Lab 2.
为了找到 Put/Get 中某个 key 对应的 shardgrp,Clerk 会从 kvsrv(图中的黑色方框)获取配置;这个 kvsrv 是你在 Lab 2 中实现的。
The configuration (not shown) describes the mapping from shards to shardgrps (e.g., shard 1 is served by shardgrp 3).
配置(图中未画出)描述 shard 到 shardgrp 的映射,例如 shard 1 由 shardgrp 3 提供服务。
An administrator (i.e., the tester) uses another client, the controller (shown with a purple circle), to add/remove shardgrps from the cluster and update which shardgrp should serve a shard.
管理员(也就是 tester)使用另一个客户端 Controller(图中的紫色圆圈)向集群中添加或移除 shardgrp,并更新某个 shard 应由哪个 shardgrp 提供服务。
The controller has one main method:
ChangeConfigTo, which takes as argument a new configuration and changes the system from the current configuration to the new configuration; this involves moving shards to new shardgrps that are joining the system and moving shards away from shardgrps that are leaving the system.
Controller 有一个主要方法:ChangeConfigTo。它接收一个新配置,把系统从当前配置变更到新配置;这包括把 shard 移动到新加入的 shardgrp,以及把 shard 从即将离开的 shardgrp 移走。
To do so the controller 1) makes RPCs (
FreezeShard,InstallShard, andDeleteShard) to shardgrps, and 2) updates the configuration stored in kvsrv.
为此,Controller 会:1)向 shardgrp 发送 FreezeShard、InstallShard 和 DeleteShard RPC;2)更新保存在 kvsrv 中的配置。
The reason for the controller is that a sharded storage system must be able to shift shards among shardgrps.
引入 Controller 的原因是,分片存储系统必须能够在 shardgrp 之间移动 shard。
One reason is that some shardgrps may become more loaded than others, so that shards need to be moved to balance the load.
一个原因是某些 shardgrp 可能比其他组负载更高,因此需要移动 shard 来平衡负载。
Another reason is that shardgrps may join and leave the system: new shardgrps may be added to increase capacity, or existing shardgrps may be taken offline for repair or retirement.
另一个原因是 shardgrp 可能加入或离开系统:可以加入新的 shardgrp 来增加容量,也可以让已有 shardgrp 下线进行维修或退役。
The main challenges in this lab will be ensuring linearizability of
Get/Putoperations while handling 1) changes in the assignment of shards to shardgrps, and 2) recovering from a controller that fails or is partitioned duringChangeConfigTo.
本实验的主要挑战是:在处理 1)shard 到 shardgrp 的分配变化,以及 2)Controller 在 ChangeConfigTo 期间失败或被网络分区时恢复的同时,保证 Get/Put 操作的线性一致性。
ChangeConfigTomoves shards from one shardgrp to another. A risk is that some clients might use the old shardgrp while other clients use the new shardgrp, which could break linearizability.
ChangeConfigTo 会把 shard 从一个 shardgrp 移动到另一个 shardgrp。风险在于,一些客户端可能使用旧 shardgrp,而另一些客户端使用新 shardgrp,这可能破坏线性一致性。
You will need to ensure that at most one shardgrp is serving requests for each shard at any one time.
你需要保证任意时刻每个 shard 最多只有一个 shardgrp 对外提供请求服务。
If
ChangeConfigTofails while reconfiguring, some shards may be inaccessible if they have started but not completed moving from one shardgrp to another.
如果 ChangeConfigTo 在重新配置过程中失败,那么一些已经开始移动但尚未完成从一个 shardgrp 到另一个 shardgrp 的 shard 可能会变得不可访问。
To make forward progress, the tester starts a new controller, and your job is to ensure that the new one completes the reconfiguration that the previous controller started.
为了继续推进,tester 会启动新的 Controller;你的任务是保证新 Controller 完成上一个 Controller 已经开始的重新配置。
This lab uses "configuration" to refer to the assignment of shards to shardgrps. This is not the same as Raft cluster membership changes. You don't have to implement Raft cluster membership changes.
本实验中,“configuration”指 shard 到 shardgrp 的分配。这与 Raft 集群成员变更不是一回事,你不需要实现 Raft 集群成员变更。
A shardgrp server is a member of only a single shardgrp. The set of servers in a given shardgrp will never change.
一个 shardgrp 服务器只属于一个 shardgrp。给定 shardgrp 中的服务器集合永远不会变化。
Only RPC may be used for interaction among clients and servers. For example, different instances of your server are not allowed to share Go variables or files.
客户端和服务器之间只能通过 RPC 交互。例如,不允许不同的服务器实例共享 Go 变量或文件。
In Part A, you will implement a working
shardctrler, which will store and retrieve configurations in akvsrv.
在 Part A 中,你将实现一个可工作的 shardctrler,它会在 kvsrv 中保存和读取配置。
You will also implement the
shardgrp, replicated with your Raftrsmpackage, and a correspondingshardgrpclerk.
你还将实现由自己的 Raft rsm 包复制的 shardgrp,以及对应的 shardgrp Clerk。
The
shardctrlertalks to theshardgrpclerks to move shards between different groups.
shardctrler 通过 shardgrp Clerk 与 shardgrp 交互,把 shard 在不同组之间移动。
In Part B, you will modify your
shardctrlerto handle failures and partitions during config changes.
在 Part B 中,你将修改 shardctrler,处理配置变更期间的故障和网络分区。
In Part C, you will extend your
shardctrlerto allow for concurrent controllers without interfering with each other.
在 Part C 中,你将扩展 shardctrler,允许多个 Controller 并发运行而互不干扰。
Finally, in Part D, you will have the opportunity to extend your solution in any way you like.
最后,在 Part D 中,你可以按照自己的想法扩展实现。
This lab's sharded key/value service follows the same general design as Flat Datacenter Storage, BigTable, Spanner, FAWN, Apache HBase, Rosebud, Spinnaker, and many others.
本实验的分片键值服务采用了与 Flat Datacenter Storage、BigTable、Spanner、FAWN、Apache HBase、Rosebud、Spinnaker 等系统相同的一般设计。
These systems differ in many details from this lab, though, and are also typically more sophisticated and capable.
不过,这些系统在很多细节上都不同于本实验,而且通常更加复杂、能力也更强。
For example, the lab doesn't evolve the sets of peers in each Raft group; its data and query models are simple; and so on.
例如,本实验不会动态改变每个 Raft 组中的 peer 集合,数据模型和查询模型也比较简单,等等。
Lab 5 will use your
kvsrvfrom Lab 2, and yourrsmandRaftfrom Lab 4.
Lab 5 会使用你在 Lab 2 中实现的 kvsrv,以及你在 Lab 4 中实现的 rsm 和 Raft。
Your Lab 5 and Lab 4 must use the same
rsmandRaftimplementations.
Lab 5 和 Lab 4 必须使用同一份 rsm 和 Raft 实现。
You may use late hours for Part A, but you may not use late hours for Parts B-D.
Part A 可以使用 late hours,但 Parts B-D 不能使用 late hours。
Getting Started
Getting Started
开始实验
Do a
git pullto get the latest lab software.
执行 git pull 获取最新的实验软件。
cd ~/6.5840
git pull
We supply you with tests and skeleton code in
src/shardkv1:
我们在 src/shardkv1 中提供了测试和骨架代码:
client.gofor the shardkv clerk
用于实现 shardkv Clerk 的 client.go。
shardcfgpackage for computing shard configurations
用于计算 shard 配置的 shardcfg 包。
shardgrppackage: for the shardgrp clerk and server.
用于实现 shardgrp Clerk 和服务器的 shardgrp 包。
shardctrlerpackage, which containsshardctrler.gowith methods for the controller to change a configuration (ChangeConfigTo) and to get a configuration (Query)
shardctrler 包,其中的 shardctrler.go 包含 Controller 修改配置的 ChangeConfigTo 方法和获取配置的 Query 方法。
To get up and running, execute the following commands:
要开始运行实验,执行以下命令:
$ cd ~/6.5840
$ git pull
...
$ cd src
$ make RUN="-run 5A" shardkv
go build -race -o main/kvsrv1d main/kvsrv1d.go
go build -race -o main/shardgrp1d main/shardgrp1d.go
cd shardkv1; go test -timeout 15m -v -race -run 5A
=== RUN TestInitQuery5A
Test (5A): Init and Query ... (reliable network)...
...
Part A:Moving shards
Part A: Moving shards
Part A:移动 shard
Your first job is to implement shardgrps and the
InitConfig,Query, andChangeConfigTomethods when there are no failures.
你的第一个任务是在没有故障的情况下实现 shardgrp,以及 InitConfig、Query 和 ChangeConfigTo 方法。
We have given you the code for describing a configuration, in
shardkv1/shardcfg.
我们已经在 shardkv1/shardcfg 中提供了描述配置的代码。
Each
shardcfg.ShardConfighas a unique identifying number, a mapping from shard number to group number, and a mapping from group number to the list of servers replicating that group.
每个 shardcfg.ShardConfig 都有唯一的编号、从 shard 编号到组编号的映射,以及从组编号到复制该组的服务器列表的映射。
There will usually be more shards than groups (so that each group serves more than one shard), in order that load can be shifted at a fairly fine granularity.
通常 shard 的数量会多于 group 的数量(因此每个 group 会服务多个 shard),这样就可以以较细的粒度移动负载。
Implement these two methods in
shardctrler/shardctrler.go:
在 shardctrler/shardctrler.go 中实现下面两个方法:
The
InitConfigmethod receives the first configuration, passed to it by the tester as ashardcfg.ShardConfig.
InitConfig 方法接收第一个配置,tester 会以 shardcfg.ShardConfig 的形式传入。
InitConfigshould store the configuration in an instance of Lab 2'skvsrv.
InitConfig 应把配置存储到 Lab 2 的 kvsrv 实例中。
The
Querymethod returns the current configuration; it should read the configuration fromkvsrv, previously stored there byInitConfig.
Query 方法返回当前配置;它应该从 kvsrv 中读取之前由 InitConfig 存入的配置。
Implement
InitConfigandQuery, and store the configuration inkvsrv.
实现 InitConfig 和 Query,并把配置存储在 kvsrv 中。
You're done when your code passes the first test. Note this task doesn't require any shardgrps.
当代码通过第一个测试时,这个任务就完成了。注意,这个任务还不要求实现任何 shardgrp。
$ cd ~/6.5840/src
$ make RUN="-run TestInitQuery5A" shardkv
go build -race -o main/kvsrv1d main/kvsrv1d.go
go build -race -o main/shardgrp1d main/shardgrp1d.go
cd shardkv1; go test -timeout 15m -v -race -run TestInitQuery5A
=== RUN TestInitQuery5A
Test (5A): Init and Query ... (reliable network)...
... Passed -- time 0.0s #peers 1 #RPCs 3 #Ops 0
--- PASS: TestInitQuery5A (0.13s)
PASS
ok 6.5840/shardkv1 1.143s
Implement
InitConfigandQueryby storing and reading the initial configuration fromkvsrv: use theGet/Putmethods ofShardCtrler.IKVClerkto talk tokvsrv, use theStringmethod ofShardConfigto turn aShardConfiginto a string that you can pass toPut, and use theshardcfg.FromString()function to turn a string into aShardConfig.
实现 InitConfig 和 Query 时,把初始配置写入并读回 kvsrv:使用 ShardCtrler.IKVClerk 的 Get/Put 方法与 kvsrv 通信;使用 ShardConfig 的 String 方法把 ShardConfig 转换成可以传给 Put 的字符串;使用 shardcfg.FromString() 把字符串还原成 ShardConfig。
Implement an initial version of
shardgrpinshardkv1/shardgrp/server.goand a corresponding clerk inshardkv1/shardgrp/client.goby copying code from your Lab 4kvraftsolution.
参考 Lab 4 的 kvraft 实现,在 shardkv1/shardgrp/server.go 中实现初版 shardgrp,并在 shardkv1/shardgrp/client.go 中实现对应的 Clerk。
Implement a clerk in
shardkv1/client.gothat uses theQuerymethod to find the shardgrp for a key, and then talks to that shardgrp.
在 shardkv1/client.go 中实现一个 Clerk:使用 Query 方法找到 key 对应的 shardgrp,然后与该 shardgrp 通信。
You're done when your code passes the
Statictest.
当代码通过 Static 测试时,这个任务就完成了。
$ cd ~/6.5840/src
$ make RUN="-run TestStatiOneShardGroup5A" shardkv
go build -race -o main/kvsrv1d main/kvsrv1d.go
go build -race -o main/shardgrp1d main/shardgrp1d.go
cd shardkv1; go test -timeout 15m -v -race -run TestStaticOneShardGroup5A
=== RUN TestStaticOneShardGroup5A
Test (5A): one shard group ... (reliable network)...
... Passed -- time 9.4s #peers 1 #RPCs 822 #Ops 420
--- PASS: TestStaticOneShardGroup5A (9.52s)
PASS
ok 6.5840/shardkv1 10.542s
Copy code from your
kvraftclient.goandserver.goforPutandGet, and any other code you need fromkvraft.
从 kvraft 的 client.go 和 server.go 中复制 Put、Get 以及其他需要的代码。
The code in
shardkv1/client.goprovides thePut/Getclerk for the overall system: it finds out which shardgrp holds the desired key's shard by invoking theQuerymethod, and then talks to the shardgrp that holds that shard.
shardkv1/client.go 中的代码提供整个系统的 Put/Get Clerk:它调用 Query 找到保存目标 key 所属 shard 的 shardgrp,然后与保存该 shard 的 shardgrp 通信。
Implement
shardkv1/client.go, including itsPut/Getmethods.
实现 shardkv1/client.go,包括其中的 Put/Get 方法。
Use
shardcfg.Key2Shard()to find the shard number for a key.
使用 shardcfg.Key2Shard() 找到 key 对应的 shard 编号。
The tester passes a
ShardCtrlerobject toMakeClerkinshardkv1/client.go. Retrieve the current configuration using theQuerymethod.
tester 会把一个 ShardCtrler 对象传给 shardkv1/client.go 中的 MakeClerk。使用 Query 方法获取当前配置。
To put/get a key from a shardgrp, the shardkv clerk should create a shardgrp clerk for the shardgrp by calling
shardgrp.MakeClerk, passing in the servers found in the configuration and the shardkv clerk'sck.clnt. Use theGidServers()method fromShardConfigto get the group for a shard.
为了从 shardgrp 对 key 执行 put/get,shardkv Clerk 应调用 shardgrp.MakeClerk 为该 shardgrp 创建 Clerk,把配置中找到的服务器和 shardkv Clerk 的 ck.clnt 传进去。使用 ShardConfig 的 GidServers() 方法获取某个 shard 所属的组。
shardkv1/client.go's Put must returnErrMaybewhen the reply was maybe lost, but this Put invokesshardgrp's Put to talk a particular shardgrp. The inner Put can signal this with an error.
当回复可能丢失时,shardkv1/client.go 的 Put 必须返回 ErrMaybe;但这个 Put 会调用 shardgrp 的 Put 来访问特定 shardgrp,内部 Put 可以通过错误把这种情况传出来。
Upon creation, the first shardgrp (
shardcfg.Gid1) should initialize itself to own all shards.
创建时,第一个 shardgrp(shardcfg.Gid1)应初始化为拥有全部 shard。
Now you should support movement of shards among groups by implementing the
ChangeConfigTomethod, which changes from an old configuration to a new configuration.
现在应通过实现 ChangeConfigTo 支持在组之间移动 shard;该方法负责从旧配置变更到新配置。
The new configuration may include new shardgrps that are not present in the old configuration, and may exclude shardgrps that were present in the old configuration.
新配置可能包含旧配置中不存在的新 shardgrp,也可能排除旧配置中存在的 shardgrp。
The controller should move shards (the key/value data) so that the set of shards stored by each shardgrp matches the new configuration.
Controller 应移动 shard(也就是键值数据),使每个 shardgrp 保存的 shard 集合与新配置一致。
The approach we suggest for moving a shard is for
ChangeConfigToto first "freeze" the shard at the source shardgrp, causing that shardgrp to rejectPut's for keys in the moving shard.
我们建议的 shard 移动方法是:ChangeConfigTo 先在源 shardgrp 冻结 shard,使该 shardgrp 拒绝对正在移动的 shard 中的 key 执行 Put。
Then, copy (install) the shard to the destination shardgrp; then delete the frozen shard.
然后把 shard 复制(安装)到目标 shardgrp;接着删除已经冻结的 shard。
Finally, post a new configuration so that clients can find the moved shard.
最后发布新配置,让客户端能够找到移动后的 shard。
A nice property of this approach is that it avoids any direct interactions among the shardgrps.
这种方法的一个优点是 shardgrp 之间不需要直接交互。
It also supports serving shards that are not affected by an ongoing configuration change.
它还支持继续服务不受当前配置变更影响的 shard。
To be able to order changes to the configuration, each configuration has a unique number
Num(seeshardcfg/shardcfg.go).
为了给配置变更排序,每个配置都有唯一编号 Num(见 shardcfg/shardcfg.go)。
The tester in Part A invokes
ChangeConfigTosequentially, and the configuration passed toChangeConfigTowill have aNumone larger than the previous one; thus, a configuration with a higherNumis newer than one with a lowerNum.
Part A 的 tester 会顺序调用 ChangeConfigTo,传入的配置 Num 比上一个配置大一;因此,Num 更大的配置比 Num 更小的配置更新。
The network may delay RPCs, and RPCs may arrive out of order at the shardgrps.
网络可能延迟 RPC,并且 RPC 可能以乱序抵达 shardgrp。
To reject old
FreezeShard,InstallShard, andDeleteShardRPCs, they should includeNum(seeshardgrp/shardrpc/shardrpc.go), and shardgrps must remember the largestNumthey have seen for each shard.
为了拒绝旧的 FreezeShard、InstallShard 和 DeleteShard RPC,这些 RPC 应包含 Num(见 shardgrp/shardrpc/shardrpc.go),而 shardgrp 必须记住每个 shard 见过的最大 Num。
Implement
ChangeConfigTo(inshardctrler/shardctrler.go) and extendshardgrpto support freeze, install, and delete.
实现 ChangeConfigTo(位于 shardctrler/shardctrler.go),并扩展 shardgrp 以支持 freeze、install 和 delete。
ChangeConfigToshould always succeed in Part A because the tester doesn't induce failures in this part.
Part A 中 ChangeConfigTo 应始终成功,因为 tester 不会在这一部分注入故障。
You will need to implement
FreezeShard,InstallShard, andDeleteShardinshardgrp/client.goandshardgrp/server.gousing the RPCs in theshardgrp/shardrpcpackage, and reject old RPCs based onNum.
你需要使用 shardgrp/shardrpc 包中的 RPC,在 shardgrp/client.go 和 shardgrp/server.go 中实现 FreezeShard、InstallShard 和 DeleteShard,并依据 Num 拒绝旧 RPC。
You will also need modify the shardkv clerk in
shardkv1/client.goto handleErrWrongGroup, which a shardgrp should return if it isn't reponsible for the shard.
你还需要修改 shardkv1/client.go 中的 shardkv Clerk 来处理 ErrWrongGroup;如果某个 shardgrp 不负责该 shard,它就应该返回这个错误。
You have completed this task when you pass the
JoinBasicandDeleteBasictests. These tests focus on adding shardgrps; you don't have to worry about shardgrps leaving just yet.
当通过 JoinBasic 和 DeleteBasic 测试时,这个任务就完成了。这些测试关注加入 shardgrp;暂时还不需要处理 shardgrp 离开。
A shardgrp should respond with an
ErrWrongGrouperror to a clientPut/Getwith a key that the shardgrp isn't responsible for (i.e., for a key whose shard is not assigned to the shardgrp).
如果客户端 Put/Get 的 key 不由该 shardgrp 负责(也就是 key 所属 shard 没有分配给该组),shardgrp 应返回 ErrWrongGroup。
You will have to modify
shardkv1/client.goto reread the configuration and retry thePut/Get.
你需要修改 shardkv1/client.go,重新读取配置并重试 Put/Get。
Note that you will have to run
FreezeShard,InstallShard, andDeleteShardthrough yourrsmpackage, just likePutandGet.
注意,你必须像处理 Put 和 Get 一样,通过自己的 rsm 包运行 FreezeShard、InstallShard 和 DeleteShard。
You can send an entire map as your state in an RPC request or reply, which may help keep the code for shard transfer simple.
可以在 RPC 请求或回复中发送完整 map 作为状态,这可能有助于简化 shard 迁移代码。
If one of your RPC handlers includes in its reply a map (e.g. a key/value map) that's part of your server's state, you may get bugs due to races.
如果某个 RPC handler 在回复中直接包含属于服务器状态的 map(例如键值 map),可能会因为竞争产生 bug。
The RPC system has to read the map in order to send it to the caller, but it isn't holding a lock that covers the map.
RPC 系统必须读取这个 map 才能把它发给调用者,但它不会持有覆盖该 map 的锁。
Your server, however, may proceed to modify the same map while the RPC system is reading it.
然而,RPC 系统读取 map 时,你的服务器可能继续修改同一个 map。
The solution is for the RPC handler to include a copy of the map in the reply.
解决办法是让 RPC handler 在回复中放入 map 的副本。
Extend
ChangeConfigToto handle shard groups that leave; i.e., shardgrps that are present in the current configuration but not in the new one.
扩展 ChangeConfigTo,处理离开的 shard group,也就是当前配置中存在但新配置中不存在的 shardgrp。
Your solution should pass
TestJoinLeaveBasic5Anow.
此时实现应通过 TestJoinLeaveBasic5A。
(You may have handled this scenario already in the previous task, but the previous tests didn't test for shardgrps leaving.)
(你可能已经在上一个任务中处理了这个场景,但之前的测试没有检查 shardgrp 离开的情况。)
Make your solution pass all Part A tests, which check that your sharded key/value service supports many groups joining and leaving, shardgrps restarting from snapshots, processing
Gets while some shards are offline or involved in a configuration change, and linearizability when many clients interact with the service while the tester concurrently invokes the controller'sChangeConfigToto rebalance shards.
让实现通过全部 Part A 测试。这些测试会检查:多个 group 加入和离开、shardgrp 从快照重启、某些 shard 下线或正在配置变更时仍能处理 Get,以及 tester 并发调用 Controller 的 ChangeConfigTo 平衡 shard 时,多客户端交互仍保持线性一致性。
$ cd ~/6.5840/src
$ make RUN="-run 5A" shardkv
go build -race -o main/kvsrv1d main/kvsrv1d.go
go build -race -o main/shardgrp1d main/shardgrp1d.go
cd shardkv1; go test -timeout 15m -v -race -run 5A
Test (5A): Init and Query ... (reliable network)...
... Passed -- time 0.0s #peers 1 #RPCs 3 #Ops 0
Test (5A): one shard group ... (reliable network)...
... Passed -- time 5.1s #peers 1 #RPCs 792 #Ops 180
Test (5A): a group joins... (reliable network)...
... Passed -- time 12.9s #peers 1 #RPCs 6300 #Ops 180
Test (5A): delete ... (reliable network)...
... Passed -- time 8.4s #peers 1 #RPCs 1533 #Ops 360
Test (5A): basic groups join/leave ... (reliable network)...
... Passed -- time 13.7s #peers 1 #RPCs 5676 #Ops 240
Test (5A): many groups join/leave ... (reliable network)...
... Passed -- time 22.1s #peers 1 #RPCs 3529 #Ops 180
Test (5A): many groups join/leave ... (unreliable network)...
... Passed -- time 54.8s #peers 1 #RPCs 5055 #Ops 180
Test (5A): shutdown ... (reliable network)...
... Passed -- time 11.7s #peers 1 #RPCs 2807 #Ops 180
Test (5A): progress ... (reliable network)...
... Passed -- time 8.8s #peers 1 #RPCs 974 #Ops 82
Test (5A): progress ... (reliable network)...
... Passed -- time 13.9s #peers 1 #RPCs 2443 #Ops 390
Test (5A): one concurrent clerk reliable... (reliable network)...
... Passed -- time 20.0s #peers 1 #RPCs 5326 #Ops 1248
Test (5A): many concurrent clerks reliable... (reliable network)...
... Passed -- time 20.4s #peers 1 #RPCs 21688 #Ops 10500
Test (5A): one concurrent clerk unreliable ... (unreliable network)...
... Passed -- time 25.8s #peers 1 #RPCs 2654 #Ops 176
Test (5A): many concurrent clerks unreliable... (unreliable network)...
... Passed -- time 25.3s #peers 1 #RPCs 7553 #Ops 1896
PASS
ok 6.5840/shardkv1 243.115s
$
Your solution must continue serving shards that are not affected by an ongoing configuration change.
配置变更进行时,实现必须继续服务不受影响的 shard。
Part B:Handling a failed controller
Part B: Handling a failed controller
Part B:处理失败的 Controller
The controller is a short-lived command, which an administrator invokes: it moves shards and then exits.
Controller 是由管理员调用的短生命周期命令:它移动 shard,然后退出。
But, it may fail or lose network connectivity while moving shards.
但是,它可能在移动 shard 时失败或失去网络连接。
The main task in this part of the lab is recovering from a controller that fails to complete
ChangeConfigTo.
本部分的主要任务是从未能完成 ChangeConfigTo 的 Controller 中恢复。
The tester starts a new controller and invokes its
ChangeConfigToafter partitioning the first controller; you have to modify the controller so that the new one finishes the reconfiguration.
tester 会先把第一个 Controller 网络分区,然后启动新的 Controller 并调用它的 ChangeConfigTo;你需要修改 Controller,让新的 Controller 完成重新配置。
The tester calls
InitControllerwhen starting a controller; you can modify that function to check whether an interrupted configuration change needs to be completed.
tester 启动 Controller 时会调用 InitController;你可以修改这个函数,检查是否存在需要完成的中断配置变更。
A good approach to allowing a controller to finish a reconfiguration that a previous one started is to keep two configurations: a current one and a next one, both stored in the controller's kvsrv.
让 Controller 完成前一个 Controller 已经开始的重新配置,一个好的方法是保存两份配置:current 和 next,并且都存储在 Controller 使用的 kvsrv 中。
When a controller starts a reconfiguration, it stores the next configuration.
Controller 开始重新配置时,先保存 next configuration。
Once a controller completes the reconfiguration, it makes the next configuration the current one.
Controller 完成重新配置后,把 next configuration 变成 current configuration。
Modify
InitControllerto first check if there is a stored next configuration with a higher configuration number than the current one, and if so, complete the shard moves necessary to reconfigure to the next one.
修改 InitController:先检查是否存在编号高于 current 的已保存 next configuration;如果存在,就完成变更到 next 所需的 shard 移动。
Modify shardctrler to implement the above approach.
修改 shardctrler,实现上述方案。
A controller that picks up the work from a failed controller may repeat
FreezeShard,InstallShard, andDeleteRPCs; shardgrps can useNumto detect duplicates and reject them.
接手失败 Controller 工作的 Controller 可能会重复发送 FreezeShard、InstallShard 和 Delete RPC;shardgrp 可以利用 Num 检测重复请求并拒绝它们。
You have completed this task if your solution passes the Part B tests.
如果实现通过 Part B 测试,就完成了这个任务。
$ cd ~/6.5840/src
$ make RUN="-run 5B" shardkv
go build -race -o main/kvsrv1d main/k
go build -race -o main/shardgrp1d main/shardgrp1d.go
cd shardkv1; go test -timeout 15m -v -race -run 5B
Test (5B): Join/leave while a shardgrp is down... (reliable network)...
... Passed -- time 9.2s #peers 1 #RPCs 899 #Ops 120
Test (5B): recover controller ... (reliable network)...
... Passed -- time 26.4s #peers 1 #RPCs 3724 #Ops 360
PASS
ok 6.5840/shardkv1 35.805s
$
The tester calls
InitControllerwhen starting a controller; you can implement recovery in that method inshardctrler/shardctrler.go.
tester 启动 Controller 时会调用 InitController;你可以在 shardctrler/shardctrler.go 的这个方法中实现恢复。
Part C:Concurrent configuration changes
Part C: Concurrent configuration changes
Part C:并发配置变更
In this part of the lab you will modify the controller to allow for concurrent controllers.
本部分中,你将修改 Controller,使多个 Controller 可以并发运行。
When a controller crashes or is partitioned, the tester will start a new controller, which must finish any work that the old controller might have in progress (i.e., finishing moving shards like in Part B).
当 Controller 崩溃或被分区时,tester 会启动新的 Controller;新的 Controller 必须完成旧 Controller 可能正在进行的工作,也就是像 Part B 一样完成 shard 移动。
This means that several controllers may run concurrently and send RPCs to the shardgrps and the
kvsrvthat stores configurations.
这意味着多个 Controller 可能同时运行,并向 shardgrp 以及保存配置的 kvsrv 发送 RPC。
The main challenge is to ensure these controllers don't step on each other.
主要挑战是保证这些 Controller 不会互相覆盖或干扰。
In Part A you already fenced all the shardgrp RPCs with
Numso that old RPCs are rejected.
在 Part A 中,你已经用 Num 给所有 shardgrp RPC 加上 fencing,因此旧 RPC 会被拒绝。
Even if several controllers pick up the work of an old controller concurrently, one of them succeeds and the others repeat all the RPCs, the shardgrps will ignore them.
即使多个 Controller 并发接手旧 Controller 的工作,其中一个成功、其他 Controller 重复发送全部 RPC,shardgrp 也会忽略这些重复 RPC。
Thus the challenging case left is to ensure that only one controller updates the next configuration to avoid that two controllers (e.g., a partitioned one and a new one) put different configurations in the next one.
因此剩下的挑战是保证只有一个 Controller 能更新 next configuration,避免两个 Controller(例如一个被分区的旧控制器和一个新控制器)把不同配置写入 next。
To stress this scenario, the tester runs several controllers concurrently and each one computes the next configuration by reading the current configuration and updating it for a shardgrp that left or joined, and then the tester invokes
ChangeConfigTo; thus multiple controllers may invokeChangeConfigTowith different configuration with the sameNum.
为了压力测试这个场景,tester 会并发运行多个 Controller;每个 Controller 都读取 current configuration,并针对加入或离开的 shardgrp 计算 next configuration,然后 tester 调用 ChangeConfigTo。因此,多个 Controller 可能用相同的 Num、不同的配置调用 ChangeConfigTo。
You can use the version number of a key and versioned
Puts to ensure that only one controller updates the next configuration and that the other invocations return without doing anything.
你可以利用 key 的版本号和带版本的 Put,确保只有一个 Controller 更新 next configuration,其他调用直接返回且不做任何事情。
Modify your controller so that only one controller can post a next configuration for a configuration
Num.
修改控制器,确保对于给定配置编号 Num,只有一个控制器能发布 next 配置。
The tester will start many controllers but only one should start
ChangeConfigTofor a new configuation.
tester 会启动很多 Controller,但对于一个新配置,最终只能有一个 Controller 真正开始执行 ChangeConfigTo。
You have completed this task if you pass the concurrent tests of Part C.
如果通过 Part C 的并发测试,就完成了这个任务。
$ cd ~/6.5840/src
$ make RUN="-run TestConcurrentReliable5C" shardkv
go build -race -o main/kvsrv1d main/k
go build -race -o main/shardgrp1d main/shardgrp1d.go
cd shardkv1; go test -timeout 15m -v -race -run TestConcurrentReliable5C
Test (5C): Concurrent ctrlers ... (reliable network)...
... Passed -- time 8.2s #peers 1 #RPCs 1753 #Ops 120
PASS
ok 6.5840/shardkv1 8.364s
$ make RUN="-run TestAcquireLockConcurrentUnreliable5C" shardkv
go build -race -o main/kvsrv1d main/k
go build -race -o main/shardgrp1d main/shardgrp1d.go
cd shardkv1; go test -timeout 15m -v -race -run TestAcquireLockConcurrentUnreliable5C
Test (5C): Concurrent ctrlers ... (unreliable network)...
... Passed -- time 23.8s #peers 1 #RPCs 1850 #Ops 120
PASS
ok 6.5840/shardkv1 24.008s
$
See
concurCtrlerintest.goto see how the tester runs controllers concurrently.
查看 test.go 中的 concurCtrler,了解 tester 如何并发运行 Controller。
In this exercise you will put recovery of an old controller together with a new controller: a new controller should perform recovery from Part B.
在这个练习中,你要把旧 Controller 的恢复和新 Controller 结合起来:新 Controller 应执行 Part B 中的恢复逻辑。
If the old controller was partitioned during
ChangeConfigTo, you will have to make sure that the old controller doesn't interfere with the new controller.
如果旧 Controller 在 ChangeConfigTo 期间被网络分区,你必须保证旧 Controller 不会干扰新 Controller。
If all the controller's updates are already properly fenced with
Numchecks from Part B, you don't have to write extra code.
如果 Controller 的所有更新已经通过 Part B 的 Num 检查正确 fencing,就不需要额外编写代码。
You have completed this task if you pass the
Partitiontests.
如果通过 Partition 测试,就完成了这个任务。
$ cd ~/6.5840/src
$ make RUN="-run Partition" shardkv
go build -race -o main/kvsrv1d main/k
go build -race -o main/shardgrp1d main/shardgrp1d.go
cd shardkv1; go test -timeout 15m -v -race -run Partition
Test (5C): partition controller in join... (reliable network)...
... Passed -- time 7.8s #peers 1 #RPCs 876 #Ops 120
Test (5C): controllers with leased leadership ... (reliable network)...
... Passed -- time 36.8s #peers 1 #RPCs 3981 #Ops 360
Test (5C): controllers with leased leadership ... (unreliable network)...
... Passed -- time 52.4s #peers 1 #RPCs 2901 #Ops 240
Test (5C): controllers with leased leadership ... (reliable network)...
... Passed -- time 60.2s #peers 1 #RPCs 27415 #Ops 11182
Test (5C): controllers with leased leadership ... (unreliable network)...
... Passed -- time 60.5s #peers 1 #RPCs 11422 #Ops 2336
PASS
ok 6.5840/shardkv1 217.779s
$
You have completed implementing a highly-available sharded key/value service with many shard groups for scalability, reconfiguration to handle changes in load, and with a fault-tolerant controller; congrats!
恭喜你:你已经实现了一个高可用分片键值服务,它通过多个 shard group 获得可扩展性,通过重新配置应对负载变化,并且拥有容错 Controller!
Rerun all tests to check that your recent changes to the controller haven't broken earlier tests.
重新运行全部测试,确认最近对 Controller 的修改没有破坏之前的测试。
Gradescope will rerun the Lab 3A-D and Lab 4A-C tests on your submission, in addition to the 5C tests.
除了 5C 测试外,Gradescope 还会在提交结果上重新运行 Lab 3A-D 和 Lab 4A-C 测试。
Before submitting, double check that your solution works:
提交前,再次确认实现可以正常工作:
$ make raft1
$ make kvraft1
$ make shardkv
Part D:Extend your solution
Part D: Extend your solution
Part D:扩展你的实现
In this final part of the lab, you get to extend your solution in any way you like.
在实验的最后一部分,你可以按照自己的想法扩展实现。
You will have to write your own tests for whatever extensions you choose to implement.
无论选择什么扩展,都需要自己编写对应测试。
Implement one of the ideas below or come up with your own idea.
实现下面的一个想法,或者提出自己的想法。
Write a paragraph in a file
extension.mddescribing your extension, and uploadextension.mdto Gradescope.
在 extension.md 中写一段话描述扩展,并把 extension.md 上传到 Gradescope。
If you would like to do one of the harder, open-ended extensions, feel free to partner up with another student in the class.
如果想做更难、更开放的扩展,可以与班上的另一位同学合作。
Here are some ideas for possible extension (the first few are easy and the later ones are more open ended):
下面是一些可选扩展思路,前几项比较简单,后面的更开放:
Change the tester to use
kvraftinstead ofkvsrv(i.e., replace thekvsrv.StartKVServerinMakeTestMaxRaftintest.gowithkvraft.StartKVServer) so that the controller uses your kvraft to store its configuration.
把 tester 改成使用 kvraft 而不是 kvsrv:将 test.go 中 MakeTestMaxRaft 的 kvsrv.StartKVServer 替换为 kvraft.StartKVServer,让 Controller 使用你的 kvraft 保存配置。
Write a test that checks that the controller can query and update the configuration while one of the kvraft peers is down.
编写测试,检查一个 kvraft peer 下线时,Controller 仍能查询和更新配置。
The existing code for the tester is distributed across
src/kvtest1,src/shardkv1, andsrc/tester1.
现有测试器代码分布在 src/kvtest1、src/shardkv1 和 src/tester1 中。
Change
kvsrvto implement exactly-once semantics forPut/Getas in Lab 2 from last year (see the dropped messages part).
按照去年的 Lab 2,把 kvsrv 改造成对 Put/Get 提供恰好一次语义,参见该实验的消息丢失部分。
You may be able to port over some tests from 2024 instead of having to write your own from scratch.
你可以移植部分 2024 年的测试,不必全部从头编写。
Implement exactly-once also in your
kvraft.
也在 kvraft 中实现恰好一次语义。
Change
kvsrvto support aRangefunction, which returns all keys in the rangelowkey tohighkey.
让 kvsrv 支持 Range 函数,返回从 low key 到 high key 范围内的全部 key。
The lazy way to implement
Rangeis to iterate through the key/value map that the server maintains; a better way is to use a data structure that supports range searches (e.g., B-tree).
实现 Range 的简单方法是遍历服务器维护的 map;更好的方法是使用支持范围查询的数据结构,例如 B-tree。
Include a test that fails the lazy solution but passes on the better solution.
编写一个能够让简单遍历方案失败、但让更好方案通过的测试。
Modify your
kvraftimplementation to allow the leader to serveGets without running theGetthroughrsm.
修改 kvraft,允许 Leader 不经过 rsm 就直接服务 Get。
That is, implement the optimization described at the end of section 8 of the raft paper, including leases, to ensure that kvraft maintains linearizability.
实现 Raft 论文第 8 节末尾描述的优化,包括 lease,以确保 kvraft 仍保持线性一致性。
Your implementation should pass the existing
kvrafttests.
实现应通过现有的 kvraft 测试。
You should also add a test that checks that your optimized implementation is faster (e.g., by comparing the number of RPCs) and a test that checks that term switches are slower because a new leader must wait until the lease has expired.
还应增加测试:一个通过比较 RPC 数量等方式检查优化后的实现更快,另一个检查 term 切换更慢,因为新 Leader 必须等待 lease 过期。
Support transactions in
kvraftso that a developer can perform severalPutandGets atomically.
让 kvraft 支持事务,使开发者能够原子地执行多个 Put 和 Get。
Once you have transaction, you don't need version Puts anymore; transactions subsume versioned Puts.
有了事务后,就不再需要带版本的 Put,因为事务已经包含了版本 Put 的能力。
Look at etcd's transactions for an example interface. Write tests to demonstrate your extension works.
可以参考 etcd 的事务接口。编写测试证明扩展确实有效。
Modify
shardkvto support transactions so that a developer can perform severalPuts andGets atomically across shards.
修改 shardkv 支持事务,使开发者能够跨 shard 原子地执行多个 Put 和 Get。
This requires implementing two-phase-commit and two-phase locking. Write tests to demonstrate that your extension works.
这需要实现两阶段提交和两阶段锁定。编写测试证明扩展有效。
Handin procedure
Handin procedure
提交流程
Before submitting, please run all the tests one final time.
提交前,请最后运行一次全部测试。
Before submitting, double check that your solution works with:
提交前,再次确认实现可以通过:
$ make raft1
$ make kvraft1
$ make shardkv
实现映射
1. 配置控制器:版本化 current/next
本实现把配置写入 Lab 2 kvsrv,而不是把 Controller 自己变成一个 Raft 集群。current 表示客户端当前可见的稳定配置,next 表示正在推进的目标配置。更新 next 时使用 kvsrv 的版本号作为 CAS:只有读到旧版本的 Controller 才能成功写入,其他并发 Controller 会因 ErrVersion 退出或重新读取。
stateDiagram-v2
[*] --> Current
Current --> NextWritten: CAS 写入 next
NextWritten --> Frozen: FreezeShard(source, Num)
Frozen --> Installed: InstallShard(destination, Num)
Installed --> Deleted: DeleteShard(source, Num)
Deleted --> CurrentPublished: CAS 发布 current
CurrentPublished --> Current: 清理 next
NextWritten --> Current: 控制器崩溃后由新控制器恢复
2. Shard group:所有变化都经过 RSM
普通 Get/Put 和 FreezeShard/InstallShard/DeleteShard 都通过 Lab 4 的 rsm.Submit 进入 Raft 日志。这样每个副本会以相同顺序修改 KV map、ownership、冻结标志和迁移编号;RPC handler 本身不直接修改共享状态。
3. 客户端路由
客户端先用 Key2Shard(key) 计算 shard,再通过 Controller 查询配置,根据 GidServers() 找到目标 group。目标 group 返回 ErrWrongGroup 时,客户端重新查询配置并重试;网络失败时保留 Lab 4 的 ErrMaybe 语义,避免把可能已经成功的 Put 当成确定失败。
sequenceDiagram
participant C as shardkv Clerk
participant Ctrl as ShardCtrler/kvsrv
participant G1 as 当前 ShardGrp
participant G2 as 新 ShardGrp
C->>Ctrl: Query()
Ctrl-->>C: config.Num + shard→gid
C->>G1: Get/Put(key)
G1-->>C: OK 或 ErrWrongGroup
C->>Ctrl: ErrWrongGroup 后重新 Query()
C->>G2: 重试请求
关键状态机
迁移不变量
迁移过程中必须满足三个不变量:
- 源 shard 在发送数据前先冻结,冻结后不再接受该 shard 的写入。
- 目标 shard 安装完整副本后,源 shard 才允许删除。
- 新配置发布前,客户端仍按照旧配置访问;新配置发布后,目标 group 已拥有完整数据。
flowchart LR
Old[旧配置:源组服务 shard] --> Freeze[源组 FreezeShard]
Freeze --> Copy[复制完整 shard 状态]
Copy --> Install[目标组 InstallShard]
Install --> Delete[源组 DeleteShard]
Delete --> Publish[发布新配置]
Publish --> New[新配置:目标组服务 shard]
幂等与 fencing
每个 shard 记录已经见过的最大配置编号 Num。重复的 Freeze、Install、Delete RPC 不会重复破坏状态;更旧编号的 RPC 会被拒绝。Controller 侧则用 kvsrv 的版本化 Put 防止两个并发 Controller 把不同的配置写进同一个 next。
快照恢复
Shard group 的快照不仅要包含 KV 数据,还必须包含 owned、frozen、每个 shard 的最大迁移编号,以及 RSM 需要恢复的状态。重启或 InstallSnapshot 后,如果遗漏这些元数据,服务可能重新对已冻结 shard 接受写入,或者重复安装旧数据。
测试验收
实现代码:implementation/6.5840。建议从下面的命令开始:
cd "/Users/maxingfang/w/w/21 数据库实现/04 分布式共识系统/implementation/6.5840/src"
make RUN='-run 5A' shardkv
make RUN='-run 5B' shardkv
make RUN='-run TestConcurrentReliable5C' shardkv
make RUN='-run TestAcquireLockConcurrentUnreliable5C' shardkv
当前已验证:
make kvsrv1:可靠 Put、并发 Put、内存压力和不可靠网络测试通过。- 本轮 Raft 每批最多发送 256 条日志;客户端触发的复制受并发槽位限制,但周期心跳不再被延迟回复堵住。最新版完整回归结果统一见 分布式共识系统专栏索引。
- Lab 5 Part A 的基础、迁移、不可靠网络、关闭/恢复、进度和多 Clerk 压力测试已通过;分片 Clerk 对旧组 Get/Put 使用整次操作的 2 秒有界等待,旧组关闭后可重新查询配置。
go test -race -count=3 -timeout 300s -run '^TestManyConcurrentClerkUnreliable5A$' ./shardkv1:3 次均通过;TestOneConcurrentClerkUnreliable5A也连续 3 次通过。go test -timeout 90s -race -run '^(TestJoinLeave5B|TestRecoverCtrler5B)$' ./shardkv1:两项均通过。- 5C 的 7 个场景均已逐项独立通过:
TestConcurrentReliable5C、TestAcquireLockConcurrentUnreliable5C、TestPartitionControllerJoin5C、TestPartitionRecoveryReliableNoClerk5C、TestPartitionRecoveryUnreliableNoClerk5C、TestPartitionRecoveryReliableClerks5C和TestPartitionRecoveryUnreliableClerks5C。 - Raft 侧当前已验证
TestReElection3A、TestPersist23C、TestFigure8Unreliable3C和TestSnapshotInstallUnreliable3D的-race回归;快照安装不可靠测试单独运行约 103 秒。 - 历史版本曾通过完整
shardkv1测试;不能用旧结果代替本轮修改后的验收,最新结果见专栏索引。 - 并发控制器修正:
ChangeConfigTo不再重复调用InitController;恢复只在控制器启动/重连时执行,避免多个并发 Controller 重复迁移同一个next配置。
当前客户端策略与限制:单个 shard-group 操作等待上限为 2 秒;顶层持续不可用判断窗口为 5 秒,配置查询等待上限为 3 秒,实际退出时间还会受正在执行的请求影响。配置查询超时只停止等待,不能取消底层 Query();因此不能宣称已经消除所有后台 goroutine 遗留。本轮在 Lab 2 Clerk 的失败重试中加入 10 毫秒暂停,已用回归测试确认不会立即空转。超时和 ErrMaybe 属于实现语义的审查重点,不是 Raft 协议本身的要求。
下一步
建议按这个顺序继续:
- 阅读已有配套篇 etcd raft 源码导读 与 TLA+ 与正确性验证;这两篇是原创讲解,并非官方资料全文译本。
- 选择 Part D 扩展:优先实现 Range + B+ Tree,或者实现单 shard / 跨 shard 事务。
- 与 分布式共识系统专栏索引、Lab 4 容错 KV 与复制状态机、Lab 3D 快照与日志压缩 串联阅读。
参考资料
评论 (0)