2020-04-18 08:12:01 +00:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
2020-04-20 06:37:04 +00:00
|
|
|
"fmt"
|
2021-09-25 08:18:44 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2020-09-09 18:21:23 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util/log_buffer"
|
2020-04-18 08:12:01 +00:00
|
|
|
"io"
|
2020-04-30 00:40:08 +00:00
|
|
|
"strings"
|
2020-04-18 08:12:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2020-04-18 08:12:01 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2020-04-19 07:18:32 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2022-07-02 05:43:25 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/mq_pb"
|
2020-04-18 08:12:01 +00:00
|
|
|
)
|
|
|
|
|
2022-07-02 05:43:25 +00:00
|
|
|
func (broker *MessageBroker) Subscribe(stream mq_pb.SeaweedMessaging_SubscribeServer) error {
|
2020-04-18 08:12:01 +00:00
|
|
|
|
|
|
|
// process initial request
|
|
|
|
in, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-17 01:53:54 +00:00
|
|
|
var processedTsNs int64
|
2020-04-20 06:37:04 +00:00
|
|
|
var messageCount int64
|
2020-04-18 08:12:01 +00:00
|
|
|
subscriberId := in.Init.SubscriberId
|
2020-04-19 07:18:32 +00:00
|
|
|
|
|
|
|
// TODO look it up
|
2022-07-02 05:43:25 +00:00
|
|
|
topicConfig := &mq_pb.TopicConfiguration{
|
2020-04-30 10:05:34 +00:00
|
|
|
// IsTransient: true,
|
2020-04-20 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 08:12:01 +00:00
|
|
|
// get lock
|
|
|
|
tp := TopicPartition{
|
|
|
|
Namespace: in.Init.Namespace,
|
|
|
|
Topic: in.Init.Topic,
|
|
|
|
Partition: in.Init.Partition,
|
|
|
|
}
|
2020-05-17 01:53:54 +00:00
|
|
|
fmt.Printf("+ subscriber %s for %s\n", subscriberId, tp.String())
|
|
|
|
defer func() {
|
|
|
|
fmt.Printf("- subscriber %s for %s %d messages last %v\n", subscriberId, tp.String(), messageCount, time.Unix(0, processedTsNs))
|
|
|
|
}()
|
|
|
|
|
2020-05-12 15:48:00 +00:00
|
|
|
lock := broker.topicManager.RequestLock(tp, topicConfig, false)
|
|
|
|
defer broker.topicManager.ReleaseLock(tp, false)
|
2020-04-18 08:12:01 +00:00
|
|
|
|
2020-05-16 15:57:29 +00:00
|
|
|
isConnected := true
|
|
|
|
go func() {
|
|
|
|
for isConnected {
|
2020-05-17 15:57:47 +00:00
|
|
|
if _, err := stream.Recv(); err != nil {
|
2020-05-18 00:38:31 +00:00
|
|
|
// println("disconnecting connection to", subscriberId, tp.String())
|
2020-05-16 15:57:29 +00:00
|
|
|
isConnected = false
|
|
|
|
lock.cond.Signal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-04-18 08:12:01 +00:00
|
|
|
lastReadTime := time.Now()
|
|
|
|
switch in.Init.StartPosition {
|
2022-07-02 05:43:25 +00:00
|
|
|
case mq_pb.SubscriberMessage_InitMessage_TIMESTAMP:
|
2020-04-18 08:12:01 +00:00
|
|
|
lastReadTime = time.Unix(0, in.Init.TimestampNs)
|
2022-07-02 05:43:25 +00:00
|
|
|
case mq_pb.SubscriberMessage_InitMessage_LATEST:
|
|
|
|
case mq_pb.SubscriberMessage_InitMessage_EARLIEST:
|
2020-05-10 10:48:35 +00:00
|
|
|
lastReadTime = time.Unix(0, 0)
|
2020-04-18 08:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// how to process each message
|
|
|
|
// an error returned will end the subscription
|
2022-07-02 05:43:25 +00:00
|
|
|
eachMessageFn := func(m *mq_pb.Message) error {
|
|
|
|
err := stream.Send(&mq_pb.BrokerMessage{
|
2020-04-20 06:37:04 +00:00
|
|
|
Data: m,
|
2020-04-18 08:12:01 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("=> subscriber %v: %+v", subscriberId, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:05:44 +00:00
|
|
|
eachLogEntryFn := func(logEntry *filer_pb.LogEntry) error {
|
2022-07-02 05:43:25 +00:00
|
|
|
m := &mq_pb.Message{}
|
2020-04-20 06:37:04 +00:00
|
|
|
if err = proto.Unmarshal(logEntry.Data, m); err != nil {
|
2022-07-02 05:43:25 +00:00
|
|
|
glog.Errorf("unexpected unmarshal mq_pb.Message: %v", err)
|
2020-04-20 06:37:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-30 10:05:34 +00:00
|
|
|
// fmt.Printf("sending : %d bytes ts %d\n", len(m.Value), logEntry.TsNs)
|
2020-04-20 06:37:04 +00:00
|
|
|
if err = eachMessageFn(m); err != nil {
|
|
|
|
glog.Errorf("sending %d bytes to %s: %s", len(m.Value), subscriberId, err)
|
|
|
|
return err
|
|
|
|
}
|
2020-05-08 09:47:22 +00:00
|
|
|
if m.IsClose {
|
|
|
|
// println("processed EOF")
|
|
|
|
return io.EOF
|
|
|
|
}
|
2020-04-30 00:40:08 +00:00
|
|
|
processedTsNs = logEntry.TsNs
|
2020-05-08 09:47:22 +00:00
|
|
|
messageCount++
|
2020-04-20 06:37:04 +00:00
|
|
|
return nil
|
2020-04-28 09:05:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 01:53:54 +00:00
|
|
|
// fmt.Printf("subscriber %s read %d on disk log %v\n", subscriberId, messageCount, lastReadTime)
|
|
|
|
|
2020-09-09 18:21:23 +00:00
|
|
|
for {
|
2021-01-11 10:08:55 +00:00
|
|
|
|
|
|
|
if err = broker.readPersistedLogBuffer(&tp, lastReadTime, eachLogEntryFn); err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
// println("stopping from persisted logs", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if processedTsNs != 0 {
|
|
|
|
lastReadTime = time.Unix(0, processedTsNs)
|
|
|
|
}
|
|
|
|
|
2022-05-30 22:25:21 +00:00
|
|
|
lastReadTime, _, err = lock.logBuffer.LoopProcessLogData("broker", lastReadTime, 0, func() bool {
|
2020-09-09 18:21:23 +00:00
|
|
|
lock.Mutex.Lock()
|
|
|
|
lock.cond.Wait()
|
|
|
|
lock.Mutex.Unlock()
|
|
|
|
return isConnected
|
|
|
|
}, eachLogEntryFn)
|
|
|
|
if err != nil {
|
2021-01-11 10:08:55 +00:00
|
|
|
if err == log_buffer.ResumeFromDiskError {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-09 18:21:23 +00:00
|
|
|
glog.Errorf("processed to %v: %v", lastReadTime, err)
|
|
|
|
if err != log_buffer.ResumeError {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 06:37:04 +00:00
|
|
|
|
|
|
|
return err
|
2020-04-18 08:12:01 +00:00
|
|
|
|
|
|
|
}
|
2020-04-30 00:40:08 +00:00
|
|
|
|
|
|
|
func (broker *MessageBroker) readPersistedLogBuffer(tp *TopicPartition, startTime time.Time, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) (err error) {
|
2020-08-30 04:01:14 +00:00
|
|
|
startTime = startTime.UTC()
|
2020-04-30 00:40:08 +00:00
|
|
|
startDate := fmt.Sprintf("%04d-%02d-%02d", startTime.Year(), startTime.Month(), startTime.Day())
|
2021-09-25 08:18:44 +00:00
|
|
|
startHourMinute := fmt.Sprintf("%02d-%02d", startTime.Hour(), startTime.Minute())
|
2020-04-30 00:40:08 +00:00
|
|
|
|
|
|
|
sizeBuf := make([]byte, 4)
|
|
|
|
startTsNs := startTime.UnixNano()
|
|
|
|
|
2020-05-09 07:31:34 +00:00
|
|
|
topicDir := genTopicDir(tp.Namespace, tp.Topic)
|
2020-04-30 10:05:34 +00:00
|
|
|
partitionSuffix := fmt.Sprintf(".part%02d", tp.Partition)
|
2020-04-30 00:40:08 +00:00
|
|
|
|
|
|
|
return filer_pb.List(broker, topicDir, "", func(dayEntry *filer_pb.Entry, isLast bool) error {
|
|
|
|
dayDir := fmt.Sprintf("%s/%s", topicDir, dayEntry.Name)
|
|
|
|
return filer_pb.List(broker, dayDir, "", func(hourMinuteEntry *filer_pb.Entry, isLast bool) error {
|
|
|
|
if dayEntry.Name == startDate {
|
2021-09-25 08:18:44 +00:00
|
|
|
hourMinute := util.FileNameBase(hourMinuteEntry.Name)
|
|
|
|
if strings.Compare(hourMinute, startHourMinute) < 0 {
|
2020-04-30 00:40:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 09:47:22 +00:00
|
|
|
if !strings.HasSuffix(hourMinuteEntry.Name, partitionSuffix) {
|
2020-04-30 10:05:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// println("partition", tp.Partition, "processing", dayDir, "/", hourMinuteEntry.Name)
|
2020-09-01 07:21:19 +00:00
|
|
|
chunkedFileReader := filer.NewChunkStreamReader(broker, hourMinuteEntry.Chunks)
|
2020-04-30 00:40:08 +00:00
|
|
|
defer chunkedFileReader.Close()
|
2022-05-30 22:04:19 +00:00
|
|
|
if _, err := filer.ReadEachLogEntry(chunkedFileReader, sizeBuf, startTsNs, 0, eachLogEntryFn); err != nil {
|
2020-04-30 00:40:08 +00:00
|
|
|
chunkedFileReader.Close()
|
|
|
|
if err == io.EOF {
|
2020-05-08 09:47:22 +00:00
|
|
|
return err
|
2020-04-30 00:40:08 +00:00
|
|
|
}
|
|
|
|
return fmt.Errorf("reading %s/%s: %v", dayDir, hourMinuteEntry.Name, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, "", false, 24*60)
|
|
|
|
}, startDate, true, 366)
|
|
|
|
|
|
|
|
}
|