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.

131 lines
3.6 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
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. lastTsNs := logBuffer.LastTsNs
  61. for lastTsNs == logBuffer.LastTsNs {
  62. if waitForDataFn() {
  63. continue
  64. } else {
  65. isDone = true
  66. return
  67. }
  68. }
  69. if logBuffer.IsStopping() {
  70. isDone = true
  71. return
  72. }
  73. continue
  74. }
  75. buf := bytesBuf.Bytes()
  76. // fmt.Printf("ReadFromBuffer %s by %v size %d\n", readerName, lastReadPosition, len(buf))
  77. batchSize := 0
  78. for pos := 0; pos+4 < len(buf); {
  79. size := util.BytesToUint32(buf[pos : pos+4])
  80. if pos+4+int(size) > len(buf) {
  81. err = ResumeError
  82. 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))
  83. return
  84. }
  85. entryData := buf[pos+4 : pos+4+int(size)]
  86. logEntry := &filer_pb.LogEntry{}
  87. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  88. glog.Errorf("unexpected unmarshal mq_pb.Message: %v", err)
  89. pos += 4 + int(size)
  90. continue
  91. }
  92. if stopTsNs != 0 && logEntry.TsNs > stopTsNs {
  93. isDone = true
  94. // println("stopTsNs", stopTsNs, "logEntry.TsNs", logEntry.TsNs)
  95. return
  96. }
  97. lastReadPosition = NewMessagePosition(logEntry.TsNs, batchIndex)
  98. if isDone, err = eachLogDataFn(logEntry); err != nil {
  99. glog.Errorf("LoopProcessLogData: %s process log entry %d %v: %v", readerName, batchSize+1, logEntry, err)
  100. return
  101. }
  102. if isDone {
  103. glog.V(0).Infof("LoopProcessLogData2: %s process log entry %d", readerName, batchSize+1)
  104. return
  105. }
  106. pos += 4 + int(size)
  107. batchSize++
  108. entryCounter++
  109. }
  110. glog.V(4).Infof("%s sent messages ts[%+v,%+v] size %d\n", readerName, startPosition, lastReadPosition, batchSize)
  111. }
  112. }