seaweedfs/weed/messaging/msgclient/subscriber.go

121 lines
3.2 KiB
Go
Raw Normal View History

2020-05-05 09:05:28 +00:00
package msgclient
2020-04-18 23:05:29 +00:00
import (
"context"
"io"
"time"
2020-05-18 00:33:53 +00:00
"sync"
2020-04-18 23:05:29 +00:00
"github.com/chrislusf/seaweedfs/weed/messaging/broker"
2020-04-18 23:05:29 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
2020-05-10 10:50:30 +00:00
"google.golang.org/grpc"
2020-04-18 23:05:29 +00:00
)
type Subscriber struct {
subscriberClients []messaging_pb.SeaweedMessaging_SubscribeClient
2020-05-17 18:10:45 +00:00
subscriberCancels []context.CancelFunc
subscriberId string
2020-04-18 23:05:29 +00:00
}
2020-05-17 01:53:54 +00:00
func (mc *MessagingClient) NewSubscriber(subscriberId, namespace, topic string, partitionId int, startTime time.Time) (*Subscriber, error) {
// read topic configuration
topicConfiguration := &messaging_pb.TopicConfiguration{
PartitionCount: 4,
}
subscriberClients := make([]messaging_pb.SeaweedMessaging_SubscribeClient, topicConfiguration.PartitionCount)
2020-05-17 18:10:45 +00:00
subscriberCancels := make([]context.CancelFunc, topicConfiguration.PartitionCount)
for i := 0; i < int(topicConfiguration.PartitionCount); i++ {
2020-05-17 01:53:54 +00:00
if partitionId>=0 && i != partitionId {
continue
}
tp := broker.TopicPartition{
Namespace: namespace,
Topic: topic,
Partition: int32(i),
}
grpcClientConn, err := mc.findBroker(tp)
if err != nil {
return nil, err
}
2020-05-17 18:10:45 +00:00
ctx, cancel := context.WithCancel(context.Background())
client, err := setupSubscriberClient(ctx, grpcClientConn, tp, subscriberId, startTime)
if err != nil {
return nil, err
}
subscriberClients[i] = client
2020-05-17 18:10:45 +00:00
subscriberCancels[i] = cancel
}
return &Subscriber{
subscriberClients: subscriberClients,
2020-05-17 18:10:45 +00:00
subscriberCancels: subscriberCancels,
subscriberId: subscriberId,
}, nil
}
2020-05-17 18:10:45 +00:00
func setupSubscriberClient(ctx context.Context, grpcConnection *grpc.ClientConn, tp broker.TopicPartition, subscriberId string, startTime time.Time) (stream messaging_pb.SeaweedMessaging_SubscribeClient, err error) {
stream, err = messaging_pb.NewSeaweedMessagingClient(grpcConnection).Subscribe(ctx)
2020-05-05 09:05:28 +00:00
if err != nil {
return
2020-04-18 23:05:29 +00:00
}
// send init message
err = stream.Send(&messaging_pb.SubscriberMessage{
Init: &messaging_pb.SubscriberMessage_InitMessage{
Namespace: tp.Namespace,
Topic: tp.Topic,
Partition: tp.Partition,
2020-04-18 23:05:29 +00:00
StartPosition: messaging_pb.SubscriberMessage_InitMessage_TIMESTAMP,
2020-04-30 09:19:51 +00:00
TimestampNs: startTime.UnixNano(),
2020-04-18 23:05:29 +00:00
SubscriberId: subscriberId,
},
})
if err != nil {
2020-05-05 09:05:28 +00:00
return
2020-04-18 23:05:29 +00:00
}
return stream, nil
}
2020-05-17 01:53:54 +00:00
func doSubscribe(subscriberClient messaging_pb.SeaweedMessaging_SubscribeClient, processFn func(m *messaging_pb.Message)) error {
2020-04-18 23:05:29 +00:00
for {
2020-05-17 01:53:54 +00:00
resp, listenErr := subscriberClient.Recv()
2020-04-18 23:05:29 +00:00
if listenErr == io.EOF {
return nil
}
if listenErr != nil {
println(listenErr.Error())
2020-04-18 23:05:29 +00:00
return listenErr
}
if resp.Data == nil {
// this could be heartbeat from broker
continue
}
2020-04-18 23:05:29 +00:00
processFn(resp.Data)
}
}
// Subscribe starts goroutines to process the messages
func (s *Subscriber) Subscribe(processFn func(m *messaging_pb.Message)) {
2020-05-18 00:33:53 +00:00
var wg sync.WaitGroup
2020-04-29 20:26:02 +00:00
for i := 0; i < len(s.subscriberClients); i++ {
2020-05-17 01:53:54 +00:00
if s.subscriberClients[i] != nil {
2020-05-18 00:33:53 +00:00
wg.Add(1)
2020-05-18 00:38:21 +00:00
go func(subscriberClient messaging_pb.SeaweedMessaging_SubscribeClient) {
2020-05-18 00:33:53 +00:00
defer wg.Done()
2020-05-18 00:38:21 +00:00
doSubscribe(subscriberClient, processFn)
}(s.subscriberClients[i])
2020-05-17 01:53:54 +00:00
}
}
2020-05-18 00:33:53 +00:00
wg.Wait()
}
2020-05-17 18:10:45 +00:00
func (s *Subscriber) Shutdown() {
for i := 0; i < len(s.subscriberClients); i++ {
if s.subscriberCancels[i] != nil {
s.subscriberCancels[i]()
}
}
}