forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookupd.go
460 lines (417 loc) · 14 KB
/
lookupd.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
package lookupd
import (
"errors"
"fmt"
"log"
"net/url"
"sort"
"strings"
"sync"
"time"
"github.com/bitly/nsq/util"
"github.com/bitly/nsq/util/semver"
)
// GetLookupdTopics returns a []string containing a union of all the topics
// from all the given lookupd
func GetLookupdTopics(lookupdHTTPAddrs []string) ([]string, error) {
success := false
allTopics := make([]string, 0)
var lock sync.Mutex
var wg sync.WaitGroup
for _, addr := range lookupdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/topics", addr)
log.Printf("LOOKUPD: querying %s", endpoint)
go func(endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
// {"data":{"topics":["test"]}}
topics, _ := data.Get("topics").StringArray()
allTopics = util.StringUnion(allTopics, topics)
}(endpoint)
}
wg.Wait()
sort.Strings(allTopics)
if success == false {
return nil, errors.New("unable to query any lookupd")
}
return allTopics, nil
}
// GetLookupdTopicChannels returns a []string containing a union of the channels
// from all the given lookupd for the given topic
func GetLookupdTopicChannels(topic string, lookupdHTTPAddrs []string) ([]string, error) {
success := false
allChannels := make([]string, 0)
var lock sync.Mutex
var wg sync.WaitGroup
for _, addr := range lookupdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/channels?topic=%s", addr, url.QueryEscape(topic))
log.Printf("LOOKUPD: querying %s", endpoint)
go func(endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
// {"data":{"channels":["test"]}}
channels, _ := data.Get("channels").StringArray()
allChannels = util.StringUnion(allChannels, channels)
}(endpoint)
}
wg.Wait()
sort.Strings(allChannels)
if success == false {
return nil, errors.New("unable to query any lookupd")
}
return allChannels, nil
}
// GetLookupdProducers returns a slice of pointers to Producer structs
// containing metadata for each node connected to given lookupds
func GetLookupdProducers(lookupdHTTPAddrs []string) ([]*Producer, error) {
success := false
allProducers := make(map[string]*Producer, 0)
output := make([]*Producer, 0)
maxVersion, _ := semver.Parse("0.0.0")
var lock sync.Mutex
var wg sync.WaitGroup
for _, addr := range lookupdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/nodes", addr)
log.Printf("LOOKUPD: querying %s", endpoint)
go func(addr string, endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
producers := data.Get("producers")
producersArray, _ := producers.Array()
for i := range producersArray {
producer := producers.GetIndex(i)
remoteAddress := producer.Get("remote_address").MustString()
if remoteAddress == "" {
remoteAddress = "NA"
}
hostname := producer.Get("hostname").MustString()
broadcastAddress := producer.Get("broadcast_address").MustString()
httpPort := producer.Get("http_port").MustInt()
tcpPort := producer.Get("tcp_port").MustInt()
key := fmt.Sprintf("%s:%d:%d", broadcastAddress, httpPort, tcpPort)
p, ok := allProducers[key]
if !ok {
var tombstones []bool
var topics ProducerTopics
topicList, _ := producer.Get("topics").Array()
tombstoneList, err := producer.Get("tombstones").Array()
if err != nil {
// backwards compatibility with nsqlookupd < v0.2.22
tombstones = make([]bool, len(topicList))
} else {
for _, t := range tombstoneList {
tombstones = append(tombstones, t.(bool))
}
}
for i, t := range topicList {
topics = append(topics, ProducerTopic{
Topic: t.(string),
Tombstoned: tombstones[i],
})
}
sort.Sort(topics)
version := producer.Get("version").MustString("unknown")
versionObj, err := semver.Parse(version)
if err != nil {
versionObj = maxVersion
}
if maxVersion.Less(versionObj) {
maxVersion = versionObj
}
p = &Producer{
Hostname: hostname,
BroadcastAddress: broadcastAddress,
TcpPort: tcpPort,
HttpPort: httpPort,
Version: version,
VersionObj: versionObj,
Topics: topics,
}
allProducers[key] = p
output = append(output, p)
}
p.RemoteAddresses = append(p.RemoteAddresses, fmt.Sprintf("%s/%s", addr, remoteAddress))
}
}(addr, endpoint)
}
wg.Wait()
for _, producer := range allProducers {
if producer.VersionObj.Less(maxVersion) {
producer.OutOfDate = true
}
}
sort.Sort(ProducersByHost{output})
if success == false {
return nil, errors.New("unable to query any lookupd")
}
return output, nil
}
// GetLookupdTopicProducers returns a []string of the broadcast_address:http_port of all the
// producers for a given topic by unioning the results returned from the given lookupd
func GetLookupdTopicProducers(topic string, lookupdHTTPAddrs []string) ([]string, error) {
success := false
allSources := make([]string, 0)
var lock sync.Mutex
var wg sync.WaitGroup
for _, addr := range lookupdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/lookup?topic=%s", addr, url.QueryEscape(topic))
log.Printf("LOOKUPD: querying %s", endpoint)
go func(endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
producers := data.Get("producers")
producersArray, _ := producers.Array()
for i := range producersArray {
producer := producers.GetIndex(i)
broadcastAddress := producer.Get("broadcast_address").MustString()
httpPort := producer.Get("http_port").MustInt()
key := fmt.Sprintf("%s:%d", broadcastAddress, httpPort)
allSources = util.StringAdd(allSources, key)
}
}(endpoint)
}
wg.Wait()
if success == false {
return nil, errors.New("unable to query any lookupd")
}
return allSources, nil
}
// GetNSQDTopics returns a []string containing all the topics
// produced by the given nsqd
func GetNSQDTopics(nsqdHTTPAddrs []string) ([]string, error) {
topics := make([]string, 0)
var lock sync.Mutex
var wg sync.WaitGroup
success := false
for _, addr := range nsqdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/stats?format=json", addr)
log.Printf("NSQD: querying %s", endpoint)
go func(endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
topicList, _ := data.Get("topics").Array()
for i := range topicList {
topicInfo := data.Get("topics").GetIndex(i)
topics = util.StringAdd(topics, topicInfo.Get("topic_name").MustString())
}
}(endpoint)
}
wg.Wait()
sort.Strings(topics)
if success == false {
return nil, errors.New("unable to query any nsqd")
}
return topics, nil
}
// GetNSQDTopicProducers returns a []string containing the addresses of all the nsqd
// that produce the given topic out of the given nsqd
func GetNSQDTopicProducers(topic string, nsqdHTTPAddrs []string) ([]string, error) {
addresses := make([]string, 0)
var lock sync.Mutex
var wg sync.WaitGroup
success := false
for _, addr := range nsqdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/stats?format=json", addr)
log.Printf("NSQD: querying %s", endpoint)
go func(endpoint string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
topicList, _ := data.Get("topics").Array()
for i := range topicList {
topicInfo := data.Get("topics").GetIndex(i)
if topicInfo.Get("topic_name").MustString() == topic {
addresses = append(addresses, addr)
return
}
}
}(endpoint)
}
wg.Wait()
if success == false {
return nil, errors.New("unable to query any nsqd")
}
return addresses, nil
}
// GetNSQDStats returns aggregate topic and channel stats from the given NSQD instances
//
// if selectedTopic is empty, this will return stats for *all* topic/channels
// and the ChannelStats dict will be keyed by topic + ':' + channel
func GetNSQDStats(nsqdHTTPAddrs []string, selectedTopic string) ([]*TopicStats, map[string]*ChannelStats, error) {
var lock sync.Mutex
var wg sync.WaitGroup
topicStatsList := make(TopicStatsList, 0)
channelStatsMap := make(map[string]*ChannelStats)
success := false
for _, addr := range nsqdHTTPAddrs {
wg.Add(1)
endpoint := fmt.Sprintf("http://%s/stats?format=json", addr)
log.Printf("NSQD: querying %s", endpoint)
go func(endpoint string, addr string) {
data, err := util.ApiRequest(endpoint)
lock.Lock()
defer lock.Unlock()
defer wg.Done()
if err != nil {
log.Printf("ERROR: lookupd %s - %s", endpoint, err.Error())
return
}
success = true
topics, _ := data.Get("topics").Array()
for i := range topics {
t := data.Get("topics").GetIndex(i)
topicName := t.Get("topic_name").MustString()
if selectedTopic != "" && topicName != selectedTopic {
continue
}
depth := t.Get("depth").MustInt64()
backendDepth := t.Get("backend_depth").MustInt64()
channels := t.Get("channels").MustArray()
e2eProcessingLatency := util.E2eProcessingLatencyAggregateFromJson(t.Get("e2e_processing_latency"), topicName, "", addr)
topicStats := &TopicStats{
HostAddress: addr,
TopicName: topicName,
Depth: depth,
BackendDepth: backendDepth,
MemoryDepth: depth - backendDepth,
MessageCount: t.Get("message_count").MustInt64(),
ChannelCount: len(channels),
Paused: t.Get("paused").MustBool(),
E2eProcessingLatency: e2eProcessingLatency,
}
topicStatsList = append(topicStatsList, topicStats)
for j := range channels {
c := t.Get("channels").GetIndex(j)
channelName := c.Get("channel_name").MustString()
key := channelName
if selectedTopic == "" {
key = fmt.Sprintf("%s:%s", topicName, channelName)
}
channelStats, ok := channelStatsMap[key]
if !ok {
channelStats = &ChannelStats{
HostAddress: addr,
TopicName: topicName,
ChannelName: channelName,
}
channelStatsMap[key] = channelStats
}
depth := c.Get("depth").MustInt64()
backendDepth := c.Get("backend_depth").MustInt64()
clients := c.Get("clients").MustArray()
e2eProcessingLatency := util.E2eProcessingLatencyAggregateFromJson(c.Get("e2e_processing_latency"), topicName, channelName, addr)
hostChannelStats := &ChannelStats{
HostAddress: addr,
TopicName: topicName,
ChannelName: channelName,
Depth: depth,
BackendDepth: backendDepth,
MemoryDepth: depth - backendDepth,
Paused: c.Get("paused").MustBool(),
InFlightCount: c.Get("in_flight_count").MustInt64(),
DeferredCount: c.Get("deferred_count").MustInt64(),
MessageCount: c.Get("message_count").MustInt64(),
RequeueCount: c.Get("requeue_count").MustInt64(),
TimeoutCount: c.Get("timeout_count").MustInt64(),
E2eProcessingLatency: e2eProcessingLatency,
// TODO: this is sort of wrong; clients should be de-duped
// client A that connects to NSQD-a and NSQD-b should only be counted once. right?
ClientCount: len(clients),
}
channelStats.Add(hostChannelStats)
for k := range clients {
client := c.Get("clients").GetIndex(k)
connected := time.Unix(client.Get("connect_ts").MustInt64(), 0)
connectedDuration := time.Now().Sub(connected).Seconds()
clientId := client.Get("clientId").MustString()
if clientId == "" {
// TODO: deprecated, remove in 1.0
name := client.Get("name").MustString()
remoteAddressParts := strings.Split(client.Get("remote_address").MustString(), ":")
port := remoteAddressParts[len(remoteAddressParts)-1]
if len(remoteAddressParts) < 2 {
port = "NA"
}
clientId = fmt.Sprintf("%s:%s", name, port)
}
clientStats := &ClientStats{
HostAddress: addr,
Version: client.Get("version").MustString(),
ClientID: clientId,
Hostname: client.Get("hostname").MustString(),
UserAgent: client.Get("user_agent").MustString(),
ConnectedDuration: time.Duration(int64(connectedDuration)) * time.Second, // truncate to second
InFlightCount: client.Get("in_flight_count").MustInt(),
ReadyCount: client.Get("ready_count").MustInt(),
FinishCount: client.Get("finish_count").MustInt64(),
RequeueCount: client.Get("requeue_count").MustInt64(),
MessageCount: client.Get("message_count").MustInt64(),
SampleRate: int32(client.Get("sample_rate").MustInt()),
TLS: client.Get("tls").MustBool(),
Deflate: client.Get("deflate").MustBool(),
Snappy: client.Get("snappy").MustBool(),
}
hostChannelStats.Clients = append(hostChannelStats.Clients, clientStats)
channelStats.Clients = append(channelStats.Clients, clientStats)
}
sort.Sort(ClientsByHost{hostChannelStats.Clients})
sort.Sort(ClientsByHost{channelStats.Clients})
topicStats.Channels = append(topicStats.Channels, hostChannelStats)
}
}
sort.Sort(TopicStatsByHost{topicStatsList})
}(endpoint, addr)
}
wg.Wait()
if success == false {
return nil, nil, errors.New("unable to query any nsqd")
}
return topicStatsList, channelStatsMap, nil
}