broker_grpc_pub_balancer.go 1.3 KB

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