-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
allocator_test.go
1488 lines (1415 loc) · 42.4 KB
/
allocator_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 2014 The Cockroach Authors.
//
// 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.
//
// Author: Spencer Kimball ([email protected])
// Author: Kathy Spradlin ([email protected])
// Author: Levon Lloyd ([email protected])
package storage
import (
"fmt"
"math"
"math/rand"
"reflect"
"sort"
"sync"
"testing"
"time"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/testutils/gossiputil"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
const firstRange = roachpb.RangeID(1)
const noStore = roachpb.StoreID(-1)
var simpleZoneConfig = config.ZoneConfig{
NumReplicas: 1,
Constraints: config.Constraints{
Constraints: []config.Constraint{
{Value: "a"},
{Value: "ssd"},
},
},
}
var multiDCConfig = config.ZoneConfig{
NumReplicas: 2,
Constraints: config.Constraints{Constraints: []config.Constraint{{Value: "ssd"}}},
}
var singleStore = []*roachpb.StoreDescriptor{
{
StoreID: 1,
Attrs: roachpb.Attributes{Attrs: []string{"ssd"}},
Node: roachpb.NodeDescriptor{
NodeID: 1,
Attrs: roachpb.Attributes{Attrs: []string{"a"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
}
var sameDCStores = []*roachpb.StoreDescriptor{
{
StoreID: 1,
Attrs: roachpb.Attributes{Attrs: []string{"ssd"}},
Node: roachpb.NodeDescriptor{
NodeID: 1,
Attrs: roachpb.Attributes{Attrs: []string{"a"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
{
StoreID: 2,
Attrs: roachpb.Attributes{Attrs: []string{"ssd"}},
Node: roachpb.NodeDescriptor{
NodeID: 2,
Attrs: roachpb.Attributes{Attrs: []string{"a"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
{
StoreID: 3,
Attrs: roachpb.Attributes{Attrs: []string{"hdd"}},
Node: roachpb.NodeDescriptor{
NodeID: 2,
Attrs: roachpb.Attributes{Attrs: []string{"a"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
{
StoreID: 4,
Attrs: roachpb.Attributes{Attrs: []string{"hdd"}},
Node: roachpb.NodeDescriptor{
NodeID: 3,
Attrs: roachpb.Attributes{Attrs: []string{"a"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
{
StoreID: 5,
Attrs: roachpb.Attributes{Attrs: []string{"mem"}},
Node: roachpb.NodeDescriptor{
NodeID: 4,
Attrs: roachpb.Attributes{Attrs: []string{"a"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
}
var multiDCStores = []*roachpb.StoreDescriptor{
{
StoreID: 1,
Attrs: roachpb.Attributes{Attrs: []string{"ssd"}},
Node: roachpb.NodeDescriptor{
NodeID: 1,
Attrs: roachpb.Attributes{Attrs: []string{"a"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
{
StoreID: 2,
Attrs: roachpb.Attributes{Attrs: []string{"ssd"}},
Node: roachpb.NodeDescriptor{
NodeID: 2,
Attrs: roachpb.Attributes{Attrs: []string{"b"}},
},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 200,
},
},
}
// createTestAllocator creates a stopper, gossip, store pool and allocator for
// use in tests. Stopper must be stopped by the caller.
func createTestAllocator() (*stop.Stopper, *gossip.Gossip, *StorePool, Allocator, *hlc.ManualClock) {
stopper, g, manualClock, storePool := createTestStorePool(TestTimeUntilStoreDeadOff)
manualClock.Set(hlc.UnixNano())
a := MakeAllocator(storePool, AllocatorOptions{AllowRebalance: true})
return stopper, g, storePool, a, manualClock
}
// mockStorePool sets up a collection of a alive and dead stores in the store
// pool for testing purposes. It also adds dead replicas to the stores and
// ranges in deadReplicas.
func mockStorePool(
storePool *StorePool,
aliveStoreIDs, deadStoreIDs []roachpb.StoreID,
deadReplicas []roachpb.ReplicaIdent,
) {
storePool.mu.Lock()
defer storePool.mu.Unlock()
storePool.mu.storeDetails = make(map[roachpb.StoreID]*storeDetail)
for _, storeID := range aliveStoreIDs {
detail := newStoreDetail(context.Background())
detail.desc = &roachpb.StoreDescriptor{
StoreID: storeID,
Node: roachpb.NodeDescriptor{NodeID: roachpb.NodeID(storeID)},
}
storePool.mu.storeDetails[storeID] = detail
}
for _, storeID := range deadStoreIDs {
detail := newStoreDetail(context.TODO())
detail.dead = true
detail.desc = &roachpb.StoreDescriptor{
StoreID: storeID,
Node: roachpb.NodeDescriptor{NodeID: roachpb.NodeID(storeID)},
}
storePool.mu.storeDetails[storeID] = detail
}
for storeID, detail := range storePool.mu.storeDetails {
for _, replica := range deadReplicas {
if storeID != replica.Replica.StoreID {
continue
}
detail.deadReplicas[replica.RangeID] = append(detail.deadReplicas[replica.RangeID], replica.Replica)
}
}
}
func TestAllocatorSimpleRetrieval(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, a, _ := createTestAllocator()
defer stopper.Stop()
gossiputil.NewStoreGossiper(g).GossipStores(singleStore, t)
result, err := a.AllocateTarget(
simpleZoneConfig.Constraints,
[]roachpb.ReplicaDescriptor{},
firstRange,
)
if err != nil {
t.Fatalf("Unable to perform allocation: %v", err)
}
if result.Node.NodeID != 1 || result.StoreID != 1 {
t.Errorf("expected NodeID 1 and StoreID 1: %+v", result)
}
}
// TestAllocatorCorruptReplica ensures that the allocator never attempts to
// allocate a new replica on top of a dead (corrupt) one.
func TestAllocatorCorruptReplica(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, sp, a, _ := createTestAllocator()
defer stopper.Stop()
gossiputil.NewStoreGossiper(g).GossipStores(sameDCStores, t)
const store1ID = roachpb.StoreID(1)
// Set store 1 to have a dead replica in the store pool.
sp.mu.Lock()
sp.mu.storeDetails[store1ID].deadReplicas[firstRange] =
[]roachpb.ReplicaDescriptor{{
NodeID: roachpb.NodeID(1),
StoreID: store1ID,
}}
sp.mu.Unlock()
result, err := a.AllocateTarget(
simpleZoneConfig.Constraints,
[]roachpb.ReplicaDescriptor{},
firstRange,
)
if err != nil {
t.Fatal(err)
}
if result.Node.NodeID != 2 || result.StoreID != 2 {
t.Errorf("expected NodeID 2 and StoreID 2: %+v", result)
}
}
func TestAllocatorNoAvailableDisks(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, _, _, a, _ := createTestAllocator()
defer stopper.Stop()
result, err := a.AllocateTarget(
simpleZoneConfig.Constraints,
[]roachpb.ReplicaDescriptor{},
firstRange,
)
if result != nil {
t.Errorf("expected nil result: %+v", result)
}
if err == nil {
t.Errorf("allocation succeeded despite there being no available disks: %v", result)
}
}
func TestAllocatorTwoDatacenters(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, a, _ := createTestAllocator()
defer stopper.Stop()
gossiputil.NewStoreGossiper(g).GossipStores(multiDCStores, t)
result1, err := a.AllocateTarget(
multiDCConfig.Constraints,
[]roachpb.ReplicaDescriptor{},
firstRange,
)
if err != nil {
t.Fatalf("Unable to perform allocation: %v", err)
}
result2, err := a.AllocateTarget(
multiDCConfig.Constraints,
[]roachpb.ReplicaDescriptor{{
NodeID: result1.Node.NodeID,
StoreID: result1.StoreID,
}},
firstRange,
)
if err != nil {
t.Fatalf("Unable to perform allocation: %v", err)
}
ids := []int{int(result1.Node.NodeID), int(result2.Node.NodeID)}
sort.Ints(ids)
if expected := []int{1, 2}; !reflect.DeepEqual(ids, expected) {
t.Errorf("Expected nodes %+v: %+v vs %+v", expected, result1.Node, result2.Node)
}
// Verify that no result is forthcoming if we already have a replica.
result3, err := a.AllocateTarget(
multiDCConfig.Constraints,
[]roachpb.ReplicaDescriptor{
{
NodeID: result1.Node.NodeID,
StoreID: result1.StoreID,
},
{
NodeID: result2.Node.NodeID,
StoreID: result2.StoreID,
},
},
firstRange,
)
if err == nil {
t.Errorf("expected error on allocation without available stores: %+v", result3)
}
}
func TestAllocatorExistingReplica(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, a, _ := createTestAllocator()
defer stopper.Stop()
gossiputil.NewStoreGossiper(g).GossipStores(sameDCStores, t)
result, err := a.AllocateTarget(
config.Constraints{
Constraints: []config.Constraint{
{Value: "a"},
{Value: "hdd"},
},
},
[]roachpb.ReplicaDescriptor{
{
NodeID: 2,
StoreID: 2,
},
},
firstRange,
)
if err != nil {
t.Fatalf("Unable to perform allocation: %v", err)
}
if result.Node.NodeID != 3 || result.StoreID != 4 {
t.Errorf("expected result to have node 3 and store 4: %+v", result)
}
}
// TestAllocatorRelaxConstraints verifies that attribute constraints
// will be relaxed in order to match nodes lacking required attributes,
// if necessary to find an allocation target.
func TestAllocatorRelaxConstraints(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, a, _ := createTestAllocator()
defer stopper.Stop()
gossiputil.NewStoreGossiper(g).GossipStores(multiDCStores, t)
testCases := []struct {
required []config.Constraint // attribute strings
existing []int // existing store/node ID
expID int // expected store/node ID on allocate
expErr bool
}{
// The two stores in the system have attributes:
// storeID=1 {"a", "ssd"}
// storeID=2 {"b", "ssd"}
{
[]config.Constraint{
{Value: "a"},
{Value: "ssd"},
},
[]int{}, 1, false,
},
{
[]config.Constraint{
{Value: "a"},
{Value: "ssd"},
},
[]int{1}, 2, false,
},
{
[]config.Constraint{
{Value: "a", Type: config.Constraint_REQUIRED},
{Value: "ssd", Type: config.Constraint_REQUIRED},
},
[]int{1}, 0, true,
},
{
[]config.Constraint{
{Value: "a"},
{Value: "ssd"},
},
[]int{1, 2}, 0, true,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "ssd"},
},
[]int{}, 2, false,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "ssd"},
},
[]int{1}, 2, false,
},
{
[]config.Constraint{
{Value: "b", Type: config.Constraint_REQUIRED},
{Value: "ssd", Type: config.Constraint_REQUIRED},
},
[]int{2}, 0, true,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "ssd"},
},
[]int{2}, 1, false,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "ssd"},
},
[]int{1, 2}, 0, true,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "hdd"},
},
[]int{}, 2, false,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "hdd"},
},
[]int{2}, 1, false,
},
{
[]config.Constraint{
{Value: "b", Type: config.Constraint_REQUIRED},
{Value: "hdd", Type: config.Constraint_REQUIRED},
},
[]int{2}, 0, true,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "hdd"},
},
[]int{1, 2}, 0, true,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "ssd"},
{Value: "gpu"},
},
[]int{}, 2, false,
},
{
[]config.Constraint{
{Value: "b"},
{Value: "hdd"},
{Value: "gpu"},
},
[]int{}, 2, false,
},
}
for i, test := range testCases {
var existing []roachpb.ReplicaDescriptor
for _, id := range test.existing {
existing = append(existing, roachpb.ReplicaDescriptor{NodeID: roachpb.NodeID(id), StoreID: roachpb.StoreID(id)})
}
constraints := config.Constraints{Constraints: test.required}
result, err := a.AllocateTarget(constraints, existing, firstRange)
if haveErr := (err != nil); haveErr != test.expErr {
t.Errorf("%d: expected error %t; got %t: %s", i, test.expErr, haveErr, err)
} else if err == nil && roachpb.StoreID(test.expID) != result.StoreID {
t.Errorf("%d: expected result to have store %d; got %+v", i, test.expID, result)
}
}
}
// TestAllocatorRebalance verifies that rebalance targets are chosen
// randomly from amongst stores over the minAvailCapacityThreshold.
func TestAllocatorRebalance(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, a, _ := createTestAllocator()
defer stopper.Stop()
stores := []*roachpb.StoreDescriptor{
{
StoreID: 1,
Node: roachpb.NodeDescriptor{NodeID: 1},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 100, RangeCount: 1},
},
{
StoreID: 2,
Node: roachpb.NodeDescriptor{NodeID: 2},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 50, RangeCount: 1},
},
{
StoreID: 3,
Node: roachpb.NodeDescriptor{NodeID: 3},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 100 - int64(100*maxFractionUsedThreshold),
RangeCount: 5,
},
},
{
// This store must not be rebalanced to, because it's too full.
StoreID: 4,
Node: roachpb.NodeDescriptor{NodeID: 4},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: (100 - int64(100*maxFractionUsedThreshold)) / 2,
RangeCount: 10,
},
},
{
// This store will not be rebalanced to, because it already has more
// replicas than the mean range count.
StoreID: 5,
Node: roachpb.NodeDescriptor{NodeID: 5},
Capacity: roachpb.StoreCapacity{
Capacity: 100,
Available: 100,
RangeCount: 10,
},
},
}
gossiputil.NewStoreGossiper(g).GossipStores(stores, t)
// Every rebalance target must be either stores 1 or 2.
for i := 0; i < 10; i++ {
result, err := a.RebalanceTarget(
config.Constraints{},
[]roachpb.ReplicaDescriptor{{StoreID: 3}},
noStore,
firstRange,
)
if err != nil {
t.Fatal(err)
}
if result == nil {
t.Fatal("nil result")
}
if result.StoreID != 1 && result.StoreID != 2 {
t.Errorf("%d: expected store 1 or 2; got %d", i, result.StoreID)
}
}
// Verify shouldRebalance results.
for i, store := range stores {
desc, ok := a.storePool.getStoreDescriptor(store.StoreID)
if !ok {
t.Fatalf("%d: unable to get store %d descriptor", i, store.StoreID)
}
sl, _, _ := a.storePool.getStoreList(firstRange)
result := a.shouldRebalance(desc, sl)
if expResult := (i >= 2); expResult != result {
t.Errorf("%d: expected rebalance %t; got %t", i, expResult, result)
}
}
}
// TestAllocatorRebalanceThrashing tests that the rebalancer does not thrash
// when replica counts are balanced, within the appropriate thresholds, across
// stores.
func TestAllocatorRebalanceThrashing(t *testing.T) {
defer leaktest.AfterTest(t)()
type testStore struct {
rangeCount int32
shouldRebalanceFrom bool
}
// Returns a slice of stores with the specified mean. The first replica will
// have a range count that's above the target range count for the rebalancer,
// so it should be rebalanced from.
oneStoreAboveRebalanceTarget := func(mean int32, numStores int) []testStore {
stores := make([]testStore, numStores)
for i := range stores {
stores[i].rangeCount = mean
}
surplus := int32(math.Ceil(float64(mean)*RebalanceThreshold + 1))
stores[0].rangeCount += surplus
stores[0].shouldRebalanceFrom = true
for i := 1; i < len(stores); i++ {
stores[i].rangeCount -= int32(math.Ceil(float64(surplus) / float64(len(stores)-1)))
}
return stores
}
// Returns a slice of stores with the specified mean such that the first store
// has few enough replicas to make it a rebalance target.
oneUnderusedStore := func(mean int32, numStores int) []testStore {
stores := make([]testStore, numStores)
for i := range stores {
stores[i].rangeCount = mean
}
// Subtract enough ranges from the first store to make it a suitable
// rebalance target. To maintain the specified mean, we then add that delta
// back to the rest of the replicas.
deficit := int32(math.Ceil(float64(mean)*RebalanceThreshold + 1))
stores[0].rangeCount -= deficit
for i := 1; i < len(stores); i++ {
stores[i].rangeCount += int32(math.Ceil(float64(deficit) / float64(len(stores)-1)))
stores[i].shouldRebalanceFrom = true
}
return stores
}
// Each test case defines the range counts for the test stores and whether we
// should rebalance from the store.
testCases := [][]testStore{
// An evenly balanced cluster should not rebalance.
{{5, false}, {5, false}, {5, false}, {5, false}},
// A very nearly balanced cluster should not rebalance.
{{5, false}, {5, false}, {5, false}, {6, false}},
// Adding an empty node to a 3-node cluster triggers rebalancing from
// existing nodes.
{{100, true}, {100, true}, {100, true}, {0, false}},
// A cluster where all range counts are within RebalanceThreshold should
// not rebalance. This assumes RebalanceThreshold > 2%.
{{98, false}, {99, false}, {101, false}, {102, false}},
// 5-nodes, each with a single store above the rebalancer target range
// count.
oneStoreAboveRebalanceTarget(100, 5),
oneStoreAboveRebalanceTarget(1000, 5),
oneStoreAboveRebalanceTarget(10000, 5),
oneUnderusedStore(1000, 5),
oneUnderusedStore(1000, 10),
}
for i, tc := range testCases {
t.Logf("test case %d: %v", i, tc)
// It doesn't make sense to test sets of stores containing fewer than 4
// stores, because 4 stores is the minimum number of stores needed to
// trigger rebalancing with the default replication factor of 3. Also, the
// above local functions need a minimum number of stores to properly create
// the desired distribution of range counts.
const minStores = 4
if numStores := len(tc); numStores < minStores {
t.Fatalf("%d: numStores %d < min %d", i, numStores, minStores)
}
stopper, g, _, a, _ := createTestAllocator()
defer stopper.Stop()
a.storePool.TestSetDeterministic(true)
// Create stores with the range counts from the test case and gossip them.
var stores []*roachpb.StoreDescriptor
for j, store := range tc {
stores = append(stores, &roachpb.StoreDescriptor{
StoreID: roachpb.StoreID(j + 1),
Node: roachpb.NodeDescriptor{NodeID: roachpb.NodeID(j + 1)},
Capacity: roachpb.StoreCapacity{Capacity: 1, Available: 1, RangeCount: store.rangeCount},
})
}
gossiputil.NewStoreGossiper(g).GossipStores(stores, t)
// Ensure gossiped store descriptor changes have propagated.
util.SucceedsSoon(t, func() error {
sl, _, _ := a.storePool.getStoreList(firstRange)
for j, s := range sl.stores {
if a, e := s.Capacity.RangeCount, tc[j].rangeCount; a != e {
return errors.Errorf("tc %d: range count for %d = %d != expected %d", i, j, a, e)
}
}
return nil
})
sl, _, _ := a.storePool.getStoreList(firstRange)
// Verify shouldRebalance returns the expected value.
for j, store := range stores {
desc, ok := a.storePool.getStoreDescriptor(store.StoreID)
if !ok {
t.Fatalf("[tc %d,store %d]: unable to get store %d descriptor", i, j, store.StoreID)
}
if a, e := a.shouldRebalance(desc, sl), tc[j].shouldRebalanceFrom; a != e {
t.Errorf("[tc %d,store %d]: shouldRebalance %t != expected %t", i, store.StoreID, a, e)
}
}
}
}
// TestAllocatorRebalanceByCount verifies that rebalance targets are
// chosen by range counts in the event that available capacities
// exceed the maxAvailCapacityThreshold.
func TestAllocatorRebalanceByCount(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, a, _ := createTestAllocator()
defer stopper.Stop()
// Setup the stores so that only one is below the standard deviation threshold.
stores := []*roachpb.StoreDescriptor{
{
StoreID: 1,
Node: roachpb.NodeDescriptor{NodeID: 1},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 100, RangeCount: 10},
},
{
StoreID: 2,
Node: roachpb.NodeDescriptor{NodeID: 2},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 99, RangeCount: 10},
},
{
StoreID: 3,
Node: roachpb.NodeDescriptor{NodeID: 3},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 98, RangeCount: 10},
},
{
StoreID: 4,
Node: roachpb.NodeDescriptor{NodeID: 4},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 98, RangeCount: 2},
},
}
gossiputil.NewStoreGossiper(g).GossipStores(stores, t)
// Every rebalance target must be store 4 (or nil for case of missing the only option).
for i := 0; i < 10; i++ {
result, err := a.RebalanceTarget(
config.Constraints{},
[]roachpb.ReplicaDescriptor{{StoreID: 1}},
stores[0].StoreID,
firstRange,
)
if err != nil {
t.Fatal(err)
}
if result != nil && result.StoreID != 4 {
t.Errorf("expected store 4; got %d", result.StoreID)
}
}
// Verify shouldRebalance results.
for i, store := range stores {
desc, ok := a.storePool.getStoreDescriptor(store.StoreID)
if !ok {
t.Fatalf("%d: unable to get store %d descriptor", i, store.StoreID)
}
sl, _, _ := a.storePool.getStoreList(firstRange)
result := a.shouldRebalance(desc, sl)
if expResult := (i < 3); expResult != result {
t.Errorf("%d: expected rebalance %t; got %t", i, expResult, result)
}
}
}
// TestAllocatorRemoveTarget verifies that the replica chosen by RemoveTarget is
// the one with the lowest capacity.
func TestAllocatorRemoveTarget(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, g, _, a, _ := createTestAllocator()
defer stopper.Stop()
// List of replicas that will be passed to RemoveTarget
replicas := []roachpb.ReplicaDescriptor{
{
StoreID: 1,
NodeID: 1,
ReplicaID: 1,
},
{
StoreID: 2,
NodeID: 2,
ReplicaID: 2,
},
{
StoreID: 3,
NodeID: 3,
ReplicaID: 3,
},
{
StoreID: 4,
NodeID: 4,
ReplicaID: 4,
},
}
// Setup the stores so that store 3 is the worst candidate and store 2 is
// the 2nd worst.
stores := []*roachpb.StoreDescriptor{
{
StoreID: 1,
Node: roachpb.NodeDescriptor{NodeID: 1},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 100, RangeCount: 10},
},
{
StoreID: 2,
Node: roachpb.NodeDescriptor{NodeID: 2},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 65, RangeCount: 12},
},
{
StoreID: 3,
Node: roachpb.NodeDescriptor{NodeID: 3},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 60, RangeCount: 15},
},
{
StoreID: 4,
Node: roachpb.NodeDescriptor{NodeID: 4},
Capacity: roachpb.StoreCapacity{Capacity: 100, Available: 65, RangeCount: 10},
},
}
sg := gossiputil.NewStoreGossiper(g)
sg.GossipStores(stores, t)
targetRepl, err := a.RemoveTarget(config.Constraints{}, replicas, stores[0].StoreID)
if err != nil {
t.Fatal(err)
}
if a, e := targetRepl, replicas[2]; a != e {
t.Fatalf("RemoveTarget did not select expected replica; expected %v, got %v", e, a)
}
// Now perform the same test, but pass in the store ID of store 3 so it's
// excluded.
targetRepl, err = a.RemoveTarget(config.Constraints{}, replicas, stores[2].StoreID)
if err != nil {
t.Fatal(err)
}
if a, e := targetRepl, replicas[1]; a != e {
t.Fatalf("RemoveTarget did not select expected replica; expected %v, got %v", e, a)
}
}
func TestAllocatorComputeAction(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper, _, sp, a, _ := createTestAllocator()
defer stopper.Stop()
// Set up eight stores. Stores six and seven are marked as dead. Replica eight
// is dead.
mockStorePool(sp,
[]roachpb.StoreID{1, 2, 3, 4, 5, 8},
[]roachpb.StoreID{6, 7},
[]roachpb.ReplicaIdent{{
RangeID: 0,
Replica: roachpb.ReplicaDescriptor{
NodeID: 8,
StoreID: 8,
ReplicaID: 8,
},
}})
// Each test case should describe a repair situation which has a lower
// priority than the previous test case.
testCases := []struct {
zone config.ZoneConfig
desc roachpb.RangeDescriptor
expectedAction AllocatorAction
}{
// Needs three replicas, two are on dead stores.
{
zone: config.ZoneConfig{
NumReplicas: 3,
Constraints: config.Constraints{Constraints: []config.Constraint{{Value: "us-east"}}},
RangeMinBytes: 0,
RangeMaxBytes: 64000,
},
desc: roachpb.RangeDescriptor{
Replicas: []roachpb.ReplicaDescriptor{
{
StoreID: 1,
NodeID: 1,
ReplicaID: 1,
},
{
StoreID: 7,
NodeID: 7,
ReplicaID: 7,
},
{
StoreID: 6,
NodeID: 6,
ReplicaID: 6,
},
},
},
expectedAction: AllocatorRemoveDead,
},
// Needs three replicas, one is on a dead store.
{
zone: config.ZoneConfig{
NumReplicas: 3,
Constraints: config.Constraints{Constraints: []config.Constraint{{Value: "us-east"}}},
RangeMinBytes: 0,
RangeMaxBytes: 64000,
},
desc: roachpb.RangeDescriptor{
Replicas: []roachpb.ReplicaDescriptor{
{
StoreID: 1,
NodeID: 1,
ReplicaID: 1,
},
{
StoreID: 2,
NodeID: 2,
ReplicaID: 2,
},
{
StoreID: 6,
NodeID: 6,
ReplicaID: 6,
},
},
},
expectedAction: AllocatorRemoveDead,
},
// Needs three replicas, one is dead.
{
zone: config.ZoneConfig{
NumReplicas: 3,
Constraints: config.Constraints{Constraints: []config.Constraint{{Value: "us-east"}}},
RangeMinBytes: 0,
RangeMaxBytes: 64000,
},
desc: roachpb.RangeDescriptor{
Replicas: []roachpb.ReplicaDescriptor{
{
StoreID: 1,
NodeID: 1,
ReplicaID: 1,
},
{
StoreID: 2,
NodeID: 2,
ReplicaID: 2,
},
{
StoreID: 8,
NodeID: 8,
ReplicaID: 8,
},
},
},
expectedAction: AllocatorRemoveDead,
},
// Needs five replicas, one is on a dead store.
{
zone: config.ZoneConfig{
NumReplicas: 3,
Constraints: config.Constraints{Constraints: []config.Constraint{{Value: "us-east"}}},
RangeMinBytes: 0,
RangeMaxBytes: 64000,
},
desc: roachpb.RangeDescriptor{
Replicas: []roachpb.ReplicaDescriptor{
{
StoreID: 1,
NodeID: 1,
ReplicaID: 1,
},
{
StoreID: 2,
NodeID: 2,
ReplicaID: 2,
},
{
StoreID: 3,
NodeID: 3,
ReplicaID: 3,
},
{
StoreID: 4,
NodeID: 4,
ReplicaID: 4,
},
{
StoreID: 6,
NodeID: 6,
ReplicaID: 6,
},
},
},
expectedAction: AllocatorRemoveDead,