Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsqd: panic on /stats when clients present #245

Merged
merged 1 commit into from
Aug 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions nsqd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,34 @@ func (n *NSQd) getStats() []TopicStats {
n.RLock()
defer n.RUnlock()

realTopics := make([]*Topic, len(n.topicMap))
topics := make([]TopicStats, len(n.topicMap))
topic_index := 0
realTopics := make([]*Topic, 0, len(n.topicMap))
for _, t := range n.topicMap {
realTopics[topic_index] = t
topic_index++
realTopics = append(realTopics, t)
}

sort.Sort(TopicsByName{realTopics})
for topic_index, t := range realTopics {

topics := make([]TopicStats, 0, len(n.topicMap))
for _, t := range realTopics {
t.RLock()

realChannels := make([]*Channel, len(t.channelMap))
channel_index := 0
realChannels := make([]*Channel, 0, len(t.channelMap))
for _, c := range t.channelMap {
realChannels[channel_index] = c
channel_index++
realChannels = append(realChannels, c)
}
sort.Sort(ChannelsByName{realChannels})

channels := make([]ChannelStats, len(t.channelMap))
for channel_index, c := range realChannels {
channels := make([]ChannelStats, 0, len(t.channelMap))
for _, c := range realChannels {
c.RLock()
clients := make([]ClientStats, len(c.clients))
for client_index, client := range c.clients {
clients[client_index] = client.Stats()
clients := make([]ClientStats, 0, len(c.clients))
for _, client := range c.clients {
clients = append(clients, client.Stats())
}
channels[channel_index] = NewChannelStats(c, clients)
channels = append(channels, NewChannelStats(c, clients))
c.RUnlock()
}

topics[topic_index] = NewTopicStats(t, channels)
topics = append(topics, NewTopicStats(t, channels))

t.RUnlock()
}
Expand Down
38 changes: 38 additions & 0 deletions nsqd/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"github.com/bitly/nsq/nsq"
"github.com/bmizerany/assert"
"io/ioutil"
"log"
"os"
"strconv"
"testing"
"time"
)

func TestStats(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)

options := NewNsqdOptions()
tcpAddr, _, nsqd := mustStartNSQd(options)
defer nsqd.Exit()

topicName := "test_stats" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, []byte("test body"))
topic.PutMessage(msg)

conn, err := mustConnectNSQd(tcpAddr)
assert.Equal(t, err, nil)

identify(t, conn)
sub(t, conn, topicName, "ch")

stats := nsqd.getStats()
assert.Equal(t, len(stats), 1)
assert.Equal(t, len(stats[0].Channels), 1)
assert.Equal(t, len(stats[0].Channels[0].Clients), 1)
log.Printf("stats: %+v", stats)
}