broker_stats.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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(1).Infof("broker %s found balancer %s", self, brokerBalancer)
  32. // connect to the lock owner
  33. err = pb.WithBrokerGrpcClient(false, brokerBalancer, broker.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  34. stream, err := client.ConnectToBalancer(context.Background())
  35. if err != nil {
  36. return fmt.Errorf("connect to balancer %v: %v", brokerBalancer, err)
  37. }
  38. defer stream.CloseSend()
  39. err = stream.Send(&mq_pb.ConnectToBalancerRequest{
  40. Message: &mq_pb.ConnectToBalancerRequest_Init{
  41. Init: &mq_pb.ConnectToBalancerRequest_InitMessage{
  42. Broker: self,
  43. },
  44. },
  45. })
  46. if err != nil {
  47. return fmt.Errorf("send init message: %v", err)
  48. }
  49. for {
  50. stats := broker.localTopicManager.CollectStats(time.Second * 5)
  51. err = stream.Send(&mq_pb.ConnectToBalancerRequest{
  52. Message: &mq_pb.ConnectToBalancerRequest_Stats{
  53. Stats: stats,
  54. },
  55. })
  56. if err != nil {
  57. return fmt.Errorf("send stats message: %v", err)
  58. }
  59. time.Sleep(time.Millisecond*5000 + time.Duration(rand.Intn(1000))*time.Millisecond)
  60. }
  61. return nil
  62. })
  63. return err
  64. }