broker_grpc_balancer.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package broker
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/glog"
  4. "github.com/seaweedfs/seaweedfs/weed/mq/balancer"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  6. "google.golang.org/grpc/codes"
  7. "google.golang.org/grpc/status"
  8. )
  9. // BrokerConnectToBalancer receives connections from brokers and collects stats
  10. func (broker *MessageQueueBroker) ConnectToBalancer(stream mq_pb.SeaweedMessaging_ConnectToBalancerServer) error {
  11. if !broker.lockAsBalancer.IsLocked() {
  12. return status.Errorf(codes.Unavailable, "not current broker balancer")
  13. }
  14. req, err := stream.Recv()
  15. if err != nil {
  16. return err
  17. }
  18. // process init message
  19. initMessage := req.GetInit()
  20. brokerStats := balancer.NewBrokerStats()
  21. if initMessage != nil {
  22. broker.Balancer.Brokers.Set(initMessage.Broker, brokerStats)
  23. } else {
  24. return status.Errorf(codes.InvalidArgument, "balancer init message is empty")
  25. }
  26. defer func() {
  27. broker.Balancer.Brokers.Remove(initMessage.Broker)
  28. }()
  29. // process stats message
  30. for {
  31. req, err := stream.Recv()
  32. if err != nil {
  33. return err
  34. }
  35. if !broker.lockAsBalancer.IsLocked() {
  36. return status.Errorf(codes.Unavailable, "not current broker balancer")
  37. }
  38. if receivedStats := req.GetStats(); receivedStats != nil {
  39. brokerStats.UpdateStats(receivedStats)
  40. glog.V(3).Infof("broker %s stats: %+v", initMessage.Broker, brokerStats)
  41. glog.V(3).Infof("received stats: %+v", receivedStats)
  42. }
  43. }
  44. return nil
  45. }