2020-05-05 09:05:28 +00:00
|
|
|
package msgclient
|
2020-04-18 19:44:55 +00:00
|
|
|
|
2020-04-18 23:05:29 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2020-05-16 15:57: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
|
|
|
)
|
2020-04-18 19:44:55 +00:00
|
|
|
|
|
|
|
type Subscriber struct {
|
2020-04-21 07:59:55 +00:00
|
|
|
subscriberClients []messaging_pb.SeaweedMessaging_SubscribeClient
|
|
|
|
subscriberId string
|
2020-04-18 23:05:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 09:19:51 +00:00
|
|
|
func (mc *MessagingClient) NewSubscriber(subscriberId, namespace, topic string, startTime time.Time) (*Subscriber, error) {
|
2020-04-21 07:59:55 +00:00
|
|
|
// read topic configuration
|
|
|
|
topicConfiguration := &messaging_pb.TopicConfiguration{
|
|
|
|
PartitionCount: 4,
|
|
|
|
}
|
|
|
|
subscriberClients := make([]messaging_pb.SeaweedMessaging_SubscribeClient, topicConfiguration.PartitionCount)
|
|
|
|
|
|
|
|
for i := 0; i < int(topicConfiguration.PartitionCount); i++ {
|
2020-05-16 15:57:29 +00:00
|
|
|
tp := broker.TopicPartition{
|
|
|
|
Namespace: namespace,
|
|
|
|
Topic: topic,
|
|
|
|
Partition: int32(i),
|
|
|
|
}
|
|
|
|
grpcClientConn, err := mc.findBroker(tp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client, err := setupSubscriberClient(grpcClientConn, tp, subscriberId, startTime)
|
2020-04-21 07:59:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
subscriberClients[i] = client
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Subscriber{
|
|
|
|
subscriberClients: subscriberClients,
|
|
|
|
subscriberId: subscriberId,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-16 15:57:29 +00:00
|
|
|
func setupSubscriberClient(grpcConnection *grpc.ClientConn, tp broker.TopicPartition, subscriberId string, startTime time.Time) (stream messaging_pb.SeaweedMessaging_SubscribeClient, err error) {
|
2020-05-05 09:05:28 +00:00
|
|
|
stream, err = messaging_pb.NewSeaweedMessagingClient(grpcConnection).Subscribe(context.Background())
|
|
|
|
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{
|
2020-05-16 15:57:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// process init response
|
2020-05-08 09:47:22 +00:00
|
|
|
_, err = stream.Recv()
|
2020-04-18 23:05:29 +00:00
|
|
|
if err != nil {
|
2020-05-05 09:05:28 +00:00
|
|
|
return
|
2020-04-18 23:05:29 +00:00
|
|
|
}
|
2020-04-21 07:59:55 +00:00
|
|
|
return stream, nil
|
2020-04-18 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 07:59:55 +00:00
|
|
|
func (s *Subscriber) doSubscribe(partition int, processFn func(m *messaging_pb.Message)) error {
|
2020-04-18 23:05:29 +00:00
|
|
|
for {
|
2020-04-21 07:59:55 +00:00
|
|
|
resp, listenErr := s.subscriberClients[partition].Recv()
|
2020-04-18 23:05:29 +00:00
|
|
|
if listenErr == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if listenErr != nil {
|
2020-04-21 07:59:55 +00:00
|
|
|
println(listenErr.Error())
|
2020-04-18 23:05:29 +00:00
|
|
|
return listenErr
|
|
|
|
}
|
2020-05-16 15:57:29 +00:00
|
|
|
if resp.Data == nil {
|
|
|
|
// this could be heartbeat from broker
|
|
|
|
continue
|
|
|
|
}
|
2020-04-18 23:05:29 +00:00
|
|
|
processFn(resp.Data)
|
|
|
|
}
|
2020-04-18 19:44:55 +00:00
|
|
|
}
|
2020-04-21 07:59:55 +00:00
|
|
|
|
|
|
|
// Subscribe starts goroutines to process the messages
|
|
|
|
func (s *Subscriber) Subscribe(processFn func(m *messaging_pb.Message)) {
|
2020-04-29 20:26:02 +00:00
|
|
|
for i := 0; i < len(s.subscriberClients); i++ {
|
2020-04-21 07:59:55 +00:00
|
|
|
go s.doSubscribe(i, processFn)
|
|
|
|
}
|
|
|
|
}
|