broker_connect.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package broker
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  8. "io"
  9. "math/rand"
  10. "time"
  11. )
  12. // BrokerConnectToBalancer connects to the broker balancer and sends stats
  13. func (b *MessageQueueBroker) BrokerConnectToBalancer(brokerBalancer string, stopCh chan struct{}) error {
  14. self := string(b.option.BrokerAddress())
  15. glog.V(0).Infof("broker %s connects to balancer %s", self, brokerBalancer)
  16. if brokerBalancer == "" {
  17. return fmt.Errorf("no balancer found")
  18. }
  19. // connect to the lock owner
  20. return pb.WithBrokerGrpcClient(true, brokerBalancer, b.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  21. stream, err := client.PublisherToPubBalancer(context.Background())
  22. if err != nil {
  23. return fmt.Errorf("connect to balancer %v: %v", brokerBalancer, err)
  24. }
  25. defer stream.CloseSend()
  26. err = stream.Send(&mq_pb.PublisherToPubBalancerRequest{
  27. Message: &mq_pb.PublisherToPubBalancerRequest_Init{
  28. Init: &mq_pb.PublisherToPubBalancerRequest_InitMessage{
  29. Broker: self,
  30. },
  31. },
  32. })
  33. if err != nil {
  34. return fmt.Errorf("send init message: %v", err)
  35. }
  36. for {
  37. // check if the broker is stopping
  38. select {
  39. case <-stopCh:
  40. return nil
  41. default:
  42. }
  43. stats := b.localTopicManager.CollectStats(time.Second * 5)
  44. err = stream.Send(&mq_pb.PublisherToPubBalancerRequest{
  45. Message: &mq_pb.PublisherToPubBalancerRequest_Stats{
  46. Stats: stats,
  47. },
  48. })
  49. if err != nil {
  50. if err == io.EOF {
  51. // return err
  52. }
  53. return fmt.Errorf("send stats message to balancer %s: %v", brokerBalancer, err)
  54. }
  55. // glog.V(3).Infof("sent stats: %+v", stats)
  56. time.Sleep(time.Millisecond*5000 + time.Duration(rand.Intn(1000))*time.Millisecond)
  57. }
  58. })
  59. }
  60. func (b *MessageQueueBroker) KeepConnectedToBrokerBalancer(newBrokerBalancerCh chan string) {
  61. var stopPrevRunChan chan struct{}
  62. for {
  63. select {
  64. case newBrokerBalancer := <-newBrokerBalancerCh:
  65. if stopPrevRunChan != nil {
  66. close(stopPrevRunChan)
  67. stopPrevRunChan = nil
  68. }
  69. thisRunStopChan := make(chan struct{})
  70. if newBrokerBalancer != "" {
  71. stopPrevRunChan = thisRunStopChan
  72. go func() {
  73. for {
  74. err := b.BrokerConnectToBalancer(newBrokerBalancer, thisRunStopChan)
  75. if err != nil {
  76. glog.V(0).Infof("connect to balancer %s: %v", newBrokerBalancer, err)
  77. time.Sleep(time.Second)
  78. } else {
  79. break
  80. }
  81. select {
  82. case <-thisRunStopChan:
  83. return
  84. default:
  85. }
  86. }
  87. }()
  88. }
  89. }
  90. }
  91. }