-
Notifications
You must be signed in to change notification settings - Fork 6
/
codecs.go
108 lines (93 loc) · 2.66 KB
/
codecs.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"regexp"
"strconv"
"strings"
kazoo "github.com/wvanbergen/kazoo-go"
)
func GetPartitionListFromReader(in io.Reader, isJSON bool) (*PartitionList, error) {
pl := &PartitionList{}
if isJSON {
dec := json.NewDecoder(in)
err := dec.Decode(pl)
if err != nil {
return nil, fmt.Errorf("failed parsing json: %s", err)
}
if pl.Version != 1 {
return nil, fmt.Errorf("wrong partition list version: expected 1, got %d", pl.Version)
}
} else {
scanner := bufio.NewScanner(in)
re := regexp.MustCompile("^\tTopic: ([^\t]*)\tPartition: ([0-9]*)\tLeader: ([0-9]*)\tReplicas: ([0-9,]*)\tIsr: ([0-9,]*)")
for scanner.Scan() {
m := re.FindStringSubmatch(scanner.Text())
if m == nil {
continue
}
partition, _ := strconv.Atoi(m[2])
strreplicas := strings.Split(m[4], ",")
var replicas []BrokerID
for _, strreplica := range strreplicas {
replica, _ := strconv.Atoi(strreplica)
replicas = append(replicas, BrokerID(replica))
}
pl.Partitions = append(pl.Partitions, Partition{
Topic: TopicName(m[1]),
Partition: PartitionID(partition),
Replicas: replicas,
})
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("failed reading file: %s", err)
}
}
if len(pl.Partitions) == 0 {
return nil, fmt.Errorf("empty partition list")
}
return pl, nil
}
func WritePartitionList(out io.Writer, pl *PartitionList) error {
enc := json.NewEncoder(out)
pl.Version = 1
err := enc.Encode(pl)
if err != nil {
return fmt.Errorf("failed serializing json: %s", err)
}
return nil
}
func GetPartitionListFromZookeeper(zkConnStr string) (*PartitionList, error) {
zk, err := kazoo.NewKazooFromConnectionString(zkConnStr, nil)
if err != nil {
return nil, fmt.Errorf("failed parsing zk connection string: %v", err)
}
defer zk.Close()
pl := &PartitionList{}
topics, err := zk.Topics()
if err != nil {
return nil, fmt.Errorf("failed reading topic list from zk: %v", err)
}
for _, topic := range topics {
partitions, err := topic.Partitions()
if err != nil {
return nil, fmt.Errorf("failed reading partition list for topic %s from zk: %v", topic.Name, err)
}
for _, partition := range partitions {
replicas := make([]BrokerID, 0, len(partition.Replicas))
for _, replica := range partition.Replicas {
replicas = append(replicas, BrokerID(replica))
}
pl.Partitions = append(pl.Partitions, Partition{
Topic: TopicName(topic.Name),
Partition: PartitionID(partition.ID),
Replicas: replicas,
// NumConsumers: <number of consumer groups>,
// Weight: <number of messages> or <size of messages>,
})
}
}
return pl, nil
}