forked from grafana/cortex-tools
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
203 lines (170 loc) · 4.49 KB
/
main.go
File metadata and controls
203 lines (170 loc) · 4.49 KB
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
package main
import (
"fmt"
"math"
"math/rand"
"strings"
"github.com/gonum/stat"
)
const (
numTenants = 1000
numReplicas = 100
replicationFactor = 3
function = "linear"
)
var (
// Must be sorted by increasing avgSeries.
tenantsDistribution = []struct {
name string
percentage float64
avgSeries int // After replication
}{
{
name: "small",
percentage: 0.7,
avgSeries: 10000,
},
{
name: "medium",
percentage: 0.2,
avgSeries: 100000,
},
{
name: "large",
percentage: 0.1,
avgSeries: 1000000,
},
}
)
func main() {
// Print CSV header.
fmt.Printf(
"k, min series / replica, max series / replica, avg series / replica, std dev series, %% tenants affected by double node outage, setup = %s function / %d tenants / %s / %d replicas / %dx replication factor\n",
function, numTenants, tenantsDistributionToString(), numReplicas, replicationFactor)
switch function {
case "linear":
for k := 1000; k <= 100000; k += 1000 {
run(k, linear(k))
}
case "log":
for k := 1; k <= 100; k++ {
run(k, log(k))
}
}
}
func linear(k int) func(float64) int {
return func(series float64) int {
return int(math.Ceil(series / float64(k)))
}
}
func log(k int) func(float64) int {
return func(series float64) int {
return int(math.Ceil(math.Log(series) / float64(k)))
}
}
func run(k int, sizer func(float64) int) {
nodeSeries := make([]float64, numReplicas)
nodeTenants := make([][]int, numReplicas)
// Simulate the distribution of tenants across replicas.
tenantID := 0
for classIdx, class := range tenantsDistribution {
for n := 0; n < int(float64(numTenants)*class.percentage); n++ {
// Seed the pseudo-random generator with the tenant ID, so that different runs
// get the same number of series for the same tenant.
entropy := rand.New(rand.NewSource(int64(tenantID)))
// Get the average series of the previous class.
prevClassAvgSeries := 0
if classIdx > 0 {
prevClassAvgSeries = tenantsDistribution[classIdx-1].avgSeries
}
// Compute a random number of series for the tenant, ensuring it's greater than
// the previous class.
numSeries := float64(prevClassAvgSeries) + (entropy.ExpFloat64() * float64(class.avgSeries-prevClassAvgSeries))
shardSize := sizer(numSeries)
if shardSize > numReplicas {
shardSize = numReplicas
} else if shardSize < replicationFactor {
shardSize = replicationFactor
}
replicaIDs := shuffleShard(entropy, shardSize, numReplicas)
for _, replicaID := range replicaIDs {
nodeSeries[replicaID] += numSeries / float64(shardSize)
nodeTenants[replicaID] = append(nodeTenants[replicaID], tenantID)
}
tenantID++
}
}
// Count tenants affected by double node outage.
maxAffectedTenants := calculateMaxAffectedTenants(nodeTenants)
fmt.Printf("%d, %d, %d, %d, %f, %f\n",
k,
int(minFloat(nodeSeries)),
int(maxFloat(nodeSeries)),
int(stat.Mean(nodeSeries, nil)),
stat.StdDev(nodeSeries, nil),
float64(maxAffectedTenants)/float64(numTenants))
}
func calculateMaxAffectedTenants(nodeTenants [][]int) int {
maxAffectedTenants := 0
for i := 0; i < len(nodeTenants); i++ {
for j := 0; j < len(nodeTenants); j++ {
// Skip the same node.
if i == j {
continue
}
tenants := 0
for k := 0; k < len(nodeTenants[i]); k++ {
for l := 0; l < len(nodeTenants[j]); l++ {
if nodeTenants[i][k] == nodeTenants[j][l] {
tenants++
}
}
}
if tenants > maxAffectedTenants {
maxAffectedTenants = tenants
}
}
}
return maxAffectedTenants
}
func shuffleShard(entropy *rand.Rand, shardSize, numReplicas int) []int {
// Randomly pick shardSize different replicas.
replicas := map[int]struct{}{}
for len(replicas) < shardSize {
replicas[entropy.Intn(numReplicas)] = struct{}{}
}
// Build the list of replica IDs for this tenant.
ids := make([]int, 0, len(replicas))
for id := range replicas {
ids = append(ids, id)
}
return ids
}
func minFloat(fs []float64) float64 {
result := math.MaxFloat64
for _, f := range fs {
if f < result {
result = f
}
}
return result
}
func maxFloat(fs []float64) float64 {
result := 0.0
for _, f := range fs {
if f > result {
result = f
}
}
return result
}
func tenantsDistributionToString() string {
out := strings.Builder{}
for _, t := range tenantsDistribution {
if out.Len() > 0 {
out.WriteString(" + ")
}
out.WriteString(fmt.Sprintf("%.0f%% %s with %dK avg series", t.percentage*100, t.name, t.avgSeries/1000))
}
return out.String()
}