nsqlookupd_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package nsqlookupd
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/nsqio/go-nsq"
  8. "github.com/nsqio/nsq/internal/clusterinfo"
  9. "github.com/nsqio/nsq/internal/http_api"
  10. "github.com/nsqio/nsq/internal/test"
  11. )
  12. const (
  13. ConnectTimeout = 2 * time.Second
  14. RequestTimeout = 5 * time.Second
  15. TCPPort = 5000
  16. HTTPPort = 5555
  17. HostAddr = "ip.address"
  18. NSQDVersion = "fake-version"
  19. )
  20. type ProducersDoc struct {
  21. Producers []interface{} `json:"producers"`
  22. }
  23. type TopicsDoc struct {
  24. Topics []interface{} `json:"topics"`
  25. }
  26. type LookupDoc struct {
  27. Channels []interface{} `json:"channels"`
  28. Producers []*PeerInfo `json:"producers"`
  29. }
  30. func mustStartLookupd(opts *Options) (*net.TCPAddr, *net.TCPAddr, *NSQLookupd) {
  31. opts.TCPAddress = "127.0.0.1:0"
  32. opts.HTTPAddress = "127.0.0.1:0"
  33. nsqlookupd, err := New(opts)
  34. if err != nil {
  35. panic(err)
  36. }
  37. go func() {
  38. err := nsqlookupd.Main()
  39. if err != nil {
  40. panic(err)
  41. }
  42. }()
  43. return nsqlookupd.RealTCPAddr(), nsqlookupd.RealHTTPAddr(), nsqlookupd
  44. }
  45. func mustConnectLookupd(t *testing.T, tcpAddr *net.TCPAddr) net.Conn {
  46. conn, err := net.DialTimeout("tcp", tcpAddr.String(), time.Second)
  47. if err != nil {
  48. t.Fatal("failed to connect to lookupd")
  49. }
  50. conn.Write(nsq.MagicV1)
  51. return conn
  52. }
  53. func identify(t *testing.T, conn net.Conn) {
  54. ci := make(map[string]interface{})
  55. ci["tcp_port"] = TCPPort
  56. ci["http_port"] = HTTPPort
  57. ci["broadcast_address"] = HostAddr
  58. ci["hostname"] = HostAddr
  59. ci["version"] = NSQDVersion
  60. cmd, _ := nsq.Identify(ci)
  61. _, err := cmd.WriteTo(conn)
  62. test.Nil(t, err)
  63. _, err = nsq.ReadResponse(conn)
  64. test.Nil(t, err)
  65. }
  66. func TestBasicLookupd(t *testing.T) {
  67. opts := NewOptions()
  68. opts.Logger = test.NewTestLogger(t)
  69. tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
  70. defer nsqlookupd.Exit()
  71. topics := nsqlookupd.DB.FindRegistrations("topic", "*", "*")
  72. test.Equal(t, 0, len(topics))
  73. topicName := "connectmsg"
  74. conn := mustConnectLookupd(t, tcpAddr)
  75. identify(t, conn)
  76. nsq.Register(topicName, "channel1").WriteTo(conn)
  77. v, err := nsq.ReadResponse(conn)
  78. test.Nil(t, err)
  79. test.Equal(t, []byte("OK"), v)
  80. pr := ProducersDoc{}
  81. endpoint := fmt.Sprintf("http://%s/nodes", httpAddr)
  82. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &pr)
  83. test.Nil(t, err)
  84. t.Logf("got %v", pr)
  85. test.Equal(t, 1, len(pr.Producers))
  86. topics = nsqlookupd.DB.FindRegistrations("topic", topicName, "")
  87. test.Equal(t, 1, len(topics))
  88. producers := nsqlookupd.DB.FindProducers("topic", topicName, "")
  89. test.Equal(t, 1, len(producers))
  90. producer := producers[0]
  91. test.Equal(t, HostAddr, producer.peerInfo.BroadcastAddress)
  92. test.Equal(t, HostAddr, producer.peerInfo.Hostname)
  93. test.Equal(t, TCPPort, producer.peerInfo.TCPPort)
  94. test.Equal(t, HTTPPort, producer.peerInfo.HTTPPort)
  95. tr := TopicsDoc{}
  96. endpoint = fmt.Sprintf("http://%s/topics", httpAddr)
  97. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &tr)
  98. test.Nil(t, err)
  99. t.Logf("got %v", tr)
  100. test.Equal(t, 1, len(tr.Topics))
  101. lr := LookupDoc{}
  102. endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
  103. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &lr)
  104. test.Nil(t, err)
  105. t.Logf("got %v", lr)
  106. test.Equal(t, 1, len(lr.Channels))
  107. test.Equal(t, 1, len(lr.Producers))
  108. for _, p := range lr.Producers {
  109. test.Equal(t, TCPPort, p.TCPPort)
  110. test.Equal(t, HTTPPort, p.HTTPPort)
  111. test.Equal(t, HostAddr, p.BroadcastAddress)
  112. test.Equal(t, NSQDVersion, p.Version)
  113. }
  114. conn.Close()
  115. time.Sleep(10 * time.Millisecond)
  116. // now there should be no producers, but still topic/channel entries
  117. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &lr)
  118. test.Nil(t, err)
  119. test.Equal(t, 1, len(lr.Channels))
  120. test.Equal(t, 0, len(lr.Producers))
  121. }
  122. func TestChannelUnregister(t *testing.T) {
  123. opts := NewOptions()
  124. opts.Logger = test.NewTestLogger(t)
  125. tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
  126. defer nsqlookupd.Exit()
  127. topics := nsqlookupd.DB.FindRegistrations("topic", "*", "*")
  128. test.Equal(t, 0, len(topics))
  129. topicName := "channel_unregister"
  130. conn := mustConnectLookupd(t, tcpAddr)
  131. defer conn.Close()
  132. identify(t, conn)
  133. nsq.Register(topicName, "ch1").WriteTo(conn)
  134. v, err := nsq.ReadResponse(conn)
  135. test.Nil(t, err)
  136. test.Equal(t, []byte("OK"), v)
  137. topics = nsqlookupd.DB.FindRegistrations("topic", topicName, "")
  138. test.Equal(t, 1, len(topics))
  139. channels := nsqlookupd.DB.FindRegistrations("channel", topicName, "*")
  140. test.Equal(t, 1, len(channels))
  141. nsq.UnRegister(topicName, "ch1").WriteTo(conn)
  142. v, err = nsq.ReadResponse(conn)
  143. test.Nil(t, err)
  144. test.Equal(t, []byte("OK"), v)
  145. topics = nsqlookupd.DB.FindRegistrations("topic", topicName, "")
  146. test.Equal(t, 1, len(topics))
  147. // we should still have mention of the topic even though there is no producer
  148. // (ie. we haven't *deleted* the channel, just unregistered as a producer)
  149. channels = nsqlookupd.DB.FindRegistrations("channel", topicName, "*")
  150. test.Equal(t, 1, len(channels))
  151. pr := ProducersDoc{}
  152. endpoint := fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
  153. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &pr)
  154. test.Nil(t, err)
  155. t.Logf("got %v", pr)
  156. test.Equal(t, 1, len(pr.Producers))
  157. }
  158. func TestTombstoneRecover(t *testing.T) {
  159. opts := NewOptions()
  160. opts.Logger = test.NewTestLogger(t)
  161. opts.TombstoneLifetime = 50 * time.Millisecond
  162. tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
  163. defer nsqlookupd.Exit()
  164. topicName := "tombstone_recover"
  165. topicName2 := topicName + "2"
  166. conn := mustConnectLookupd(t, tcpAddr)
  167. defer conn.Close()
  168. identify(t, conn)
  169. nsq.Register(topicName, "channel1").WriteTo(conn)
  170. _, err := nsq.ReadResponse(conn)
  171. test.Nil(t, err)
  172. nsq.Register(topicName2, "channel2").WriteTo(conn)
  173. _, err = nsq.ReadResponse(conn)
  174. test.Nil(t, err)
  175. endpoint := fmt.Sprintf("http://%s/topic/tombstone?topic=%s&node=%s:%d",
  176. httpAddr, topicName, HostAddr, HTTPPort)
  177. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).POSTV1(endpoint)
  178. test.Nil(t, err)
  179. pr := ProducersDoc{}
  180. endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
  181. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &pr)
  182. test.Nil(t, err)
  183. test.Equal(t, 0, len(pr.Producers))
  184. endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName2)
  185. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &pr)
  186. test.Nil(t, err)
  187. test.Equal(t, 1, len(pr.Producers))
  188. time.Sleep(75 * time.Millisecond)
  189. endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
  190. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &pr)
  191. test.Nil(t, err)
  192. test.Equal(t, 1, len(pr.Producers))
  193. }
  194. func TestTombstoneUnregister(t *testing.T) {
  195. opts := NewOptions()
  196. opts.Logger = test.NewTestLogger(t)
  197. opts.TombstoneLifetime = 50 * time.Millisecond
  198. tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
  199. defer nsqlookupd.Exit()
  200. topicName := "tombstone_unregister"
  201. conn := mustConnectLookupd(t, tcpAddr)
  202. defer conn.Close()
  203. identify(t, conn)
  204. nsq.Register(topicName, "channel1").WriteTo(conn)
  205. _, err := nsq.ReadResponse(conn)
  206. test.Nil(t, err)
  207. endpoint := fmt.Sprintf("http://%s/topic/tombstone?topic=%s&node=%s:%d",
  208. httpAddr, topicName, HostAddr, HTTPPort)
  209. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).POSTV1(endpoint)
  210. test.Nil(t, err)
  211. pr := ProducersDoc{}
  212. endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
  213. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &pr)
  214. test.Nil(t, err)
  215. test.Equal(t, 0, len(pr.Producers))
  216. nsq.UnRegister(topicName, "").WriteTo(conn)
  217. _, err = nsq.ReadResponse(conn)
  218. test.Nil(t, err)
  219. time.Sleep(55 * time.Millisecond)
  220. endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
  221. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &pr)
  222. test.Nil(t, err)
  223. test.Equal(t, 0, len(pr.Producers))
  224. }
  225. func TestInactiveNodes(t *testing.T) {
  226. opts := NewOptions()
  227. opts.Logger = test.NewTestLogger(t)
  228. opts.InactiveProducerTimeout = 200 * time.Millisecond
  229. tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
  230. defer nsqlookupd.Exit()
  231. lookupdHTTPAddrs := []string{fmt.Sprintf("%s", httpAddr)}
  232. topicName := "inactive_nodes"
  233. conn := mustConnectLookupd(t, tcpAddr)
  234. defer conn.Close()
  235. identify(t, conn)
  236. nsq.Register(topicName, "channel1").WriteTo(conn)
  237. _, err := nsq.ReadResponse(conn)
  238. test.Nil(t, err)
  239. ci := clusterinfo.New(nil, http_api.NewClient(nil, ConnectTimeout, RequestTimeout))
  240. producers, _ := ci.GetLookupdProducers(lookupdHTTPAddrs)
  241. test.Equal(t, 1, len(producers))
  242. test.Equal(t, 1, len(producers[0].Topics))
  243. test.Equal(t, topicName, producers[0].Topics[0].Topic)
  244. test.Equal(t, false, producers[0].Topics[0].Tombstoned)
  245. time.Sleep(250 * time.Millisecond)
  246. producers, _ = ci.GetLookupdProducers(lookupdHTTPAddrs)
  247. test.Equal(t, 0, len(producers))
  248. }
  249. func TestTombstonedNodes(t *testing.T) {
  250. opts := NewOptions()
  251. opts.Logger = test.NewTestLogger(t)
  252. tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
  253. defer nsqlookupd.Exit()
  254. lookupdHTTPAddrs := []string{fmt.Sprintf("%s", httpAddr)}
  255. topicName := "inactive_nodes"
  256. conn := mustConnectLookupd(t, tcpAddr)
  257. defer conn.Close()
  258. identify(t, conn)
  259. nsq.Register(topicName, "channel1").WriteTo(conn)
  260. _, err := nsq.ReadResponse(conn)
  261. test.Nil(t, err)
  262. ci := clusterinfo.New(nil, http_api.NewClient(nil, ConnectTimeout, RequestTimeout))
  263. producers, _ := ci.GetLookupdProducers(lookupdHTTPAddrs)
  264. test.Equal(t, 1, len(producers))
  265. test.Equal(t, 1, len(producers[0].Topics))
  266. test.Equal(t, topicName, producers[0].Topics[0].Topic)
  267. test.Equal(t, false, producers[0].Topics[0].Tombstoned)
  268. endpoint := fmt.Sprintf("http://%s/topic/tombstone?topic=%s&node=%s:%d",
  269. httpAddr, topicName, HostAddr, HTTPPort)
  270. err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).POSTV1(endpoint)
  271. test.Nil(t, err)
  272. producers, _ = ci.GetLookupdProducers(lookupdHTTPAddrs)
  273. test.Equal(t, 1, len(producers))
  274. test.Equal(t, 1, len(producers[0].Topics))
  275. test.Equal(t, topicName, producers[0].Topics[0].Topic)
  276. test.Equal(t, true, producers[0].Topics[0].Tombstoned)
  277. }