seaweedfs/weed/messaging/msgclient/subscriber.go

121 lines
3.2 KiB
Go
Raw Normal View History

2020-05-05 17:05:28 +08:00
package msgclient
2020-04-19 07:05:29 +08:00
import (
"context"
"io"
"time"
2020-05-18 08:33:53 +08:00
"sync"
2020-04-19 07:05:29 +08:00
"github.com/chrislusf/seaweedfs/weed/messaging/broker"
2020-04-19 07:05:29 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
2020-05-10 18:50:30 +08:00
"google.golang.org/grpc"
2020-04-19 07:05:29 +08:00
)
type Subscriber struct {
subscriberClients []messaging_pb.SeaweedMessaging_SubscribeClient
2020-05-18 02:10:45 +08:00
subscriberCancels []context.CancelFunc
subscriberId string
2020-04-19 07:05:29 +08:00
}
2020-05-17 09:53:54 +08: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-18 02:10:45 +08:00
subscriberCancels := make([]context.CancelFunc, topicConfiguration.PartitionCount)
for i := 0; i < int(topicConfiguration.PartitionCount); i++ {
2020-05-17 09:53:54 +08: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-18 02:10:45 +08: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-18 02:10:45 +08:00
subscriberCancels[i] = cancel
}
return &Subscriber{
subscriberClients: subscriberClients,
2020-05-18 02:10:45 +08:00
subscriberCancels: subscriberCancels,
subscriberId: subscriberId,
}, nil
}
2020-05-18 02:10:45 +08: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 17:05:28 +08:00
if err != nil {
return
2020-04-19 07:05:29 +08: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-19 07:05:29 +08:00
StartPosition: messaging_pb.SubscriberMessage_InitMessage_TIMESTAMP,
2020-04-30 17:19:51 +08:00
TimestampNs: startTime.UnixNano(),
2020-04-19 07:05:29 +08:00
SubscriberId: subscriberId,
},
})
if err != nil {
2020-05-05 17:05:28 +08:00
return
2020-04-19 07:05:29 +08:00
}
return stream, nil
}
2020-05-17 09:53:54 +08:00
func doSubscribe(subscriberClient messaging_pb.SeaweedMessaging_SubscribeClient, processFn func(m *messaging_pb.Message)) error {
2020-04-19 07:05:29 +08:00
for {
2020-05-17 09:53:54 +08:00
resp, listenErr := subscriberClient.Recv()
2020-04-19 07:05:29 +08:00
if listenErr == io.EOF {
return nil
}
if listenErr != nil {
println(listenErr.Error())
2020-04-19 07:05:29 +08:00
return listenErr
}
if resp.Data == nil {
// this could be heartbeat from broker
continue
}
2020-04-19 07:05:29 +08:00
processFn(resp.Data)
}
}
// Subscribe starts goroutines to process the messages
func (s *Subscriber) Subscribe(processFn func(m *messaging_pb.Message)) {
2020-05-18 08:33:53 +08:00
var wg sync.WaitGroup
2020-04-30 04:26:02 +08:00
for i := 0; i < len(s.subscriberClients); i++ {
2020-05-17 09:53:54 +08:00
if s.subscriberClients[i] != nil {
2020-05-18 08:33:53 +08:00
wg.Add(1)
2020-05-18 08:38:21 +08:00
go func(subscriberClient messaging_pb.SeaweedMessaging_SubscribeClient) {
2020-05-18 08:33:53 +08:00
defer wg.Done()
2020-05-18 08:38:21 +08:00
doSubscribe(subscriberClient, processFn)
}(s.subscriberClients[i])
2020-05-17 09:53:54 +08:00
}
}
2020-05-18 08:33:53 +08:00
wg.Wait()
}
2020-05-18 02:10:45 +08:00
func (s *Subscriber) Shutdown() {
for i := 0; i < len(s.subscriberClients); i++ {
if s.subscriberCancels[i] != nil {
s.subscriberCancels[i]()
}
}
}