You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package broker
  2. import (
  3. "fmt"
  4. "io"
  5. "strings"
  6. "time"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  12. )
  13. func (broker *MessageBroker) Subscribe(stream messaging_pb.SeaweedMessaging_SubscribeServer) error {
  14. // process initial request
  15. in, err := stream.Recv()
  16. if err == io.EOF {
  17. return nil
  18. }
  19. if err != nil {
  20. return err
  21. }
  22. var messageCount int64
  23. subscriberId := in.Init.SubscriberId
  24. fmt.Printf("+ subscriber %s\n", subscriberId)
  25. defer func() {
  26. fmt.Printf("- subscriber %s: %d messages\n", subscriberId, messageCount)
  27. }()
  28. // TODO look it up
  29. topicConfig := &messaging_pb.TopicConfiguration{
  30. // IsTransient: true,
  31. }
  32. if err = stream.Send(&messaging_pb.BrokerMessage{
  33. }); err != nil {
  34. return err
  35. }
  36. // get lock
  37. tp := TopicPartition{
  38. Namespace: in.Init.Namespace,
  39. Topic: in.Init.Topic,
  40. Partition: in.Init.Partition,
  41. }
  42. lock := broker.topicLocks.RequestLock(tp, topicConfig, false)
  43. defer broker.topicLocks.ReleaseLock(tp, false)
  44. lastReadTime := time.Now()
  45. switch in.Init.StartPosition {
  46. case messaging_pb.SubscriberMessage_InitMessage_TIMESTAMP:
  47. lastReadTime = time.Unix(0, in.Init.TimestampNs)
  48. case messaging_pb.SubscriberMessage_InitMessage_LATEST:
  49. case messaging_pb.SubscriberMessage_InitMessage_EARLIEST:
  50. }
  51. var processedTsNs int64
  52. // how to process each message
  53. // an error returned will end the subscription
  54. eachMessageFn := func(m *messaging_pb.Message) error {
  55. err := stream.Send(&messaging_pb.BrokerMessage{
  56. Data: m,
  57. })
  58. if err != nil {
  59. glog.V(0).Infof("=> subscriber %v: %+v", subscriberId, err)
  60. }
  61. return err
  62. }
  63. eachLogEntryFn := func(logEntry *filer_pb.LogEntry) error {
  64. m := &messaging_pb.Message{}
  65. if err = proto.Unmarshal(logEntry.Data, m); err != nil {
  66. glog.Errorf("unexpected unmarshal messaging_pb.Message: %v", err)
  67. return err
  68. }
  69. // fmt.Printf("sending : %d bytes ts %d\n", len(m.Value), logEntry.TsNs)
  70. if err = eachMessageFn(m); err != nil {
  71. glog.Errorf("sending %d bytes to %s: %s", len(m.Value), subscriberId, err)
  72. return err
  73. }
  74. if m.IsClose {
  75. // println("processed EOF")
  76. return io.EOF
  77. }
  78. processedTsNs = logEntry.TsNs
  79. messageCount++
  80. return nil
  81. }
  82. if err := broker.readPersistedLogBuffer(&tp, lastReadTime, eachLogEntryFn); err != nil {
  83. // println("stopping from persisted logs")
  84. return err
  85. }
  86. if processedTsNs != 0 {
  87. lastReadTime = time.Unix(0, processedTsNs)
  88. }
  89. err = lock.logBuffer.LoopProcessLogData(lastReadTime, func() bool {
  90. lock.Mutex.Lock()
  91. lock.cond.Wait()
  92. lock.Mutex.Unlock()
  93. return true
  94. }, eachLogEntryFn)
  95. return err
  96. }
  97. func (broker *MessageBroker) readPersistedLogBuffer(tp *TopicPartition, startTime time.Time, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) (err error) {
  98. startDate := fmt.Sprintf("%04d-%02d-%02d", startTime.Year(), startTime.Month(), startTime.Day())
  99. startHourMinute := fmt.Sprintf("%02d-%02d.segment", startTime.Hour(), startTime.Minute())
  100. sizeBuf := make([]byte, 4)
  101. startTsNs := startTime.UnixNano()
  102. topicDir := genTopicDir(tp.Namespace, tp.Topic)
  103. partitionSuffix := fmt.Sprintf(".part%02d", tp.Partition)
  104. return filer_pb.List(broker, topicDir, "", func(dayEntry *filer_pb.Entry, isLast bool) error {
  105. dayDir := fmt.Sprintf("%s/%s", topicDir, dayEntry.Name)
  106. return filer_pb.List(broker, dayDir, "", func(hourMinuteEntry *filer_pb.Entry, isLast bool) error {
  107. if dayEntry.Name == startDate {
  108. if strings.Compare(hourMinuteEntry.Name, startHourMinute) < 0 {
  109. return nil
  110. }
  111. }
  112. if !strings.HasSuffix(hourMinuteEntry.Name, partitionSuffix) {
  113. return nil
  114. }
  115. // println("partition", tp.Partition, "processing", dayDir, "/", hourMinuteEntry.Name)
  116. chunkedFileReader := filer2.NewChunkStreamReader(broker, hourMinuteEntry.Chunks)
  117. defer chunkedFileReader.Close()
  118. if err := filer2.ReadEachLogEntry(chunkedFileReader, sizeBuf, startTsNs, eachLogEntryFn); err != nil {
  119. chunkedFileReader.Close()
  120. if err == io.EOF {
  121. return err
  122. }
  123. return fmt.Errorf("reading %s/%s: %v", dayDir, hourMinuteEntry.Name, err)
  124. }
  125. return nil
  126. }, "", false, 24*60)
  127. }, startDate, true, 366)
  128. }