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.

118 lines
3.3 KiB

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