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-12-11 20:05:54 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
|
2023-09-15 06:49:05 +00:00
|
|
|
"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-12-11 20:05:54 +00:00
|
|
|
// PublisherToPubBalancer receives connections from brokers and collects stats
|
|
|
|
func (b *MessageQueueBroker) PublisherToPubBalancer(stream mq_pb.SeaweedMessaging_PublisherToPubBalancerServer) error {
|
|
|
|
if !b.lockAsBalancer.IsLocked() {
|
2023-09-16 22:05:38 +00:00
|
|
|
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-12-11 20:05:54 +00:00
|
|
|
var brokerStats *pub_balancer.BrokerStats
|
2023-09-15 06:49:05 +00:00
|
|
|
if initMessage != nil {
|
2023-12-11 20:05:54 +00:00
|
|
|
brokerStats = b.Balancer.OnBrokerConnected(initMessage.Broker)
|
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() {
|
2023-12-11 20:05:54 +00:00
|
|
|
b.Balancer.OnBrokerDisconnected(initMessage.Broker, brokerStats)
|
2023-09-15 06:49:05 +00:00
|
|
|
}()
|
|
|
|
|
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-12-11 20:05:54 +00:00
|
|
|
if !b.lockAsBalancer.IsLocked() {
|
2023-09-16 22:05:38 +00:00
|
|
|
return status.Errorf(codes.Unavailable, "not current broker balancer")
|
|
|
|
}
|
2023-09-15 06:49:05 +00:00
|
|
|
if receivedStats := req.GetStats(); receivedStats != nil {
|
2023-12-11 20:05:54 +00:00
|
|
|
b.Balancer.OnBrokerStatsUpdated(initMessage.Broker, brokerStats, receivedStats)
|
2023-12-29 04:35:15 +00:00
|
|
|
glog.V(4).Infof("received from %v: %+v", initMessage.Broker, receivedStats)
|
2023-09-15 06:49:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|