mq.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package mq
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "log"
  8. "strings"
  9. "time"
  10. mqtt "github.com/eclipse/paho.mqtt.golang"
  11. "github.com/gravitl/netmaker/database"
  12. "github.com/gravitl/netmaker/logger"
  13. "github.com/gravitl/netmaker/logic"
  14. "github.com/gravitl/netmaker/models"
  15. "github.com/gravitl/netmaker/servercfg"
  16. )
  17. const KEEPALIVE_TIMEOUT = 60 //timeout in seconds
  18. const MQ_DISCONNECT = 250
  19. // DefaultHandler default message queue handler - only called when GetDebug == true
  20. var DefaultHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  21. logger.Log(0, "MQTT Message: Topic: ", string(msg.Topic()), " Message: ", string(msg.Payload()))
  22. }
  23. // Ping message Handler -- handles ping topic from client nodes
  24. var Ping mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  25. logger.Log(0, "Ping Handler: ", msg.Topic())
  26. go func() {
  27. id, err := GetID(msg.Topic())
  28. if err != nil {
  29. logger.Log(0, "error getting node.ID sent on ping topic ")
  30. return
  31. }
  32. node, err := logic.GetNodeByID(id)
  33. if err != nil {
  34. logger.Log(0, "mq-ping error getting node: ", err.Error())
  35. record, err := database.FetchRecord(database.NODES_TABLE_NAME, id)
  36. if err != nil {
  37. logger.Log(0, "error reading database ", err.Error())
  38. return
  39. }
  40. logger.Log(0, "record from database")
  41. logger.Log(0, record)
  42. return
  43. }
  44. _, decryptErr := decryptMsg(&node, msg.Payload())
  45. if decryptErr != nil {
  46. logger.Log(0, "error updating node ", node.ID, err.Error())
  47. return
  48. }
  49. node.SetLastCheckIn()
  50. if err := logic.UpdateNode(&node, &node); err != nil {
  51. logger.Log(0, "error updating node ", err.Error())
  52. }
  53. logger.Log(3, "ping processed for node", node.ID)
  54. // --TODO --set client version once feature is implemented.
  55. //node.SetClientVersion(msg.Payload())
  56. }()
  57. }
  58. // UpdateNode message Handler -- handles updates from client nodes
  59. var UpdateNode mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
  60. go func() {
  61. id, err := GetID(msg.Topic())
  62. if err != nil {
  63. logger.Log(1, "error getting node.ID sent on ", msg.Topic(), err.Error())
  64. return
  65. }
  66. currentNode, err := logic.GetNodeByID(id)
  67. if err != nil {
  68. logger.Log(1, "error getting node ", id, err.Error())
  69. return
  70. }
  71. decrypted, decryptErr := decryptMsg(&currentNode, msg.Payload())
  72. if decryptErr != nil {
  73. logger.Log(1, "failed to decrypt message for node ", id, decryptErr.Error())
  74. return
  75. }
  76. logger.Log(1, "Update Node Handler", id)
  77. var newNode models.Node
  78. if err := json.Unmarshal(decrypted, &newNode); err != nil {
  79. logger.Log(1, "error unmarshaling payload ", err.Error())
  80. return
  81. }
  82. var shouldUpdatePeers = logic.ShouldPeersUpdate(&currentNode, &newNode)
  83. if err := logic.UpdateNode(&currentNode, &newNode); err != nil {
  84. logger.Log(1, "error saving node", err.Error())
  85. }
  86. if shouldUpdatePeers {
  87. if err := PublishPeerUpdate(&newNode); err != nil {
  88. logger.Log(1, "error publishing peer update ", err.Error())
  89. return
  90. }
  91. }
  92. logger.Log(1, "no need to update peers")
  93. }()
  94. }
  95. // PublishPeerUpdate --- deterines and publishes a peer update to all the peers of a node
  96. func PublishPeerUpdate(newNode *models.Node) error {
  97. // shouldn't need this becaus of runServerPeerUpdate, but test to make sure peers are getting updated
  98. // if newNode.IsServer == "yes" {
  99. // logic.SetPeersIfLeader(newNode)
  100. // }
  101. networkNodes, err := logic.GetNetworkNodes(newNode.Network)
  102. if err != nil {
  103. logger.Log(1, "err getting Network Nodes", err.Error())
  104. return err
  105. }
  106. for _, node := range networkNodes {
  107. if node.IsServer == "yes" {
  108. continue
  109. }
  110. peerUpdate, err := logic.GetPeerUpdate(&node)
  111. if err != nil {
  112. logger.Log(1, "error getting peer update for node", node.ID, err.Error())
  113. continue
  114. }
  115. data, err := json.Marshal(&peerUpdate)
  116. if err != nil {
  117. logger.Log(2, "error marshaling peer update for node", node.ID, err.Error())
  118. continue
  119. }
  120. if err = publish(&node, fmt.Sprintf("peers/%s/%s", node.Network, node.ID), data); err != nil {
  121. logger.Log(1, "failed to publish peer update for node", node.ID)
  122. } else {
  123. logger.Log(0, "sent peer update for network, %s and node, %s, peers sent: \n%v", node.Network, node.Name, string(data))
  124. }
  125. }
  126. return nil
  127. }
  128. // GetID -- decodes a message queue topic and returns the embedded node.ID
  129. func GetID(topic string) (string, error) {
  130. parts := strings.Split(topic, "/")
  131. count := len(parts)
  132. if count == 1 {
  133. return "", errors.New("invalid topic")
  134. }
  135. //the last part of the topic will be the node.ID
  136. return parts[count-1], nil
  137. }
  138. // NodeUpdate -- publishes a node update
  139. func NodeUpdate(node *models.Node) error {
  140. if !servercfg.IsMessageQueueBackend() {
  141. return nil
  142. }
  143. logger.Log(3, "publishing node update to "+node.Name)
  144. data, err := json.Marshal(node)
  145. if err != nil {
  146. logger.Log(2, "error marshalling node update ", err.Error())
  147. return err
  148. }
  149. if err = publish(node, fmt.Sprintf("update/%s/%s", node.Network, node.ID), data); err != nil {
  150. logger.Log(2, "error publishing node update to peer ", node.ID, err.Error())
  151. return err
  152. }
  153. return nil
  154. }
  155. // SetupMQTT creates a connection to broker and return client
  156. func SetupMQTT() mqtt.Client {
  157. opts := mqtt.NewClientOptions()
  158. broker := servercfg.GetMessageQueueEndpoint()
  159. opts.AddBroker(broker)
  160. client := mqtt.NewClient(opts)
  161. tperiod := time.Now().Add(10 * time.Second)
  162. for {
  163. if token := client.Connect(); token.Wait() && token.Error() != nil {
  164. logger.Log(2, "unable to connect to broker, retrying ...")
  165. if time.Now().After(tperiod) {
  166. log.Fatal(0, "could not connect to broker, exiting ...", token.Error())
  167. }
  168. } else {
  169. break
  170. }
  171. time.Sleep(2 * time.Second)
  172. }
  173. logger.Log(2, "connected to message queue", broker)
  174. return client
  175. }
  176. // Keepalive -- periodically pings all nodes to let them know server is still alive and doing well
  177. func Keepalive(ctx context.Context) {
  178. for {
  179. select {
  180. case <-ctx.Done():
  181. return
  182. case <-time.After(time.Second * KEEPALIVE_TIMEOUT):
  183. client := SetupMQTT()
  184. networks, err := logic.GetNetworks()
  185. if err != nil {
  186. logger.Log(1, "error retrieving networks for keepalive", err.Error())
  187. }
  188. for _, network := range networks {
  189. var id string
  190. for _, servAddr := range network.DefaultServerAddrs {
  191. if servAddr.IsLeader {
  192. id = servAddr.ID
  193. }
  194. }
  195. if id == "" {
  196. logger.Log(0, "leader not defined for network", network.NetID)
  197. continue
  198. }
  199. if token := client.Publish("serverkeepalive/"+id, 0, false, servercfg.GetVersion()); token.Wait() && token.Error() != nil {
  200. logger.Log(1, "error publishing server keepalive for network", network.NetID, token.Error().Error())
  201. } else {
  202. logger.Log(2, "keepalive sent for network", network.NetID)
  203. }
  204. client.Disconnect(MQ_DISCONNECT)
  205. }
  206. }
  207. }
  208. }