log_read.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 EachLogEntryFuncType) (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(4).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. lastTsNs := logBuffer.LastTsNs.Load()
  61. for lastTsNs == logBuffer.LastTsNs.Load() {
  62. if waitForDataFn() {
  63. continue
  64. } else {
  65. isDone = true
  66. return
  67. }
  68. }
  69. if logBuffer.IsStopping() {
  70. isDone = true
  71. return
  72. }
  73. continue
  74. }
  75. buf := bytesBuf.Bytes()
  76. // fmt.Printf("ReadFromBuffer %s by %v size %d\n", readerName, lastReadPosition, len(buf))
  77. batchSize := 0
  78. for pos := 0; pos+4 < len(buf); {
  79. size := util.BytesToUint32(buf[pos : pos+4])
  80. if pos+4+int(size) > len(buf) {
  81. err = ResumeError
  82. 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))
  83. return
  84. }
  85. entryData := buf[pos+4 : pos+4+int(size)]
  86. logEntry := &filer_pb.LogEntry{}
  87. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  88. glog.Errorf("unexpected unmarshal mq_pb.Message: %v", err)
  89. pos += 4 + int(size)
  90. continue
  91. }
  92. if stopTsNs != 0 && logEntry.TsNs > stopTsNs {
  93. isDone = true
  94. // println("stopTsNs", stopTsNs, "logEntry.TsNs", logEntry.TsNs)
  95. return
  96. }
  97. lastReadPosition = NewMessagePosition(logEntry.TsNs, batchIndex)
  98. if isDone, err = eachLogDataFn(logEntry); err != nil {
  99. glog.Errorf("LoopProcessLogData: %s process log entry %d %v: %v", readerName, batchSize+1, logEntry, err)
  100. return
  101. }
  102. if isDone {
  103. glog.V(0).Infof("LoopProcessLogData2: %s process log entry %d", readerName, batchSize+1)
  104. return
  105. }
  106. pos += 4 + int(size)
  107. batchSize++
  108. entryCounter++
  109. }
  110. glog.V(4).Infof("%s sent messages ts[%+v,%+v] size %d\n", readerName, startPosition, lastReadPosition, batchSize)
  111. }
  112. }
  113. // LoopProcessLogDataWithBatchIndex is similar to LoopProcessLogData but provides batchIndex to the callback
  114. func (logBuffer *LogBuffer) LoopProcessLogDataWithBatchIndex(readerName string, startPosition MessagePosition, stopTsNs int64,
  115. waitForDataFn func() bool, eachLogDataFn EachLogEntryWithBatchIndexFuncType) (lastReadPosition MessagePosition, isDone bool, err error) {
  116. // loop through all messages
  117. var bytesBuf *bytes.Buffer
  118. var batchIndex int64
  119. lastReadPosition = startPosition
  120. var entryCounter int64
  121. defer func() {
  122. if bytesBuf != nil {
  123. logBuffer.ReleaseMemory(bytesBuf)
  124. }
  125. // println("LoopProcessLogDataWithBatchIndex", readerName, "sent messages total", entryCounter)
  126. }()
  127. for {
  128. if bytesBuf != nil {
  129. logBuffer.ReleaseMemory(bytesBuf)
  130. }
  131. bytesBuf, batchIndex, err = logBuffer.ReadFromBuffer(lastReadPosition)
  132. if err == ResumeFromDiskError {
  133. time.Sleep(1127 * time.Millisecond)
  134. return lastReadPosition, isDone, ResumeFromDiskError
  135. }
  136. readSize := 0
  137. if bytesBuf != nil {
  138. readSize = bytesBuf.Len()
  139. }
  140. glog.V(4).Infof("%s ReadFromBuffer at %v batch %d. Read bytes %v batch %d", readerName, lastReadPosition, lastReadPosition.BatchIndex, readSize, batchIndex)
  141. if bytesBuf == nil {
  142. if batchIndex >= 0 {
  143. lastReadPosition = NewMessagePosition(lastReadPosition.UnixNano(), batchIndex)
  144. }
  145. if stopTsNs != 0 {
  146. isDone = true
  147. return
  148. }
  149. lastTsNs := logBuffer.LastTsNs.Load()
  150. for lastTsNs == logBuffer.LastTsNs.Load() {
  151. if waitForDataFn() {
  152. continue
  153. } else {
  154. isDone = true
  155. return
  156. }
  157. }
  158. if logBuffer.IsStopping() {
  159. isDone = true
  160. return
  161. }
  162. continue
  163. }
  164. buf := bytesBuf.Bytes()
  165. // fmt.Printf("ReadFromBuffer %s by %v size %d\n", readerName, lastReadPosition, len(buf))
  166. batchSize := 0
  167. for pos := 0; pos+4 < len(buf); {
  168. size := util.BytesToUint32(buf[pos : pos+4])
  169. if pos+4+int(size) > len(buf) {
  170. err = ResumeError
  171. glog.Errorf("LoopProcessLogDataWithBatchIndex: %s read buffer %v read %d entries [%d,%d) from [0,%d)", readerName, lastReadPosition, batchSize, pos, pos+int(size)+4, len(buf))
  172. return
  173. }
  174. entryData := buf[pos+4 : pos+4+int(size)]
  175. logEntry := &filer_pb.LogEntry{}
  176. if err = proto.Unmarshal(entryData, logEntry); err != nil {
  177. glog.Errorf("unexpected unmarshal mq_pb.Message: %v", err)
  178. pos += 4 + int(size)
  179. continue
  180. }
  181. if stopTsNs != 0 && logEntry.TsNs > stopTsNs {
  182. isDone = true
  183. // println("stopTsNs", stopTsNs, "logEntry.TsNs", logEntry.TsNs)
  184. return
  185. }
  186. lastReadPosition = NewMessagePosition(logEntry.TsNs, batchIndex)
  187. if isDone, err = eachLogDataFn(logEntry, batchIndex); err != nil {
  188. glog.Errorf("LoopProcessLogDataWithBatchIndex: %s process log entry %d %v: %v", readerName, batchSize+1, logEntry, err)
  189. return
  190. }
  191. if isDone {
  192. glog.V(0).Infof("LoopProcessLogDataWithBatchIndex: %s process log entry %d", readerName, batchSize+1)
  193. return
  194. }
  195. pos += 4 + int(size)
  196. batchSize++
  197. entryCounter++
  198. }
  199. }
  200. }