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-10-13 03:28:11 +00:00
|
|
|
// ConnectToBalancer receives connections from brokers and collects stats
|
|
|
|
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()
|
2023-09-25 06:10:33 +00:00
|
|
|
var brokerStats *balancer.BrokerStats
|
2023-09-15 06:49:05 +00:00
|
|
|
if initMessage != nil {
|
2023-09-25 06:10:33 +00:00
|
|
|
var found bool
|
|
|
|
brokerStats, found = broker.Balancer.Brokers.Get(initMessage.Broker)
|
|
|
|
if !found {
|
|
|
|
brokerStats = balancer.NewBrokerStats()
|
2023-09-25 06:34:31 +00:00
|
|
|
if !broker.Balancer.Brokers.SetIfAbsent(initMessage.Broker, brokerStats) {
|
|
|
|
brokerStats, _ = broker.Balancer.Brokers.Get(initMessage.Broker)
|
|
|
|
}
|
2023-09-25 06:10:33 +00:00
|
|
|
}
|
2023-09-15 06:49:05 +00:00
|
|
|
} else {
|
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
|
|
|
|
}
|