-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsteps.go
226 lines (185 loc) · 5.42 KB
/
steps.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
package main
import "fmt"
// ValidateWeights make sure that either all partitions have an explicit,
// strictly positive weight or that all partitions have no weight
func ValidateWeights(pl *PartitionList, _ RebalanceConfig) (*PartitionList, error) {
hasWeights := pl.Partitions[0].Weight != 0
for _, p := range pl.Partitions {
if hasWeights && p.Weight == 0 {
return nil, fmt.Errorf("partition %v has no weight", p)
}
if !hasWeights && p.Weight != 0 {
return nil, fmt.Errorf("partition %v has no weight", pl.Partitions[0])
}
if p.Weight < 0 {
return nil, fmt.Errorf("partition %v has negative weight", p)
}
}
return nil, nil
}
// ValidateReplicas checks that partitions don't have more than one replica per
// broker
func ValidateReplicas(pl *PartitionList, _ RebalanceConfig) (*PartitionList, error) {
for _, p := range pl.Partitions {
replicaset := toBrokerSet(p.Replicas)
if len(replicaset) != len(p.Replicas) {
return nil, fmt.Errorf("partition %v has duplicated replicas", p)
}
}
return nil, nil
}
// FillDefaults fills in default values for Weight, Brokers and NumReplicas
func FillDefaults(pl *PartitionList, cfg RebalanceConfig) (*PartitionList, error) {
// if the weights are 0, set them to 1
if pl.Partitions[0].Weight == 0 {
for idx := range pl.Partitions {
pl.Partitions[idx].Weight = 1.0
}
}
// if the set of candidate brokers is empty, fill it with the default set
brokers := cfg.Brokers
if brokers == nil {
brokers = getBrokerList(pl)
}
for idx := range pl.Partitions {
if pl.Partitions[idx].Brokers == nil {
pl.Partitions[idx].Brokers = brokers
}
}
// if the desired number of replicas is 0, fill it with the current number
for idx := range pl.Partitions {
if pl.Partitions[idx].NumReplicas == 0 {
pl.Partitions[idx].NumReplicas = len(pl.Partitions[idx].Replicas)
}
}
return nil, nil
}
// RemoveExtraReplicas removes replicas from partitions having lower NumReplicas
// than the current number of replicas
func RemoveExtraReplicas(pl *PartitionList, _ RebalanceConfig) (*PartitionList, error) {
loads := getBrokerLoad(pl)
for _, p := range pl.Partitions {
if p.NumReplicas >= len(p.Replicas) {
continue
}
brokersByLoad := getBrokerListByLoad(loads, p.Brokers)
for _, b := range brokersByLoad {
if inBrokerList(p.Replicas, b) {
return replacepl(p, b, -1), nil
}
}
return nil, fmt.Errorf("partition %v unable to pick replica to remove", p)
}
return nil, nil
}
// AddMissingReplicas adds replicas to partitions having NumReplicas greater
// than the current number of replicas
func AddMissingReplicas(pl *PartitionList, _ RebalanceConfig) (*PartitionList, error) {
loads := getBrokerLoad(pl)
// add missing replicas
for _, p := range pl.Partitions {
if p.NumReplicas <= len(p.Replicas) {
continue
}
brokersByLoad := getBrokerListByLoad(loads, p.Brokers)
for idx := len(brokersByLoad) - 1; idx >= 0; idx-- {
b := brokersByLoad[idx]
if !inBrokerList(p.Replicas, b) {
return addpl(p, b), nil
}
}
return nil, fmt.Errorf("partition %v unable to pick replica to add", p)
}
return nil, nil
}
// MoveDisallowedReplicas moves replicas from non-allowed brokers to the least
// loaded ones
func MoveDisallowedReplicas(pl *PartitionList, cfg RebalanceConfig) (*PartitionList, error) {
loads := getBrokerLoad(pl)
bl := getBL(loads)
for _, p := range pl.Partitions {
brokersByLoad := getBrokerListByLoadBL(bl, p.Brokers)
for _, id := range p.Replicas {
if inBrokerList(brokersByLoad, id) {
continue
}
for _, b := range brokersByLoad {
if inBrokerList(p.Replicas, b) {
continue
}
return replacepl(p, id, b), nil
}
return nil, fmt.Errorf("partition %v unable to pick replica to replace broker %d", p, id)
}
}
return nil, nil
}
func move(pl *PartitionList, cfg RebalanceConfig, leaders bool) (*PartitionList, error) {
var cp Partition
var cr, cb BrokerID
loads := getBrokerLoad(pl)
for _, id := range cfg.Brokers {
if _, found := loads[id]; !found {
loads[id] = 0
}
}
bl := getBL(loads)
su := getUnbalanceBL(bl)
cu := su
for _, p := range pl.Partitions {
if p.NumReplicas < cfg.MinReplicasForRebalancing {
continue
}
replicas := p.Replicas[1:]
if leaders {
replicas = p.Replicas[0:1]
}
for _, r := range replicas {
ridx := -1
var rload float64
for idx, b := range bl {
if b.ID == r {
ridx = idx
rload = b.Load
bl[idx].Load -= p.Weight
}
}
if ridx == -1 {
return nil, fmt.Errorf("assertion failed: replica %d not in broker loads %v", r, bl)
}
for idx, b := range bl {
if !inBrokerList(p.Brokers, b.ID) {
continue
}
if inBrokerList(p.Replicas, b.ID) {
continue
}
bload := bl[idx].Load
bl[idx].Load += p.Weight
u := getUnbalanceBL(bl)
if u < cu {
cu, cp, cr, cb = u, p, r, b.ID
}
bl[idx].Load = bload
}
bl[ridx].Load = rload
}
}
if cu < su-cfg.MinUnbalance {
return replacepl(cp, cr, cb), nil
}
return nil, nil
}
// MoveNonLeaders moves non-leader replicas from overloaded brokers to
// underloaded brokers
func MoveNonLeaders(pl *PartitionList, cfg RebalanceConfig) (*PartitionList, error) {
return move(pl, cfg, false)
}
// MoveLeaders moves leader replicas from overloaded brokers to underloaded
// brokers
func MoveLeaders(pl *PartitionList, cfg RebalanceConfig) (*PartitionList, error) {
if !cfg.AllowLeaderRebalancing {
return nil, nil
}
return move(pl, cfg, true)
}