-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_manager_test.go
461 lines (422 loc) · 9.89 KB
/
dir_manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package bakemono
import (
"crypto/rand"
"fmt"
"reflect"
"testing"
)
func TestNewDirManager(t *testing.T) {
dm := &DirManager{}
dm.Init(123457)
if dm.ChunksNum != 123456 {
t.Error("ChunksMaxNum should be 123456")
}
if dm.BucketsNum != 30864 {
t.Error("BucketsNum should be 30864")
}
if dm.BucketsNumPerSegment != 15432 {
t.Error("BucketsNumPerSegment should be 15432")
}
return
}
func countDirFreeInChain(dm *DirManager) (int, error) {
// dir check
var index uint16
var counter1 int
var counter2 int
for seg := 0; seg < int(dm.SegmentsNum); seg++ {
index = dm.DirFreeStart[segId(seg)]
if index == 0 {
return 0, fmt.Errorf("dir free start should not be 0")
}
for index != 0 {
counter1++
index = dm.Dirs[segId(seg)][index].next()
}
}
//reverse counter
for seg := 0; seg < int(dm.SegmentsNum); seg++ {
index = dm.DirFreeStart[segId(seg)]
for dm.Dirs[segId(seg)][index].next() != 0 {
index = dm.Dirs[segId(seg)][index].next()
}
for index != 0 {
counter2++
index = dm.Dirs[segId(seg)][index].prev()
}
}
if counter1 != counter2 {
return 0, fmt.Errorf("dir free chain should be same")
}
return counter1, nil
}
func TestDirManager_InitEmptyDirs(t *testing.T) {
dm := &DirManager{}
dm.Init(123457)
dm.InitEmptyDirs()
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("dir free chain length: %d", counter)
if Offset(counter) != (dm.SegmentsNum * dm.BucketsNumPerSegment * (DirDepth - 1)) {
t.Error("dir free chain not match")
}
return
}
func TestDirManager_Marshal_Unmarshal(t *testing.T) {
dm := &DirManager{}
dm.Init(123457)
dm.InitEmptyDirs()
// marshal
data, err := dm.MarshalBinary()
if err != nil {
t.Fatal(err)
}
// unmarshal
dm2 := &DirManager{}
dm2.Init(123457)
err = dm2.UnmarshalBinary(data)
if err != nil {
t.Fatal(err)
}
// check if dm2 is equal to dm
if !reflect.DeepEqual(dm, dm2) {
t.Error("dm2 not equal to dm")
}
return
}
func TestDirManager_FreeChainDelete(t *testing.T) {
// NOTE: only free dir has `prev`. Panic if `prev` is not 0.
dm := &DirManager{}
dm.Init(123457)
dm.InitEmptyDirs()
// 1. delete head
{
seg := segId(0)
index := dm.DirFreeStart[seg]
t.Logf("to delete index: %d", index)
dm.freeChainDelete(seg, Offset(index))
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("dir free chain length: %d", counter)
if Offset(counter) != (dm.SegmentsNum*dm.BucketsNumPerSegment*(DirDepth-1) - 1) {
t.Error("dir free chain not match")
}
}
// 2. delete middle
{
seg := segId(0)
index := dm.DirFreeStart[seg]
index = dm.Dirs[seg][index].next()
t.Logf("to delete index: %d", index)
dm.freeChainDelete(seg, Offset(index))
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("dir free chain length: %d", counter)
if Offset(counter) != (dm.SegmentsNum*dm.BucketsNumPerSegment*(DirDepth-1) - 2) {
t.Error("dir free chain not match")
}
}
// 3. delete tail
{
seg := segId(0)
index := dm.DirFreeStart[seg]
for dm.Dirs[seg][index].next() != 0 {
index = dm.Dirs[seg][index].next()
}
t.Logf("to delete index: %d", index)
dm.freeChainDelete(seg, Offset(index))
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("dir free chain length: %d", counter)
if Offset(counter) != (dm.SegmentsNum*dm.BucketsNumPerSegment*(DirDepth-1) - 3) {
t.Error("dir free chain not match")
}
}
}
func TestDirManager_FreeChainRebuild(t *testing.T) {
// NOTE: only free dir has `prev`. Panic if `prev` is not 0.
dm := &DirManager{}
dm.Init(123457)
dm.InitEmptyDirs()
// 1. head
{
seg := segId(0)
index := dm.DirFreeStart[seg]
t.Logf("to pad index: %d", index)
dm.Dirs[seg][index].setOffset(1)
dm.freeChainRebuild(seg)
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("dir free chain length: %d", counter)
if Offset(counter) != (dm.SegmentsNum*dm.BucketsNumPerSegment*(DirDepth-1) - 1) {
t.Error("dir free chain not match")
}
}
// 2. middle
{
seg := segId(0)
index := dm.DirFreeStart[seg]
index = dm.Dirs[seg][index].next()
t.Logf("to pad index: %d", index)
dm.Dirs[seg][index].setOffset(1)
dm.freeChainRebuild(seg)
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("dir free chain length: %d", counter)
if Offset(counter) != (dm.SegmentsNum*dm.BucketsNumPerSegment*(DirDepth-1) - 2) {
t.Error("dir free chain not match")
}
}
// 3. tail
{
seg := segId(0)
index := dm.DirFreeStart[seg]
for dm.Dirs[seg][index].next() != 0 {
index = dm.Dirs[seg][index].next()
}
t.Logf("to pad index: %d", index)
dm.Dirs[seg][index].setOffset(1)
dm.freeChainRebuild(seg)
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("dir free chain length: %d", counter)
if Offset(counter) != (dm.SegmentsNum*dm.BucketsNumPerSegment*(DirDepth-1) - 3) {
t.Error("dir free chain not match")
}
}
}
func TestDirManager_Probe(t *testing.T) {
// 1 miss head bucket dir
{
dirs := make([]*Dir, 12)
for i := 0; i < len(dirs); i++ {
dirs[i] = &Dir{}
}
_ = linkEmptyDirs(dirs)
hit, pos, _ := dirProbe(1, 0, dirs)
if hit {
t.Error("should not hit")
}
if pos != 0 {
t.Error("pos should be 0")
}
hit, pos, _ = dirProbe(1, 1, dirs)
if hit {
t.Error("should not hit")
}
if pos != 4 {
t.Error("pos should be 4")
}
}
// 2 hit head bucket dir
{
dirs := make([]*Dir, 12)
for i := 0; i < len(dirs); i++ {
dirs[i] = &Dir{}
}
_ = linkEmptyDirs(dirs)
dirs[0].setOffset(1)
dirs[0].setTag(1)
hit, pos, _ := dirProbe(1, 0, dirs)
if !hit {
t.Error("should hit")
}
if pos != 0 {
t.Error("pos should be 0")
}
dirs[4].setOffset(1)
dirs[4].setTag(1)
hit, pos, _ = dirProbe(1, 1, dirs)
if !hit {
t.Error("should hit")
}
if pos != 4 {
t.Error("pos should be 4")
}
}
// 3 miss non-head bucket dir
{
dirs := make([]*Dir, 12)
for i := 0; i < len(dirs); i++ {
dirs[i] = &Dir{}
}
_ = linkEmptyDirs(dirs)
dirs[0].setOffset(1)
dirs[0].setTag(1)
dirs[0].setNext(1)
dirs[1].setOffset(1)
dirs[1].setTag(2)
dirs[1].setNext(0)
hit, pos, _ := dirProbe(3, 0, dirs)
if hit {
t.Error("should not hit")
}
if pos != 0 {
t.Error("pos should be 0")
}
}
// 4 hit non-head bucket dir
{
dirs := make([]*Dir, 12)
for i := 0; i < len(dirs); i++ {
dirs[i] = &Dir{}
}
_ = linkEmptyDirs(dirs)
dirs[0].setOffset(1)
dirs[0].setTag(1)
dirs[0].setNext(1)
dirs[1].setOffset(1)
dirs[1].setTag(2)
dirs[1].setNext(0)
hit, pos, _ := dirProbe(2, 0, dirs)
if !hit {
t.Error("should not hit")
}
if pos != 1 {
t.Error("pos should be 0")
}
dirs[4].setOffset(1)
dirs[4].setTag(1)
dirs[4].setNext(5)
dirs[5].setOffset(1)
dirs[5].setTag(2)
dirs[5].setNext(6)
dirs[6].setOffset(1)
dirs[6].setTag(3)
dirs[6].setNext(0)
hit, pos, _ = dirProbe(2, 1, dirs)
if !hit {
t.Error("should not hit")
}
if pos != 5 {
t.Error("pos should be 5")
}
}
}
func TestDirManager_PurgeRandom10WhenFull(t *testing.T) {
dm := &DirManager{}
dm.Init(123457)
dm.InitEmptyDirs()
// set every dir to be used
counter1 := 0
for seg := segId(0); Offset(seg) < dm.SegmentsNum; seg++ {
dm.DirFreeStart[seg] = 0
for b := Offset(0); b < dm.BucketsNumPerSegment; b++ {
index := b * DirDepth
for i := Offset(0); i < DirDepth; i++ {
counter1++
dm.Dirs[seg][index+i].setNext(0)
dm.Dirs[seg][index+i].setOffset(1)
dm.Dirs[seg][index+i].setTag(1)
if i != DirDepth-1 {
dm.Dirs[seg][index+i].setNext(uint16(index + i + 1))
}
}
}
build := dm.freeChainRebuild(seg)
t.Logf("seg %v free chain rebuild: %v", seg, build)
}
t.Logf("set every dir to be used, dir total: %d", counter1)
// purge 10 random dirs
for seg := segId(0); Offset(seg) < dm.SegmentsNum; seg++ {
p := dm.purgeRandom10(seg, 0)
t.Logf("seg %v purge 10 random dirs: %v", seg, p)
}
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("after purge 10, dir free chain length: %d", counter)
if Offset(counter) == 0 {
t.Error("dir free chain empty")
}
}
func TestDirManager_FreeChainPop(t *testing.T) {
dm := &DirManager{}
dm.Init(123457)
dm.InitEmptyDirs()
// set every dir to be used
counter1 := 0
for seg := segId(0); Offset(seg) < dm.SegmentsNum; seg++ {
dm.DirFreeStart[seg] = 0
for b := Offset(0); b < dm.BucketsNumPerSegment; b++ {
index := b * DirDepth
for i := Offset(0); i < DirDepth; i++ {
counter1++
dm.Dirs[seg][index+i].setNext(0)
dm.Dirs[seg][index+i].setOffset(1)
dm.Dirs[seg][index+i].setTag(1)
if i != DirDepth-1 {
dm.Dirs[seg][index+i].setNext(uint16(index + i + 1))
}
}
}
build := dm.freeChainRebuild(seg)
t.Logf("seg %v free chain rebuild: %v", seg, build)
}
t.Logf("set every dir to be used, dir total: %d", counter1)
// pop 10 random dirs
for seg := segId(0); Offset(seg) < dm.SegmentsNum; seg++ {
p := dm.freeChainPop(seg, 0)
t.Logf("seg %v pop 10 random dirs: %v", seg, p)
}
counter, err := countDirFreeInChain(dm)
if err != nil {
t.Error(err)
}
t.Logf("after pop 10, dir free chain length: %d", counter)
if Offset(counter) == 0 {
t.Error("dir free chain empty")
}
}
func TestDirManager_GetSet(t *testing.T) {
dm := &DirManager{}
dm.Init(20)
dm.InitEmptyDirs()
// set same key
{
randomKey := make([]byte, 20)
_, err := rand.Read(randomKey)
pos, err := dm.Set(randomKey, 100, 200)
if err != nil {
t.Fatal(err)
}
t.Logf("pos: %v", pos)
pos2, err := dm.Set(randomKey, 100, 200)
if err != nil {
t.Fatal(err)
}
t.Logf("pos: %v", pos2)
if pos != pos2 {
t.Error("pos should be same")
}
// get
hit, pos3, d := dm.Get(randomKey)
if !hit {
t.Fatal("should hit")
}
if pos3 != pos {
t.Error("pos should be same")
}
if d.offset() != 100 {
t.Error("offset should be 100")
}
if d.approxSize() == 0 {
t.Error("approxSize should be > 0")
}
}
}