2020-04-20 06:37:04 +00:00
|
|
|
package log_buffer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-09-09 18:21:23 +00:00
|
|
|
"fmt"
|
2020-04-20 06:37:04 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-04-20 06:37:04 +00:00
|
|
|
)
|
|
|
|
|
2020-09-09 18:21:23 +00:00
|
|
|
var (
|
2021-01-12 10:30:12 +00:00
|
|
|
ResumeError = fmt.Errorf("resume")
|
2021-01-11 10:08:55 +00:00
|
|
|
ResumeFromDiskError = fmt.Errorf("resumeFromDisk")
|
2020-09-09 18:21:23 +00:00
|
|
|
)
|
|
|
|
|
2022-05-30 22:25:21 +00:00
|
|
|
func (logBuffer *LogBuffer) LoopProcessLogData(readerName string, startReadTime time.Time, stopTsNs int64,
|
|
|
|
waitForDataFn func() bool, eachLogDataFn func(logEntry *filer_pb.LogEntry) error) (lastReadTime time.Time, isDone bool, err error) {
|
2020-04-20 06:37:04 +00:00
|
|
|
// loop through all messages
|
|
|
|
var bytesBuf *bytes.Buffer
|
2022-05-30 22:25:21 +00:00
|
|
|
lastReadTime = startReadTime
|
2020-04-20 06:37:04 +00:00
|
|
|
defer func() {
|
|
|
|
if bytesBuf != nil {
|
2020-08-27 05:40:15 +00:00
|
|
|
logBuffer.ReleaseMemory(bytesBuf)
|
2020-04-20 06:37:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
if bytesBuf != nil {
|
2020-08-27 05:40:15 +00:00
|
|
|
logBuffer.ReleaseMemory(bytesBuf)
|
2020-04-20 06:37:04 +00:00
|
|
|
}
|
2021-01-11 10:08:55 +00:00
|
|
|
bytesBuf, err = logBuffer.ReadFromBuffer(lastReadTime)
|
|
|
|
if err == ResumeFromDiskError {
|
2022-05-24 07:23:53 +00:00
|
|
|
time.Sleep(1127 * time.Millisecond)
|
2022-05-30 22:25:21 +00:00
|
|
|
return lastReadTime, isDone, ResumeFromDiskError
|
2021-01-11 10:08:55 +00:00
|
|
|
}
|
2021-06-27 12:51:28 +00:00
|
|
|
// glog.V(4).Infof("%s ReadFromBuffer by %v", readerName, lastReadTime)
|
2020-04-20 06:37:04 +00:00
|
|
|
if bytesBuf == nil {
|
2022-05-30 23:16:23 +00:00
|
|
|
if stopTsNs != 0 {
|
|
|
|
isDone = true
|
|
|
|
return
|
|
|
|
}
|
2020-04-20 06:37:04 +00:00
|
|
|
if waitForDataFn() {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytesBuf.Bytes()
|
2021-06-27 12:51:28 +00:00
|
|
|
// fmt.Printf("ReadFromBuffer %s by %v size %d\n", readerName, lastReadTime, len(buf))
|
2020-04-20 06:37:04 +00:00
|
|
|
|
|
|
|
batchSize := 0
|
|
|
|
|
|
|
|
for pos := 0; pos+4 < len(buf); {
|
|
|
|
|
|
|
|
size := util.BytesToUint32(buf[pos : pos+4])
|
2020-09-09 18:21:23 +00:00
|
|
|
if pos+4+int(size) > len(buf) {
|
|
|
|
err = ResumeError
|
2021-06-27 12:51:28 +00:00
|
|
|
glog.Errorf("LoopProcessLogData: %s read buffer %v read %d [%d,%d) from [0,%d)", readerName, lastReadTime, batchSize, pos, pos+int(size)+4, len(buf))
|
2020-09-09 18:21:23 +00:00
|
|
|
return
|
|
|
|
}
|
2020-04-20 06:37:04 +00:00
|
|
|
entryData := buf[pos+4 : pos+4+int(size)]
|
|
|
|
|
|
|
|
logEntry := &filer_pb.LogEntry{}
|
|
|
|
if err = proto.Unmarshal(entryData, logEntry); err != nil {
|
2022-07-02 05:43:25 +00:00
|
|
|
glog.Errorf("unexpected unmarshal mq_pb.Message: %v", err)
|
2020-04-20 06:37:04 +00:00
|
|
|
pos += 4 + int(size)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-30 22:25:21 +00:00
|
|
|
if stopTsNs != 0 && logEntry.TsNs > stopTsNs {
|
|
|
|
isDone = true
|
|
|
|
return
|
2020-04-20 06:37:04 +00:00
|
|
|
}
|
2022-05-30 22:25:21 +00:00
|
|
|
lastReadTime = time.Unix(0, logEntry.TsNs)
|
2020-04-20 06:37:04 +00:00
|
|
|
|
|
|
|
if err = eachLogDataFn(logEntry); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += 4 + int(size)
|
|
|
|
batchSize++
|
2021-06-27 12:51:28 +00:00
|
|
|
|
2020-04-20 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 12:51:28 +00:00
|
|
|
// glog.V(4).Infof("%s sent messages ts[%+v,%+v] size %d\n", readerName, startReadTime, lastReadTime, batchSize)
|
2020-04-20 06:37:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:06 +00:00
|
|
|
}
|