-
Notifications
You must be signed in to change notification settings - Fork 55
/
brokers.go
490 lines (413 loc) · 11.3 KB
/
brokers.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package kafkazk
import (
"fmt"
"math/rand"
"sort"
)
const (
// StubBrokerID is the platform int max.
StubBrokerID int = int(^uint(0) >> 1)
)
// BrokerMetricsMap holds a mapping of broker
// ID to BrokerMetrics.
type BrokerMetricsMap map[int]*BrokerMetrics
// BrokerMetrics holds broker metric
// data fetched from ZK.
type BrokerMetrics struct {
StorageFree float64
}
// BrokerUseStats holds counts
// of partition ownership.
type BrokerUseStats struct {
ID int
Leader int
Follower int
}
// BrokerUseStatsList is a map of IDs to *BrokerUseStats.
type BrokerUseStatsMap map[int]*BrokerUseStats
// List returns a BrokerUseStatsList from a BrokerUseStatsMap.
func (b BrokerUseStatsMap) List() BrokerUseStatsList {
var l BrokerUseStatsList
for _, s := range b {
l = append(l, s)
}
sort.Sort(l)
return l
}
// BrokerUseStatsList is a slice of *BrokerUseStats.
type BrokerUseStatsList []*BrokerUseStats
func (b BrokerUseStatsList) Len() int { return len(b) }
func (b BrokerUseStatsList) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b BrokerUseStatsList) Less(i, j int) bool { return b[i].ID < b[j].ID }
// BrokerStatus summarizes change counts
// from an input and output broker list.
type BrokerStatus struct {
New int
Missing int
OldMissing int
Replace int
RackMissing int
}
// Changes returns a bool that indicates whether a
// BrokerStatus values represent a change in brokers.
func (bs BrokerStatus) Changes() bool {
switch {
case bs.New != 0, bs.Missing != 0, bs.OldMissing != 0, bs.Replace != 0, bs.RackMissing != 0:
return true
}
return false
}
// Broker associates metadata with a real broker by ID.
type Broker struct {
ID int
Locality string
Used int
StorageFree float64
Replace bool
Missing bool
New bool
}
// BrokerMap holds a mapping of broker IDs to *Broker.
type BrokerMap map[int]*Broker
// NewBrokerMap returns a new BrokerMap.
func NewBrokerMap() BrokerMap {
return BrokerMap{
StubBrokerID: &Broker{Used: 0, ID: StubBrokerID, Replace: true},
}
}
// BrokerList is a slice of brokers for sorting by used count.
type BrokerList []*Broker
// Wrapper types for sort by methods.
type brokersByCount BrokerList
type brokersByStorage BrokerList
type brokersByID BrokerList
// Satisfy the sort interface for BrokerList types.
// By used field value.
func (b brokersByCount) Len() int { return len(b) }
func (b brokersByCount) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b brokersByCount) Less(i, j int) bool {
if b[i].Used < b[j].Used {
return true
}
if b[i].Used > b[j].Used {
return false
}
return b[i].ID < b[j].ID
}
// By StorageFree value.
func (b brokersByStorage) Len() int { return len(b) }
func (b brokersByStorage) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b brokersByStorage) Less(i, j int) bool {
if b[i].StorageFree > b[j].StorageFree {
return true
}
if b[i].StorageFree < b[j].StorageFree {
return false
}
return b[i].ID < b[j].ID
}
// By ID value ascending.
func (b brokersByID) Len() int { return len(b) }
func (b brokersByID) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b brokersByID) Less(i, j int) bool { return b[i].ID < b[j].ID }
// Sort methods.
// SortByCount sorts the BrokerList by Used values.
func (b BrokerList) SortByCount() {
sort.Sort(brokersByCount(b))
}
// SortByStorage sorts the BrokerList by StorageFree values.
func (b BrokerList) SortByStorage() {
sort.Sort(brokersByStorage(b))
}
// SortByID sorts the BrokerList by ID values.
func (b BrokerList) SortByID() {
sort.Sort(brokersByID(b))
}
// SortByIDDesc sorts the BrokerList by ID values.
func (b BrokerList) SortByIDDesc() {
sort.Sort(sort.Reverse(brokersByID(b)))
}
// BrokerFilterFn is a filter function
// for BrokerList and BrokerMap types.
type BrokerFilterFn func(*Broker) bool
// AllBrokersFn returns all brokers.
var AllBrokersFn BrokerFilterFn = func(b *Broker) bool { return true }
var NotReplacedBrokersFn = func(b *Broker) bool { return !b.Replace }
var ReplacedBrokersFn = func(b *Broker) bool { return b.Replace }
// SortPseudoShuffle takes a BrokerList and performs a sort by count.
// For each sequence of brokers with equal counts, the sub-slice is
// pseudo random shuffled using the provided seed value s.
func (b BrokerList) SortPseudoShuffle(seed int64) {
sort.Sort(brokersByCount(b))
if len(b) <= 2 {
return
}
rand.Seed(seed)
s := 0
stop := len(b) - 1
currVal := b[0].Used
// For each continuous run of
// a given Used value, shuffle
// that range of the slice.
for k := range b {
switch {
case b[k].Used != currVal:
currVal = b[k].Used
rand.Shuffle(len(b[s:k]), func(i, j int) {
b[s:k][i], b[s:k][j] = b[s:k][j], b[s:k][i]
})
s = k
case k == stop:
rand.Shuffle(len(b[s:]), func(i, j int) {
b[s:][i], b[s:][j] = b[s:][j], b[s:][i]
})
}
}
}
// Update takes a []int of broker IDs and BrokerMetaMap then adds them to the
// BrokerMap, returning the count of marked for replacement, newly included,
// and brokers that weren't found in ZooKeeper. Additionally, a channel
// of msgs describing changes is returned.
func (b BrokerMap) Update(bl []int, bm BrokerMetaMap) (*BrokerStatus, <-chan string) {
bs := &BrokerStatus{}
msgs := make(chan string, (len(b)+len(bl)+len(bm))*4)
var includeAllExisting = false
var includeAllDiscovered = false
// Build a map from the provided broker list.
providedBrokers := map[int]bool{}
for _, broker := range bl {
switch {
// -1 is a placeholder that is substituted with
// all brokers already found in the BrokerMap.
case broker == -1:
includeAllExisting = true
continue
// -2 is a placeholder that is substituted with
// all brokers discovered in the cluster.
case broker == -2:
includeAllDiscovered = true
continue
}
providedBrokers[broker] = true
}
if includeAllExisting {
for id := range b {
providedBrokers[id] = true
}
}
if includeAllDiscovered {
for id := range bm {
providedBrokers[id] = true
}
}
// Do an initial pass on existing brokers
// and see if any are missing in ZooKeeper.
if len(bm) > 0 {
for id := range b {
// Skip the reserved ID.
if id == StubBrokerID {
continue
}
if _, exist := bm[id]; !exist {
msgs <- fmt.Sprintf("Previous broker %d missing", id)
b[id].Replace = true
b[id].Missing = true
// If this broker is missing and was provided in
// the broker list, consider it a "missing provided broker".
if _, ok := providedBrokers[id]; len(bm) > 0 && ok {
bs.Missing++
} else {
bs.OldMissing++
}
}
}
}
// Set the replace flag for existing brokers
// not in the provided broker map.
for _, broker := range b {
if broker.ID == StubBrokerID {
continue
}
if _, ok := providedBrokers[broker.ID]; !ok {
bs.Replace++
b[broker.ID].Replace = true
msgs <- fmt.Sprintf("Broker %d marked for removal", broker.ID)
}
}
// Merge provided brokers with existing brokers.
for id := range providedBrokers {
// Don't overwrite existing (which will be most brokers).
if b[id] == nil {
// Skip metadata lookups if
// meta is not being used.
if len(bm) == 0 {
b[id] = &Broker{
Used: 0,
ID: id,
Replace: false,
New: true,
}
bs.New++
continue
}
// Else check the broker against
// the broker metadata map.
if meta, exists := bm[id]; exists {
b[id] = &Broker{
Used: 0,
ID: id,
Replace: false,
Locality: meta.Rack,
StorageFree: meta.StorageFree,
New: true,
}
bs.New++
} else {
bs.Missing++
msgs <- fmt.Sprintf("Broker %d not found in ZooKeeper", id)
}
}
}
// Log new brokers.
for _, broker := range b {
if broker.New {
msgs <- fmt.Sprintf("New broker %d", broker.ID)
}
}
// Check if any brokers have missing rack.ids when brokermetada is available
if len(bm) > 0 {
for _, broker := range b {
if broker.ID == StubBrokerID {
continue
}
// ignore missing brokers not found in zk
if !b[broker.ID].Missing {
if broker.Locality == "" {
bs.RackMissing++
msgs <- fmt.Sprintf("Broker %d does not have a rack.id defined", broker.ID)
}
}
}
}
close(msgs)
return bs, msgs
}
// SubStorageAll takes a PartitionMap, PartitionMetaMap, and a function. For all
// brokers that return true as an input to function f, the size of all partitions
// held is added back to the broker StorageFree value.
func (b BrokerMap) SubStorage(pm *PartitionMap, pmm PartitionMetaMap, f BrokerFilterFn) error {
// Get the size of each partition.
for _, partn := range pm.Partitions {
size, err := pmm.Size(partn)
if err != nil {
return err
}
// Add this size back to the
// StorageFree for all mapped brokers.
for _, bid := range partn.Replicas {
if broker, exists := b[bid]; exists {
if f(broker) {
broker.StorageFree += size
}
} else {
return fmt.Errorf("Broker %d not found in broker map", bid)
}
}
}
return nil
}
// Filter returns a BrokerMap of brokers that return
// true as an input to function f.
func (b BrokerMap) Filter(f BrokerFilterFn) BrokerMap {
bm := BrokerMap{}
for _, broker := range b {
if broker.ID == StubBrokerID {
continue
}
if f(broker) {
bm[broker.ID] = broker
}
}
return bm
}
// Filter returns a BrokerList of brokers that return
// true as an input to function f.
func (b BrokerList) Filter(f BrokerFilterFn) BrokerList {
bl := BrokerList{}
for _, broker := range b {
if broker.ID == StubBrokerID {
continue
}
if f(broker) {
bl = append(bl, broker)
}
}
return bl
}
// List take a BrokerMap and returns a BrokerList.
func (b BrokerMap) List() BrokerList {
bl := BrokerList{}
for broker := range b {
bl = append(bl, b[broker])
}
return bl
}
// BrokerMapFromPartitionMap creates a BrokerMap from a partitionMap.
func BrokerMapFromPartitionMap(pm *PartitionMap, bm BrokerMetaMap, force bool) BrokerMap {
bmap := BrokerMap{}
// For each partition.
for _, partition := range pm.Partitions {
// For each broker in the
// partition replica set.
for _, id := range partition.Replicas {
// In offline partitions, the broker ID value
// gets set to -1. Skip these.
if id == -1 {
continue
}
// If the broker isn't in the
// broker map, add it.
if bmap[id] == nil {
// If we're doing a force rebuid, replace
// should be set to true.
bmap[id] = &Broker{Used: 0, ID: id, Replace: false}
}
// Track use scoring unless we're
// doing a force rebuild. In this case,
// we're treating existing brokers the same
// as new brokers (which start with a score of 0).
if !force {
bmap[id].Used++
}
// Add metadata if we have it.
if meta, exists := bm[id]; exists {
bmap[id].Locality = meta.Rack
bmap[id].StorageFree = meta.StorageFree
}
}
}
// Include the StubBrokerID.
bmap[StubBrokerID] = &Broker{Used: 0, ID: StubBrokerID, Replace: true}
return bmap
}
// Copy returns a copy of a BrokerMap.
func (b BrokerMap) Copy() BrokerMap {
c := BrokerMap{}
for id, br := range b {
copy := br.Copy()
c[id] = ©
}
return c
}
// Copy returns a copy of a Broker.
func (b Broker) Copy() Broker {
return Broker{
ID: b.ID,
Locality: b.Locality,
Used: b.Used,
StorageFree: b.StorageFree,
Replace: b.Replace,
Missing: b.Missing,
New: b.New,
}
}