Skip to content

Commit 9aa010e

Browse files
committed
添加注释(config.go用于构建实验的环境)
1 parent e2d96b5 commit 9aa010e

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

6.824/src/raft/config.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package raft
1010
// 我们使用最初的config.go文件分级测试你们的代码,所以你们可与修改这边的代码帮助你们调试,
1111
// 请在提交之前测试。
1212

13-
import "labrpc"
13+
import "../labrpc"
1414
import "log"
1515
import "sync"
1616
import "testing"
@@ -29,20 +29,23 @@ func randstring(n int) string {
2929
return s[0:n]
3030
}
3131

32+
// 构建测试环境对象
3233
type config struct {
3334
mu sync.Mutex
3435
t *testing.T
35-
net *labrpc.Network
36-
n int
37-
done int32 // tell internal threads to die
38-
rafts []*Raft
39-
applyErr []string // from apply channel readers
40-
connected []bool // whether each server is on the net
41-
saved []*Persister
42-
endnames [][]string // the port file names each sends to
43-
logs []map[int]int // copy of each server's committed entries
36+
net *labrpc.Network // 模拟网络
37+
n int // 服务器的数量
38+
done int32 // tell internal threads to die
39+
// 下面slice的长度都为n
40+
rafts []*Raft // 管理主机实例,数量为n
41+
applyErr []string // from apply channel readers
42+
connected []bool // whether each server is on the net
43+
saved []*Persister // 数据持久化:Raft状态值和快照数据
44+
endnames [][]string // the port file names each sends to
45+
logs []map[int]int // copy of each server's committed entries,每台服务器的日志副本
4446
}
4547

48+
// 构建测试环境
4649
func make_config(t *testing.T, n int, unreliable bool) *config {
4750
runtime.GOMAXPROCS(4)
4851
cfg := &config{}
@@ -56,17 +59,19 @@ func make_config(t *testing.T, n int, unreliable bool) *config {
5659
cfg.endnames = make([][]string, cfg.n)
5760
cfg.logs = make([]map[int]int, cfg.n)
5861

59-
cfg.setunreliable(unreliable)
62+
cfg.setunreliable(unreliable) // 设置网络为不可靠网络
6063

61-
cfg.net.LongDelays(true)
64+
cfg.net.LongDelays(true) // 存在长延时的网络
6265

6366
// create a full set of Rafts.
67+
// 创建raft集合(为什么是 cfg.n × cfg.n 个端点)
6468
for i := 0; i < cfg.n; i++ {
6569
cfg.logs[i] = map[int]int{}
6670
cfg.start1(i)
6771
}
6872

6973
// connect everyone
74+
// 连接每一个raft实例
7075
for i := 0; i < cfg.n; i++ {
7176
cfg.connect(i)
7277
}
@@ -112,20 +117,25 @@ func (cfg *config) crash1(i int) {
112117
// state persister, to isolate previous instance of
113118
// this server. since we cannot really kill it.
114119
//
120+
// 启动或者重新启动一个Raft实例,如果实例已经存在那么我们下“杀死”(译注:应该只是隔离,重新启动一个新的raft实例)。
121+
// 重新分配一个新的输出端口的文件名(???)和一个新的状态持久化对象,为了隔离之前本机的服务器实例。
122+
// 因为我们不能杀死它。
115123
func (cfg *config) start1(i int) {
116124
cfg.crash1(i)
117125

118126
// a fresh set of outgoing ClientEnd names.
119127
// so that old crashed instance's ClientEnds can't send.
128+
// 一套新鲜的对外ClientEnd名字,所以旧的崩溃实例ClientEnds不能发送。
120129
cfg.endnames[i] = make([]string, cfg.n)
121130
for j := 0; j < cfg.n; j++ {
122-
cfg.endnames[i][j] = randstring(20)
131+
cfg.endnames[i][j] = randstring(20) // 随机产生的名字,这边产生 cfg.n个名字???
123132
}
124133

125134
// a fresh set of ClientEnds.
135+
// 新鲜的ClientEnds集合
126136
ends := make([]*labrpc.ClientEnd, cfg.n)
127137
for j := 0; j < cfg.n; j++ {
128-
ends[j] = cfg.net.MakeEnd(cfg.endnames[i][j])
138+
ends[j] = cfg.net.MakeEnd(cfg.endnames[i][j]) // 创建名为cfg.endnames[i][j]的ClientEnd
129139
cfg.net.Connect(cfg.endnames[i][j], j)
130140
}
131141

@@ -135,6 +145,7 @@ func (cfg *config) start1(i int) {
135145
// new instance's persisted state.
136146
// but copy old persister's content so that we always
137147
// pass Make() the last persisted state.
148+
// 一个新的持久化对象,
138149
if cfg.saved[i] != nil {
139150
cfg.saved[i] = cfg.saved[i].Copy()
140151
} else {
@@ -144,6 +155,7 @@ func (cfg *config) start1(i int) {
144155
cfg.mu.Unlock()
145156

146157
// listen to messages from Raft indicating newly committed messages.
158+
// 创建channel处理消息
147159
applyCh := make(chan ApplyMsg)
148160
go func() {
149161
for m := range applyCh {
@@ -152,6 +164,7 @@ func (cfg *config) start1(i int) {
152164
// ignore the snapshot
153165
} else if v, ok := (m.Command).(int); ok {
154166
cfg.mu.Lock()
167+
// 日志处理
155168
for j := 0; j < len(cfg.logs); j++ {
156169
if old, oldok := cfg.logs[j][m.Index]; oldok && old != v {
157170
// some server has already committed a different value for this entry!
@@ -179,12 +192,15 @@ func (cfg *config) start1(i int) {
179192
}
180193
}()
181194

195+
// 创建Raft实例
182196
rf := Make(ends, i, cfg.saved[i], applyCh)
183197

198+
// 记录自己的位置
184199
cfg.mu.Lock()
185200
cfg.rafts[i] = rf
186201
cfg.mu.Unlock()
187202

203+
// 创建提供Raft相关方法的实例,添加到本网络
188204
svc := labrpc.MakeService(rf)
189205
srv := labrpc.MakeServer()
190206
srv.AddService(svc)
@@ -207,14 +223,20 @@ func (cfg *config) connect(i int) {
207223
cfg.connected[i] = true
208224

209225
// outgoing ClientEnds
226+
// 1 0 0
227+
// 1 1 0
228+
// 1 1 1
210229
for j := 0; j < cfg.n; j++ {
211230
if cfg.connected[j] {
212231
endname := cfg.endnames[i][j]
213-
cfg.net.Enable(endname, true)
232+
cfg.net.Enable(endname, true) // 之前创建的端点存在于此网络
214233
}
215234
}
216235

217236
// incoming ClientEnds
237+
// 1 1 1
238+
// 1 1 1
239+
// 1 1 1
218240
for j := 0; j < cfg.n; j++ {
219241
if cfg.connected[j] {
220242
endname := cfg.endnames[j][i]

0 commit comments

Comments
 (0)