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.

163 lines
4.5 KiB

5 years ago
5 years ago
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{}); err != nil {
  33. return err
  34. }
  35. // get lock
  36. tp := TopicPartition{
  37. Namespace: in.Init.Namespace,
  38. Topic: in.Init.Topic,
  39. Partition: in.Init.Partition,
  40. }
  41. lock := broker.topicManager.RequestLock(tp, topicConfig, false)
  42. defer broker.topicManager.ReleaseLock(tp, false)
  43. isConnected := true
  44. go func() {
  45. for isConnected {
  46. time.Sleep(1737 * time.Millisecond)
  47. if err = stream.Send(&messaging_pb.BrokerMessage{}); err != nil {
  48. isConnected = false
  49. lock.cond.Signal()
  50. }
  51. }
  52. }()
  53. lastReadTime := time.Now()
  54. switch in.Init.StartPosition {
  55. case messaging_pb.SubscriberMessage_InitMessage_TIMESTAMP:
  56. lastReadTime = time.Unix(0, in.Init.TimestampNs)
  57. case messaging_pb.SubscriberMessage_InitMessage_LATEST:
  58. case messaging_pb.SubscriberMessage_InitMessage_EARLIEST:
  59. lastReadTime = time.Unix(0, 0)
  60. }
  61. var processedTsNs int64
  62. // how to process each message
  63. // an error returned will end the subscription
  64. eachMessageFn := func(m *messaging_pb.Message) error {
  65. err := stream.Send(&messaging_pb.BrokerMessage{
  66. Data: m,
  67. })
  68. if err != nil {
  69. glog.V(0).Infof("=> subscriber %v: %+v", subscriberId, err)
  70. }
  71. return err
  72. }
  73. eachLogEntryFn := func(logEntry *filer_pb.LogEntry) error {
  74. m := &messaging_pb.Message{}
  75. if err = proto.Unmarshal(logEntry.Data, m); err != nil {
  76. glog.Errorf("unexpected unmarshal messaging_pb.Message: %v", err)
  77. return err
  78. }
  79. // fmt.Printf("sending : %d bytes ts %d\n", len(m.Value), logEntry.TsNs)
  80. if err = eachMessageFn(m); err != nil {
  81. glog.Errorf("sending %d bytes to %s: %s", len(m.Value), subscriberId, err)
  82. return err
  83. }
  84. if m.IsClose {
  85. // println("processed EOF")
  86. return io.EOF
  87. }
  88. processedTsNs = logEntry.TsNs
  89. messageCount++
  90. return nil
  91. }
  92. if err := broker.readPersistedLogBuffer(&tp, lastReadTime, eachLogEntryFn); err != nil {
  93. if err != io.EOF {
  94. println("stopping from persisted logs", err.Error())
  95. return err
  96. }
  97. }
  98. if processedTsNs != 0 {
  99. lastReadTime = time.Unix(0, processedTsNs)
  100. }
  101. err = lock.logBuffer.LoopProcessLogData(lastReadTime, func() bool {
  102. lock.Mutex.Lock()
  103. lock.cond.Wait()
  104. lock.Mutex.Unlock()
  105. return isConnected
  106. }, eachLogEntryFn)
  107. return err
  108. }
  109. func (broker *MessageBroker) readPersistedLogBuffer(tp *TopicPartition, startTime time.Time, eachLogEntryFn func(logEntry *filer_pb.LogEntry) error) (err error) {
  110. startDate := fmt.Sprintf("%04d-%02d-%02d", startTime.Year(), startTime.Month(), startTime.Day())
  111. startHourMinute := fmt.Sprintf("%02d-%02d.segment", startTime.Hour(), startTime.Minute())
  112. sizeBuf := make([]byte, 4)
  113. startTsNs := startTime.UnixNano()
  114. topicDir := genTopicDir(tp.Namespace, tp.Topic)
  115. partitionSuffix := fmt.Sprintf(".part%02d", tp.Partition)
  116. return filer_pb.List(broker, topicDir, "", func(dayEntry *filer_pb.Entry, isLast bool) error {
  117. dayDir := fmt.Sprintf("%s/%s", topicDir, dayEntry.Name)
  118. return filer_pb.List(broker, dayDir, "", func(hourMinuteEntry *filer_pb.Entry, isLast bool) error {
  119. if dayEntry.Name == startDate {
  120. if strings.Compare(hourMinuteEntry.Name, startHourMinute) < 0 {
  121. return nil
  122. }
  123. }
  124. if !strings.HasSuffix(hourMinuteEntry.Name, partitionSuffix) {
  125. return nil
  126. }
  127. // println("partition", tp.Partition, "processing", dayDir, "/", hourMinuteEntry.Name)
  128. chunkedFileReader := filer2.NewChunkStreamReader(broker, hourMinuteEntry.Chunks)
  129. defer chunkedFileReader.Close()
  130. if err := filer2.ReadEachLogEntry(chunkedFileReader, sizeBuf, startTsNs, eachLogEntryFn); err != nil {
  131. chunkedFileReader.Close()
  132. if err == io.EOF {
  133. return err
  134. }
  135. return fmt.Errorf("reading %s/%s: %v", dayDir, hourMinuteEntry.Name, err)
  136. }
  137. return nil
  138. }, "", false, 24*60)
  139. }, startDate, true, 366)
  140. }