seaweedfs/weed/mount/page_writer_pattern.go

41 lines
971 B
Go
Raw Permalink Normal View History

package mount
2022-08-27 00:04:11 +00:00
import "sync/atomic"
type WriterPattern struct {
isSequentialCounter int64
lastWriteStopOffset int64
chunkSize int64
}
2022-08-07 17:14:01 +00:00
const ModeChangeLimit = 3
// For streaming write: only cache the first chunk
// For random write: fall back to temp file approach
func NewWriterPattern(chunkSize int64) *WriterPattern {
return &WriterPattern{
isSequentialCounter: 0,
lastWriteStopOffset: 0,
chunkSize: chunkSize,
}
}
func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
2022-08-27 00:04:11 +00:00
lastOffset := atomic.SwapInt64(&rp.lastWriteStopOffset, offset+int64(size))
counter := atomic.LoadInt64(&rp.isSequentialCounter)
if lastOffset == offset {
if counter < ModeChangeLimit {
atomic.AddInt64(&rp.isSequentialCounter, 1)
2022-08-07 17:14:01 +00:00
}
} else {
2022-08-27 00:04:11 +00:00
if counter > -ModeChangeLimit {
atomic.AddInt64(&rp.isSequentialCounter, -1)
2022-08-07 17:14:01 +00:00
}
}
}
func (rp *WriterPattern) IsSequentialMode() bool {
2022-08-27 00:04:11 +00:00
return atomic.LoadInt64(&rp.isSequentialCounter) >= 0
}