func main() { flagSet.Parse(os.Args[1:]) if *showVersion { fmt.Println(version.String("nsqlookupd")) return } signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) var cfg map[string]interface{} if *config != "" { _, err := toml.DecodeFile(*config, &cfg) if err != nil { log.Fatalf("ERROR: failed to load config file %s - %s", *config, err.Error()) } } opts := nsqlookupd.NewOptions() options.Resolve(opts, flagSet, cfg) daemon := nsqlookupd.New(opts) daemon.Main() <-signalChan daemon.Exit() }
func TestHTTPconfig(t *testing.T) { lopts := nsqlookupd.NewOptions() lopts.Logger = test.NewTestLogger(t) _, _, lookupd1 := mustStartNSQLookupd(lopts) defer lookupd1.Exit() _, _, lookupd2 := mustStartNSQLookupd(lopts) defer lookupd2.Exit() opts := NewOptions() opts.Logger = test.NewTestLogger(t) _, httpAddr, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) defer nsqd.Exit() url := fmt.Sprintf("http://%s/config/nsqlookupd_tcp_addresses", httpAddr) resp, err := http.Get(url) test.Nil(t, err) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) test.Equal(t, 200, resp.StatusCode) test.Equal(t, "[]", string(body)) client := http.Client{} addrs := fmt.Sprintf(`["%s","%s"]`, lookupd1.RealTCPAddr().String(), lookupd2.RealTCPAddr().String()) url = fmt.Sprintf("http://%s/config/nsqlookupd_tcp_addresses", httpAddr) req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(addrs))) test.Nil(t, err) resp, err = client.Do(req) test.Nil(t, err) defer resp.Body.Close() body, _ = ioutil.ReadAll(resp.Body) test.Equal(t, 200, resp.StatusCode) test.Equal(t, addrs, string(body)) }
// we need a bit of magic first, to turn on some nsq stuff (thanks snakes) func bootstrapNSQCluster(t *testing.T) (string, []*nsqd.NSQD, []*nsqlookupd.NSQLookupd) { nsqlookupdOpts := nsqlookupd.NewOptions() nsqlookupdOpts.TCPAddress = "127.0.0.1:0" nsqlookupdOpts.HTTPAddress = "127.0.0.1:0" nsqlookupdOpts.BroadcastAddress = "127.0.0.1" nsqlookupd1 := nsqlookupd.New(nsqlookupdOpts) go nsqlookupd1.Main() time.Sleep(100 * time.Millisecond) nsqdOpts := nsqd.NewOptions() nsqdOpts.TCPAddress = "127.0.0.1:0" nsqdOpts.HTTPAddress = "127.0.0.1:0" nsqdOpts.BroadcastAddress = "127.0.0.1" nsqdOpts.NSQLookupdTCPAddresses = []string{nsqlookupd1.RealTCPAddr().String()} tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano())) if err != nil { panic(err) } nsqdOpts.DataPath = tmpDir nsqd1 := nsqd.New(nsqdOpts) go nsqd1.Main() time.Sleep(100 * time.Millisecond) return tmpDir, []*nsqd.NSQD{nsqd1}, []*nsqlookupd.NSQLookupd{nsqlookupd1} }
func TestHTTPconfig(t *testing.T) { dataPath, nsqds, nsqlookupds, nsqadmin1 := bootstrapNSQCluster(t) defer os.RemoveAll(dataPath) defer nsqds[0].Exit() defer nsqlookupds[0].Exit() defer nsqadmin1.Exit() lopts := nsqlookupd.NewOptions() lopts.Logger = test.NewTestLogger(t) _, _, lookupd1 := mustStartNSQLookupd(lopts) defer lookupd1.Exit() _, _, lookupd2 := mustStartNSQLookupd(lopts) defer lookupd2.Exit() url := fmt.Sprintf("http://%s/config/nsqlookupd_http_addresses", nsqadmin1.RealHTTPAddr()) resp, err := http.Get(url) test.Nil(t, err) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) test.Equal(t, 200, resp.StatusCode) origaddrs := fmt.Sprintf(`["%s"]`, nsqlookupds[0].RealHTTPAddr().String()) test.Equal(t, origaddrs, string(body)) client := http.Client{} addrs := fmt.Sprintf(`["%s","%s"]`, lookupd1.RealHTTPAddr().String(), lookupd2.RealHTTPAddr().String()) url = fmt.Sprintf("http://%s/config/nsqlookupd_http_addresses", nsqadmin1.RealHTTPAddr()) req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(addrs))) test.Nil(t, err) resp, err = client.Do(req) test.Nil(t, err) defer resp.Body.Close() body, _ = ioutil.ReadAll(resp.Body) test.Equal(t, 200, resp.StatusCode) test.Equal(t, addrs, string(body)) }
func (b *Broker) StartLookUp() { b.logger.Info("start lookup service") var wg sync.WaitGroup wg.Add(1) go func() { opt := nsqlookupd.NewOptions() opt.TCPAddress = b.LookUpTcpAddrr + ":" + b.LookUpTcpPort opt.HTTPAddress = b.LookUpHttpAddrr + ":" + b.LookUpHttpPort b.lookup = nsqlookupd.New(opt) b.lookup.Main() wg.Done() b.logger.Info("lookup service started") }() wg.Wait() }
func TestReconfigure(t *testing.T) { lopts := nsqlookupd.NewOptions() lopts.Logger = newTestLogger(t) _, _, lookupd1 := mustStartNSQLookupd(lopts) defer lookupd1.Exit() _, _, lookupd2 := mustStartNSQLookupd(lopts) defer lookupd2.Exit() _, _, lookupd3 := mustStartNSQLookupd(lopts) defer lookupd3.Exit() opts := NewOptions() opts.Logger = newTestLogger(t) _, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) defer nsqd.Exit() time.Sleep(50 * time.Millisecond) newOpts := *opts newOpts.NSQLookupdTCPAddresses = []string{lookupd1.RealTCPAddr().String()} nsqd.swapOpts(&newOpts) nsqd.triggerOptsNotification() equal(t, len(nsqd.getOpts().NSQLookupdTCPAddresses), 1) time.Sleep(50 * time.Millisecond) numLookupPeers := len(nsqd.lookupPeers.Load().([]*lookupPeer)) equal(t, numLookupPeers, 1) newOpts = *opts newOpts.NSQLookupdTCPAddresses = []string{lookupd2.RealTCPAddr().String(), lookupd3.RealTCPAddr().String()} nsqd.swapOpts(&newOpts) nsqd.triggerOptsNotification() equal(t, len(nsqd.getOpts().NSQLookupdTCPAddresses), 2) time.Sleep(50 * time.Millisecond) var lookupPeers []string for _, lp := range nsqd.lookupPeers.Load().([]*lookupPeer) { lookupPeers = append(lookupPeers, lp.addr) } equal(t, len(lookupPeers), 2) equal(t, lookupPeers, newOpts.NSQLookupdTCPAddresses) }
func (p *program) Start() error { flagSet.Parse(os.Args[1:]) if *showVersion { fmt.Println(version.String("nsqlookupd")) os.Exit(0) } var cfg map[string]interface{} if *config != "" { _, err := toml.DecodeFile(*config, &cfg) if err != nil { log.Fatalf("ERROR: failed to load config file %s - %s", *config, err.Error()) } } opts := nsqlookupd.NewOptions() options.Resolve(opts, flagSet, cfg) daemon := nsqlookupd.New(opts) daemon.Main() p.nsqlookupd = daemon return nil }
func TestCluster(t *testing.T) { lopts := nsqlookupd.NewOptions() lopts.Logger = newTestLogger(t) lopts.BroadcastAddress = "127.0.0.1" _, _, lookupd := mustStartNSQLookupd(lopts) opts := NewOptions() opts.Logger = newTestLogger(t) opts.NSQLookupdTCPAddresses = []string{lookupd.RealTCPAddr().String()} opts.BroadcastAddress = "127.0.0.1" _, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) defer nsqd.Exit() topicName := "cluster_test" + strconv.Itoa(int(time.Now().Unix())) hostname, err := os.Hostname() equal(t, err, nil) url := fmt.Sprintf("http://%s/topic/create?topic=%s", nsqd.RealHTTPAddr(), topicName) err = http_api.NewClient(nil).POSTV1(url) equal(t, err, nil) url = fmt.Sprintf("http://%s/channel/create?topic=%s&channel=ch", nsqd.RealHTTPAddr(), topicName) err = http_api.NewClient(nil).POSTV1(url) equal(t, err, nil) // allow some time for nsqd to push info to nsqlookupd time.Sleep(350 * time.Millisecond) endpoint := fmt.Sprintf("http://%s/debug", lookupd.RealHTTPAddr()) data, err := API(endpoint) equal(t, err, nil) topicData := data.Get("topic:" + topicName + ":") producers, _ := topicData.Array() equal(t, len(producers), 1) producer := topicData.GetIndex(0) equal(t, producer.Get("hostname").MustString(), hostname) equal(t, producer.Get("broadcast_address").MustString(), "127.0.0.1") equal(t, producer.Get("tcp_port").MustInt(), nsqd.RealTCPAddr().Port) equal(t, producer.Get("tombstoned").MustBool(), false) channelData := data.Get("channel:" + topicName + ":ch") producers, _ = channelData.Array() equal(t, len(producers), 1) producer = topicData.GetIndex(0) equal(t, producer.Get("hostname").MustString(), hostname) equal(t, producer.Get("broadcast_address").MustString(), "127.0.0.1") equal(t, producer.Get("tcp_port").MustInt(), nsqd.RealTCPAddr().Port) equal(t, producer.Get("tombstoned").MustBool(), false) endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", lookupd.RealHTTPAddr(), topicName) data, err = API(endpoint) producers, _ = data.Get("producers").Array() equal(t, len(producers), 1) producer = data.Get("producers").GetIndex(0) equal(t, producer.Get("hostname").MustString(), hostname) equal(t, producer.Get("broadcast_address").MustString(), "127.0.0.1") equal(t, producer.Get("tcp_port").MustInt(), nsqd.RealTCPAddr().Port) channels, _ := data.Get("channels").Array() equal(t, len(channels), 1) channel := channels[0].(string) equal(t, channel, "ch") url = fmt.Sprintf("http://%s/topic/delete?topic=%s", nsqd.RealHTTPAddr(), topicName) err = http_api.NewClient(nil).POSTV1(url) equal(t, err, nil) // allow some time for nsqd to push info to nsqlookupd time.Sleep(350 * time.Millisecond) endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", lookupd.RealHTTPAddr(), topicName) data, err = API(endpoint) equal(t, err, nil) producers, _ = data.Get("producers").Array() equal(t, len(producers), 0) endpoint = fmt.Sprintf("http://%s/debug", lookupd.RealHTTPAddr()) data, err = API(endpoint) equal(t, err, nil) producers, _ = data.Get("topic:" + topicName + ":").Array() equal(t, len(producers), 0) producers, _ = data.Get("channel:" + topicName + ":ch").Array() equal(t, len(producers), 0) }
func TestCluster(t *testing.T) { lopts := nsqlookupd.NewOptions() lopts.Logger = test.NewTestLogger(t) lopts.BroadcastAddress = "127.0.0.1" _, _, lookupd := mustStartNSQLookupd(lopts) opts := NewOptions() opts.Logger = test.NewTestLogger(t) opts.NSQLookupdTCPAddresses = []string{lookupd.RealTCPAddr().String()} opts.BroadcastAddress = "127.0.0.1" _, _, nsqd := mustStartNSQD(opts) defer os.RemoveAll(opts.DataPath) defer nsqd.Exit() topicName := "cluster_test" + strconv.Itoa(int(time.Now().Unix())) hostname, err := os.Hostname() test.Nil(t, err) url := fmt.Sprintf("http://%s/topic/create?topic=%s", nsqd.RealHTTPAddr(), topicName) err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).POSTV1(url) test.Nil(t, err) url = fmt.Sprintf("http://%s/channel/create?topic=%s&channel=ch", nsqd.RealHTTPAddr(), topicName) err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).POSTV1(url) test.Nil(t, err) // allow some time for nsqd to push info to nsqlookupd time.Sleep(350 * time.Millisecond) var d map[string][]struct { Hostname string `json:"hostname"` BroadcastAddress string `json:"broadcast_address"` TCPPort int `json:"tcp_port"` Tombstoned bool `json:"tombstoned"` } endpoint := fmt.Sprintf("http://%s/debug", lookupd.RealHTTPAddr()) err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &d) test.Nil(t, err) topicData := d["topic:"+topicName+":"] test.Equal(t, 1, len(topicData)) test.Equal(t, hostname, topicData[0].Hostname) test.Equal(t, "127.0.0.1", topicData[0].BroadcastAddress) test.Equal(t, nsqd.RealTCPAddr().Port, topicData[0].TCPPort) test.Equal(t, false, topicData[0].Tombstoned) channelData := d["channel:"+topicName+":ch"] test.Equal(t, 1, len(channelData)) test.Equal(t, hostname, channelData[0].Hostname) test.Equal(t, "127.0.0.1", channelData[0].BroadcastAddress) test.Equal(t, nsqd.RealTCPAddr().Port, channelData[0].TCPPort) test.Equal(t, false, channelData[0].Tombstoned) var lr struct { Producers []struct { Hostname string `json:"hostname"` BroadcastAddress string `json:"broadcast_address"` TCPPort int `json:"tcp_port"` } `json:"producers"` Channels []string `json:"channels"` } endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", lookupd.RealHTTPAddr(), topicName) err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &lr) test.Nil(t, err) test.Equal(t, 1, len(lr.Producers)) test.Equal(t, hostname, lr.Producers[0].Hostname) test.Equal(t, "127.0.0.1", lr.Producers[0].BroadcastAddress) test.Equal(t, nsqd.RealTCPAddr().Port, lr.Producers[0].TCPPort) test.Equal(t, 1, len(lr.Channels)) test.Equal(t, "ch", lr.Channels[0]) url = fmt.Sprintf("http://%s/topic/delete?topic=%s", nsqd.RealHTTPAddr(), topicName) err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).POSTV1(url) test.Nil(t, err) // allow some time for nsqd to push info to nsqlookupd time.Sleep(350 * time.Millisecond) endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", lookupd.RealHTTPAddr(), topicName) err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &lr) test.Nil(t, err) test.Equal(t, 0, len(lr.Producers)) var dd map[string][]interface{} endpoint = fmt.Sprintf("http://%s/debug", lookupd.RealHTTPAddr()) err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &dd) test.Nil(t, err) test.Equal(t, 0, len(dd["topic:"+topicName+":"])) test.Equal(t, 0, len(dd["channel:"+topicName+":ch"])) }