seaweedfs/weed/mq/broker/broker_grpc_balancer.go

53 lines
1.4 KiB
Go
Raw Normal View History

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