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.

85 lines
1.9 KiB

4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
  1. package log_buffer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "time"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. var (
  12. ResumeError = fmt.Errorf("resume")
  13. )
  14. func (logBuffer *LogBuffer) LoopProcessLogData(
  15. startTreadTime time.Time,
  16. waitForDataFn func() bool,
  17. eachLogDataFn func(logEntry *filer_pb.LogEntry) error) (lastReadTime time.Time, err error) {
  18. // loop through all messages
  19. var bytesBuf *bytes.Buffer
  20. lastReadTime = startTreadTime
  21. defer func() {
  22. if bytesBuf != nil {
  23. logBuffer.ReleaseMemory(bytesBuf)
  24. }
  25. }()
  26. for {
  27. if bytesBuf != nil {
  28. logBuffer.ReleaseMemory(bytesBuf)
  29. }
  30. bytesBuf = logBuffer.ReadFromBuffer(lastReadTime)
  31. // fmt.Printf("ReadFromBuffer by %v\n", lastReadTime)
  32. if bytesBuf == nil {
  33. if waitForDataFn() {
  34. continue
  35. } else {
  36. return
  37. }
  38. }
  39. buf := bytesBuf.Bytes()
  40. // fmt.Printf("ReadFromBuffer by %v size %d\n", lastReadTime, len(buf))
  41. batchSize := 0
  42. var startReadTime time.Time
  43. for pos := 0; pos+4 < len(buf); {
  44. size := util.BytesToUint32(buf[pos : pos+4])
  45. if pos+4+int(size) > len(buf) {
  46. err = ResumeError
  47. glog.Errorf("LoopProcessLogData: read buffer %v read %d [%d,%d) from [0,%d)", lastReadTime, batchSize, 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. glog.Errorf("unexpected unmarshal messaging_pb.Message: %v", err)
  54. pos += 4 + int(size)
  55. continue
  56. }
  57. lastReadTime = time.Unix(0, logEntry.TsNs)
  58. if startReadTime.IsZero() {
  59. startReadTime = lastReadTime
  60. }
  61. if err = eachLogDataFn(logEntry); err != nil {
  62. return
  63. }
  64. pos += 4 + int(size)
  65. batchSize++
  66. }
  67. // fmt.Printf("sent message ts[%d,%d] size %d\n", startReadTime.UnixNano(), lastReadTime.UnixNano(), batchSize)
  68. }
  69. }