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.

165 lines
5.2 KiB

12 months ago
  1. package broker
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/filer"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
  11. "google.golang.org/protobuf/proto"
  12. "math"
  13. "time"
  14. )
  15. func (b *MessageQueueBroker) genLogFlushFunc(t topic.Topic, partition *mq_pb.Partition) log_buffer.LogFlushFuncType {
  16. topicDir := fmt.Sprintf("%s/%s/%s", filer.TopicsDir, t.Namespace, t.Name)
  17. partitionGeneration := time.Unix(0, partition.UnixTimeNs).UTC().Format(topic.TIME_FORMAT)
  18. partitionDir := fmt.Sprintf("%s/%s/%04d-%04d", topicDir, partitionGeneration, partition.RangeStart, partition.RangeStop)
  19. return func(startTime, stopTime time.Time, buf []byte) {
  20. if len(buf) == 0 {
  21. return
  22. }
  23. startTime, stopTime = startTime.UTC(), stopTime.UTC()
  24. targetFile := fmt.Sprintf("%s/%s",partitionDir, startTime.Format(topic.TIME_FORMAT))
  25. // TODO append block with more metadata
  26. for {
  27. if err := b.appendToFile(targetFile, buf); err != nil {
  28. glog.V(0).Infof("metadata log write failed %s: %v", targetFile, err)
  29. time.Sleep(737 * time.Millisecond)
  30. } else {
  31. break
  32. }
  33. }
  34. }
  35. }
  36. func (b *MessageQueueBroker) genLogOnDiskReadFunc(t topic.Topic, partition *mq_pb.Partition) log_buffer.LogReadFromDiskFuncType {
  37. topicDir := fmt.Sprintf("%s/%s/%s", filer.TopicsDir, t.Namespace, t.Name)
  38. partitionGeneration := time.Unix(0, partition.UnixTimeNs).UTC().Format(topic.TIME_FORMAT)
  39. partitionDir := fmt.Sprintf("%s/%s/%04d-%04d", topicDir, partitionGeneration, partition.RangeStart, partition.RangeStop)
  40. lookupFileIdFn := func(fileId string) (targetUrls []string, err error) {
  41. return b.MasterClient.LookupFileId(fileId)
  42. }
  43. eachChunkFn := func (buf []byte, eachLogEntryFn log_buffer.EachLogEntryFuncType, starTsNs, stopTsNs int64) (processedTsNs int64, err error) {
  44. for pos := 0; pos+4 < len(buf); {
  45. size := util.BytesToUint32(buf[pos : pos+4])
  46. if pos+4+int(size) > len(buf) {
  47. err = fmt.Errorf("LogOnDiskReadFunc: read [%d,%d) from [0,%d)", pos, pos+int(size)+4, len(buf))
  48. return
  49. }
  50. entryData := buf[pos+4 : pos+4+int(size)]
  51. logEntry := &filer_pb.LogEntry{}
  52. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  53. pos += 4 + int(size)
  54. err = fmt.Errorf("unexpected unmarshal mq_pb.Message: %v", err)
  55. return
  56. }
  57. if logEntry.TsNs < starTsNs {
  58. pos += 4 + int(size)
  59. continue
  60. }
  61. if stopTsNs != 0 && logEntry.TsNs > stopTsNs {
  62. println("stopTsNs", stopTsNs, "logEntry.TsNs", logEntry.TsNs)
  63. return
  64. }
  65. if err = eachLogEntryFn(logEntry); err != nil {
  66. err = fmt.Errorf("process log entry %v: %v", logEntry, err)
  67. return
  68. }
  69. processedTsNs = logEntry.TsNs
  70. pos += 4 + int(size)
  71. }
  72. return
  73. }
  74. eachFileFn := func(entry *filer_pb.Entry, eachLogEntryFn log_buffer.EachLogEntryFuncType, starTsNs, stopTsNs int64) (processedTsNs int64, err error) {
  75. if len(entry.Content) > 0 {
  76. glog.Warningf("this should not happen. unexpected content in %s/%s", partitionDir, entry.Name)
  77. return
  78. }
  79. var urlStrings []string
  80. for _, chunk := range entry.Chunks {
  81. if chunk.Size == 0 {
  82. continue
  83. }
  84. if chunk.IsChunkManifest{
  85. glog.Warningf("this should not happen. unexpected chunk manifest in %s/%s", partitionDir, entry.Name)
  86. return
  87. }
  88. urlStrings, err = lookupFileIdFn(chunk.FileId)
  89. if err != nil {
  90. err = fmt.Errorf("lookup %s: %v", chunk.FileId, err)
  91. return
  92. }
  93. if len(urlStrings) == 0 {
  94. err = fmt.Errorf("no url found for %s", chunk.FileId)
  95. return
  96. }
  97. // try one of the urlString until util.Get(urlString) succeeds
  98. var processed bool
  99. for _, urlString := range urlStrings {
  100. // TODO optimization opportunity: reuse the buffer
  101. var data []byte
  102. if data, _, err = util.Get(urlString); err == nil {
  103. processed = true
  104. if processedTsNs, err = eachChunkFn(data, eachLogEntryFn, starTsNs, stopTsNs); err != nil {
  105. return
  106. }
  107. break
  108. }
  109. }
  110. if !processed {
  111. err = fmt.Errorf("no data processed for %s %s", entry.Name, chunk.FileId)
  112. return
  113. }
  114. }
  115. return
  116. }
  117. return func(startPosition log_buffer.MessagePosition, stopTsNs int64, eachLogEntryFn log_buffer.EachLogEntryFuncType) (lastReadPosition log_buffer.MessagePosition, isDone bool, err error) {
  118. startFileName := startPosition.UTC().Format(topic.TIME_FORMAT)
  119. startTsNs := startPosition.Time.UnixNano()
  120. stopTime := time.Unix(0, stopTsNs)
  121. var processedTsNs int64
  122. err = b.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  123. return filer_pb.SeaweedList(client, partitionDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  124. if entry.IsDirectory {
  125. return nil
  126. }
  127. if stopTsNs!=0 && entry.Name > stopTime.UTC().Format(topic.TIME_FORMAT) {
  128. isDone = true
  129. return nil
  130. }
  131. if entry.Name < startPosition.UTC().Format(topic.TIME_FORMAT) {
  132. return nil
  133. }
  134. if processedTsNs, err = eachFileFn(entry, eachLogEntryFn, startTsNs, stopTsNs); err != nil {
  135. return err
  136. }
  137. return nil
  138. }, startFileName, true, math.MaxInt32)
  139. })
  140. lastReadPosition = log_buffer.NewMessagePosition(processedTsNs, -2)
  141. return
  142. }
  143. }