-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
rand_gen.go
95 lines (83 loc) · 2.65 KB
/
rand_gen.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
// Copyright 2023 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package tests
import (
"math/rand"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/config"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/gen"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/state"
)
// randomClusterInfoGen randomly picks a predefined configuration.
func (f randTestingFramework) randomClusterInfoGen(randSource *rand.Rand) gen.LoadedCluster {
chosenIndex := randSource.Intn(len(state.ClusterOptions))
chosenType := state.ClusterOptions[chosenIndex]
return loadClusterInfo(chosenType)
}
type RandomizedBasicRanges struct {
gen.BaseRanges
placementType gen.PlacementType
randSource *rand.Rand
}
func NewRandomizedBasicRanges(
randSource *rand.Rand,
ranges int,
keySpace int,
placementType gen.PlacementType,
replicationFactor int,
bytes int64,
) RandomizedBasicRanges {
if placementType == gen.WeightedRandom {
// BETTER WARNING
panic("cannot use randomized basic ranges")
}
return RandomizedBasicRanges{
BaseRanges: gen.BaseRanges{
Ranges: ranges,
KeySpace: keySpace,
ReplicationFactor: replicationFactor,
Bytes: bytes,
},
placementType: placementType,
randSource: randSource,
}
}
var _ gen.RangeGen = &RandomizedBasicRanges{}
func (r RandomizedBasicRanges) Generate(
seed int64, settings *config.SimulationSettings, s state.State,
) state.State {
rangesInfo := r.GetRangesInfo(r.placementType, len(s.Stores()), r.randSource, []float64{})
r.LoadRangeInfo(s, rangesInfo)
return s
}
type WeightedRandomizedBasicRanges struct {
RandomizedBasicRanges
weightedRand []float64
}
var _ gen.RangeGen = &WeightedRandomizedBasicRanges{}
func NewWeightedRandomizedBasicRanges(
randSource *rand.Rand,
weightedRand []float64,
ranges int,
keySpace int,
replicationFactor int,
bytes int64,
) WeightedRandomizedBasicRanges {
return WeightedRandomizedBasicRanges{
RandomizedBasicRanges: NewRandomizedBasicRanges(randSource, ranges, keySpace, gen.WeightedRandom, replicationFactor, bytes),
weightedRand: weightedRand,
}
}
func (wr WeightedRandomizedBasicRanges) Generate(
seed int64, settings *config.SimulationSettings, s state.State,
) state.State {
rangesInfo := wr.GetRangesInfo(wr.placementType, len(s.Stores()), wr.randSource, wr.weightedRand)
wr.LoadRangeInfo(s, rangesInfo)
return s
}