You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. package broker
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/mq/balancer"
  7. "github.com/seaweedfs/seaweedfs/weed/pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  10. "math/rand"
  11. "time"
  12. )
  13. // BrokerConnectToBalancer connects to the broker balancer and sends stats
  14. func (broker *MessageQueueBroker) BrokerConnectToBalancer(self string) error {
  15. // find the lock owner
  16. var brokerBalancer string
  17. err := broker.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  18. resp, err := client.FindLockOwner(context.Background(), &filer_pb.FindLockOwnerRequest{
  19. Name: balancer.LockBrokerBalancer,
  20. })
  21. if err != nil {
  22. return err
  23. }
  24. brokerBalancer = resp.Owner
  25. return nil
  26. })
  27. if err != nil {
  28. return err
  29. }
  30. broker.currentBalancer = pb.ServerAddress(brokerBalancer)
  31. glog.V(0).Infof("broker %s found balancer %s", self, brokerBalancer)
  32. if brokerBalancer == "" {
  33. return fmt.Errorf("no balancer found")
  34. }
  35. // connect to the lock owner
  36. err = pb.WithBrokerGrpcClient(false, brokerBalancer, broker.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  37. stream, err := client.ConnectToBalancer(context.Background())
  38. if err != nil {
  39. return fmt.Errorf("connect to balancer %v: %v", brokerBalancer, err)
  40. }
  41. defer stream.CloseSend()
  42. err = stream.Send(&mq_pb.ConnectToBalancerRequest{
  43. Message: &mq_pb.ConnectToBalancerRequest_Init{
  44. Init: &mq_pb.ConnectToBalancerRequest_InitMessage{
  45. Broker: self,
  46. },
  47. },
  48. })
  49. if err != nil {
  50. return fmt.Errorf("send init message: %v", err)
  51. }
  52. for {
  53. stats := broker.localTopicManager.CollectStats(time.Second * 5)
  54. err = stream.Send(&mq_pb.ConnectToBalancerRequest{
  55. Message: &mq_pb.ConnectToBalancerRequest_Stats{
  56. Stats: stats,
  57. },
  58. })
  59. if err != nil {
  60. return fmt.Errorf("send stats message: %v", err)
  61. }
  62. glog.V(3).Infof("sent stats: %+v", stats)
  63. time.Sleep(time.Millisecond*5000 + time.Duration(rand.Intn(1000))*time.Millisecond)
  64. }
  65. return nil
  66. })
  67. return err
  68. }