mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-07-27 02:23:23 +00:00
* mount: tolerance-window write pattern detection for concurrent writeback WriterPattern decides whether each dirty chunk is buffered in RAM (NewMemChunk) or spilled to an on-disk swap file (NewSwapFileChunk), so a sequential stream misclassified as random is pushed through disk-backed swap. The old detector compared each write's start to the previous write's exact stop offset (atomic.Swap of lastWriteStopOffset, then lastOffset == offset). That is brittle under concurrent FUSE writeback: MonitorWriteAt runs before the per-handle write lock, so parallel Write upcalls interleave the swap, and writeback flushes dirty pages slightly out of offset order. A genuinely sequential stream then repeatedly reads as random and spills to swap. Replace the exact match with the same frontier + tolerance approach used on the read side: track a never-regressing max frontier (offset+size) via a CAS loop and treat a write whose start is within SeqTolerance (8 MiB) of that frontier as sequential. The existing +/-ModeChangeLimit hysteresis is kept, so a single outlier write can't flip the mode. Adds page_writer_pattern_test.go covering sequentiality, hysteresis, reorder tolerance, the inclusive tolerance boundary, frontier non-regression (the CAS invariant), and recovery back to sequential mode. * mount: read write frontier inside the CAS loop Capture the frontier from the CAS loop's own load rather than a separate up-front snapshot, so the sequentiality diff is judged against the freshest pre-image even if a concurrent writeback upcall advances the frontier while we loop. Also drops the now-redundant load. Behavior is unchanged for the single-threaded tests; addresses review feedback on #9984.
69 lines
2.3 KiB
Go
69 lines
2.3 KiB
Go
package mount
|
|
|
|
import "sync/atomic"
|
|
|
|
type WriterPattern struct {
|
|
isSequentialCounter int64
|
|
writeFrontier int64 // highest (offset+size) observed across writes
|
|
chunkSize int64
|
|
}
|
|
|
|
const ModeChangeLimit = 3
|
|
|
|
// SeqTolerance: a write whose start is within this many bytes of the current
|
|
// write frontier still counts as sequential. Using a tolerance window rather
|
|
// than strict contiguity absorbs reordered/concurrent FUSE writeback — multiple
|
|
// Write upcalls for one handle interleave (MonitorWriteAt runs before the
|
|
// per-handle write lock) and writeback flushes dirty pages slightly out of
|
|
// offset order — while still rejecting far random seeks.
|
|
const SeqTolerance = 8 << 20 // 8 MiB
|
|
|
|
// For streaming write: keep dirty chunks in memory (NewMemChunk).
|
|
// For random write: fall back to the on-disk swap file (NewSwapFileChunk).
|
|
|
|
func NewWriterPattern(chunkSize int64) *WriterPattern {
|
|
return &WriterPattern{
|
|
isSequentialCounter: 0,
|
|
writeFrontier: 0,
|
|
chunkSize: chunkSize,
|
|
}
|
|
}
|
|
|
|
func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
|
|
// Advance the frontier to max(frontier, offset+size) and capture, in the same
|
|
// CAS loop, the pre-image this write is judged against. Reading the frontier
|
|
// inside the loop (rather than once up front) keeps `diff` below comparing
|
|
// against the freshest value even if a concurrent writeback upcall advances
|
|
// the frontier while we loop. Lock-free, consistent with the atomic counter.
|
|
end := offset + int64(size)
|
|
var frontier int64
|
|
for {
|
|
frontier = atomic.LoadInt64(&rp.writeFrontier)
|
|
if end <= frontier || atomic.CompareAndSwapInt64(&rp.writeFrontier, frontier, end) {
|
|
break
|
|
}
|
|
}
|
|
|
|
// near = this write starts within SeqTolerance of where writes had reached.
|
|
// Hysteresis (the ±ModeChangeLimit counter) keeps a single outlier write
|
|
// from flipping the mode and spilling a sequential stream to the swap file.
|
|
diff := offset - frontier
|
|
if diff < 0 {
|
|
diff = -diff
|
|
}
|
|
counter := atomic.LoadInt64(&rp.isSequentialCounter)
|
|
if diff <= SeqTolerance {
|
|
if counter < ModeChangeLimit {
|
|
atomic.AddInt64(&rp.isSequentialCounter, 1)
|
|
}
|
|
} else {
|
|
if counter > -ModeChangeLimit {
|
|
atomic.AddInt64(&rp.isSequentialCounter, -1)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (rp *WriterPattern) IsSequentialMode() bool {
|
|
return atomic.LoadInt64(&rp.isSequentialCounter) >= 0
|
|
}
|