6
6
package state
7
7
8
8
import (
9
+ "fmt"
9
10
"math/rand"
11
+ "strings"
10
12
"testing"
11
13
"time"
12
14
@@ -15,6 +17,9 @@ import (
15
17
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb"
16
18
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/load"
17
19
"github.com/cockroachdb/cockroach/pkg/roachpb"
20
+ "github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
21
+ "github.com/cockroachdb/cockroach/pkg/testutils/echotest"
22
+ "github.com/cockroachdb/cockroach/pkg/util/leaktest"
18
23
"github.com/stretchr/testify/require"
19
24
)
20
25
@@ -414,53 +419,6 @@ func TestOrderedStateLists(t *testing.T) {
414
419
s = NewStateWeightedRandDistribution (defaultSeed , []float64 {0.0 , 0.1 , 0.3 , 0.6 }, 1400 , 10000 , 3 , settings )
415
420
assertListsOrdered (s )
416
421
}
417
- func TestSkewedDistribution (t * testing.T ) {
418
- rangeInfo := RangesInfoSkewedDistribution (
419
- 6 /*stores*/ , 100 /*ranges*/ , 1 /*minKey*/ , 10000 /*maxKey*/ , 3 /*replicationFactor*/ , 10000 /*rangeSize*/ )
420
- expectedStoreReplicas := map [roachpb.StoreID ]int {
421
- 1 : 100 ,
422
- 2 : 87 ,
423
- 3 : 49 ,
424
- 4 : 30 ,
425
- 5 : 20 ,
426
- 6 : 14 ,
427
- }
428
-
429
- totalReplicas := 0
430
- stores := map [roachpb.StoreID ]int {}
431
- for _ , rng := range rangeInfo {
432
- for _ , repl := range rng .Descriptor .InternalReplicas {
433
- stores [repl .StoreID ]++
434
- totalReplicas ++
435
- }
436
- }
437
- require .Equal (t , 300 , totalReplicas )
438
- require .Equal (t , expectedStoreReplicas , stores )
439
- require .Equal (t , 6 , len (stores ))
440
- }
441
- func TestEvenDistribution (t * testing.T ) {
442
- rangeInfo := RangesInfoEvenDistribution (
443
- 6 /*stores*/ , 100 /*ranges*/ , 1 /*minKey*/ , 10000 /*maxKey*/ , 3 /*replicationFactor*/ , 10000 /*rangeSize*/ )
444
- expectedStoreReplicas := map [roachpb.StoreID ]int {
445
- 1 : 50 ,
446
- 2 : 50 ,
447
- 3 : 50 ,
448
- 4 : 50 ,
449
- 5 : 50 ,
450
- 6 : 50 ,
451
- }
452
- totalReplicas := 0
453
- stores := map [roachpb.StoreID ]int {}
454
- for _ , rng := range rangeInfo {
455
- for _ , repl := range rng .Descriptor .InternalReplicas {
456
- stores [repl .StoreID ]++
457
- totalReplicas ++
458
- }
459
- }
460
- require .Equal (t , 300 , totalReplicas )
461
- require .Equal (t , expectedStoreReplicas , stores )
462
- require .Equal (t , 6 , len (stores ))
463
- }
464
422
465
423
// TestNewStateDeterministic asserts that the state returned from the new state
466
424
// utility functions is deterministic.
@@ -834,3 +792,73 @@ func TestCapacityOverride(t *testing.T) {
834
792
// reason.
835
793
require .Equal (t , 500.0 , capacity .WritesPerSecond )
836
794
}
795
+
796
+ // TestDistribution tests the distribution helper functions. The invariants
797
+ // are that the distributions sum to 1.0 and that the distribution is
798
+ // expected.
799
+ func TestDistribution (t * testing.T ) {
800
+ defer leaktest .AfterTest (t )()
801
+
802
+ sum := func (values []float64 ) float64 {
803
+ total := 0.0
804
+ for _ , v := range values {
805
+ total += v
806
+ }
807
+ return total
808
+ }
809
+
810
+ const seed = 42
811
+ randSource := rand .New (rand .NewSource (seed ))
812
+
813
+ testCases := []struct {
814
+ numStores int
815
+ fns []struct {
816
+ name string
817
+ fn func () []float64
818
+ }
819
+ }{
820
+ {
821
+ numStores : 3 ,
822
+ fns : []struct {
823
+ name string
824
+ fn func () []float64
825
+ }{
826
+ {name : "even" , fn : func () []float64 { return evenDistribution (3 ) }},
827
+ {name : "skewed" , fn : func () []float64 { return skewedDistribution (3 ) }},
828
+ {name : "exact" , fn : func () []float64 { return exactDistribution ([]int {1 , 1 , 1 }) }},
829
+ {name : "weighted_rand" , fn : func () []float64 {
830
+ return weightedRandDistribution (randSource , []float64 {0.6 , 0.2 , 0.2 })
831
+ }},
832
+ {name : "rand" , fn : func () []float64 { return randDistribution (randSource , 3 ) }},
833
+ },
834
+ },
835
+ {
836
+ numStores : 10 ,
837
+ fns : []struct {
838
+ name string
839
+ fn func () []float64
840
+ }{
841
+ {name : "even" , fn : func () []float64 { return evenDistribution (10 ) }},
842
+ {name : "skewed" , fn : func () []float64 { return skewedDistribution (10 ) }},
843
+ {name : "exact" , fn : func () []float64 { return exactDistribution ([]int {2 , 2 , 2 , 2 , 2 , 1 , 1 , 1 , 1 , 1 }) }},
844
+ {name : "weighted_rand" , fn : func () []float64 {
845
+ return weightedRandDistribution (randSource , []float64 {0.5 , 0.1 , 0.05 , 0.05 , 0.05 , 0.05 , 0.05 , 0.05 , 0.05 , 0.05 })
846
+ }},
847
+ {name : "rand" , fn : func () []float64 { return randDistribution (randSource , 10 ) }},
848
+ },
849
+ },
850
+ }
851
+ w := echotest .NewWalker (t , datapathutils .TestDataPath (t , "echotest" ))
852
+ for _ , testCase := range testCases {
853
+ t .Run (fmt .Sprintf ("%d_stores" , testCase .numStores ), func (t * testing.T ) {
854
+ t .Run ("distribution" , w .Run (t , fmt .Sprintf ("%d_stores" , testCase .numStores ), func (t * testing.T ) string {
855
+ var str strings.Builder
856
+ for _ , fn := range testCase .fns {
857
+ dist := fn .fn ()
858
+ str .WriteString (fmt .Sprintf ("[%s: %.2f, sum: %.2f]\n " , fn .name , dist , sum (dist )))
859
+ }
860
+ return str .String ()
861
+ }))
862
+ })
863
+ }
864
+ }
0 commit comments