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.

58 lines
1.6 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. 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. }