2023-09-15 14:49:05 +08:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
2024-05-17 02:02:48 +08:00
|
|
|
"fmt"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
|
2023-09-15 14:49:05 +08:00
|
|
|
"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-12-12 04:05:54 +08:00
|
|
|
// PublisherToPubBalancer receives connections from brokers and collects stats
|
|
|
|
func (b *MessageQueueBroker) PublisherToPubBalancer(stream mq_pb.SeaweedMessaging_PublisherToPubBalancerServer) error {
|
2024-02-06 10:00:50 +08:00
|
|
|
if !b.isLockOwner() {
|
2023-09-17 06:05:38 +08:00
|
|
|
return status.Errorf(codes.Unavailable, "not current broker balancer")
|
|
|
|
}
|
2023-09-15 14:49:05 +08:00
|
|
|
req, err := stream.Recv()
|
|
|
|
if err != nil {
|
2024-05-17 02:02:48 +08:00
|
|
|
return fmt.Errorf("receive init message: %v", err)
|
2023-09-15 14:49:05 +08:00
|
|
|
}
|
2023-09-17 06:05:38 +08:00
|
|
|
|
|
|
|
// process init message
|
2023-09-15 14:49:05 +08:00
|
|
|
initMessage := req.GetInit()
|
2023-12-12 04:05:54 +08:00
|
|
|
var brokerStats *pub_balancer.BrokerStats
|
2023-09-15 14:49:05 +08:00
|
|
|
if initMessage != nil {
|
2024-01-21 03:41:11 +08:00
|
|
|
brokerStats = b.Balancer.AddBroker(initMessage.Broker)
|
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() {
|
2024-01-21 03:41:11 +08:00
|
|
|
b.Balancer.RemoveBroker(initMessage.Broker, brokerStats)
|
2023-09-15 14:49:05 +08:00
|
|
|
}()
|
|
|
|
|
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 {
|
2024-05-17 02:02:48 +08:00
|
|
|
return fmt.Errorf("receive stats message from %s: %v", initMessage.Broker, err)
|
2023-09-15 14:49:05 +08:00
|
|
|
}
|
2024-02-06 10:00:50 +08:00
|
|
|
if !b.isLockOwner() {
|
2023-09-17 06:05:38 +08:00
|
|
|
return status.Errorf(codes.Unavailable, "not current broker balancer")
|
|
|
|
}
|
2023-09-15 14:49:05 +08:00
|
|
|
if receivedStats := req.GetStats(); receivedStats != nil {
|
2023-12-12 04:05:54 +08:00
|
|
|
b.Balancer.OnBrokerStatsUpdated(initMessage.Broker, brokerStats, receivedStats)
|
2024-01-08 16:03:08 +08:00
|
|
|
// glog.V(4).Infof("received from %v: %+v", initMessage.Broker, receivedStats)
|
2023-09-15 14:49:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|