seaweedfs/weed/messaging/broker/broker_grpc_server_subscribe.go

95 lines
2.3 KiB
Go
Raw Normal View History

2020-04-18 16:12:01 +08:00
package broker
import (
"fmt"
2020-04-18 16:12:01 +08:00
"io"
"time"
"github.com/golang/protobuf/proto"
"github.com/chrislusf/seaweedfs/weed/glog"
2020-04-19 15:18:32 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-04-18 16:12:01 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
)
func (broker *MessageBroker) Subscribe(stream messaging_pb.SeaweedMessaging_SubscribeServer) error {
// process initial request
in, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
var messageCount int64
2020-04-18 16:12:01 +08:00
subscriberId := in.Init.SubscriberId
fmt.Printf("+ subscriber %s\n", subscriberId)
defer func() {
fmt.Printf("- subscriber %s: %d messages\n", subscriberId, messageCount)
}()
2020-04-19 15:18:32 +08:00
// TODO look it up
topicConfig := &messaging_pb.TopicConfiguration{
IsTransient: true,
}
if err = stream.Send(&messaging_pb.BrokerMessage{
Redirect: nil,
}); err != nil {
return err
2020-04-19 15:18:32 +08:00
}
2020-04-18 16:12:01 +08:00
// get lock
tp := TopicPartition{
Namespace: in.Init.Namespace,
Topic: in.Init.Topic,
Partition: in.Init.Partition,
}
2020-04-19 15:18:32 +08:00
lock := broker.topicLocks.RequestLock(tp, topicConfig, false)
2020-04-18 16:12:01 +08:00
defer broker.topicLocks.ReleaseLock(tp, false)
lastReadTime := time.Now()
switch in.Init.StartPosition {
case messaging_pb.SubscriberMessage_InitMessage_TIMESTAMP:
lastReadTime = time.Unix(0, in.Init.TimestampNs)
case messaging_pb.SubscriberMessage_InitMessage_LATEST:
case messaging_pb.SubscriberMessage_InitMessage_EARLIEST:
}
// how to process each message
// an error returned will end the subscription
eachMessageFn := func(m *messaging_pb.Message) error {
err := stream.Send(&messaging_pb.BrokerMessage{
Data: m,
2020-04-18 16:12:01 +08:00
})
if err != nil {
glog.V(0).Infof("=> subscriber %v: %+v", subscriberId, err)
}
return err
}
messageCount, err = lock.logBuffer.LoopProcessLogData(lastReadTime, func() bool {
2020-04-18 16:12:01 +08:00
lock.Mutex.Lock()
2020-04-19 15:18:32 +08:00
lock.cond.Wait()
2020-04-18 16:12:01 +08:00
lock.Mutex.Unlock()
return true
}, func(logEntry *filer_pb.LogEntry) error {
m := &messaging_pb.Message{}
if err = proto.Unmarshal(logEntry.Data, m); err != nil {
glog.Errorf("unexpected unmarshal messaging_pb.Message: %v", err)
return err
}
// fmt.Printf("sending : %d bytes\n", len(m.Value))
if err = eachMessageFn(m); err != nil {
glog.Errorf("sending %d bytes to %s: %s", len(m.Value), subscriberId, err)
return err
}
return nil
})
return err
2020-04-18 16:12:01 +08:00
}