-
Notifications
You must be signed in to change notification settings - Fork 1
/
ring_test.go
1327 lines (1195 loc) · 49.4 KB
/
ring_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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 Christos Katsakioris
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lfchring
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"math/big"
"math/rand"
"runtime"
"sort"
"strconv"
"strings"
"testing"
"time"
//"golang.org/x/crypto/blake2b"
)
var (
//blake2bHash = func(in []byte) []byte {
// out := blake2b.Sum256(in)
// return out[:]
//}
sha256Hash = func(in []byte) []byte {
out := sha256.Sum256(in)
return out[:]
}
hashFunc = sha256Hash
r *HashRing
)
func checkVirtualNodes(t *testing.T, r *HashRing) {
t.Helper()
state := r.state.Load().(*hashRingState)
numVNIDs := make(map[Node]int)
for i := 0; i < len(state.virtualNodes)-1; i++ {
if _, exists := numVNIDs[state.virtualNodes[i].Node()]; exists {
numVNIDs[state.virtualNodes[i].Node()]++
} else {
numVNIDs[state.virtualNodes[i].Node()] = 1
}
if bytes.Compare(state.virtualNodes[i].Name(), state.virtualNodes[i+1].Name()) >= 0 {
t.Errorf("%x == state.virtualNodes[%d] >= state.virtualNodes[%d] == %x\n",
state.virtualNodes[i], i, i+1, state.virtualNodes[i+1])
}
}
// Include the last one, which was left out of the loop above.
if _, exists := numVNIDs[state.virtualNodes[len(state.virtualNodes)-1].node]; exists {
numVNIDs[state.virtualNodes[len(state.virtualNodes)-1].node]++
} else {
numVNIDs[state.virtualNodes[len(state.virtualNodes)-1].node] = 1
}
// Check if all nodes were counted.
if len(numVNIDs) != r.Size() {
t.Errorf("Counted %d nodes; expected %d.\n", len(numVNIDs), r.Size())
}
// Check if all vnodes were counted.
numVnodes := 0
for _, vnCount := range numVNIDs {
numVnodes += vnCount
}
if numVnodes != r.Size()*int(state.virtualNodeCount) {
t.Errorf("Counted %d virtual nodes; expected %d.\n", numVnodes, r.Size()*int(state.virtualNodeCount))
}
t.Log("Ring state's slice of virtual nodes looks OK.")
}
func TestNewRingBadValues(t *testing.T) {
if _, err := NewHashRing(nil, 5, 5); err != nil {
t.Logf("NewHashRing(nil, 5, 5): %v\n", err)
} else {
t.Errorf("Expected error from NewHashRing()\n")
}
if _, err := NewHashRing(hashFunc, 1<<8, 1<<16); err != nil {
t.Logf("NewHashRing(hashFunc, 1<<8, 1<<16): %v\n", err)
} else {
t.Errorf("Expected error from NewHashRing()\n")
}
if _, err := NewHashRing(hashFunc, 1<<8-1, 1<<16); err != nil {
t.Logf("NewHashRing(hashFunc, 1<<8-1, 1<<16): %v\n", err)
} else {
t.Errorf("Expected error from NewHashRing()\n")
}
}
func TestNewEmptyRing(t *testing.T) {
r, err := NewHashRing(hashFunc, 3, 4)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
if r.Size() != 0 {
t.Errorf("Size of empty ring was reported to be %d.\n", r.Size())
}
}
func TestNewRingReplicationFactorLessThanDistinctNodeCount(t *testing.T) {
r, err := NewHashRing(hashFunc, 3, 6, "node-0", "node-1")
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
checkVirtualNodes(t, r)
t.Log(r)
}
func testNewRing(t *testing.T, replicationFactor, numVnodes, numNodes int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
if r.Size() != numNodes {
t.Errorf("r.Size() == %d; expected %d.\n", r.Size(), numNodes)
}
}
func TestNewTinyRing(t *testing.T) { testNewRing(t, 3, 4, 4) }
func TestNewRfLtDnRing(t *testing.T) { testNewRing(t, 16, 128, 15) }
func TestNewMedium1Ring(t *testing.T) { testNewRing(t, 4, 64, 32) }
func TestNewMedium2Ring(t *testing.T) { testNewRing(t, 4, 128, 8) }
func TestNewBigRing(t *testing.T) { testNewRing(t, 4, 128, 128) }
func TestNewHugeRing(t *testing.T) { testNewRing(t, 4, 256, 512) }
func TestNewGiganticRing(t *testing.T) { testNewRing(t, 4, 512, 2048) }
var garbageStr string
func TestStringGiganticRing(t *testing.T) {
replicationFactor := 4
numVnodes := 512
numNodes := 1024
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
garbageStr = r.String()
t.Log("Size of ring's representation:", len(garbageStr), "bytes.")
}
func TestClone(t *testing.T) {
r1, err := NewHashRing(hashFunc, 2, 4, Node("a"))
if err != nil {
t.Errorf("r1: NewHashRing(): %v\n", err)
t.FailNow()
}
t.Logf("r1:\n%#v\n%s\n", r1.state.Load().(*hashRingState).virtualNodes, r1)
checkVirtualNodes(t, r1)
r2 := r1.Clone()
if _, err = r2.Insert(Node("b")); err != nil {
t.Errorf("r2: Insert(): %v\n", err)
t.FailNow()
}
t.Logf("r2:\n%#v\n%s\n", r2.state.Load().(*hashRingState).virtualNodes, r2)
checkVirtualNodes(t, r2)
if r1.Size()*2 != r2.Size() {
t.Errorf("r1.Size() == %d; r2.Size() == %d\n", r1.Size(), r2.Size())
t.FailNow()
}
r3 := r2.Clone()
if _, err = r3.Remove(Node("a")); err != nil {
t.Errorf("r3: Remove(): %v\n", err)
t.FailNow()
}
t.Logf("r3:\n%#v\n%s\n", r3.state.Load().(*hashRingState).virtualNodes, r3)
checkVirtualNodes(t, r3)
if r1.Size() != r3.Size() || 2*r3.Size() != r2.Size() {
t.Errorf("r1.Size() == %d; r2.Size() == %d; r3.Size() == %d\n", r1.Size(), r2.Size(), r3.Size())
t.FailNow()
}
}
func testClone(t *testing.T, replicationFactor, virtualNodeCount, numNodes int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
oldRing, err := NewHashRing(hashFunc, replicationFactor, virtualNodeCount, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
checkVirtualNodes(t, oldRing)
newRing := oldRing.Clone()
checkVirtualNodes(t, newRing)
// check size
if oldRing.Size() != newRing.Size() {
t.Errorf("oldRing.Size() == %d != %d == newRing.Size()", oldRing.Size(), newRing.Size())
t.FailNow()
} else {
t.Log("Sizes OK.")
}
// check virtual nodes
oldVNs := oldRing.VirtualNodes(nil)
newVNs := newRing.VirtualNodes(nil)
for i := 0; i < oldRing.Size()*virtualNodeCount; i++ {
oldNext := <-oldVNs
newNext := <-newVNs
if bytes.Compare(oldNext.name, newNext.name) != 0 {
t.Errorf("%#v != %#v", oldNext, newNext)
t.FailNow()
}
oldNodes := oldRing.NodesForKey(oldNext.name)
newNodes := newRing.NodesForKey(newNext.name)
for i, oldNode := range oldNodes {
newNode := newNodes[i]
if newNode != oldNode {
t.Errorf("oldNodes[%d] == %s != %s == newNodes[%d]", i, oldNode, newNode, i)
t.FailNow()
}
}
}
// Make sure channels are drained.
if oldNext := <-oldVNs; oldNext != nil {
t.Errorf("Something is wrong with the iteration channel.")
t.FailNow()
}
if newNext := <-newVNs; newNext != nil {
t.Errorf("New ring also has this: %#v", newNext)
t.FailNow()
}
t.Log("Virtual nodes OK.")
}
func TestCloneTinyRing(t *testing.T) { testClone(t, 3, 4, 2) }
func TestCloneRfLtDnRing(t *testing.T) { testClone(t, 16, 128, 15) }
func TestCloneMedium1Ring(t *testing.T) { testClone(t, 3, 64, 32) }
func TestCloneMedium2Ring(t *testing.T) { testClone(t, 3, 128, 8) }
func TestCloneBigRing(t *testing.T) { testClone(t, 4, 128, 128) }
func TestCloneHugeRing(t *testing.T) { testClone(t, 3, 256, 512) }
func TestCloneGiganticRing(t *testing.T) { testClone(t, 3, 256, 1024) }
/*
//FIXME: reflect.DeepEqual() is not helpful in our case, because replicaOwners
//maps are not deeply equal because of the XXX below. See godoc for more
//information.
func TestCloneDeepEqual(t *testing.T) {
r, err := NewHashRing(hashFunc, 2, 4, "node-0", "node-1")
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
r2 := r.Clone()
if !reflect.DeepEqual(r, r2) {
t.Errorf("*HashRing.Clone() malfunction.")
t.Log("state reflect.DeepEqual():", reflect.DeepEqual(r.state, r2.state))
t.Log("state.Load().(*hashRingState) reflect.DeepEqual():",
reflect.DeepEqual(r.state.Load().(*hashRingState), r2.state.Load().(*hashRingState)))
t.Log("state.Load().(*hashRingState).numVirtualNodes reflect.DeepEqual():",
reflect.DeepEqual(
r.state.Load().(*hashRingState).numVirtualNodes,
r2.state.Load().(*hashRingState).numVirtualNodes,
),
)
t.Log("state.Load().(*hashRingState).replicationFactor reflect.DeepEqual():",
reflect.DeepEqual(
r.state.Load().(*hashRingState).replicationFactor,
r2.state.Load().(*hashRingState).replicationFactor,
),
)
t.Log("state.Load().(*hashRingState).virtualNodes reflect.DeepEqual():",
reflect.DeepEqual(
r.state.Load().(*hashRingState).virtualNodes,
r2.state.Load().(*hashRingState).virtualNodes,
),
)
t.Log("state.Load().(*hashRingState).replicaOwners reflect.DeepEqual():",
reflect.DeepEqual(
r.state.Load().(*hashRingState).replicaOwners,
r2.state.Load().(*hashRingState).replicaOwners,
),
)
rFirst := <-r.VirtualNodes(nil)
r2First := <-r2.VirtualNodes(nil)
t.Log("state.Load().(*hashRingState).replicaOwners[first] reflect.DeepEqual():",
reflect.DeepEqual(
r.state.Load().(*hashRingState).replicaOwners[rFirst],
r2.state.Load().(*hashRingState).replicaOwners[r2First],
),
)
t.Log("state.Load().(*hashRingState).replicaOwners key [first] reflect.DeepEqual():",
reflect.DeepEqual(
rFirst,
r2First,
),
)
t.Log("rFirst == r2First :", rFirst == r2First) // XXX
for k, v := range r.state.Load().(*hashRingState).replicaOwners {
if v2, exists2 := r2.state.Load().(*hashRingState).replicaOwners[k]; !exists2 {
t.Errorf("Key {%v} not present in r2!\n\n", k)
} else if !reflect.DeepEqual(v, v2) {
t.Errorf("This is not equal:\nKey: {%#v} -->\nv1: {%#v}\nv2: {%#v}\n\n", k, v, v2)
}
}
t.Logf("%#v", r)
t.Logf("%#v", r2)
t.Logf("%#v", r.state.Load().(*hashRingState).replicaOwners)
t.Logf("%#v", r2.state.Load().(*hashRingState).replicaOwners)
//t.Log("r:\n", r)
t.Log("r2:\n", r2)
//r.Add("node-2")
//t.Log("r (later):\n", r)
}
}
*/
func TestAddExistingNode(t *testing.T) {
r, err := NewHashRing(hashFunc, 2, 4, "node-0", "node-1")
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
_, err = r.Insert("node-0")
if err != nil {
t.Logf("Add(): returned error %q, as expected\n", err.Error())
} else {
t.Errorf("Add(): Expected an error for adding an existing node\n")
}
}
func testAdd(t *testing.T, replicationFactor, numVnodes, numNodes int) {
// Create the ring
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
for i := numNodes; i < numNodes+10; i += 2 {
_, err := r.Insert(Node(fmt.Sprintf("node-%d", i)), Node(fmt.Sprintf("node-%d", i+1)))
if err != nil {
t.Errorf("Add(): %v\n", err)
}
}
checkVirtualNodes(t, r)
}
func TestAddTinyRing(t *testing.T) { testAdd(t, 3, 4, 3) }
func TestAddRfLtDnRing(t *testing.T) { testAdd(t, 16, 128, 15) }
func TestAddMedium1Ring(t *testing.T) { testAdd(t, 3, 64, 32) }
func TestAddMedium2Ring(t *testing.T) { testAdd(t, 3, 128, 8) }
func TestAddBigRing(t *testing.T) { testAdd(t, 3, 128, 128) }
func TestAddHugeRing(t *testing.T) { testAdd(t, 2, 256, 512) }
func TestAddGiganticRing(t *testing.T) { testAdd(t, 2, 512, 1024) }
func testParallelRW(t *testing.T, replicationFactor, numVnodes, numNodes, concurrency int) {
// Create the ring
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
checkVirtualNodes(t, r)
// readers
start := time.Now()
done := make(chan struct{})
for goroutine := 0; goroutine < concurrency; goroutine++ {
go func(workerID int) {
readCnt := 0
size := 0
for {
select {
case <-done:
return
case <-time.After(20 * time.Millisecond):
newSize := r.Size()
readCnt++
if newSize < size {
t.Errorf("[reader-%d] +%s: newSize < size", workerID, time.Since(start))
} else {
t.Logf("[reader-%d] +%s: OK after reading %d time(s). (%d->%d)",
workerID, time.Since(start), readCnt, size, newSize)
}
size = newSize
}
}
}(goroutine)
}
// writer
for i := numNodes; i < numNodes+(numNodes/10); i++ {
<-time.After(30 * time.Millisecond)
if _, err := r.Insert(Node(fmt.Sprintf("node-%d", i))); err != nil {
t.Errorf("[writer] +%s: Add(\"node-%d\"): %v\n", time.Since(start), i, err)
} else {
checkVirtualNodes(t, r)
t.Logf("[writer] +%s: OK after adding %d node(s).", time.Since(start), i+1)
}
}
<-time.After(30 * time.Millisecond)
close(done)
}
func TestParallelRWTinyRing(t *testing.T) { testParallelRW(t, 3, 4, 4, 10) }
func TestParallelRWRfLtDnRing(t *testing.T) { testParallelRW(t, 16, 128, 15, 10) }
func TestParallelRWMedium1Ring(t *testing.T) { testParallelRW(t, 3, 64, 32, 10) }
func TestParallelRWMedium2Ring(t *testing.T) { testParallelRW(t, 3, 128, 8, 10) }
func TestParallelRWBigRing(t *testing.T) { testParallelRW(t, 3, 128, 128, 10) }
func TestParallelRWHugeRing(t *testing.T) { testParallelRW(t, 3, 64, 256, 10) }
// NOTE: The ring is not expected to be able to handle multiple writers.
func testParallelAdd(t *testing.T, replicationFactor, numVnodes, numNodes, concurrency int) {
// Create the ring
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
c := make(chan int, concurrency)
done := make(chan struct{})
go func() {
defer close(c)
for i := numNodes; i < numNodes+concurrency; i++ {
c <- i
}
}()
runtime.GOMAXPROCS(runtime.NumCPU())
start := time.Now()
for goroutine := 0; goroutine < concurrency; goroutine++ {
go func(workerID int) {
for i := range c {
n := Node(fmt.Sprintf("node-%d", i))
t.Logf("[goroutine-%d] +%s: Adding \"%s\"\n", workerID, time.Since(start), n)
_, err := r.Insert(n)
if err != nil {
t.Errorf("[goroutine-%d] +%s: r.Add(): %v\n", workerID, time.Since(start), err)
}
}
done <- struct{}{}
}(goroutine)
}
t.Logf("[main-goroutine] +%s: Waiting for all goroutines to finish...", time.Since(start))
for i := 0; i < concurrency; i++ {
<-done
}
if r.Size() != numNodes+concurrency {
t.Errorf("[main-goroutine] +%s: r.Size() == %d; expected %d.\n",
time.Since(start), r.Size(), numNodes+concurrency)
} else {
t.Logf("[main-goroutine] +%s: The number of total nodes looks OK.", time.Since(start))
}
}
//func TestParallelAddTinyRing(t *testing.T) { testParallelAdd(t, 3, 2, 2, 3) }
//func TestParallelAddMedium2Ring(t *testing.T) { testParallelAdd(t, 3, 128, 2, 10) }
func TestRemoveNontExistentNode(t *testing.T) {
r, err := NewHashRing(hashFunc, 2, 8, "node-0", "node-1", "node-2")
if err != nil {
t.Errorf("NewHashRing(): %v", err)
t.FailNow()
}
_, err = r.Remove("node-42")
if err != nil {
t.Logf("Remove(): returned error %q, as expected\n", err.Error())
} else {
t.Errorf("Remove(): expected error for removing non-existent node\n")
}
checkVirtualNodes(t, r)
}
func testRemoveFromRing(t *testing.T, replicationFactor, virtualNodeCount, numNodes int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, virtualNodeCount, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
checkVirtualNodes(t, r)
if r.Size() != numNodes {
t.Errorf("r.Size() == %d; expected %d.\n", r.Size(), numNodes)
} else {
t.Log("r.Size() before removal:", r.Size())
}
for i := 0; i < r.Size(); i += 2 {
if _, err := r.Remove(Node(fmt.Sprintf("node-%d", i))); err != nil {
t.Errorf("(*HashRing).Remove(\"node-%d\"): %v\n", i, err)
t.FailNow()
} else {
numNodes--
}
}
checkVirtualNodes(t, r)
if r.Size() != numNodes {
t.Errorf("r.Size() == %d; expected %d.\n", r.Size(), numNodes)
} else {
t.Log("r.Size() after removal:", r.Size(), "OK")
}
}
func TestRemoveFromTinyRing(t *testing.T) { testRemoveFromRing(t, 3, 2, 4) }
func TestRemoveRfLtDnRing(t *testing.T) { testRemoveFromRing(t, 16, 128, 15) }
func TestRemoveFromMedium1Ring(t *testing.T) { testRemoveFromRing(t, 3, 64, 32) }
func TestRemoveFromMedium2Ring(t *testing.T) { testRemoveFromRing(t, 3, 128, 8) }
func TestRemoveFromBigRing(t *testing.T) { testRemoveFromRing(t, 3, 128, 128) }
func TestRemoveFromHugeRing(t *testing.T) { testRemoveFromRing(t, 3, 256, 512) }
func TestNodesForKeyTinyRing(t *testing.T) {
numNodes := 5
replicationFactor := 4
numVnodes := 4
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
joinedResults := make([]string, 0)
for _, vn := range r.state.Load().(*hashRingState).virtualNodes {
joinedResults = append(joinedResults, "\n"+vn.String()+" (vnode)\n")
}
for i := 0x00; i < 0x10; i++ {
// keyS examples: 000...00, 111...11, ..., fff...ff
// <- 64 -> <- 64 -> <- 64 ->
keyS := strings.Repeat(strconv.FormatInt(int64(i), 16), 64)
// keyB is the 64B-long keyS string's []byte representation ([32]byte underneath)
keyB, _ := hex.DecodeString(keyS)
nodes := r.NodesForKey(keyB)
joinedResults = append(joinedResults, fmt.Sprintf("\n%s (key)\n\t@ nodes: %v\n", keyS, nodes))
}
sort.Strings(joinedResults)
t.Log(joinedResults)
}
func TestNodesForObjectBadReader(t *testing.T) {
pr, pw := io.Pipe()
_ = pw.CloseWithError(fmt.Errorf("test"))
defer pr.Close()
r, err := NewHashRing(hashFunc, 3, 3)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
if _, err = r.NodesForObject(pr); err != nil {
t.Logf("NodesForObject(): Returned error %q, as expected\n", err)
} else {
t.Errorf("NodesForObject(): expected an error")
}
}
func TestNodesForObjectTinyRing(t *testing.T) {
numNodes := 5
replicationFactor := 4
numVnodes := 4
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
joinedResults := make([]string, 0)
for _, vn := range r.state.Load().(*hashRingState).virtualNodes {
joinedResults = append(joinedResults, "\n"+vn.String()+" (vnode)\n")
}
for i := 0x00; i < 0x10; i++ {
objB := []byte(strconv.FormatInt(int64(i), 16))
keyB := hashFunc(objB)
keyS := hex.EncodeToString(keyB[:])
nodes, err := r.NodesForObject(bytes.NewReader(objB))
if err != nil {
t.Error(err)
}
joinedResults = append(joinedResults, fmt.Sprintf("\n%s (key)\n\t@nodes: %v\n", keyS, nodes))
}
sort.Strings(joinedResults)
t.Log(joinedResults)
}
func TestIterStop(t *testing.T) {
r, err := NewHashRing(hashFunc, 2, 4, "node-0", "node-1")
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
stop := make(chan struct{})
defer close(stop)
vns := r.VirtualNodes(stop)
for i := 0; i < r.Size(); i++ {
<-vns
}
// BUG ^ goroutine-leak
}
func testIter(t *testing.T, replicationFactor, numVnodes, numNodes int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
iterVNList := make([]*VirtualNode, 0)
for vn := range r.VirtualNodes(nil) {
iterVNList = append(iterVNList, vn)
}
if len(iterVNList) != numVnodes*numNodes {
t.Errorf("len(iterVNList) == %d != %d == numVnodes*numNodes\n", len(iterVNList), numVnodes*numNodes)
t.FailNow()
} else {
t.Logf("The number of iterated virtual nodes looks OK.")
}
}
func TestIterTinyRing(t *testing.T) { testIter(t, 3, 4, 4) }
func TestIterRfLtDnRing(t *testing.T) { testIter(t, 16, 128, 15) }
func TestIterMedium1Ring(t *testing.T) { testIter(t, 2, 64, 32) }
func TestIterMedium2Ring(t *testing.T) { testIter(t, 2, 128, 2) }
func TestIterBigRing(t *testing.T) { testIter(t, 2, 128, 128) }
func TestIterHugeRing(t *testing.T) { testIter(t, 2, 256, 512) }
func TestIterGiganticRing(t *testing.T) { testIter(t, 2, 512, 1024) }
func testParallelIter(t *testing.T, replicationFactor, numVnodes, numNodes, concurrency int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
done := make(chan struct{})
runtime.GOMAXPROCS(runtime.NumCPU())
start := time.Now()
for goroutine := 0; goroutine < concurrency; goroutine++ {
go func(workerID int) {
iterList := make([]*VirtualNode, 0)
for vn := range r.VirtualNodes(nil) {
iterList = append(iterList, vn)
time.Sleep(time.Duration(rand.Int31n(1<<10)) * time.Microsecond)
}
if len(iterList) != numNodes*numVnodes {
t.Errorf("[goroutine-%d] +%s: len(iterList) == %d != %d == numVnodes*numNodes\n",
workerID, time.Since(start), len(iterList), numVnodes*numNodes)
} else {
t.Logf("[goroutine-%d] +%s: The number of iterated virtual nodes looks OK.",
workerID, time.Since(start))
}
done <- struct{}{}
}(goroutine)
}
t.Logf("[main-goroutine] +%s: Waiting for all worker goroutines to finish...\n", time.Since(start))
for i := 0; i < concurrency; i++ {
<-done
}
}
func TestParallelIterTinyRing(t *testing.T) { testParallelIter(t, 3, 4, 4, 10) }
func TestParallelIterRfLtDnRing(t *testing.T) { testParallelIter(t, 16, 128, 15, 10) }
func TestParallelIterBigRing(t *testing.T) { testParallelIter(t, 2, 128, 128, 15) }
func TestIterReversedStop(t *testing.T) {
r, err := NewHashRing(hashFunc, 2, 4, "node-0", "node-1")
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
stop := make(chan struct{})
defer close(stop)
vns := r.VirtualNodesReversed(stop)
for i := 0; i < r.Size(); i++ {
<-vns
}
// BUG ^ goroutine-leak
}
func testParallelIterReversed(t *testing.T, replicationFactor, numVnodes, numNodes, concurrency int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
vns := make([]*VirtualNode, 0)
for vn := range r.VirtualNodes(nil) {
vns = append(vns, vn)
}
done := make(chan struct{})
runtime.GOMAXPROCS(runtime.NumCPU())
start := time.Now()
for goroutine := 0; goroutine < concurrency; goroutine++ {
go func(workerID int) {
stop := make(chan struct{})
defer close(stop)
i := 0
for vn := range r.VirtualNodesReversed(stop) {
if bytes.Compare(vn.name, vns[len(vns)-1-i].name) != 0 {
t.Errorf("[goroutine-%d] +%s: reversed[%d] should be %x instead of %x\n",
workerID, time.Since(start), i, vns[len(vns)-1-i].name, vn.name)
}
i++
}
done <- struct{}{}
}(goroutine)
}
for i := 0; i < concurrency; i++ {
<-done
}
t.Logf("[goroutine-main]: +%s: All good.\n", time.Since(start))
}
func TestParallelIterReversedTinyRing(t *testing.T) { testParallelIterReversed(t, 3, 4, 4, 10) }
func TestParallelIterReversedRfLtDnRing(t *testing.T) { testParallelIterReversed(t, 16, 128, 15, 10) }
func TestParallelIterReversedBigRing(t *testing.T) { testParallelIterReversed(t, 2, 128, 128, 15) }
func testIterator(t *testing.T, replicationFactor, numVnodes, numNodes int, reverse bool) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
iterVNList := make([]*VirtualNode, 0, len(r.state.Load().(*hashRingState).virtualNodes))
if !reverse {
for vnIter := r.NewVirtualNodesIterator(); vnIter.HasNext(); {
iterVNList = append(iterVNList, vnIter.Next())
}
} else {
for vnIter := r.NewVirtualNodesReverseIterator(); vnIter.HasNext(); {
iterVNList = append(iterVNList, vnIter.Next())
}
}
if len(iterVNList) != numVnodes*numNodes {
t.Errorf("len(iterVNList) == %d != %d == numVnodes*numNodes\n", len(iterVNList), numVnodes*numNodes)
t.FailNow()
} else {
t.Logf("The number of iterated virtual nodes looks OK.")
}
}
func TestIteratorTinyRing(t *testing.T) { testIterator(t, 3, 4, 4, false) }
func TestReverseIteratorTinyRing(t *testing.T) { testIterator(t, 3, 4, 4, true) }
func TestIteratorRfLtDnRing(t *testing.T) { testIterator(t, 16, 128, 15, false) }
func TestReversedIteratorRfLtDnRing(t *testing.T) { testIterator(t, 16, 128, 15, true) }
func TestIteratorMedium1Ring(t *testing.T) { testIterator(t, 2, 64, 32, false) }
func TestReverseIteratorMedium1Ring(t *testing.T) { testIterator(t, 2, 64, 32, true) }
func TestIteratorMedium2Ring(t *testing.T) { testIterator(t, 2, 128, 2, false) }
func TestReverseIteratorMedium2Ring(t *testing.T) { testIterator(t, 2, 128, 2, true) }
func TestIteratorBigRing(t *testing.T) { testIterator(t, 2, 128, 128, false) }
func TestReverseIteratorBigRing(t *testing.T) { testIterator(t, 2, 128, 128, true) }
func TestIteratorHugeRing(t *testing.T) { testIterator(t, 2, 256, 512, false) }
func TestReverseIteratorHugeRing(t *testing.T) { testIterator(t, 2, 256, 512, true) }
func TestIteratorGiganticRing(t *testing.T) { testIterator(t, 2, 512, 1024, false) }
func TestReverseIteratorGiganticRing(t *testing.T) { testIterator(t, 2, 512, 1024, true) }
func testVirtualNodeForKey(t *testing.T, replicationFactor, numVnodes, numNodes int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
stop := make(chan struct{})
defer close(stop)
for vn := range r.VirtualNodes(stop) {
if bytes.Compare(r.VirtualNodeForKey(vn.name).name, vn.name) != 0 {
t.Errorf("VirtualNodeForKey(%x) != %x\n", r.VirtualNodeForKey(vn.name), vn.name)
}
}
lastKey, _ := hex.DecodeString(strings.Repeat("f", 64))
firstVN := <-r.VirtualNodes(nil)
if bytes.Compare(r.VirtualNodeForKey(lastKey).name, firstVN.name) != 0 {
t.Errorf("VirtualNodeForKey(%x) != %x\n", r.VirtualNodeForKey(lastKey), firstVN.name)
}
}
func TestVirtualNodeForKeyTinyRing(t *testing.T) { testVirtualNodeForKey(t, 2, 2, 2) }
func TestVirtualNodeForKeyRfLtDnRing(t *testing.T) { testVirtualNodeForKey(t, 16, 128, 15) }
func TestVirtualNodeForKeyMedium1Ring(t *testing.T) { testVirtualNodeForKey(t, 2, 64, 32) }
func TestVirtualNodeForKeyMedium2Ring(t *testing.T) { testVirtualNodeForKey(t, 2, 128, 8) }
func TestVirtualNodeForKeyBigRing(t *testing.T) { testVirtualNodeForKey(t, 2, 128, 128) }
func TestVirtualNodeForKeyHugeRing(t *testing.T) { testVirtualNodeForKey(t, 2, 256, 512) }
func TestVirtualNodeForKeyGiganticRing(t *testing.T) { testVirtualNodeForKey(t, 2, 512, 1024) }
func TestPredSuccEmptyRing(t *testing.T) {
r, err := NewHashRing(hashFunc, 2, 2)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
vnodeHash := hashFunc([]byte("node-42"))
if _, err = r.Predecessor(vnodeHash[:]); err != nil {
t.Logf("Received error %q, as expected.\n", err.Error())
} else {
t.Errorf("Expected error for calling Predecessor() on empty ring.\n")
}
if _, err = r.Successor(vnodeHash[:]); err != nil {
t.Logf("Received error %q, as expected.\n", err.Error())
} else {
t.Errorf("Expected error for calling Successor() on empty ring.\n")
}
}
func TestPredSuccNodeEmptyRing(t *testing.T) {
r, err := NewHashRing(hashFunc, 2, 2)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
vnodeHash := hashFunc([]byte("node-42"))
if _, err = r.PredecessorNode(vnodeHash[:]); err != nil {
t.Logf("Received error %q, as expected.\n", err.Error())
} else {
t.Errorf("Expected error for calling PredecessorNode() on empty ring.\n")
}
if _, err = r.SuccessorNode(vnodeHash[:]); err != nil {
t.Logf("Received error %q, as expected.\n", err.Error())
} else {
t.Errorf("Expected error for calling SuccessorNode() on empty ring.\n")
}
}
func TestPredSuccNodeSingleNodeRing(t *testing.T) {
r, err := NewHashRing(hashFunc, 2, 2, "node-42")
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
vn := <-r.VirtualNodes(nil)
if _, err := r.PredecessorNode(vn.name); err != nil {
t.Logf("Received error %q, as expected.\n", err.Error())
} else {
t.Errorf("Expected error for calling PredecessorNode() on single-node ring.\n")
}
if _, err = r.SuccessorNode(vn.name); err != nil {
t.Logf("Received error %q, as expected.\n", err.Error())
} else {
t.Errorf("Expected error for calling SuccessorNode() on single-node ring.\n")
}
}
func testPredecessor(t *testing.T, replicationFactor, numVnodes, numNodes int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
vns := make([]*VirtualNode, 0)
for vn := range r.VirtualNodes(nil) {
vns = append(vns, vn)
}
reportedPredecessors := make([]*VirtualNode, len(vns))
for i := 1; i < len(vns)+1; i++ {
reportedPredecessors[i-1], err = r.Predecessor(vns[i%len(vns)].name)
if err != nil {
t.Errorf("Predecessor(%x): %v\n", vns[i%len(vns)].name, err)
}
}
for i, vn := range reportedPredecessors {
if bytes.Compare(vns[i].name, vn.name) != 0 {
t.Errorf("reportedPredecessors[%d] == %x != %x == virtualNodes[%d]\n", i, vns[i].name, vn.name, i)
}
}
}
func TestPredecessorRfLtDnRing(t *testing.T) { testPredecessor(t, 16, 128, 15) }
func TestPredecessorMedium1Ring(t *testing.T) { testPredecessor(t, 3, 64, 32) }
func TestPredecessorMedium2Ring(t *testing.T) { testPredecessor(t, 3, 128, 8) }
func TestPredecessorBigRing(t *testing.T) { testPredecessor(t, 3, 128, 128) }
func TestPredecessorHugeRing(t *testing.T) { testPredecessor(t, 3, 256, 512) }
func TestPredecessorGiganticRing(t *testing.T) { testPredecessor(t, 3, 512, 1024) }
func testSuccessor(t *testing.T, replicationFactor, numVnodes, numNodes int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))
}
r, err := NewHashRing(hashFunc, replicationFactor, numVnodes, nodes...)
if err != nil {
t.Errorf("NewHashRing(): %v\n", err)
t.FailNow()
}
vns := make([]*VirtualNode, 0)
for vn := range r.VirtualNodes(nil) {
vns = append(vns, vn)
}
reportedSuccessors := make([]*VirtualNode, len(vns))
for i := 0; i < len(vns); i++ {
reportedSuccessors[i], err = r.Successor(vns[i].name)
if err != nil {
t.Errorf("Successor(%x): %v\n", vns[i].name, err)
}
}
for i, vn := range reportedSuccessors {
if bytes.Compare(vns[(i+1)%len(vns)].name, vn.name) != 0 {
t.Errorf("reportedSuccessors[%d] == %x != %x == virtualNodes[%d]\n",
i, vns[(i+1)%len(vns)].name, vn.name, i)
}
}
}
func TestSuccessorRfLtDnRing(t *testing.T) { testSuccessor(t, 16, 128, 15) }
func TestSuccessorMedium1Ring(t *testing.T) { testSuccessor(t, 3, 64, 32) }
func TestSuccessorMedium2Ring(t *testing.T) { testSuccessor(t, 3, 128, 8) }
func TestSuccessorBigRing(t *testing.T) { testSuccessor(t, 3, 128, 128) }
func TestSuccessorHugeRing(t *testing.T) { testSuccessor(t, 3, 256, 512) }
func TestSuccessorGiganticRing(t *testing.T) { testSuccessor(t, 3, 512, 1024) }
func testPredecessorNode(t *testing.T, replicationFactor, numVnodes, numNodes int) {
nodes := make([]Node, numNodes)
for i := 0; i < numNodes; i++ {
nodes[i] = Node(fmt.Sprintf("node-%d", i))