lookup.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package nsqd
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net"
  6. "os"
  7. "strconv"
  8. "time"
  9. "github.com/nsqio/go-nsq"
  10. "github.com/nsqio/nsq/internal/version"
  11. )
  12. func connectCallback(n *NSQD, hostname string) func(*lookupPeer) {
  13. return func(lp *lookupPeer) {
  14. ci := make(map[string]interface{})
  15. ci["version"] = version.Binary
  16. ci["tcp_port"] = n.getOpts().BroadcastTCPPort
  17. ci["http_port"] = n.getOpts().BroadcastHTTPPort
  18. ci["hostname"] = hostname
  19. ci["broadcast_address"] = n.getOpts().BroadcastAddress
  20. cmd, err := nsq.Identify(ci)
  21. if err != nil {
  22. lp.Close()
  23. return
  24. }
  25. resp, err := lp.Command(cmd)
  26. if err != nil {
  27. n.logf(LOG_ERROR, "LOOKUPD(%s): %s - %s", lp, cmd, err)
  28. return
  29. } else if bytes.Equal(resp, []byte("E_INVALID")) {
  30. n.logf(LOG_INFO, "LOOKUPD(%s): lookupd returned %s", lp, resp)
  31. lp.Close()
  32. return
  33. } else {
  34. err = json.Unmarshal(resp, &lp.Info)
  35. if err != nil {
  36. n.logf(LOG_ERROR, "LOOKUPD(%s): parsing response - %s", lp, resp)
  37. lp.Close()
  38. return
  39. } else {
  40. n.logf(LOG_INFO, "LOOKUPD(%s): peer info %+v", lp, lp.Info)
  41. if lp.Info.BroadcastAddress == "" {
  42. n.logf(LOG_ERROR, "LOOKUPD(%s): no broadcast address", lp)
  43. }
  44. }
  45. }
  46. // build all the commands first so we exit the lock(s) as fast as possible
  47. var commands []*nsq.Command
  48. n.RLock()
  49. for _, topic := range n.topicMap {
  50. topic.RLock()
  51. if len(topic.channelMap) == 0 {
  52. commands = append(commands, nsq.Register(topic.name, ""))
  53. } else {
  54. for _, channel := range topic.channelMap {
  55. commands = append(commands, nsq.Register(channel.topicName, channel.name))
  56. }
  57. }
  58. topic.RUnlock()
  59. }
  60. n.RUnlock()
  61. for _, cmd := range commands {
  62. n.logf(LOG_INFO, "LOOKUPD(%s): %s", lp, cmd)
  63. _, err := lp.Command(cmd)
  64. if err != nil {
  65. n.logf(LOG_ERROR, "LOOKUPD(%s): %s - %s", lp, cmd, err)
  66. return
  67. }
  68. }
  69. }
  70. }
  71. func (n *NSQD) lookupLoop() {
  72. var lookupPeers []*lookupPeer
  73. var lookupAddrs []string
  74. connect := true
  75. hostname, err := os.Hostname()
  76. if err != nil {
  77. n.logf(LOG_FATAL, "failed to get hostname - %s", err)
  78. os.Exit(1)
  79. }
  80. // for announcements, lookupd determines the host automatically
  81. ticker := time.Tick(15 * time.Second)
  82. for {
  83. if connect {
  84. for _, host := range n.getOpts().NSQLookupdTCPAddresses {
  85. if in(host, lookupAddrs) {
  86. continue
  87. }
  88. n.logf(LOG_INFO, "LOOKUP(%s): adding peer", host)
  89. lookupPeer := newLookupPeer(host, n.getOpts().MaxBodySize, n.logf,
  90. connectCallback(n, hostname))
  91. lookupPeer.Command(nil) // start the connection
  92. lookupPeers = append(lookupPeers, lookupPeer)
  93. lookupAddrs = append(lookupAddrs, host)
  94. }
  95. n.lookupPeers.Store(lookupPeers)
  96. connect = false
  97. }
  98. select {
  99. case <-ticker:
  100. // send a heartbeat and read a response (read detects closed conns)
  101. for _, lookupPeer := range lookupPeers {
  102. n.logf(LOG_DEBUG, "LOOKUPD(%s): sending heartbeat", lookupPeer)
  103. cmd := nsq.Ping()
  104. _, err := lookupPeer.Command(cmd)
  105. if err != nil {
  106. n.logf(LOG_ERROR, "LOOKUPD(%s): %s - %s", lookupPeer, cmd, err)
  107. }
  108. }
  109. case val := <-n.notifyChan:
  110. var cmd *nsq.Command
  111. var branch string
  112. switch val.(type) {
  113. case *Channel:
  114. // notify all nsqlookupds that a new channel exists, or that it's removed
  115. branch = "channel"
  116. channel := val.(*Channel)
  117. if channel.Exiting() == true {
  118. cmd = nsq.UnRegister(channel.topicName, channel.name)
  119. } else {
  120. cmd = nsq.Register(channel.topicName, channel.name)
  121. }
  122. case *Topic:
  123. // notify all nsqlookupds that a new topic exists, or that it's removed
  124. branch = "topic"
  125. topic := val.(*Topic)
  126. if topic.Exiting() == true {
  127. cmd = nsq.UnRegister(topic.name, "")
  128. } else {
  129. cmd = nsq.Register(topic.name, "")
  130. }
  131. }
  132. for _, lookupPeer := range lookupPeers {
  133. n.logf(LOG_INFO, "LOOKUPD(%s): %s %s", lookupPeer, branch, cmd)
  134. _, err := lookupPeer.Command(cmd)
  135. if err != nil {
  136. n.logf(LOG_ERROR, "LOOKUPD(%s): %s - %s", lookupPeer, cmd, err)
  137. }
  138. }
  139. case <-n.optsNotificationChan:
  140. var tmpPeers []*lookupPeer
  141. var tmpAddrs []string
  142. for _, lp := range lookupPeers {
  143. if in(lp.addr, n.getOpts().NSQLookupdTCPAddresses) {
  144. tmpPeers = append(tmpPeers, lp)
  145. tmpAddrs = append(tmpAddrs, lp.addr)
  146. continue
  147. }
  148. n.logf(LOG_INFO, "LOOKUP(%s): removing peer", lp)
  149. lp.Close()
  150. }
  151. lookupPeers = tmpPeers
  152. lookupAddrs = tmpAddrs
  153. connect = true
  154. case <-n.exitChan:
  155. goto exit
  156. }
  157. }
  158. exit:
  159. n.logf(LOG_INFO, "LOOKUP: closing")
  160. }
  161. func in(s string, lst []string) bool {
  162. for _, v := range lst {
  163. if s == v {
  164. return true
  165. }
  166. }
  167. return false
  168. }
  169. func (n *NSQD) lookupdHTTPAddrs() []string {
  170. var lookupHTTPAddrs []string
  171. lookupPeers := n.lookupPeers.Load()
  172. if lookupPeers == nil {
  173. return nil
  174. }
  175. for _, lp := range lookupPeers.([]*lookupPeer) {
  176. if len(lp.Info.BroadcastAddress) <= 0 {
  177. continue
  178. }
  179. addr := net.JoinHostPort(lp.Info.BroadcastAddress, strconv.Itoa(lp.Info.HTTPPort))
  180. lookupHTTPAddrs = append(lookupHTTPAddrs, addr)
  181. }
  182. return lookupHTTPAddrs
  183. }