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.

75 lines
1.7 KiB

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, waitForDataFn func() bool,
  12. eachLogDataFn func(logEntry *filer_pb.LogEntry) error) (processed int64, err error) {
  13. // loop through all messages
  14. var bytesBuf *bytes.Buffer
  15. lastReadTime := startTreadTime
  16. defer func() {
  17. if bytesBuf != nil {
  18. logBuffer.ReleaseMeory(bytesBuf)
  19. }
  20. }()
  21. for {
  22. if bytesBuf != nil {
  23. logBuffer.ReleaseMeory(bytesBuf)
  24. }
  25. bytesBuf = logBuffer.ReadFromBuffer(lastReadTime)
  26. if bytesBuf == nil {
  27. if waitForDataFn() {
  28. continue
  29. } else {
  30. return
  31. }
  32. }
  33. buf := bytesBuf.Bytes()
  34. batchSize := 0
  35. var startReadTime time.Time
  36. for pos := 0; pos+4 < len(buf); {
  37. size := util.BytesToUint32(buf[pos : pos+4])
  38. entryData := buf[pos+4 : pos+4+int(size)]
  39. // fmt.Printf("read buffer read %d [%d,%d) from [0,%d)\n", batchSize, pos, pos+int(size)+4, len(buf))
  40. logEntry := &filer_pb.LogEntry{}
  41. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  42. glog.Errorf("unexpected unmarshal messaging_pb.Message: %v", err)
  43. pos += 4 + int(size)
  44. continue
  45. }
  46. lastReadTime = time.Unix(0, logEntry.TsNs)
  47. if startReadTime.IsZero() {
  48. startReadTime = lastReadTime
  49. }
  50. if err = eachLogDataFn(logEntry); err != nil {
  51. return
  52. }
  53. pos += 4 + int(size)
  54. batchSize++
  55. processed++
  56. }
  57. // fmt.Printf("sent message ts[%d,%d] size %d\n", startReadTime.UnixNano(), lastReadTime.UnixNano(), batchSize)
  58. }
  59. }