seaweedfs/weed/mq/broker/broker_grpc_balancer.go

57 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()
2023-09-25 14:10:33 +08:00
var brokerStats *balancer.BrokerStats
2023-09-15 14:49:05 +08:00
if initMessage != nil {
2023-09-25 14:10:33 +08:00
var found bool
brokerStats, found = broker.Balancer.Brokers.Get(initMessage.Broker)
if !found {
brokerStats = balancer.NewBrokerStats()
broker.Balancer.Brokers.Set(initMessage.Broker, brokerStats)
}
2023-09-15 14:49:05 +08:00
} 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 {
2023-09-20 05:02:08 +08:00
brokerStats.UpdateStats(receivedStats)
2023-09-17 06:05:38 +08:00
2023-09-25 14:05:41 +08:00
glog.V(4).Infof("broker %s stats: %+v", initMessage.Broker, brokerStats)
glog.V(4).Infof("received stats: %+v", receivedStats)
2023-09-15 14:49:05 +08:00
}
}
return nil
}