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.

139 lines
3.8 KiB

4 years ago
10 months ago
12 months ago
4 years ago
9 months ago
4 years ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
12 months ago
3 years ago
9 months ago
12 months ago
12 months ago
9 months ago
5 years ago
  1. package log_buffer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "time"
  6. "google.golang.org/protobuf/proto"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. )
  11. var (
  12. ResumeError = fmt.Errorf("resume")
  13. ResumeFromDiskError = fmt.Errorf("resumeFromDisk")
  14. )
  15. type MessagePosition struct {
  16. time.Time // this is the timestamp of the message
  17. BatchIndex int64 // this is only used when the timestamp is not enough to identify the next message, when the timestamp is in the previous batch.
  18. }
  19. func NewMessagePosition(tsNs int64, batchIndex int64) MessagePosition {
  20. return MessagePosition{
  21. Time: time.Unix(0, tsNs).UTC(),
  22. BatchIndex: batchIndex,
  23. }
  24. }
  25. func (logBuffer *LogBuffer) LoopProcessLogData(readerName string, startPosition MessagePosition, stopTsNs int64,
  26. waitForDataFn func() bool, eachLogDataFn EachLogEntryFuncType) (lastReadPosition MessagePosition, isDone bool, err error) {
  27. // loop through all messages
  28. var bytesBuf *bytes.Buffer
  29. var batchIndex int64
  30. lastReadPosition = startPosition
  31. var entryCounter int64
  32. defer func() {
  33. if bytesBuf != nil {
  34. logBuffer.ReleaseMemory(bytesBuf)
  35. }
  36. // println("LoopProcessLogData", readerName, "sent messages total", entryCounter)
  37. }()
  38. for {
  39. if bytesBuf != nil {
  40. logBuffer.ReleaseMemory(bytesBuf)
  41. }
  42. bytesBuf, batchIndex, err = logBuffer.ReadFromBuffer(lastReadPosition)
  43. if err == ResumeFromDiskError {
  44. time.Sleep(1127 * time.Millisecond)
  45. return lastReadPosition, isDone, ResumeFromDiskError
  46. }
  47. readSize := 0
  48. if bytesBuf != nil {
  49. readSize = bytesBuf.Len()
  50. }
  51. glog.V(4).Infof("%s ReadFromBuffer at %v batch %d. Read bytes %v batch %d", readerName, lastReadPosition, lastReadPosition.BatchIndex, readSize, batchIndex)
  52. if bytesBuf == nil {
  53. if batchIndex >= 0 {
  54. lastReadPosition = NewMessagePosition(lastReadPosition.UnixNano(), batchIndex)
  55. }
  56. if stopTsNs != 0 {
  57. isDone = true
  58. return
  59. }
  60. logBuffer.RLock()
  61. lastTsNs := logBuffer.LastTsNs
  62. logBuffer.RUnlock()
  63. loopTsNs := lastTsNs // make a copy
  64. for lastTsNs == loopTsNs {
  65. if waitForDataFn() {
  66. // Update loopTsNs and loop again
  67. logBuffer.RLock()
  68. loopTsNs = logBuffer.LastTsNs
  69. logBuffer.RUnlock()
  70. continue
  71. } else {
  72. isDone = true
  73. return
  74. }
  75. }
  76. if logBuffer.IsStopping() {
  77. isDone = true
  78. return
  79. }
  80. continue
  81. }
  82. buf := bytesBuf.Bytes()
  83. // fmt.Printf("ReadFromBuffer %s by %v size %d\n", readerName, lastReadPosition, len(buf))
  84. batchSize := 0
  85. for pos := 0; pos+4 < len(buf); {
  86. size := util.BytesToUint32(buf[pos : pos+4])
  87. if pos+4+int(size) > len(buf) {
  88. err = ResumeError
  89. glog.Errorf("LoopProcessLogData: %s read buffer %v read %d entries [%d,%d) from [0,%d)", readerName, lastReadPosition, batchSize, pos, pos+int(size)+4, len(buf))
  90. return
  91. }
  92. entryData := buf[pos+4 : pos+4+int(size)]
  93. logEntry := &filer_pb.LogEntry{}
  94. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  95. glog.Errorf("unexpected unmarshal mq_pb.Message: %v", err)
  96. pos += 4 + int(size)
  97. continue
  98. }
  99. if stopTsNs != 0 && logEntry.TsNs > stopTsNs {
  100. isDone = true
  101. // println("stopTsNs", stopTsNs, "logEntry.TsNs", logEntry.TsNs)
  102. return
  103. }
  104. lastReadPosition = NewMessagePosition(logEntry.TsNs, batchIndex)
  105. if isDone, err = eachLogDataFn(logEntry); err != nil {
  106. glog.Errorf("LoopProcessLogData: %s process log entry %d %v: %v", readerName, batchSize+1, logEntry, err)
  107. return
  108. }
  109. if isDone {
  110. glog.V(0).Infof("LoopProcessLogData2: %s process log entry %d", readerName, batchSize+1)
  111. return
  112. }
  113. pos += 4 + int(size)
  114. batchSize++
  115. entryCounter++
  116. }
  117. glog.V(4).Infof("%s sent messages ts[%+v,%+v] size %d\n", readerName, startPosition, lastReadPosition, batchSize)
  118. }
  119. }