-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata_topics.go
266 lines (252 loc) · 6.83 KB
/
metadata_topics.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
package kafka
import (
"sort"
"github.com/Shopify/sarama"
"github.com/spf13/cast"
)
// TopicSummary contains a summary for a Topic.
type TopicSummary struct {
Topic string
Parts string
RFactor int
ISRs int
OfflineReplicas int
Partitions []int32
}
// TopicMeta contains detailed information for a Topic.
type TopicMeta struct {
Topic string
Partition int32
Leader int32
Replicas []int32
ISRs []int32
OfflineReplicas []int32
}
// TopicOffsetMap returns offset details for a Topic.
type TopicOffsetMap struct {
Topic string
TopicMeta []TopicMeta
PartitionOffsets map[int32]int64
PartitionOldest map[int32]int64
PartitionLeaders map[int32]int32
}
type partitionOffset struct {
topic string
partition int32
offset int64
oldest int64
}
// MakeTopicOffsetMap creates a TopicOffsetMap for the given TopicMeta.
func (kc *KClient) MakeTopicOffsetMap(topicMeta []TopicMeta) []TopicOffsetMap {
var TOM []TopicOffsetMap
parts := make(map[string][]int32)
tmMap := make(map[string][]TopicMeta)
for _, tm := range topicMeta {
parts[tm.Topic] = append(parts[tm.Topic], tm.Partition)
tmMap[tm.Topic] = append(tmMap[tm.Topic], tm)
}
tomChan := make(chan TopicOffsetMap, len(tmMap))
for topic := range tmMap {
tm := tmMap[topic]
pars := parts[topic]
go func(topic string, tMeta []TopicMeta, parts []int32) {
poMap := make(map[int32]int64)
oldMap := make(map[int32]int64)
poChan := make(chan partitionOffset, 10000)
for _, p := range parts {
go func(topic string, p int32) {
off, err := kc.GetOffsetNewest(topic, p)
if err != nil {
off = -7777
}
old, err := kc.GetOffsetOldest(topic, p)
if err != nil {
old = -7777
}
po := partitionOffset{
topic: topic,
partition: p,
offset: off,
oldest: old,
}
poChan <- po
}(topic, p)
}
for i := 0; i < len(parts); i++ {
po := <-poChan
poMap[po.partition] = po.offset
oldMap[po.partition] = po.oldest
}
pLdrMap := make(map[int32]int32, len(parts))
for _, tm := range tMeta {
pLdrMap[tm.Partition] = tm.Leader
}
tom := TopicOffsetMap{
Topic: topic,
TopicMeta: tmMap[topic],
PartitionOffsets: poMap,
PartitionOldest: oldMap,
PartitionLeaders: pLdrMap,
}
tomChan <- tom
}(topic, tm, pars)
}
for i := 0; i < len(tmMap); i++ {
tom := <-tomChan
TOM = append(TOM, tom)
}
return TOM
}
func (kc *KClient) makeTopicOffsetMap2(topicMeta []TopicMeta) []TopicOffsetMap {
var TOM []TopicOffsetMap
parts := make(map[string][]int32)
tmMap := make(map[string][]TopicMeta)
for _, tm := range topicMeta {
parts[tm.Topic] = append(parts[tm.Topic], tm.Partition)
tmMap[tm.Topic] = append(tmMap[tm.Topic], tm)
}
tomChan := make(chan TopicOffsetMap, len(tmMap))
for topic := range tmMap {
tm := tmMap[topic]
pars := parts[topic]
go func(topic string, tMeta []TopicMeta, parts []int32) {
poMap := make(map[int32]int64)
for _, p := range parts {
off, err := kc.GetOffsetNewest(topic, p)
if err != nil {
off = -7777
}
poMap[p] = off
}
pLdrMap := make(map[int32]int32, len(parts))
for _, tm := range tMeta {
pLdrMap[tm.Partition] = tm.Leader
}
tom := TopicOffsetMap{
Topic: topic,
TopicMeta: tmMap[topic],
PartitionOffsets: poMap,
PartitionLeaders: pLdrMap,
}
tomChan <- tom
}(topic, tm, pars)
}
for i := 0; i < len(tmMap); i++ {
tom := <-tomChan
TOM = append(TOM, tom)
}
return TOM
}
// GetTopicSummaries creates Topic Summaries for the give TopicMeta.
func GetTopicSummaries(topicMeta []TopicMeta) []TopicSummary {
var topicSummary []TopicSummary
parts := make(map[string][]int32, len(topicMeta))
isrs := make(map[string][]int32, len(topicMeta))
reps := make(map[string][]int32, len(topicMeta))
off := make(map[string][]int32, len(topicMeta))
done := make(map[string]bool)
for _, tm := range topicMeta {
parts[tm.Topic] = append(parts[tm.Topic], tm.Partition)
isrs[tm.Topic] = append(isrs[tm.Topic], tm.ISRs...)
reps[tm.Topic] = append(reps[tm.Topic], tm.Replicas...)
off[tm.Topic] = append(off[tm.Topic], tm.OfflineReplicas...)
}
for _, tm := range topicMeta {
if !done[tm.Topic] {
done[tm.Topic] = true
partitions := makeSeqStr(parts[tm.Topic])
ts := TopicSummary{
Topic: tm.Topic,
Parts: partitions,
RFactor: len(reps[tm.Topic]) / len(parts[tm.Topic]),
ISRs: len(isrs[tm.Topic]),
OfflineReplicas: len(off[tm.Topic]),
Partitions: parts[tm.Topic],
}
topicSummary = append(topicSummary, ts)
}
}
return topicSummary
}
// GetTopicMeta returns metadata for all topics.
func (kc *KClient) GetTopicMeta() ([]TopicMeta, error) {
var topicMeta []TopicMeta
res, err := kc.ReqMetadata()
if err != nil {
return topicMeta, err
}
if res.Topics != nil {
for _, t := range res.Topics {
topicName := t.Name
for _, x := range t.Partitions {
tm := TopicMeta{
Topic: topicName,
Partition: x.ID,
Leader: x.Leader,
Replicas: x.Replicas,
ISRs: x.Isr,
OfflineReplicas: x.OfflineReplicas,
}
topicMeta = append(topicMeta, tm)
}
}
}
return topicMeta, err
}
// ListTopics returns a list of Topics on a Kafka Cluster.
func (kc *KClient) ListTopics() ([]string, error) {
res, err := kc.ReqMetadata()
if err != nil {
return nil, err
}
var topics = make([]string, 0, len(res.Topics))
for _, t := range res.Topics {
topics = append(topics, t.Name)
}
return topics, nil
}
func makeSeqStr(nums []int32) string {
seqMap := make(map[int][]int32)
sort.Slice(nums, func(i, j int) bool {
return nums[i] < nums[j]
})
var mapCount int
var done int
var switchInt int
seqMap[mapCount] = append(seqMap[mapCount], nums[done])
done++
switchInt = done
for done < len(nums) {
if nums[done] == ((seqMap[mapCount][(switchInt - 1)]) + 1) {
seqMap[mapCount] = append(seqMap[mapCount], nums[done])
switchInt++
} else {
mapCount++
seqMap[mapCount] = append(seqMap[mapCount], nums[done])
switchInt = 1
}
done++
}
var seqStr string
for k, v := range seqMap {
if k > 0 {
seqStr += ","
}
if len(v) > 1 {
seqStr += cast.ToString(v[0])
seqStr += "-"
seqStr += cast.ToString(v[len(v)-1])
} else {
seqStr += cast.ToString(v[0])
}
}
return seqStr
}
// GetOffsetNewest returns the newest offset for a topic and partition.
func (kc *KClient) GetOffsetNewest(topic string, partition int32) (int64, error) {
return kc.cl.GetOffset(topic, partition, sarama.OffsetNewest)
}
// GetOffsetOldest returns the oldest offset for a topic and partition.
func (kc *KClient) GetOffsetOldest(topic string, partition int32) (int64, error) {
return kc.cl.GetOffset(topic, partition, sarama.OffsetOldest)
}