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.

113 lines
3.0 KiB

4 years ago
4 years ago
4 years ago
3 years 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, inMemoryOnly bool, 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. defer func() {
  32. if bytesBuf != nil {
  33. logBuffer.ReleaseMemory(bytesBuf)
  34. }
  35. }()
  36. for {
  37. if bytesBuf != nil {
  38. logBuffer.ReleaseMemory(bytesBuf)
  39. }
  40. bytesBuf, batchIndex, err = logBuffer.ReadFromBuffer(lastReadPosition, inMemoryOnly)
  41. if err == ResumeFromDiskError {
  42. time.Sleep(1127 * time.Millisecond)
  43. return lastReadPosition, isDone, ResumeFromDiskError
  44. }
  45. readSize := 0
  46. if bytesBuf != nil {
  47. readSize = bytesBuf.Len()
  48. }
  49. glog.V(0).Infof("%s ReadFromBuffer at %v batch:%d, read size:%v batch:%d", readerName, lastReadPosition, lastReadPosition.BatchIndex, readSize, batchIndex)
  50. if bytesBuf == nil {
  51. if batchIndex >= 0 {
  52. lastReadPosition = NewMessagePosition(lastReadPosition.UnixNano(), batchIndex)
  53. }
  54. if stopTsNs != 0 {
  55. isDone = true
  56. return
  57. }
  58. if waitForDataFn() {
  59. continue
  60. } else {
  61. return
  62. }
  63. }
  64. buf := bytesBuf.Bytes()
  65. // fmt.Printf("ReadFromBuffer %s by %v size %d\n", readerName, lastReadPosition, len(buf))
  66. batchSize := 0
  67. for pos := 0; pos+4 < len(buf); {
  68. size := util.BytesToUint32(buf[pos : pos+4])
  69. if pos+4+int(size) > len(buf) {
  70. err = ResumeError
  71. glog.Errorf("LoopProcessLogData: %s read buffer %v read %d [%d,%d) from [0,%d)", readerName, lastReadPosition, batchSize, pos, pos+int(size)+4, len(buf))
  72. return
  73. }
  74. entryData := buf[pos+4 : pos+4+int(size)]
  75. logEntry := &filer_pb.LogEntry{}
  76. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  77. glog.Errorf("unexpected unmarshal mq_pb.Message: %v", err)
  78. pos += 4 + int(size)
  79. continue
  80. }
  81. if stopTsNs != 0 && logEntry.TsNs > stopTsNs {
  82. isDone = true
  83. return
  84. }
  85. lastReadPosition = NewMessagePosition(logEntry.TsNs, batchIndex)
  86. if err = eachLogDataFn(logEntry); err != nil {
  87. return
  88. }
  89. pos += 4 + int(size)
  90. batchSize++
  91. }
  92. // glog.V(4).Infof("%s sent messages ts[%+v,%+v] size %d\n", readerName, startReadTime, lastReadPosition, batchSize)
  93. }
  94. }