seaweedfs/weed/mq/broker/broker_grpc_balancer.go

53 lines
1.5 KiB
Go
Raw Normal View History

2023-09-15 14:49:05 +08:00
package broker
import (
2023-09-17 06:05:38 +08:00
"github.com/seaweedfs/seaweedfs/weed/glog"
2023-09-15 14:49:05 +08:00
"github.com/seaweedfs/seaweedfs/weed/mq/balancer"
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
2023-09-17 06:05:38 +08:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2023-09-15 14:49:05 +08:00
)
2023-09-17 06:05:38 +08:00
// BrokerConnectToBalancer receives connections from brokers and collects stats
2023-09-15 14:49:05 +08:00
func (broker *MessageQueueBroker) ConnectToBalancer(stream mq_pb.SeaweedMessaging_ConnectToBalancerServer) error {
2023-09-17 06:05:38 +08:00
if !broker.lockAsBalancer.IsLocked() {
return status.Errorf(codes.Unavailable, "not current broker balancer")
}
2023-09-15 14:49:05 +08:00
req, err := stream.Recv()
if err != nil {
return err
}
2023-09-17 06:05:38 +08:00
// process init message
2023-09-15 14:49:05 +08:00
initMessage := req.GetInit()
brokerStats := balancer.NewBrokerStats()
if initMessage != nil {
broker.Balancer.Brokers.Set(initMessage.Broker, brokerStats)
} else {
2023-09-17 06:05:38 +08:00
return status.Errorf(codes.InvalidArgument, "balancer init message is empty")
2023-09-15 14:49:05 +08:00
}
defer func() {
broker.Balancer.Brokers.Remove(initMessage.Broker)
}()
2023-09-17 06:05:38 +08:00
// process stats message
2023-09-15 14:49:05 +08:00
for {
req, err := stream.Recv()
if err != nil {
return err
}
2023-09-17 06:05:38 +08:00
if !broker.lockAsBalancer.IsLocked() {
return status.Errorf(codes.Unavailable, "not current broker balancer")
}
2023-09-15 14:49:05 +08:00
if receivedStats := req.GetStats(); receivedStats != nil {
brokerStats.TopicPartitionCount = receivedStats.TopicPartitionCount
2023-09-17 06:05:38 +08:00
brokerStats.ConsumerCount = receivedStats.ConsumerCount
2023-09-15 14:49:05 +08:00
brokerStats.CpuUsagePercent = receivedStats.CpuUsagePercent
2023-09-17 06:05:38 +08:00
glog.V(3).Infof("broker %s stats: %+v", initMessage.Broker, brokerStats)
2023-09-15 14:49:05 +08:00
}
}
return nil
}