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: nsqlookupd connect callback simpler, more robust #1008

Merged
merged 2 commits into from
May 12, 2018
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
61 changes: 30 additions & 31 deletions nsqd/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/nsqio/nsq/internal/version"
)

func connectCallback(n *NSQD, hostname string, syncTopicChan chan *lookupPeer) func(*lookupPeer) {
func connectCallback(n *NSQD, hostname string) func(*lookupPeer) {
return func(lp *lookupPeer) {
ci := make(map[string]interface{})
ci["version"] = version.Binary
Expand All @@ -32,10 +32,14 @@ func connectCallback(n *NSQD, hostname string, syncTopicChan chan *lookupPeer) f
return
} else if bytes.Equal(resp, []byte("E_INVALID")) {
n.logf(LOG_INFO, "LOOKUPD(%s): lookupd returned %s", lp, resp)
lp.Close()
return
} else {
err = json.Unmarshal(resp, &lp.Info)
if err != nil {
n.logf(LOG_ERROR, "LOOKUPD(%s): parsing response - %s", lp, resp)
lp.Close()
return
} else {
n.logf(LOG_INFO, "LOOKUPD(%s): peer info %+v", lp, lp.Info)
if lp.Info.BroadcastAddress == "" {
Expand All @@ -44,16 +48,36 @@ func connectCallback(n *NSQD, hostname string, syncTopicChan chan *lookupPeer) f
}
}

go func() {
syncTopicChan <- lp
}()
// build all the commands first so we exit the lock(s) as fast as possible
var commands []*nsq.Command
n.RLock()
for _, topic := range n.topicMap {
topic.RLock()
if len(topic.channelMap) == 0 {
commands = append(commands, nsq.Register(topic.name, ""))
} else {
for _, channel := range topic.channelMap {
commands = append(commands, nsq.Register(channel.topicName, channel.name))
}
}
topic.RUnlock()
}
n.RUnlock()

for _, cmd := range commands {
n.logf(LOG_INFO, "LOOKUPD(%s): %s", lp, cmd)
_, err := lp.Command(cmd)
if err != nil {
n.logf(LOG_ERROR, "LOOKUPD(%s): %s - %s", lp, cmd, err)
return
}
}
}
}

func (n *NSQD) lookupLoop() {
var lookupPeers []*lookupPeer
var lookupAddrs []string
syncTopicChan := make(chan *lookupPeer)
connect := true

hostname, err := os.Hostname()
Expand All @@ -72,7 +96,7 @@ func (n *NSQD) lookupLoop() {
}
n.logf(LOG_INFO, "LOOKUP(%s): adding peer", host)
lookupPeer := newLookupPeer(host, n.getOpts().MaxBodySize, n.logf,
connectCallback(n, hostname, syncTopicChan))
connectCallback(n, hostname))
lookupPeer.Command(nil) // start the connection
lookupPeers = append(lookupPeers, lookupPeer)
lookupAddrs = append(lookupAddrs, host)
Expand Down Expand Up @@ -124,31 +148,6 @@ func (n *NSQD) lookupLoop() {
n.logf(LOG_ERROR, "LOOKUPD(%s): %s - %s", lookupPeer, cmd, err)
}
}
case lookupPeer := <-syncTopicChan:
var commands []*nsq.Command
// build all the commands first so we exit the lock(s) as fast as possible
n.RLock()
for _, topic := range n.topicMap {
topic.RLock()
if len(topic.channelMap) == 0 {
commands = append(commands, nsq.Register(topic.name, ""))
} else {
for _, channel := range topic.channelMap {
commands = append(commands, nsq.Register(channel.topicName, channel.name))
}
}
topic.RUnlock()
}
n.RUnlock()

for _, cmd := range commands {
n.logf(LOG_INFO, "LOOKUPD(%s): %s", lookupPeer, cmd)
_, err := lookupPeer.Command(cmd)
if err != nil {
n.logf(LOG_ERROR, "LOOKUPD(%s): %s - %s", lookupPeer, cmd, err)
break
}
}
case <-n.optsNotificationChan:
var tmpPeers []*lookupPeer
var tmpAddrs []string
Expand Down
9 changes: 8 additions & 1 deletion nsqd/lookup_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ func (lp *lookupPeer) Command(cmd *nsq.Command) ([]byte, error) {
return nil, err
}
lp.state = stateConnected
lp.Write(nsq.MagicV1)
_, err = lp.Write(nsq.MagicV1)
if err != nil {
lp.Close()
return nil, err
}
if initialState == stateDisconnected {
lp.connectCallback(lp)
}
if lp.state != stateConnected {
return nil, fmt.Errorf("lookupPeer connectCallback() failed")
}
}
if cmd == nil {
return nil, nil
Expand Down
6 changes: 3 additions & 3 deletions nsqd/nsqd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ func TestReconfigure(t *testing.T) {
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()

time.Sleep(200 * time.Millisecond)
time.Sleep(250 * time.Millisecond)

newOpts := *opts
newOpts.NSQLookupdTCPAddresses = []string{lookupd1.RealTCPAddr().String()}
nsqd.swapOpts(&newOpts)
nsqd.triggerOptsNotification()
test.Equal(t, 1, len(nsqd.getOpts().NSQLookupdTCPAddresses))

time.Sleep(200 * time.Millisecond)
time.Sleep(350 * time.Millisecond)

numLookupPeers := len(nsqd.lookupPeers.Load().([]*lookupPeer))
test.Equal(t, 1, numLookupPeers)
Expand All @@ -387,7 +387,7 @@ func TestReconfigure(t *testing.T) {
nsqd.triggerOptsNotification()
test.Equal(t, 2, len(nsqd.getOpts().NSQLookupdTCPAddresses))

time.Sleep(200 * time.Millisecond)
time.Sleep(350 * time.Millisecond)

var lookupPeers []string
for _, lp := range nsqd.lookupPeers.Load().([]*lookupPeer) {
Expand Down