seaweedfs/weed/util/log_buffer/log_buffer.go

353 lines
10 KiB
Go
Raw Normal View History

2020-04-12 03:37:41 +08:00
package log_buffer
2020-03-31 13:57:45 +08:00
import (
"bytes"
2020-03-31 13:57:45 +08:00
"sync"
"sync/atomic"
2020-03-31 13:57:45 +08:00
"time"
2022-08-18 03:05:07 +08:00
"google.golang.org/protobuf/proto"
2020-03-31 13:57:45 +08:00
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
2020-03-31 13:57:45 +08:00
)
2020-04-11 17:19:45 +08:00
const BufferSize = 4 * 1024 * 1024
const PreviousBufferCount = 3
2020-04-06 07:51:30 +08:00
type dataToFlush struct {
startTime time.Time
stopTime time.Time
data *bytes.Buffer
2020-04-06 07:51:30 +08:00
}
type EachLogEntryFuncType func(logEntry *filer_pb.LogEntry) (isDone bool, err error)
type LogFlushFuncType func(logBuffer *LogBuffer, startTime, stopTime time.Time, buf []byte)
2024-01-15 16:20:12 +08:00
type LogReadFromDiskFuncType func(startPosition MessagePosition, stopTsNs int64, eachLogEntryFn EachLogEntryFuncType) (lastReadPosition MessagePosition, isDone bool, err error)
2020-03-31 13:57:45 +08:00
type LogBuffer struct {
2024-03-17 01:51:47 +08:00
LastFlushTsNs int64
name string
prevBuffers *SealedBuffers
buf []byte
batchIndex int64
idx []int
pos int
startTime time.Time
2024-03-17 01:51:16 +08:00
stopTime time.Time
lastFlushDataTime time.Time
sizeBuf []byte
2024-03-17 01:51:47 +08:00
flushInterval time.Duration
flushFn LogFlushFuncType
ReadFromDiskFn LogReadFromDiskFuncType
notifyFn func()
isStopping *atomic.Bool
flushChan chan *dataToFlush
2024-03-17 01:52:47 +08:00
LastTsNs int64
2020-04-05 15:51:16 +08:00
sync.RWMutex
2020-03-31 13:57:45 +08:00
}
2024-01-15 16:20:12 +08:00
func NewLogBuffer(name string, flushInterval time.Duration, flushFn LogFlushFuncType,
readFromDiskFn LogReadFromDiskFuncType, notifyFn func()) *LogBuffer {
2020-03-31 13:57:45 +08:00
lb := &LogBuffer{
2024-03-01 01:38:52 +08:00
name: name,
prevBuffers: newSealedBuffers(PreviousBufferCount),
buf: make([]byte, BufferSize),
sizeBuf: make([]byte, 4),
flushInterval: flushInterval,
flushFn: flushFn,
2024-01-15 16:20:12 +08:00
ReadFromDiskFn: readFromDiskFn,
2024-03-01 01:38:52 +08:00
notifyFn: notifyFn,
flushChan: make(chan *dataToFlush, 256),
isStopping: new(atomic.Bool),
2020-03-31 13:57:45 +08:00
}
go lb.loopFlush()
2020-04-06 07:51:30 +08:00
go lb.loopInterval()
2020-03-31 13:57:45 +08:00
return lb
}
2024-01-08 16:03:08 +08:00
func (logBuffer *LogBuffer) AddToBuffer(partitionKey, data []byte, processingTsNs int64) {
2020-03-31 13:57:45 +08:00
var toFlush *dataToFlush
2024-01-08 16:03:08 +08:00
logBuffer.Lock()
2020-04-10 16:35:59 +08:00
defer func() {
2024-01-08 16:03:08 +08:00
logBuffer.Unlock()
if toFlush != nil {
2024-01-08 16:03:08 +08:00
logBuffer.flushChan <- toFlush
}
2024-01-08 16:03:08 +08:00
if logBuffer.notifyFn != nil {
logBuffer.notifyFn()
2020-04-10 16:35:59 +08:00
}
}()
// need to put the timestamp inside the lock
var ts time.Time
2021-09-27 02:54:13 +08:00
if processingTsNs == 0 {
ts = time.Now()
2021-09-27 02:54:13 +08:00
processingTsNs = ts.UnixNano()
} else {
2021-09-27 02:54:13 +08:00
ts = time.Unix(0, processingTsNs)
}
2024-03-17 01:52:47 +08:00
if logBuffer.LastTsNs >= processingTsNs {
2020-05-11 16:53:54 +08:00
// this is unlikely to happen, but just in case
2024-03-17 01:52:47 +08:00
processingTsNs = logBuffer.LastTsNs + 1
2021-09-27 02:54:13 +08:00
ts = time.Unix(0, processingTsNs)
2020-05-11 16:53:54 +08:00
}
2024-03-17 01:52:47 +08:00
logBuffer.LastTsNs = processingTsNs
2020-03-31 13:57:45 +08:00
logEntry := &filer_pb.LogEntry{
2021-09-27 02:54:13 +08:00
TsNs: processingTsNs,
2020-04-16 17:55:09 +08:00
PartitionKeyHash: util.HashToInt32(partitionKey),
2020-03-31 13:57:45 +08:00
Data: data,
2024-03-08 02:53:30 +08:00
Key: partitionKey,
2020-03-31 13:57:45 +08:00
}
logEntryData, _ := proto.Marshal(logEntry)
size := len(logEntryData)
2024-01-08 16:03:08 +08:00
if logBuffer.pos == 0 {
logBuffer.startTime = ts
2020-03-31 13:57:45 +08:00
}
2024-01-08 16:03:08 +08:00
if logBuffer.startTime.Add(logBuffer.flushInterval).Before(ts) || len(logBuffer.buf)-logBuffer.pos < size+4 {
2024-01-15 16:20:12 +08:00
// glog.V(0).Infof("%s copyToFlush1 batch:%d count:%d start time %v, ts %v, remaining %d bytes", logBuffer.name, logBuffer.batchIndex, len(logBuffer.idx), logBuffer.startTime, ts, len(logBuffer.buf)-logBuffer.pos)
2024-01-08 16:03:08 +08:00
toFlush = logBuffer.copyToFlush()
logBuffer.startTime = ts
if len(logBuffer.buf) < size+4 {
logBuffer.buf = make([]byte, 2*size+4)
2020-04-05 15:51:16 +08:00
}
2020-03-31 13:57:45 +08:00
}
2024-01-08 16:03:08 +08:00
logBuffer.stopTime = ts
2020-03-31 13:57:45 +08:00
2024-01-08 16:03:08 +08:00
logBuffer.idx = append(logBuffer.idx, logBuffer.pos)
util.Uint32toBytes(logBuffer.sizeBuf, uint32(size))
copy(logBuffer.buf[logBuffer.pos:logBuffer.pos+4], logBuffer.sizeBuf)
copy(logBuffer.buf[logBuffer.pos+4:logBuffer.pos+4+size], logEntryData)
logBuffer.pos += size + 4
2024-01-06 07:16:22 +08:00
// fmt.Printf("partitionKey %v entry size %d total %d count %d\n", string(partitionKey), size, m.pos, len(m.idx))
2020-03-31 13:57:45 +08:00
}
2024-01-08 16:03:08 +08:00
func (logBuffer *LogBuffer) IsStopping() bool {
return logBuffer.isStopping.Load()
}
2024-01-16 13:31:21 +08:00
func (logBuffer *LogBuffer) ShutdownLogBuffer() {
2024-01-08 16:03:08 +08:00
isAlreadyStopped := logBuffer.isStopping.Swap(true)
if isAlreadyStopped {
2020-03-31 13:57:45 +08:00
return
}
2024-01-08 16:03:08 +08:00
toFlush := logBuffer.copyToFlush()
logBuffer.flushChan <- toFlush
close(logBuffer.flushChan)
2020-03-31 13:57:45 +08:00
}
2024-01-08 16:03:08 +08:00
func (logBuffer *LogBuffer) loopFlush() {
for d := range logBuffer.flushChan {
2020-04-06 07:51:30 +08:00
if d != nil {
2021-06-27 20:51:28 +08:00
// glog.V(4).Infof("%s flush [%v, %v] size %d", m.name, d.startTime, d.stopTime, len(d.data.Bytes()))
logBuffer.flushFn(logBuffer, d.startTime, d.stopTime, d.data.Bytes())
d.releaseMemory()
// local logbuffer is different from aggregate logbuffer here
2024-03-17 01:51:16 +08:00
logBuffer.lastFlushDataTime = d.stopTime
2020-04-06 07:51:30 +08:00
}
}
}
2024-01-08 16:03:08 +08:00
func (logBuffer *LogBuffer) loopInterval() {
for !logBuffer.IsStopping() {
time.Sleep(logBuffer.flushInterval)
if logBuffer.IsStopping() {
return
}
2024-01-08 16:03:08 +08:00
logBuffer.Lock()
toFlush := logBuffer.copyToFlush()
logBuffer.Unlock()
if toFlush != nil {
2024-03-22 14:27:19 +08:00
glog.V(4).Infof("%s flush [%v, %v] size %d", logBuffer.name, toFlush.startTime, toFlush.stopTime, len(toFlush.data.Bytes()))
2024-01-08 16:03:08 +08:00
logBuffer.flushChan <- toFlush
2024-01-06 07:16:22 +08:00
} else {
// glog.V(0).Infof("%s no flush", m.name)
}
2020-03-31 13:57:45 +08:00
}
}
2024-01-08 16:03:08 +08:00
func (logBuffer *LogBuffer) copyToFlush() *dataToFlush {
2020-04-06 03:38:29 +08:00
2024-01-08 16:03:08 +08:00
if logBuffer.pos > 0 {
2020-04-06 11:15:17 +08:00
// fmt.Printf("flush buffer %d pos %d empty space %d\n", len(m.buf), m.pos, len(m.buf)-m.pos)
2020-07-12 00:12:03 +08:00
var d *dataToFlush
2024-01-08 16:03:08 +08:00
if logBuffer.flushFn != nil {
2020-07-12 00:12:03 +08:00
d = &dataToFlush{
2024-01-08 16:03:08 +08:00
startTime: logBuffer.startTime,
stopTime: logBuffer.stopTime,
data: copiedBytes(logBuffer.buf[:logBuffer.pos]),
2020-07-12 00:12:03 +08:00
}
// glog.V(4).Infof("%s flushing [0,%d) with %d entries [%v, %v]", m.name, m.pos, len(m.idx), m.startTime, m.stopTime)
} else {
// glog.V(4).Infof("%s removed from memory [0,%d) with %d entries [%v, %v]", m.name, m.pos, len(m.idx), m.startTime, m.stopTime)
2024-03-17 01:51:16 +08:00
logBuffer.lastFlushDataTime = logBuffer.stopTime
2020-04-06 07:51:30 +08:00
}
2024-01-08 16:03:08 +08:00
logBuffer.buf = logBuffer.prevBuffers.SealBuffer(logBuffer.startTime, logBuffer.stopTime, logBuffer.buf, logBuffer.pos, logBuffer.batchIndex)
logBuffer.startTime = time.Unix(0, 0)
logBuffer.stopTime = time.Unix(0, 0)
logBuffer.pos = 0
logBuffer.idx = logBuffer.idx[:0]
logBuffer.batchIndex++
2020-04-06 07:51:30 +08:00
return d
2020-04-05 15:51:16 +08:00
}
2020-04-06 07:51:30 +08:00
return nil
2020-04-05 15:51:16 +08:00
}
2024-03-01 01:38:52 +08:00
func (logBuffer *LogBuffer) GetEarliestTime() time.Time {
2024-01-08 16:03:08 +08:00
return logBuffer.startTime
}
2024-03-01 01:38:52 +08:00
func (logBuffer *LogBuffer) GetEarliestPosition() MessagePosition {
2024-01-08 16:03:08 +08:00
return MessagePosition{
Time: logBuffer.startTime,
BatchIndex: logBuffer.batchIndex,
}
}
func (d *dataToFlush) releaseMemory() {
d.data.Reset()
bufferPool.Put(d.data)
}
2024-01-15 16:20:12 +08:00
func (logBuffer *LogBuffer) ReadFromBuffer(lastReadPosition MessagePosition) (bufferCopy *bytes.Buffer, batchIndex int64, err error) {
2024-01-08 16:03:08 +08:00
logBuffer.RLock()
defer logBuffer.RUnlock()
2020-04-05 15:51:16 +08:00
// Read from disk and memory
// 1. read from disk, last time is = td
// 2. in memory, the earliest time = tm
// if tm <= td, case 2.1
// read from memory
// if tm is empty, case 2.2
// read from memory
// if td < tm, case 2.3
// read from disk again
var tsMemory time.Time
2024-01-08 16:03:08 +08:00
var tsBatchIndex int64
if !logBuffer.startTime.IsZero() {
tsMemory = logBuffer.startTime
tsBatchIndex = logBuffer.batchIndex
}
2024-01-08 16:03:08 +08:00
for _, prevBuf := range logBuffer.prevBuffers.buffers {
if !prevBuf.startTime.IsZero() && prevBuf.startTime.Before(tsMemory) {
tsMemory = prevBuf.startTime
2024-01-08 16:03:08 +08:00
tsBatchIndex = prevBuf.batchIndex
}
}
if tsMemory.IsZero() { // case 2.2
2024-03-22 14:30:49 +08:00
// println("2.2 no data")
2024-03-01 01:38:52 +08:00
return nil, -2, nil
} else if lastReadPosition.Before(tsMemory) && lastReadPosition.BatchIndex+1 < tsBatchIndex { // case 2.3
2024-03-17 01:51:16 +08:00
if !logBuffer.lastFlushDataTime.IsZero() {
glog.V(0).Infof("resume with last flush time: %v", logBuffer.lastFlushDataTime)
2024-01-08 16:03:08 +08:00
return nil, -2, ResumeFromDiskError
}
}
// the following is case 2.1
2020-04-05 15:51:16 +08:00
2024-01-08 16:03:08 +08:00
if lastReadPosition.Equal(logBuffer.stopTime) {
return nil, logBuffer.batchIndex, nil
2020-04-05 15:51:16 +08:00
}
2024-01-08 16:03:08 +08:00
if lastReadPosition.After(logBuffer.stopTime) {
// glog.Fatalf("unexpected last read time %v, older than latest %v", lastReadPosition, m.stopTime)
return nil, logBuffer.batchIndex, nil
2020-03-31 13:57:45 +08:00
}
2024-01-08 16:03:08 +08:00
if lastReadPosition.Before(logBuffer.startTime) {
// println("checking ", lastReadPosition.UnixNano())
for _, buf := range logBuffer.prevBuffers.buffers {
if buf.startTime.After(lastReadPosition.Time) {
2021-06-27 20:51:28 +08:00
// glog.V(4).Infof("%s return the %d sealed buffer %v", m.name, i, buf.startTime)
2020-04-30 18:05:34 +08:00
// println("return the", i, "th in memory", buf.startTime.UnixNano())
2024-01-08 16:03:08 +08:00
return copiedBytes(buf.buf[:buf.size]), buf.batchIndex, nil
}
2024-01-08 16:03:08 +08:00
if !buf.startTime.After(lastReadPosition.Time) && buf.stopTime.After(lastReadPosition.Time) {
pos := buf.locateByTs(lastReadPosition.Time)
2020-04-21 08:43:50 +08:00
// fmt.Printf("locate buffer[%d] pos %d\n", i, pos)
2024-01-08 16:03:08 +08:00
return copiedBytes(buf.buf[pos:buf.size]), buf.batchIndex, nil
}
}
2024-01-08 16:03:08 +08:00
// glog.V(4).Infof("%s return the current buf %v", m.name, lastReadPosition)
2024-03-01 01:38:52 +08:00
return copiedBytes(logBuffer.buf[:logBuffer.pos]), logBuffer.batchIndex, nil
2020-04-05 15:51:16 +08:00
}
2024-01-08 16:03:08 +08:00
lastTs := lastReadPosition.UnixNano()
l, h := 0, len(logBuffer.idx)-1
2020-04-06 11:15:17 +08:00
2020-04-06 07:51:30 +08:00
/*
2020-04-10 16:35:59 +08:00
for i, pos := range m.idx {
logEntry, ts := readTs(m.buf, pos)
2020-04-13 12:00:55 +08:00
event := &filer_pb.SubscribeMetadataResponse{}
2020-04-10 16:35:59 +08:00
proto.Unmarshal(logEntry.Data, event)
entry := event.EventNotification.OldEntry
if entry == nil {
entry = event.EventNotification.NewEntry
}
fmt.Printf("entry %d ts: %v offset:%d dir:%s name:%s\n", i, time.Unix(0, ts), pos, event.Directory, entry.Name)
2020-04-06 07:51:30 +08:00
}
2020-04-10 16:35:59 +08:00
fmt.Printf("l=%d, h=%d\n", l, h)
*/
2020-04-05 15:51:16 +08:00
2020-04-06 08:34:26 +08:00
for l <= h {
2020-04-05 15:51:16 +08:00
mid := (l + h) / 2
2024-01-08 16:03:08 +08:00
pos := logBuffer.idx[mid]
_, t := readTs(logBuffer.buf, pos)
2020-04-05 15:51:16 +08:00
if t <= lastTs {
l = mid + 1
} else if lastTs < t {
var prevT int64
if mid > 0 {
2024-01-08 16:03:08 +08:00
_, prevT = readTs(logBuffer.buf, logBuffer.idx[mid-1])
2020-04-05 15:51:16 +08:00
}
if prevT <= lastTs {
// fmt.Printf("found l=%d, m-1=%d(ts=%d), m=%d(ts=%d), h=%d [%d, %d) \n", l, mid-1, prevT, mid, t, h, pos, m.pos)
2024-01-08 16:03:08 +08:00
return copiedBytes(logBuffer.buf[pos:logBuffer.pos]), logBuffer.batchIndex, nil
2020-04-05 15:51:16 +08:00
}
h = mid
2020-04-05 15:51:16 +08:00
}
// fmt.Printf("l=%d, h=%d\n", l, h)
}
2020-04-06 11:15:17 +08:00
// FIXME: this could be that the buffer has been flushed already
2024-01-08 16:03:08 +08:00
println("Not sure why no data", lastReadPosition.BatchIndex, tsBatchIndex)
return nil, -2, nil
2020-04-06 11:15:17 +08:00
2020-04-05 15:51:16 +08:00
}
2024-01-08 16:03:08 +08:00
func (logBuffer *LogBuffer) ReleaseMemory(b *bytes.Buffer) {
bufferPool.Put(b)
}
var bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
func copiedBytes(buf []byte) (copied *bytes.Buffer) {
copied = bufferPool.Get().(*bytes.Buffer)
2020-04-29 17:41:01 +08:00
copied.Reset()
copied.Write(buf)
2020-04-05 15:51:16 +08:00
return
}
func readTs(buf []byte, pos int) (size int, ts int64) {
2020-04-05 15:51:16 +08:00
size = int(util.BytesToUint32(buf[pos : pos+4]))
entryData := buf[pos+4 : pos+4+size]
2020-04-05 15:51:16 +08:00
logEntry := &filer_pb.LogEntry{}
err := proto.Unmarshal(entryData, logEntry)
if err != nil {
glog.Fatalf("unexpected unmarshal filer_pb.LogEntry: %v", err)
}
return size, logEntry.TsNs
2020-04-05 15:51:16 +08:00
2020-03-31 13:57:45 +08:00
}