Skip to content

Commit

Permalink
nsqd: fix new topic channel lookup/creation
Browse files Browse the repository at this point in the history
when an nsqd first sees a topic queries nsqlookupd to identify
any channels that the cluster might know about in order
to proactively create them (in the case where there is more than one
channel this prevents data loss due to timing of consumers subscribing).

in nsqio#176 a regression was introduced that prevent the topic's messagePump
state from being updated in the code path described above.  This meant
that an nsqd that identified channels during creation of a topic would
not begin writing those messages into its channels.
  • Loading branch information
mreiferson committed Jun 6, 2013
1 parent d2cd54e commit ee31e04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion nsqd/nsqd.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ func (n *NSQd) GetTopic(topicName string) *Topic {
// release our global nsqd lock, and switch to a more granular topic lock while we init our
// channels from lookupd. This blocks concurrent PutMessages to this topic.
t.Lock()
defer t.Unlock()
n.Unlock()
// if using lookupd, make a blocking call to get the topics, and immediately create them.
// this makes sure that any message received is buffered to the right channels
Expand All @@ -285,6 +284,18 @@ func (n *NSQd) GetTopic(topicName string) *Topic {
t.getOrCreateChannel(channelName)
}
}
t.Unlock()

// NOTE: I would prefer for this to only happen in topic.GetChannel() but we're special
// casing the code above so that we can control the locks such that it is impossible
// for a message to be written to a (new) topic while we're looking up channels
// from lookupd...
//
// update messagePump state
select {
case t.channelUpdateChan <- 1:
case <-t.exitChan:
}
}
return t
}
Expand Down
4 changes: 4 additions & 0 deletions nsqd/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ func (t *Topic) GetChannel(channelName string) *Channel {
t.Lock()
channel, isNew := t.getOrCreateChannel(channelName)
t.Unlock()

if isNew {
// update messagePump state
select {
case t.channelUpdateChan <- 1:
case <-t.exitChan:
}
}

return channel
}

Expand Down Expand Up @@ -112,6 +115,7 @@ func (t *Topic) DeleteExistingChannel(channelName string) error {
// (so that we dont leave any messages around)
channel.Delete()

// update messagePump state
select {
case t.channelUpdateChan <- 1:
case <-t.exitChan:
Expand Down

0 comments on commit ee31e04

Please sign in to comment.