-
Notifications
You must be signed in to change notification settings - Fork 21
/
topology.go
311 lines (261 loc) · 7.71 KB
/
topology.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
package command
import (
"flag"
"fmt"
"net"
"sort"
"strings"
"time"
"github.com/Shopify/sarama"
"github.com/funkygao/gafka/ctx"
"github.com/funkygao/gafka/zk"
"github.com/funkygao/gocli"
"github.com/funkygao/golib/color"
"github.com/funkygao/golib/gofmt"
)
type Topology struct {
Ui cli.Ui
Cmd string
zone string
cluster string
hostPattern string
verbose bool
watchMode bool
maxPort bool
}
func (this *Topology) Run(args []string) (exitCode int) {
cmdFlags := flag.NewFlagSet("topology", flag.ContinueOnError)
cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
cmdFlags.StringVar(&this.zone, "z", ctx.ZkDefaultZone(), "")
cmdFlags.StringVar(&this.cluster, "c", "", "")
cmdFlags.StringVar(&this.hostPattern, "host", "", "")
cmdFlags.BoolVar(&this.verbose, "l", false, "")
cmdFlags.BoolVar(&this.watchMode, "w", false, "")
cmdFlags.BoolVar(&this.maxPort, "maxport", false, "")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
// a single zone
ensureZoneValid(this.zone)
zkzone := zk.NewZkZone(zk.DefaultConfig(this.zone, ctx.ZoneZkAddrs(this.zone)))
for {
if this.maxPort {
this.displayZoneMaxPort(zkzone)
} else {
this.displayZoneTopology(zkzone)
}
if !this.watchMode {
return
}
time.Sleep(time.Second * 5)
refreshScreen()
}
return
}
type brokerHostInfo struct {
uptimes map[int]time.Time // key is broker tcp port
tcpPorts []int // listening tcp ports
topicPartitions map[string][]int32 // detailed leading topics info {topic: partitionIds}
topicMsgs map[string]int64
}
func newBrokerHostInfo() *brokerHostInfo {
return &brokerHostInfo{
tcpPorts: make([]int, 0),
uptimes: make(map[int]time.Time),
topicPartitions: make(map[string][]int32),
topicMsgs: make(map[string]int64),
}
}
func (this *brokerHostInfo) leadingPartitions() int {
r := 0
for _, p := range this.topicPartitions {
r += len(p)
}
return r
}
func (this *brokerHostInfo) totalMsgsInStock() int64 {
r := int64(0)
for _, n := range this.topicMsgs {
r += n
}
return r
}
func (this *brokerHostInfo) addPort(port int, uptime time.Time) {
this.tcpPorts = append(this.tcpPorts, port)
this.uptimes[port] = uptime
}
func (this *brokerHostInfo) addTopicPartition(topic string, partitionId int32) {
if _, present := this.topicPartitions[topic]; !present {
this.topicPartitions[topic] = []int32{partitionId}
} else {
this.topicPartitions[topic] = append(this.topicPartitions[topic], partitionId)
}
}
func (this *Topology) displayZoneMaxPort(zkzone *zk.ZkZone) {
maxPort := 0
zkzone.ForSortedBrokers(func(cluster string, liveBrokers map[string]*zk.BrokerZnode) {
for _, broker := range liveBrokers {
if maxPort < broker.Port {
maxPort = broker.Port
}
}
})
this.Ui.Output(fmt.Sprintf("max port in zone[%s]: %d", zkzone.Name(), maxPort))
}
func (this *Topology) displayZoneTopology(zkzone *zk.ZkZone) {
this.Ui.Output(zkzone.Name())
// {cluster: {topic: brokerHostInfo}}
brokerInstances := make(map[string]map[string]*brokerHostInfo)
zkzone.ForSortedBrokers(func(cluster string, liveBrokers map[string]*zk.BrokerZnode) {
if len(liveBrokers) == 0 {
this.Ui.Warn(fmt.Sprintf("empty brokers in cluster[%s]", cluster))
return
}
if this.cluster != "" && this.cluster != cluster {
return
}
brokerInstances[cluster] = make(map[string]*brokerHostInfo)
for _, broker := range liveBrokers {
if !patternMatched(broker.Host, this.hostPattern) {
continue
}
if _, present := brokerInstances[cluster][broker.Host]; !present {
brokerInstances[cluster][broker.Host] = newBrokerHostInfo()
}
brokerInstances[cluster][broker.Host].addPort(broker.Port, broker.Uptime())
}
// find how many partitions a broker is leading
zkcluster := zkzone.NewCluster(cluster)
brokerList := zkcluster.BrokerList()
if len(brokerList) == 0 {
this.Ui.Warn(fmt.Sprintf("empty brokers in cluster[%s]", cluster))
return
}
kfk, err := sarama.NewClient(brokerList, sarama.NewConfig())
if err != nil {
this.Ui.Error(color.Red(" %+v %s", brokerList, err.Error()))
return
}
topics, err := kfk.Topics()
swallow(err)
for _, topic := range topics {
partions, err := kfk.WritablePartitions(topic)
swallow(err)
for _, partitionID := range partions {
leader, err := kfk.Leader(topic, partitionID)
swallow(err)
host, _, err := net.SplitHostPort(leader.Addr())
swallow(err)
if !patternMatched(host, this.hostPattern) {
continue
}
latestOffset, err := kfk.GetOffset(topic, partitionID, sarama.OffsetNewest)
if err != nil {
this.Ui.Error(fmt.Sprintf("%s %s %v", cluster, topic, err))
continue
}
oldestOffset, err := kfk.GetOffset(topic, partitionID, sarama.OffsetOldest)
if err != nil {
this.Ui.Error(fmt.Sprintf("%s %s %v", cluster, topic, err))
continue
}
brokerInstances[cluster][host].topicMsgs[topic] += (latestOffset - oldestOffset)
brokerInstances[cluster][host].addTopicPartition(topic, partitionID)
}
}
})
hosts := make(map[string]struct{})
zkzone.ForSortedClusters(func(zkcluster *zk.ZkCluster) {
for host := range brokerInstances[zkcluster.Name()] {
hosts[host] = struct{}{}
}
})
sortedHosts := make([]string, 0)
for host := range hosts {
sortedHosts = append(sortedHosts, host)
}
sort.Strings(sortedHosts)
// sort by host ip
sortedClusters := make([]string, 0, len(brokerInstances))
for c := range brokerInstances {
sortedClusters = append(sortedClusters, c)
}
sort.Strings(sortedClusters)
portN := 0
hostN := 0
topicN := 0
partitionN := 0
for _, host := range sortedHosts {
tn := 0
pn := 0
mn := int64(0)
ports := make([]int, 0)
for _, cluster := range sortedClusters {
if _, present := brokerInstances[cluster][host]; !present {
continue
}
tn += len(brokerInstances[cluster][host].topicPartitions)
pn += brokerInstances[cluster][host].leadingPartitions()
mn += brokerInstances[cluster][host].totalMsgsInStock()
ports = append(ports, brokerInstances[cluster][host].tcpPorts...)
}
portN += len(ports)
topicN += tn
partitionN += pn
hostN += 1
this.Ui.Output(fmt.Sprintf(" %s leading: %2dT %3dP %15sM ports %2d:%+v",
color.Green("%15s", host),
tn,
pn,
gofmt.Comma(mn),
len(ports),
ports))
if this.verbose {
for _, cluster := range sortedClusters {
if _, present := brokerInstances[cluster][host]; !present {
continue
}
for _, tcpPort := range brokerInstances[cluster][host].tcpPorts {
this.Ui.Output(fmt.Sprintf("%40d %s", tcpPort,
gofmt.PrettySince(brokerInstances[cluster][host].uptimes[tcpPort])))
}
}
for _, cluster := range sortedClusters {
if _, present := brokerInstances[cluster][host]; !present {
continue
}
this.Ui.Output(color.Magenta("%30s", cluster))
for topic, partitions := range brokerInstances[cluster][host].topicPartitions {
this.Ui.Output(fmt.Sprintf("%40s: %15sM P%2d %+v",
topic,
gofmt.Comma(brokerInstances[cluster][host].topicMsgs[topic]),
len(partitions), partitions))
}
}
}
}
this.Ui.Output(fmt.Sprintf("%17s host:%d, topic:%d, partition:%d, instance:%d",
"-TOTAL-",
hostN, topicN, partitionN, portN))
}
func (*Topology) Synopsis() string {
return "Print server topology and balancing stats of kafka clusters"
}
func (this *Topology) Help() string {
help := fmt.Sprintf(`
Usage: %s topology -z zone [options]
%s
Options:
-z zone
-c cluster
-host host pattern
Display given hosts only.
-w
Run in watch mode: keep running till Ctrl^C.
-l
Use a long listing format.
-maxport
Display the max kafka broker tcp port in this zone.
`, this.Cmd, this.Synopsis())
return strings.TrimSpace(help)
}