@@ -19,14 +19,15 @@ import (
19
19
"github.com/ReshiAdavan/Sentinel/raft"
20
20
)
21
21
22
+ // randstring generates a random string of a specified length.
22
23
func randstring (n int ) string {
23
24
b := make ([]byte , 2 * n )
24
25
crand .Read (b )
25
26
s := base64 .URLEncoding .EncodeToString (b )
26
27
return s [0 :n ]
27
28
}
28
29
29
- // Randomize server handles
30
+ // random_handles shuffles the server handles for load balancing or test randomness.
30
31
func random_handles (kvh []* rpc.ClientEnd ) []* rpc.ClientEnd {
31
32
sa := make ([]* rpc.ClientEnd , len (kvh ))
32
33
copy (sa , kvh )
@@ -37,6 +38,7 @@ func random_handles(kvh []*rpc.ClientEnd) []*rpc.ClientEnd {
37
38
return sa
38
39
}
39
40
41
+ // config holds the configuration for a set of raft servers and clients for testing.
40
42
type config struct {
41
43
mu sync.Mutex
42
44
t * testing.T
@@ -55,6 +57,7 @@ type config struct {
55
57
ops int32 // number of clerk get/put/append method calls
56
58
}
57
59
60
+ // cleanup terminates all the servers in the configuration.
58
61
func (cfg * config ) cleanup () {
59
62
cfg .mu .Lock ()
60
63
defer cfg .mu .Unlock ()
@@ -65,7 +68,7 @@ func (cfg *config) cleanup() {
65
68
}
66
69
}
67
70
68
- // Maximum log size across all servers
71
+ // LogSize calculates the maximum log size across all servers.
69
72
func (cfg * config ) LogSize () int {
70
73
logsize := 0
71
74
for i := 0 ; i < cfg .n ; i ++ {
@@ -77,7 +80,7 @@ func (cfg *config) LogSize() int {
77
80
return logsize
78
81
}
79
82
80
- // Maximum snapshot size across all servers
83
+ // SnapshotSize calculates the maximum snapshot size across all servers.
81
84
func (cfg * config ) SnapshotSize () int {
82
85
snapshotsize := 0
83
86
for i := 0 ; i < cfg .n ; i ++ {
@@ -89,10 +92,7 @@ func (cfg *config) SnapshotSize() int {
89
92
return snapshotsize
90
93
}
91
94
92
- /*
93
- * Attach server i to servers listed in to caller must hold cfg.mu
94
- */
95
-
95
+ // connectUnlocked connects server i to the servers listed in `to`. Caller must hold cfg.mu.
96
96
func (cfg * config ) connectUnlocked (i int , to []int ) {
97
97
// log.Printf("connect peer %d to %v\n", i, to)
98
98
@@ -115,10 +115,7 @@ func (cfg *config) connect(i int, to []int) {
115
115
cfg .connectUnlocked (i , to )
116
116
}
117
117
118
- /*
119
- * Detach server i from the servers listed in from caller must hold cfg.mu
120
- */
121
-
118
+ // disconnectUnlocked disconnects server i from the servers listed in `from`. Caller must hold cfg.mu.
122
119
func (cfg * config ) disconnectUnlocked (i int , from []int ) {
123
120
// log.Printf("disconnect peer %d from %v\n", i, from)
124
121
@@ -145,6 +142,7 @@ func (cfg *config) disconnect(i int, from []int) {
145
142
cfg .disconnectUnlocked (i , from )
146
143
}
147
144
145
+ // All returns a slice of all server indices.
148
146
func (cfg * config ) All () []int {
149
147
all := make ([]int , cfg .n )
150
148
for i := 0 ; i < cfg .n ; i ++ {
@@ -153,6 +151,7 @@ func (cfg *config) All() []int {
153
151
return all
154
152
}
155
153
154
+ // ConnectAll connects all servers in the configuration.
156
155
func (cfg * config ) ConnectAll () {
157
156
cfg .mu .Lock ()
158
157
defer cfg .mu .Unlock ()
@@ -161,7 +160,7 @@ func (cfg *config) ConnectAll() {
161
160
}
162
161
}
163
162
164
- // Sets up 2 partitions with connectivity between servers in each partition.
163
+ // partition sets up 2 partitions with connectivity between servers in each partition.
165
164
func (cfg * config ) partition (p1 []int , p2 []int ) {
166
165
cfg .mu .Lock ()
167
166
defer cfg .mu .Unlock ()
@@ -176,10 +175,7 @@ func (cfg *config) partition(p1 []int, p2 []int) {
176
175
}
177
176
}
178
177
179
- /*
180
- * Create a clerk with clerk specific server names.
181
- * Give it connections to all of the servers, but for now enable only connections to servers in to[].
182
- */
178
+ // makeClient creates a clerk with specific server names and connections.
183
179
func (cfg * config ) makeClient (to []int ) * Clerk {
184
180
cfg .mu .Lock ()
185
181
defer cfg .mu .Unlock ()
@@ -211,7 +207,7 @@ func (cfg *config) deleteClient(ck *Clerk) {
211
207
delete (cfg .clerks , ck )
212
208
}
213
209
214
- // caller should hold cfg.mu
210
+ // ConnectClientUnlocked connects a client to specified servers. Caller should hold cfg.mu.
215
211
func (cfg * config ) ConnectClientUnlocked (ck * Clerk , to []int ) {
216
212
// log.Printf("ConnectClient %v to %v\n", ck, to)
217
213
endnames := cfg .clerks [ck ]
@@ -221,13 +217,14 @@ func (cfg *config) ConnectClientUnlocked(ck *Clerk, to []int) {
221
217
}
222
218
}
223
219
220
+ // ConnectClient wraps ConnectClientUnlocked with mutex locking.
224
221
func (cfg * config ) ConnectClient (ck * Clerk , to []int ) {
225
222
cfg .mu .Lock ()
226
223
defer cfg .mu .Unlock ()
227
224
cfg .ConnectClientUnlocked (ck , to )
228
225
}
229
226
230
- // caller should hold cfg.mu
227
+ // DisconnectClientUnlocked disconnects a client from specified servers. Caller should hold cfg.mu.
231
228
func (cfg * config ) DisconnectClientUnlocked (ck * Clerk , from []int ) {
232
229
// log.Printf("DisconnectClient %v from %v\n", ck, from)
233
230
endnames := cfg .clerks [ck ]
@@ -237,13 +234,14 @@ func (cfg *config) DisconnectClientUnlocked(ck *Clerk, from []int) {
237
234
}
238
235
}
239
236
237
+ // DisconnectClient wraps DisconnectClientUnlocked with mutex locking.
240
238
func (cfg * config ) DisconnectClient (ck * Clerk , from []int ) {
241
239
cfg .mu .Lock ()
242
240
defer cfg .mu .Unlock ()
243
241
cfg .DisconnectClientUnlocked (ck , from )
244
242
}
245
243
246
- // Shutdown a server by isolating it
244
+ // ShutdownServer isolates and shuts down a specified server.
247
245
func (cfg * config ) ShutdownServer (i int ) {
248
246
cfg .mu .Lock ()
249
247
defer cfg .mu .Unlock ()
@@ -327,7 +325,7 @@ func (cfg *config) Leader() (bool, int) {
327
325
return false , 0
328
326
}
329
327
330
- // Partition servers into 2 groups and put current leader in minority
328
+ // make_partition partitions the servers, ensuring the current leader is in the minority.
331
329
func (cfg * config ) make_partition () ([]int , []int ) {
332
330
_ , l := cfg .Leader ()
333
331
p1 := make ([]int , cfg .n / 2 + 1 )
@@ -379,15 +377,12 @@ func make_config(t *testing.T, n int, unreliable bool, maxraftstate int) *config
379
377
return cfg
380
378
}
381
379
380
+ // rpcTotal returns the total number of RPC calls made.
382
381
func (cfg * config ) rpcTotal () int {
383
382
return cfg .net .GetTotalCount ()
384
383
}
385
384
386
- /*
387
- * Start a Test.
388
- * Print the Test message.
389
- */
390
-
385
+ // begin starts a test and prints the test message.
391
386
func (cfg * config ) begin (description string ) {
392
387
fmt .Printf ("%s ...\n " , description )
393
388
cfg .t0 = time .Now ()
@@ -404,15 +399,12 @@ func (cfg *config) begin(description string) {
404
399
}()
405
400
}
406
401
402
+ // op records an operation performed by a clerk.
407
403
func (cfg * config ) op () {
408
404
atomic .AddInt32 (& cfg .ops , 1 )
409
405
}
410
406
411
- /*
412
- * End a Test -- the fact that we got here means there was no failure.
413
- * Print the Passed message, and some performance numbers.
414
- */
415
-
407
+ // end concludes a test, prints performance numbers, and checks for timeouts.
416
408
func (cfg * config ) end () {
417
409
atomic .AddInt32 (& cfg .testNum , 1 ) // suppress two-minute timeout
418
410
0 commit comments