forked from armon/go-chord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vnode_test.go
686 lines (581 loc) · 14.3 KB
/
vnode_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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
package buddystore
import (
"bytes"
"crypto/sha1"
"sort"
"testing"
"time"
)
func makeVnode() *localVnode {
min := time.Duration(10 * time.Second)
max := time.Duration(30 * time.Second)
conf := &Config{
NumSuccessors: 8,
StabilizeMin: min,
StabilizeMax: max,
HashFunc: sha1.New}
trans := InitLocalTransport(nil)
ring := &Ring{config: conf, transport: trans}
// TODO: Remove this hack - make this part of ring initialization.
ring.shutdownComplete = make(chan bool, conf.NumSuccessors)
return &localVnode{ring: ring}
}
func TestVnodeInit(t *testing.T) {
vn := makeVnode()
vn.init(0)
if vn.Id == nil {
t.Fatalf("unexpected nil")
}
if vn.successors == nil {
t.Fatalf("unexpected nil")
}
if vn.finger == nil {
t.Fatalf("unexpected nil")
}
if vn.timer != nil {
t.Fatalf("unexpected timer")
}
}
func TestVnodeSchedule(t *testing.T) {
vn := makeVnode()
vn.schedule()
if vn.timer == nil {
t.Fatalf("unexpected nil")
}
}
func TestGenId(t *testing.T) {
vn := makeVnode()
var ids [][]byte
for i := 0; i < 16; i++ {
vn.genId(uint16(i))
ids = append(ids, vn.Id)
}
for idx, val := range ids {
for i := 0; i < len(ids); i++ {
if idx != i && bytes.Compare(ids[i], val) == 0 {
t.Fatalf("unexpected id collision!")
}
}
}
}
func TestVnodeStabilizeShutdown(t *testing.T) {
vn := makeVnode()
vn.schedule()
vn.ring.requestShutdown()
vn.stabilize()
if vn.timer != nil {
t.Fatalf("unexpected timer")
}
if !vn.stabilized.IsZero() {
t.Fatalf("unexpected time")
}
select {
case <-vn.ring.shutdownComplete:
return
default:
t.Fatalf("expected message")
}
}
func TestVnodeStabilizeResched(t *testing.T) {
vn := makeVnode()
vn.init(1)
vn.successors[0] = &vn.Vnode
vn.schedule()
vn.stabilize()
if vn.timer == nil {
t.Fatalf("expected timer")
}
if vn.stabilized.IsZero() {
t.Fatalf("expected time")
}
vn.timer.Stop()
}
func TestVnodeKnownSucc(t *testing.T) {
vn := makeVnode()
vn.init(0)
if vn.knownSuccessors() != 0 {
t.Fatalf("wrong num known!")
}
vn.successors[0] = &Vnode{Id: []byte{1}}
if vn.knownSuccessors() != 1 {
t.Fatalf("wrong num known!")
}
}
// Checks panic if no successors
func TestVnodeCheckNewSuccAlivePanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("expected panic!")
}
}()
vn1 := makeVnode()
vn1.init(1)
vn1.checkNewSuccessor()
}
// Checks pinging a live successor with no changes
func TestVnodeCheckNewSuccAlive(t *testing.T) {
vn1 := makeVnode()
vn1.init(1)
vn2 := makeVnode()
vn2.ring = vn1.ring
vn2.init(2)
vn2.predecessor = &vn1.Vnode
vn1.successors[0] = &vn2.Vnode
if pred, _ := vn2.GetPredecessor(); pred != &vn1.Vnode {
t.Fatalf("expected vn1 as predecessor")
}
if err := vn1.checkNewSuccessor(); err != nil {
t.Fatalf("unexpected err %s", err)
}
if vn1.successors[0] != &vn2.Vnode {
t.Fatalf("unexpected successor!")
}
}
// Checks pinging a dead successor with no alternates
func TestVnodeCheckNewSuccDead(t *testing.T) {
vn1 := makeVnode()
vn1.init(1)
vn1.successors[0] = &Vnode{Id: []byte{0}}
if err := vn1.checkNewSuccessor(); err == nil {
t.Fatalf("err!", err)
}
if vn1.successors[0].String() != "00" {
t.Fatalf("unexpected successor!")
}
}
// Checks pinging a dead successor with alternate
func TestVnodeCheckNewSuccDeadAlternate(t *testing.T) {
r := makeRing()
sort.Sort(r)
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn3 := r.vnodes[2]
vn1.successors[0] = &vn2.Vnode
vn1.successors[1] = &vn3.Vnode
vn2.predecessor = &vn1.Vnode
vn3.predecessor = &vn2.Vnode
// Remove vn2
(r.transport.(*LocalTransport)).Deregister(&vn2.Vnode)
// Should not get an error
if err := vn1.checkNewSuccessor(); err != nil {
t.Fatalf("unexpected err %s", err)
}
// Should become vn3
if vn1.successors[0] != &vn3.Vnode {
t.Fatalf("unexpected successor!")
}
}
// Checks pinging a dead successor with all dead alternates
func TestVnodeCheckNewSuccAllDeadAlternates(t *testing.T) {
r := makeRing()
sort.Sort(r)
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn3 := r.vnodes[2]
vn1.successors[0] = &vn2.Vnode
vn1.successors[1] = &vn3.Vnode
vn2.predecessor = &vn1.Vnode
vn3.predecessor = &vn2.Vnode
// Remove vn2
(r.transport.(*LocalTransport)).Deregister(&vn2.Vnode)
(r.transport.(*LocalTransport)).Deregister(&vn3.Vnode)
// Should get an error
if err := vn1.checkNewSuccessor(); err.Error() != "All known successors dead!" {
t.Fatalf("unexpected err %s", err)
}
// Should just be vn3
if vn1.successors[0] != &vn3.Vnode {
t.Fatalf("unexpected successor!")
}
}
// Checks pinging a successor, and getting a new successor
func TestVnodeCheckNewSuccNewSucc(t *testing.T) {
r := makeRing()
sort.Sort(r)
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn3 := r.vnodes[2]
vn1.successors[0] = &vn3.Vnode
vn2.predecessor = &vn1.Vnode
vn3.predecessor = &vn2.Vnode
// vn3 pred is vn2
if pred, _ := vn3.GetPredecessor(); pred != &vn2.Vnode {
t.Fatalf("expected vn2 as predecessor")
}
// Should not get an error
if err := vn1.checkNewSuccessor(); err != nil {
t.Fatalf("unexpected err %s", err)
}
// Should become vn2
if vn1.successors[0] != &vn2.Vnode {
t.Fatalf("unexpected successor! %s", vn1.successors[0])
}
// 2nd successor should become vn3
if vn1.successors[1] != &vn3.Vnode {
t.Fatalf("unexpected 2nd successor!")
}
}
// Checks pinging a successor, and getting a new successor
// which is not alive
func TestVnodeCheckNewSuccNewSuccDead(t *testing.T) {
r := makeRing()
sort.Sort(r)
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn3 := r.vnodes[2]
vn1.successors[0] = &vn3.Vnode
vn2.predecessor = &vn1.Vnode
vn3.predecessor = &vn2.Vnode
// Remove vn2
(r.transport.(*LocalTransport)).Deregister(&vn2.Vnode)
// Should not get an error
if err := vn1.checkNewSuccessor(); err != nil {
t.Fatalf("unexpected err %s", err)
}
// Should stay vn3
if vn1.successors[0] != &vn3.Vnode {
t.Fatalf("unexpected successor!")
}
}
// Test notifying a successor successfully
func TestVnodeNotifySucc(t *testing.T) {
r := makeRing()
sort.Sort(r)
s1 := &Vnode{Id: []byte{1}}
s2 := &Vnode{Id: []byte{2}}
s3 := &Vnode{Id: []byte{3}}
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn1.successors[0] = &vn2.Vnode
vn2.predecessor = &vn1.Vnode
vn2.successors[0] = s1
vn2.successors[1] = s2
vn2.successors[2] = s3
// Should get no error
if err := vn1.notifySuccessor(); err != nil {
t.Fatalf("unexpected err %s", err)
}
// Successor list should be updated
if vn1.successors[1] != s1 {
t.Fatalf("bad succ 1")
}
if vn1.successors[2] != s2 {
t.Fatalf("bad succ 2")
}
if vn1.successors[3] != s3 {
t.Fatalf("bad succ 3")
}
// Predecessor should not updated
if vn2.predecessor != &vn1.Vnode {
t.Fatalf("bad predecessor")
}
}
// Test notifying a dead successor
func TestVnodeNotifySuccDead(t *testing.T) {
r := makeRing()
sort.Sort(r)
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn1.successors[0] = &vn2.Vnode
vn2.predecessor = &vn1.Vnode
// Remove vn2
(r.transport.(*LocalTransport)).Deregister(&vn2.Vnode)
// Should get error
if err := vn1.notifySuccessor(); err == nil {
t.Fatalf("expected err!")
}
}
func TestVnodeNotifySamePred(t *testing.T) {
r := makeRing()
sort.Sort(r)
s1 := &Vnode{Id: []byte{1}}
s2 := &Vnode{Id: []byte{2}}
s3 := &Vnode{Id: []byte{3}}
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn1.successors[0] = &vn2.Vnode
vn2.predecessor = &vn1.Vnode
vn2.successors[0] = s1
vn2.successors[1] = s2
vn2.successors[2] = s3
succs, err := vn2.Notify(&vn1.Vnode)
if err != nil {
t.Fatalf("unexpected error! %s", err)
}
if succs[0] != s1 {
t.Fatalf("unexpected succ 0")
}
if succs[1] != s2 {
t.Fatalf("unexpected succ 1")
}
if succs[2] != s3 {
t.Fatalf("unexpected succ 2")
}
if vn2.predecessor != &vn1.Vnode {
t.Fatalf("unexpected pred")
}
}
func TestVnodeNotifyNoPred(t *testing.T) {
r := makeRing()
sort.Sort(r)
s1 := &Vnode{Id: []byte{1}}
s2 := &Vnode{Id: []byte{2}}
s3 := &Vnode{Id: []byte{3}}
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn2.successors[0] = s1
vn2.successors[1] = s2
vn2.successors[2] = s3
succs, err := vn2.Notify(&vn1.Vnode)
if err != nil {
t.Fatalf("unexpected error! %s", err)
}
if succs[0] != s1 {
t.Fatalf("unexpected succ 0")
}
if succs[1] != s2 {
t.Fatalf("unexpected succ 1")
}
if succs[2] != s3 {
t.Fatalf("unexpected succ 2")
}
if vn2.predecessor != &vn1.Vnode {
t.Fatalf("unexpected pred")
}
}
func TestVnodeNotifyNewPred(t *testing.T) {
r := makeRing()
sort.Sort(r)
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn3 := r.vnodes[2]
vn3.predecessor = &vn1.Vnode
_, err := vn3.Notify(&vn2.Vnode)
if err != nil {
t.Fatalf("unexpected error! %s", err)
}
if vn3.predecessor != &vn2.Vnode {
t.Fatalf("unexpected pred")
}
}
func TestVnodeFixFinger(t *testing.T) {
r := makeRing()
sort.Sort(r)
num := len(r.vnodes)
for i := 0; i < num; i++ {
r.vnodes[i].init(i)
r.vnodes[i].successors[0] = &r.vnodes[(i+1)%num].Vnode
}
// Fix finger should not error
vn := r.vnodes[0]
if err := vn.fixFingerTable(); err != nil {
t.Fatalf("unexpected err, %s", err)
}
// Check we've progressed
if vn.last_finger != 158 {
t.Fatalf("unexpected last finger! %d", vn.last_finger)
}
// Ensure that we've setup our successor as the initial entries
for i := 0; i < vn.last_finger; i++ {
if vn.finger[i] != vn.successors[0] {
t.Fatalf("unexpected finger entry!")
}
}
// Fix next index
if err := vn.fixFingerTable(); err != nil {
t.Fatalf("unexpected err, %s", err)
}
if vn.last_finger != 0 {
t.Fatalf("unexpected last finger! %d", vn.last_finger)
}
}
func TestVnodeCheckPredNoPred(t *testing.T) {
v := makeVnode()
v.init(0)
if err := v.checkPredecessor(); err != nil {
t.Fatalf("unpexected err! %s", err)
}
}
func TestVnodeCheckLivePred(t *testing.T) {
r := makeRing()
sort.Sort(r)
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn2.predecessor = &vn1.Vnode
if err := vn2.checkPredecessor(); err != nil {
t.Fatalf("unexpected error! %s", err)
}
if vn2.predecessor != &vn1.Vnode {
t.Fatalf("unexpected pred")
}
}
func TestVnodeCheckDeadPred(t *testing.T) {
r := makeRing()
sort.Sort(r)
vn1 := r.vnodes[0]
vn2 := r.vnodes[1]
vn2.predecessor = &vn1.Vnode
// Deregister vn1
(r.transport.(*LocalTransport)).Deregister(&vn1.Vnode)
if err := vn2.checkPredecessor(); err != nil {
t.Fatalf("unexpected error! %s", err)
}
if vn2.predecessor != nil {
t.Fatalf("unexpected pred")
}
}
func TestVnodeFindSuccessors(t *testing.T) {
r := makeRing()
sort.Sort(r)
num := len(r.vnodes)
for i := 0; i < num; i++ {
r.vnodes[i].successors[0] = &r.vnodes[(i+1)%num].Vnode
}
// Get a random key
h := r.config.HashFunc()
h.Write([]byte("test"))
key := h.Sum(nil)
// Local only, should be nearest in the ring
nearest := r.nearestVnode(key)
exp := nearest.successors[0]
// Do a lookup on the key
for i := 0; i < len(r.vnodes); i++ {
vn := r.vnodes[i]
succ, err := vn.FindSuccessors(1, key)
if err != nil {
t.Fatalf("unexpected err! %s", err)
}
// Local only, should be nearest in the ring
if exp != succ[0] {
t.Fatalf("unexpected succ! K:%x Exp: %s Got:%s",
key, exp, succ[0])
}
}
}
// Ensure each node has multiple successors
func TestVnodeFindSuccessorsMultSucc(t *testing.T) {
r := makeRing()
sort.Sort(r)
num := len(r.vnodes)
for i := 0; i < num; i++ {
r.vnodes[i].successors[0] = &r.vnodes[(i+1)%num].Vnode
r.vnodes[i].successors[1] = &r.vnodes[(i+2)%num].Vnode
r.vnodes[i].successors[2] = &r.vnodes[(i+3)%num].Vnode
}
// Get a random key
h := r.config.HashFunc()
h.Write([]byte("test"))
key := h.Sum(nil)
// Local only, should be nearest in the ring
nearest := r.nearestVnode(key)
exp := nearest.successors[0]
// Do a lookup on the key
for i := 0; i < len(r.vnodes); i++ {
vn := r.vnodes[i]
succ, err := vn.FindSuccessors(1, key)
if err != nil {
t.Fatalf("unexpected err! %s", err)
}
// Local only, should be nearest in the ring
if exp != succ[0] {
t.Fatalf("unexpected succ! K:%x Exp: %s Got:%s",
key, exp, succ[0])
}
}
}
// Kill off a part of the ring and see what happens
func TestVnodeFindSuccessorsSomeDead(t *testing.T) {
r := makeRing()
sort.Sort(r)
num := len(r.vnodes)
for i := 0; i < num; i++ {
r.vnodes[i].successors[0] = &r.vnodes[(i+1)%num].Vnode
r.vnodes[i].successors[1] = &r.vnodes[(i+2)%num].Vnode
}
// Kill 2 of the nodes
(r.transport.(*LocalTransport)).Deregister(&r.vnodes[0].Vnode)
(r.transport.(*LocalTransport)).Deregister(&r.vnodes[3].Vnode)
// Get a random key
h := r.config.HashFunc()
h.Write([]byte("test"))
key := h.Sum(nil)
// Local only, should be nearest in the ring
nearest := r.nearestVnode(key)
exp := nearest.successors[0]
// Do a lookup on the key
for i := 0; i < len(r.vnodes); i++ {
vn := r.vnodes[i]
succ, err := vn.FindSuccessors(1, key)
if err != nil {
t.Fatalf("(%d) unexpected err! %s", i, err)
}
// Local only, should be nearest in the ring
if exp != succ[0] {
t.Fatalf("(%d) unexpected succ! K:%x Exp: %s Got:%s",
i, key, exp, succ[0])
}
}
}
func TestVnodeClearPred(t *testing.T) {
v := makeVnode()
v.init(0)
p := &Vnode{Id: []byte{12}}
v.predecessor = p
v.ClearPredecessor(p)
if v.predecessor != nil {
t.Fatalf("expect no predecessor!")
}
np := &Vnode{Id: []byte{14}}
v.predecessor = p
v.ClearPredecessor(np)
if v.predecessor != p {
t.Fatalf("expect p predecessor!")
}
}
func TestVnodeSkipSucc(t *testing.T) {
v := makeVnode()
v.init(0)
s1 := &Vnode{Id: []byte{10}}
s2 := &Vnode{Id: []byte{11}}
s3 := &Vnode{Id: []byte{12}}
v.successors[0] = s1
v.successors[1] = s2
v.successors[2] = s3
// s2 should do nothing
if err := v.SkipSuccessor(s2); err != nil {
t.Fatalf("unexpected err")
}
if v.successors[0] != s1 {
t.Fatalf("unexpected suc")
}
// s1 should skip
if err := v.SkipSuccessor(s1); err != nil {
t.Fatalf("unexpected err")
}
if v.successors[0] != s2 {
t.Fatalf("unexpected suc")
}
if v.knownSuccessors() != 2 {
t.Fatalf("bad num of suc")
}
}
func TestVnodeLeave(t *testing.T) {
r := makeRing()
sort.Sort(r)
num := len(r.vnodes)
for i := int(0); i < num; i++ {
r.vnodes[i].predecessor = &r.vnodes[(i+num-1)%num].Vnode
r.vnodes[i].successors[0] = &r.vnodes[(i+1)%num].Vnode
r.vnodes[i].successors[1] = &r.vnodes[(i+2)%num].Vnode
}
// Make node 0 leave
if err := r.vnodes[0].leave(); err != nil {
t.Fatalf("unexpected err")
}
if r.vnodes[4].successors[0] != &r.vnodes[1].Vnode {
t.Fatalf("unexpected suc!")
}
if r.vnodes[1].predecessor != nil {
t.Fatalf("unexpected pred!")
}
}