2023-08-28 04:13:14 +08:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
2023-12-12 04:05:54 +08:00
|
|
|
"context"
|
2023-08-28 08:50:59 +08:00
|
|
|
"fmt"
|
2023-08-28 04:13:14 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-01-06 07:14:25 +08:00
|
|
|
func (b *MessageQueueBroker) SubscribeMessage(req *mq_pb.SubscribeMessageRequest, stream mq_pb.SeaweedMessaging_SubscribeMessageServer) error {
|
2023-08-28 04:13:14 +08:00
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
t := topic.FromPbTopic(req.GetInit().Topic)
|
2024-01-06 07:24:14 +08:00
|
|
|
partition := topic.FromPbPartition(req.GetInit().GetPartitionOffset().GetPartition())
|
2023-12-12 04:05:54 +08:00
|
|
|
localTopicPartition := b.localTopicManager.GetTopicPartition(t, partition)
|
2023-08-28 04:13:14 +08:00
|
|
|
if localTopicPartition == nil {
|
2024-01-06 07:14:25 +08:00
|
|
|
stream.Send(&mq_pb.SubscribeMessageResponse{
|
|
|
|
Message: &mq_pb.SubscribeMessageResponse_Ctrl{
|
|
|
|
Ctrl: &mq_pb.SubscribeMessageResponse_CtrlMessage{
|
2023-09-01 15:36:51 +08:00
|
|
|
Error: "not initialized",
|
2023-08-28 04:13:14 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-01 12:47:57 +08:00
|
|
|
clientName := fmt.Sprintf("%s/%s-%s", req.GetInit().ConsumerGroup, req.GetInit().ConsumerId, req.GetInit().ClientId)
|
2023-12-12 04:05:54 +08:00
|
|
|
localTopicPartition.Subscribers.AddSubscriber(clientName, topic.NewLocalSubscriber())
|
|
|
|
glog.V(0).Infof("Subscriber %s connected on %v %v", clientName, t, partition)
|
|
|
|
isConnected := true
|
|
|
|
sleepIntervalCount := 0
|
|
|
|
defer func() {
|
|
|
|
isConnected = false
|
|
|
|
localTopicPartition.Subscribers.RemoveSubscriber(clientName)
|
|
|
|
glog.V(0).Infof("Subscriber %s on %v %v disconnected", clientName, t, partition)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx := stream.Context()
|
2024-01-04 07:57:36 +08:00
|
|
|
startTime := time.Now()
|
2024-01-06 07:24:14 +08:00
|
|
|
if startTs := req.GetInit().GetPartitionOffset().GetTsNs(); startTs > 0 {
|
2023-12-12 04:05:54 +08:00
|
|
|
startTime = time.Unix(0, startTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
localTopicPartition.Subscribe(clientName, startTime, func() bool {
|
|
|
|
if !isConnected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sleepIntervalCount++
|
|
|
|
if sleepIntervalCount > 10 {
|
|
|
|
sleepIntervalCount = 10
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(sleepIntervalCount) * 2339 * time.Millisecond)
|
|
|
|
|
|
|
|
// Check if the client has disconnected by monitoring the context
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := ctx.Err()
|
|
|
|
if err == context.Canceled {
|
|
|
|
// Client disconnected
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
glog.V(0).Infof("Subscriber %s disconnected: %v", clientName, err)
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
// Continue processing the request
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}, func(logEntry *filer_pb.LogEntry) error {
|
|
|
|
// reset the sleep interval count
|
|
|
|
sleepIntervalCount = 0
|
2023-09-01 15:36:51 +08:00
|
|
|
|
2023-08-28 08:50:59 +08:00
|
|
|
value := logEntry.GetData()
|
2024-01-06 07:14:25 +08:00
|
|
|
if err := stream.Send(&mq_pb.SubscribeMessageResponse{Message: &mq_pb.SubscribeMessageResponse_Data{
|
2023-08-28 04:13:14 +08:00
|
|
|
Data: &mq_pb.DataMessage{
|
2023-08-28 08:50:59 +08:00
|
|
|
Key: []byte(fmt.Sprintf("key-%d", logEntry.PartitionKeyHash)),
|
|
|
|
Value: value,
|
2023-12-12 04:05:54 +08:00
|
|
|
TsNs: logEntry.TsNs,
|
2023-08-28 04:13:14 +08:00
|
|
|
},
|
|
|
|
}}); err != nil {
|
|
|
|
glog.Errorf("Error sending setup response: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|