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"
|
2024-01-08 16:03:08 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
2023-08-28 04:13:14 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-01-17 16:15:52 +08:00
|
|
|
func (b *MessageQueueBroker) SubscribeMessage(req *mq_pb.SubscribeMessageRequest, stream mq_pb.SeaweedMessaging_SubscribeMessageServer) (err error) {
|
2023-08-28 04:13:14 +08:00
|
|
|
|
2024-01-12 15:08:02 +08:00
|
|
|
ctx := stream.Context()
|
|
|
|
clientName := fmt.Sprintf("%s/%s-%s", req.GetInit().ConsumerGroup, req.GetInit().ConsumerId, req.GetInit().ClientId)
|
|
|
|
|
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())
|
2024-01-12 15:08:02 +08:00
|
|
|
|
|
|
|
glog.V(0).Infof("Subscriber %s on %v %v connected", req.GetInit().ConsumerId, t, partition)
|
|
|
|
|
2024-01-23 03:56:55 +08:00
|
|
|
waitIntervalCount := 0
|
2024-01-29 07:55:26 +08:00
|
|
|
|
2024-01-12 15:08:02 +08:00
|
|
|
var localTopicPartition *topic.LocalPartition
|
|
|
|
for localTopicPartition == nil {
|
2024-01-29 07:55:26 +08:00
|
|
|
localTopicPartition, err = b.GetOrGenLocalPartition(t, partition)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("topic %v partition %v not setup", t, partition)
|
2024-01-23 03:44:21 +08:00
|
|
|
}
|
|
|
|
if localTopicPartition != nil {
|
|
|
|
break
|
|
|
|
}
|
2024-01-23 03:56:55 +08:00
|
|
|
waitIntervalCount++
|
|
|
|
if waitIntervalCount > 10 {
|
|
|
|
waitIntervalCount = 10
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(waitIntervalCount) * 337 * time.Millisecond)
|
2024-01-22 16:49:57 +08:00
|
|
|
// Check if the client has disconnected by monitoring the context
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
err := ctx.Err()
|
|
|
|
if err == context.Canceled {
|
|
|
|
// Client disconnected
|
2024-01-12 15:08:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-22 16:49:57 +08:00
|
|
|
glog.V(0).Infof("Subscriber %s disconnected: %v", clientName, err)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
// Continue processing the request
|
2024-01-12 15:08:02 +08:00
|
|
|
}
|
2023-08-28 04:13:14 +08:00
|
|
|
}
|
|
|
|
|
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
|
2024-01-15 16:20:12 +08:00
|
|
|
|
|
|
|
var counter int64
|
2023-12-12 04:05:54 +08:00
|
|
|
defer func() {
|
|
|
|
isConnected = false
|
|
|
|
localTopicPartition.Subscribers.RemoveSubscriber(clientName)
|
2024-01-15 16:20:12 +08:00
|
|
|
glog.V(0).Infof("Subscriber %s on %v %v disconnected, sent %d", clientName, t, partition, counter)
|
2024-01-17 00:43:07 +08:00
|
|
|
if localTopicPartition.MaybeShutdownLocalPartition() {
|
|
|
|
b.localTopicManager.RemoveTopicPartition(t, partition)
|
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
}()
|
|
|
|
|
2024-01-08 16:03:08 +08:00
|
|
|
var startPosition log_buffer.MessagePosition
|
2024-03-01 01:38:52 +08:00
|
|
|
if req.GetInit() != nil && req.GetInit().GetPartitionOffset() != nil {
|
2024-01-08 16:03:08 +08:00
|
|
|
offset := req.GetInit().GetPartitionOffset()
|
|
|
|
if offset.StartTsNs != 0 {
|
|
|
|
startPosition = log_buffer.NewMessagePosition(offset.StartTsNs, -2)
|
|
|
|
}
|
|
|
|
if offset.StartType == mq_pb.PartitionOffsetStartType_EARLIEST {
|
2024-01-29 07:55:26 +08:00
|
|
|
startPosition = log_buffer.NewMessagePosition(1, -3)
|
2024-01-08 16:03:08 +08:00
|
|
|
} else if offset.StartType == mq_pb.PartitionOffsetStartType_LATEST {
|
2024-01-29 07:55:26 +08:00
|
|
|
startPosition = log_buffer.NewMessagePosition(time.Now().UnixNano(), -4)
|
2024-01-08 16:03:08 +08:00
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
|
2024-01-15 16:20:12 +08:00
|
|
|
return localTopicPartition.Subscribe(clientName, startPosition, func() bool {
|
2023-12-12 04:05:54 +08:00
|
|
|
if !isConnected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sleepIntervalCount++
|
|
|
|
if sleepIntervalCount > 10 {
|
|
|
|
sleepIntervalCount = 10
|
|
|
|
}
|
2024-01-08 16:03:08 +08:00
|
|
|
time.Sleep(time.Duration(sleepIntervalCount) * 337 * time.Millisecond)
|
2023-12-12 04:05:54 +08:00
|
|
|
|
|
|
|
// 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
|
2024-03-08 02:50:09 +08:00
|
|
|
}, func(logEntry *filer_pb.LogEntry) (bool, error) {
|
2023-12-12 04:05:54 +08:00
|
|
|
// reset the sleep interval count
|
|
|
|
sleepIntervalCount = 0
|
2023-09-01 15:36:51 +08:00
|
|
|
|
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{
|
2024-03-08 02:53:30 +08:00
|
|
|
Key: logEntry.Key,
|
|
|
|
Value: logEntry.Data,
|
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)
|
2024-03-08 02:50:09 +08:00
|
|
|
return false, err
|
2023-08-28 04:13:14 +08:00
|
|
|
}
|
2024-01-15 16:20:12 +08:00
|
|
|
|
|
|
|
counter++
|
2024-03-08 02:50:09 +08:00
|
|
|
return false, nil
|
2023-08-28 04:13:14 +08:00
|
|
|
})
|
|
|
|
}
|