broker_grpc_pub_balancer.go 1.4 KB

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