broker_grpc_balancer.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // ConnectToBalancer 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. var brokerStats *balancer.BrokerStats
  21. if initMessage != nil {
  22. var found bool
  23. brokerStats, found = broker.Balancer.Brokers.Get(initMessage.Broker)
  24. if !found {
  25. brokerStats = balancer.NewBrokerStats()
  26. if !broker.Balancer.Brokers.SetIfAbsent(initMessage.Broker, brokerStats) {
  27. brokerStats, _ = broker.Balancer.Brokers.Get(initMessage.Broker)
  28. }
  29. }
  30. } else {
  31. return status.Errorf(codes.InvalidArgument, "balancer init message is empty")
  32. }
  33. defer func() {
  34. broker.Balancer.Brokers.Remove(initMessage.Broker)
  35. }()
  36. // process stats message
  37. for {
  38. req, err := stream.Recv()
  39. if err != nil {
  40. return err
  41. }
  42. if !broker.lockAsBalancer.IsLocked() {
  43. return status.Errorf(codes.Unavailable, "not current broker balancer")
  44. }
  45. if receivedStats := req.GetStats(); receivedStats != nil {
  46. brokerStats.UpdateStats(receivedStats)
  47. glog.V(4).Infof("broker %s stats: %+v", initMessage.Broker, brokerStats)
  48. glog.V(4).Infof("received stats: %+v", receivedStats)
  49. }
  50. }
  51. return nil
  52. }