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

nsqadmin/nsqd: refactor stats querying #929

Merged

Conversation

andyxning
Copy link
Member

@andyxning andyxning commented Aug 16, 2017

As nsqd stats api supports format, topic and channel url parameters. When displaying info for a specific topic and channel, nsqadmin should query nsqd stats api with the specified topic and channel instead querying all channels under the specified topic and laterly filter out the specified channel stats.

Query stats on demand can reduce the time for nsqadmin api to return and will reduce the possibility that nsqadmin report timeout when to query a topic with many channels and each channel with many consumer clients.

/cc @mreiferson @ploxiln

@andyxning andyxning force-pushed the refactor_stats_qeury_for_nsqadmin branch from 1c4dc28 to 8a4ba22 Compare August 16, 2017 14:44
@andyxning andyxning force-pushed the refactor_stats_qeury_for_nsqadmin branch 3 times, most recently from 50027f4 to d0ae961 Compare August 17, 2017 15:05
@mreiferson mreiferson changed the title refactor stats query for nsqadmin nsqadmin/nsqd: refactor stats querying Aug 26, 2017
Copy link
Member

@mreiferson mreiferson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting this @andyxning, just a few minor comments.

Are there any other test additions we should make here?

endpoint := fmt.Sprintf("http://%s/stats?format=json", addr)

var endpoint string
if len(selectedChannel) != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't entirely correct, as it doesn't test for selectedTopic, let's just adopt the code in #925 wholesale for this block:

if selectedTopic == "" {
	endpoint = fmt.Sprintf("http://%s/stats?format=json", addr)
} else if selectChannel == "" {
	endpoint = fmt.Sprintf("http://%s/stats?topic=%s&format=json", addr, selectedTopic)
} else {
	endpoint = fmt.Sprintf("http://%s/stats?topic=%s&channel=%s&format=json", addr, selectedTopic, selectChannel)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me humbly offer my own variant here:

endpoint := fmt.Sprintf("http://%s/stats?format=json", addr)
if selectedTopic != "" {
        endpoint += "&topic=" + selectedTopic
        if selectedChannel != "" {
                endpoint += "&channel=" + selectedChannel
        }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member Author

@andyxning andyxning Aug 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review and the more simplified and clearer refactor. @ploxiln @mreiferson

nsqd/stats.go Outdated
for _, t := range n.topicMap {
realTopics = append(realTopics, t)
var realTopics []*Topic
if len(topic) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but In Go, this test should just be:

if topic == "" {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

nsqd/stats.go Outdated
}
} else {
realTopics = make([]*Topic, 0, 1)
realTopics = append(realTopics, n.topicMap[topic])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines can be simplified to:

realTopics = []*Topic{n.topicMap[topic]}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

nsqd/stats.go Outdated
for _, c := range t.channelMap {
realChannels = append(realChannels, c)
var realChannels []*Channel
if len(channel) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same idiomatic improvement here:

if channel == "" {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

nsqd/stats.go Outdated
}
} else {
realChannels = make([]*Channel, 0, 1)
realChannels = append(realChannels, t.channelMap[channel])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And same simplification here:

realChannels = []*Channel{t.channelMap[channel]}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

@andyxning andyxning force-pushed the refactor_stats_qeury_for_nsqadmin branch from d0ae961 to 1ab3405 Compare August 29, 2017 02:29
@andyxning
Copy link
Member Author

@mreiferson @ploxiln Comments addressed. PTAL.

@@ -32,7 +32,7 @@ func TestStats(t *testing.T) {
identify(t, conn, nil, frameTypeResponse)
sub(t, conn, topicName, "ch")

stats := nsqd.GetStats()
stats := nsqd.GetStats(topicName, "ch")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's probably a good idea to expand this test a bit - put a message to one other topic and channel

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable. Done.

@ploxiln
Copy link
Member

ploxiln commented Aug 29, 2017

looking pretty good to me

@andyxning andyxning force-pushed the refactor_stats_qeury_for_nsqadmin branch from 1ab3405 to bb8fc4c Compare August 29, 2017 03:56
@andyxning
Copy link
Member Author

@ploxiln Comments addressed. PTAL.

nsqd/stats.go Outdated
realTopics = append(realTopics, t)
}
} else {
realTopics = []*Topic{n.topicMap[topic]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized we're not checking if the topic exists in topicMap, can you add that check here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mreiferson Good catch! I also have enhanced the test for both of none exist topic and channel.

PTAL.

nsqd/stats.go Outdated
realChannels = append(realChannels, c)
}
} else {
realChannels = []*Channel{t.channelMap[channel]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, need to check if channel is in channelMap first

@andyxning andyxning force-pushed the refactor_stats_qeury_for_nsqadmin branch 3 times, most recently from 1bd8e4a to bf589db Compare August 30, 2017 13:49
@mreiferson mreiferson force-pushed the refactor_stats_qeury_for_nsqadmin branch from bf589db to 9fe3205 Compare September 1, 2017 15:47
@mreiferson
Copy link
Member

@andyxning fixed up the commits, LGTM, thanks!

@mreiferson mreiferson merged commit 4f80051 into nsqio:master Sep 1, 2017
@andyxning andyxning deleted the refactor_stats_qeury_for_nsqadmin branch September 1, 2017 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants