forked from wvanbergen/kafka
/
utils.go
97 lines (80 loc) · 2.05 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
package consumergroup
import (
"crypto/rand"
"fmt"
"io"
"os"
"sort"
"github.com/wvanbergen/kazoo-go"
)
// Divides a set of partitions between a set of consumers.
func dividePartitionsBetweenConsumers(consumers kazoo.ConsumergroupInstanceList, partitions partitionLeaders) map[string][]*kazoo.Partition {
result := make(map[string][]*kazoo.Partition)
plen := len(partitions)
clen := len(consumers)
if clen == 0 {
return result
}
sort.Sort(partitions)
sort.Sort(consumers)
n := plen / clen
m := plen % clen
p := 0
for i, consumer := range consumers {
first := p
last := first + n
if m > 0 && i < m {
last++
}
if last > plen {
last = plen
}
for _, pl := range partitions[first:last] {
result[consumer.ID] = append(result[consumer.ID], pl.partition)
}
p = last
}
return result
}
type partitionLeader struct {
id int32
leader int32
partition *kazoo.Partition
}
// A sortable slice of PartitionLeader structs
type partitionLeaders []partitionLeader
func (pls partitionLeaders) Len() int {
return len(pls)
}
func (pls partitionLeaders) Less(i, j int) bool {
return pls[i].leader < pls[j].leader || (pls[i].leader == pls[j].leader && pls[i].id < pls[j].id)
}
func (s partitionLeaders) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// generateUUID Generates a UUIDv4.
func generateUUID() (string, error) {
uuid := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, uuid)
if n != len(uuid) || err != nil {
return "", err
}
// variant bits; see section 4.1.1
uuid[8] = uuid[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
uuid[6] = uuid[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}
// generateConsumerInstanceID generates a consumergroup Instance ID
// that is almost certain to be unique.
func generateConsumerInstanceID() (string, error) {
uuid, err := generateUUID()
if err != nil {
return "", err
}
hostname, err := os.Hostname()
if err != nil {
return "", err
}
return fmt.Sprintf("%s:%s", hostname, uuid), nil
}