2023-12-11 20:05:54 +00:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/sub_coordinator"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SubscriberToSubCoordinator coordinates the subscribers
|
|
|
|
func (b *MessageQueueBroker) SubscriberToSubCoordinator(stream mq_pb.SeaweedMessaging_SubscriberToSubCoordinatorServer) error {
|
|
|
|
if !b.lockAsBalancer.IsLocked() {
|
|
|
|
return status.Errorf(codes.Unavailable, "not current broker balancer")
|
|
|
|
}
|
|
|
|
req, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cgi *sub_coordinator.ConsumerGroupInstance
|
|
|
|
// process init message
|
|
|
|
initMessage := req.GetInit()
|
|
|
|
if initMessage != nil {
|
2023-12-28 19:56:37 +00:00
|
|
|
cgi = b.Coordinator.AddSubscriber(initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic)
|
|
|
|
glog.V(0).Infof("subscriber %s/%s/%s connected", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic)
|
2023-12-11 20:05:54 +00:00
|
|
|
} else {
|
|
|
|
return status.Errorf(codes.InvalidArgument, "subscriber init message is empty")
|
|
|
|
}
|
|
|
|
defer func() {
|
2023-12-28 19:56:37 +00:00
|
|
|
b.Coordinator.RemoveSubscriber(initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic)
|
|
|
|
glog.V(0).Infof("subscriber %s/%s/%s disconnected: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, err)
|
2023-12-11 20:05:54 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
ctx := stream.Context()
|
|
|
|
|
|
|
|
// process ack messages
|
|
|
|
go func() {
|
2023-12-22 19:33:50 +00:00
|
|
|
for {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
if err != nil {
|
2023-12-28 19:56:37 +00:00
|
|
|
glog.V(0).Infof("subscriber %s/%s/%s receive: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, err)
|
2023-12-22 19:33:50 +00:00
|
|
|
}
|
2023-12-11 20:05:54 +00:00
|
|
|
|
2023-12-22 19:33:50 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := ctx.Err()
|
|
|
|
if err == context.Canceled {
|
|
|
|
// Client disconnected
|
|
|
|
return
|
|
|
|
}
|
2023-12-11 20:05:54 +00:00
|
|
|
return
|
2023-12-22 19:33:50 +00:00
|
|
|
default:
|
|
|
|
// Continue processing the request
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// send commands to subscriber
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := ctx.Err()
|
|
|
|
if err == context.Canceled {
|
|
|
|
// Client disconnected
|
|
|
|
return err
|
|
|
|
}
|
2023-12-28 19:56:37 +00:00
|
|
|
glog.V(0).Infof("subscriber %s/%s/%s disconnected: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, err)
|
2023-12-11 20:05:54 +00:00
|
|
|
return err
|
2023-12-22 19:33:50 +00:00
|
|
|
case message := <-cgi.ResponseChan:
|
2024-01-12 07:07:32 +00:00
|
|
|
glog.V(0).Infof("subscriber %s/%s/%s send: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, message)
|
2023-12-11 20:05:54 +00:00
|
|
|
if err := stream.Send(message); err != nil {
|
2023-12-28 19:56:37 +00:00
|
|
|
glog.V(0).Infof("subscriber %s/%s/%s send: %v", initMessage.ConsumerGroup, initMessage.ConsumerGroupInstanceId, initMessage.Topic, err)
|
2023-12-11 20:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|