-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
176 lines (145 loc) · 3.89 KB
/
utils.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
package main
import (
"fmt"
"sort"
)
type byBrokerID []BrokerID
func (a byBrokerID) Len() int { return len(a) }
func (a byBrokerID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byBrokerID) Less(i, j int) bool { return a[i] < a[j] }
type brokerLoad struct {
ID BrokerID
Load float64
}
type byBrokerLoad []brokerLoad
func (a byBrokerLoad) Len() int { return len(a) }
func (a byBrokerLoad) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byBrokerLoad) Less(i, j int) bool {
if a[i].Load != a[j].Load {
return a[i].Load < a[j].Load
}
return a[i].ID < a[j].ID
}
func toBrokerSet(brokers []BrokerID) map[BrokerID]struct{} {
b := make(map[BrokerID]struct{})
for _, id := range brokers {
b[id] = struct{}{}
}
return b
}
func inBrokerList(haystack []BrokerID, needle BrokerID) bool {
for _, b := range haystack {
if b == needle {
return true
}
}
return false
}
func getBrokerList(pl *PartitionList) []BrokerID {
b := make(map[BrokerID]struct{})
for _, p := range pl.Partitions {
for _, r := range p.Replicas {
b[r] = struct{}{}
}
}
var brokers []BrokerID
for id := range b {
brokers = append(brokers, id)
}
sort.Sort(byBrokerID(brokers))
return brokers
}
// get the list of brokers in order from least loaded to most loaded
func getBrokerListByLoad(loads map[BrokerID]float64, brokers []BrokerID) []BrokerID {
b := make([]brokerLoad, 0, len(brokers))
for _, id := range brokers {
b = append(b, brokerLoad{ID: id, Load: loads[id]})
}
sort.Sort(byBrokerLoad(b))
r := make([]BrokerID, 0, len(brokers))
for _, broker := range b {
r = append(r, broker.ID)
}
// add the allowed brokers we don't have the load of
for _, ID := range brokers {
if !inBrokerList(r, ID) {
r = append([]BrokerID{ID}, r...)
}
}
return r
}
// get the list of brokers in order from least loaded to most loaded
func getBrokerListByLoadBL(loads []brokerLoad, brokers []BrokerID) []BrokerID {
r := make([]BrokerID, 0, len(brokers))
for _, load := range loads {
if inBrokerList(brokers, load.ID) {
r = append(r, load.ID)
}
}
// add the allowed brokers we don't have the load of
for _, ID := range brokers {
if !inBrokerList(r, ID) {
r = append([]BrokerID{ID}, r...)
}
}
return r
}
func getBrokerLoad(pl *PartitionList) map[BrokerID]float64 {
b := make(map[BrokerID]float64)
for _, p := range pl.Partitions {
for idx, r := range p.Replicas {
if idx == 0 {
b[r] += p.Weight * float64(len(p.Replicas)+p.NumConsumers)
} else {
b[r] += p.Weight
}
}
}
return b
}
func getBL(loads map[BrokerID]float64) []brokerLoad {
// if we don't iterate in a constant order, float arithmetic causes the
// results to change in the LSBs
brokers := make([]brokerLoad, 0, len(loads))
for id, load := range loads {
brokers = append(brokers, brokerLoad{ID: id, Load: load})
}
sort.Sort(byBrokerLoad(brokers))
return brokers
}
func getUnbalanceBL(brokers []brokerLoad) float64 {
var sumBrokerLoad float64
for _, broker := range brokers {
sumBrokerLoad += broker.Load
}
avgBrokerLoad := sumBrokerLoad / float64(len(brokers))
var brokerUnbalance float64
for _, broker := range brokers {
relBrokerLoad := broker.Load/avgBrokerLoad - 1.0
brokerUnbalance += relBrokerLoad * relBrokerLoad
}
return brokerUnbalance
}
func emptypl() *PartitionList {
return &PartitionList{Version: 1}
}
func singlepl(p Partition) *PartitionList {
return &PartitionList{Version: 1, Partitions: []Partition{p}}
}
func replacepl(p Partition, orig BrokerID, repl BrokerID) *PartitionList {
for idx, id := range p.Replicas {
if id == orig {
if repl == -1 {
p.Replicas = append(p.Replicas[:idx], p.Replicas[idx+1:]...)
} else {
p.Replicas[idx] = repl
}
return singlepl(p)
}
}
panic(fmt.Sprintf("partition %v replicas don't contain %d", p, orig))
}
func addpl(p Partition, b BrokerID) *PartitionList {
p.Replicas = append(p.Replicas, b)
return singlepl(p)
}