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.

77 lines
1.7 KiB

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