-
Notifications
You must be signed in to change notification settings - Fork 55
/
stats.go
271 lines (215 loc) · 5.61 KB
/
stats.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
package kafkazk
import (
"math"
"sort"
)
// DegreeDistribution counts broker to broker relationships.
type DegreeDistribution struct {
// Relationships is a an adjacency list
// where an edge between brokers is defined as
// a common occupancy in at least one replica set.
// For instance, given the replica set [1001,1002,1003],
// ID 1002 has a relationship with 1001 and 1003.
Relationships map[int]map[int]struct{}
}
// NewDegreeDistribution returns a new DegreeDistribution.
func NewDegreeDistribution() DegreeDistribution {
return DegreeDistribution{
Relationships: make(map[int]map[int]struct{}),
}
}
// Add takes a []int of broker IDs representing a
// replica set and updates the adjacency lists for
// each broker in the set.
func (dd DegreeDistribution) Add(nodes []int) {
for _, node := range nodes {
if _, exists := dd.Relationships[node]; !exists {
dd.Relationships[node] = make(map[int]struct{})
}
for _, neighbor := range nodes {
if node != neighbor {
dd.Relationships[node][neighbor] = struct{}{}
}
}
}
}
// Count takes a node ID and returns the degree distribution.
func (dd DegreeDistribution) Count(n int) int {
c, exists := dd.Relationships[n]
if !exists {
return 0
}
return len(c)
}
// DegreeDistributionStats holds general statistical
// information describing the DegreeDistribution counts.
type DegreeDistributionStats struct {
Min float64
Max float64
Avg float64
}
// Stats returns a DegreeDistributionStats.
func (dd DegreeDistribution) Stats() DegreeDistributionStats {
dds := DegreeDistributionStats{}
if len(dd.Relationships) == 0 {
return dds
}
vals := []int{}
for node := range dd.Relationships {
vals = append(vals, dd.Count(node))
}
sort.Ints(vals)
var s int
for _, v := range vals {
s += v
}
dds.Min = float64(vals[0])
dds.Max = float64(vals[len(vals)-1])
dds.Avg = float64(s) / float64(len(vals))
return dds
}
// DegreeDistribution returns the DegreeDistribution for the PartitionMap.
func (pm *PartitionMap) DegreeDistribution() DegreeDistribution {
d := NewDegreeDistribution()
for _, partn := range pm.Partitions {
d.Add(partn.Replicas)
}
return d
}
// StorageDiff takes two BrokerMaps and returns a per broker ID
// diff in storage as a [2]float64: [absolute, percentage] diff.
func (b BrokerMap) StorageDiff(b2 BrokerMap) map[int][2]float64 {
d := map[int][2]float64{}
for bid := range b {
if bid == StubBrokerID {
continue
}
if _, exist := b2[bid]; !exist {
continue
}
diff := b2[bid].StorageFree - b[bid].StorageFree
p := diff / b[bid].StorageFree * 100
d[bid] = [2]float64{diff, p}
}
return d
}
// StorageRangeSpread returns the range spread
// of free storage for all brokers in the BrokerMap.
func (b BrokerMap) StorageRangeSpread() float64 {
l, h := b.MinMax()
// Return range spread.
return (h - l) / l * 100
}
// StorageRange returns the range of free
// storage for all brokers in the BrokerMap.
func (b BrokerMap) StorageRange() float64 {
l, h := b.MinMax()
// Return range.
return h - l
}
func (b BrokerMap) MinMax() (float64, float64) {
// Get the high/low StorageFree values.
h, l := 0.00, math.MaxFloat64
for id := range b {
if id == StubBrokerID {
continue
}
v := b[id].StorageFree
// Update the high/low.
if v > h {
h = v
}
if v < l {
l = v
}
}
return l, h
}
// StorageStdDev returns the standard deviation
// of free storage for all brokers in the BrokerMap.
func (b BrokerMap) StorageStdDev() float64 {
var m float64
var t float64
var s float64
var l float64
for id := range b {
if id == StubBrokerID {
continue
}
l++
t += b[id].StorageFree
}
m = t / l
for id := range b {
if id == StubBrokerID {
continue
}
s += math.Pow(m-b[id].StorageFree, 2)
}
msq := s / l
return math.Sqrt(msq)
}
// HMean returns the harmonic mean of broker storage free.
func (b BrokerMap) HMean() float64 {
var t float64
var c float64
for _, br := range b {
if br.ID != StubBrokerID && br.StorageFree > 0 {
c++
t += (1.00 / br.StorageFree)
}
}
return c / t
}
// Mean returns the arithmetic mean of broker storage free.
func (b BrokerMap) Mean() float64 {
var t float64
var c float64
for _, br := range b {
if br.ID != StubBrokerID && br.StorageFree > 0 {
c++
t += br.StorageFree
}
}
return t / c
}
// AboveMeanFn returns a BrokerFilterFn that filters brokers that are above the
// mean by d percent (0.00 < d). The mean type is provided as a function f.
func AboveMeanFn(d float64, f func() float64) BrokerFilterFn {
m := f()
return func(b *Broker) bool { return (b.StorageFree-m)/m > d }
}
// AboveMean returns a sorted []int of broker IDs that are above the mean
// by d percent (0.00 < d). The mean type is provided as a function f.
func (b BrokerMap) AboveMean(d float64, f func() float64) []int {
var ids []int
if d <= 0.00 {
return ids
}
filtered := b.Filter(AboveMeanFn(d, f))
for _, br := range filtered {
ids = append(ids, br.ID)
}
sort.Ints(ids)
return ids
}
// BelowMeanFn returns a BrokerFilterFn that filters brokers that are below the
// mean by d percent (0.00 < d). The mean type is provided as a function f.
func BelowMeanFn(d float64, f func() float64) BrokerFilterFn {
m := f()
return func(b *Broker) bool { return (m-b.StorageFree)/m > d }
}
// BelowMean returns a sorted []int of broker IDs that are below the mean
// by d percent (0.00 < d). The mean type is provided as a function f.
func (b BrokerMap) BelowMean(d float64, f func() float64) []int {
var ids []int
if d <= 0.00 {
return ids
}
filtered := b.Filter(BelowMeanFn(d, f))
for _, br := range filtered {
ids = append(ids, br.ID)
}
sort.Ints(ids)
return ids
}