mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
atomic operation
This commit is contained in:
parent
57a46f46a0
commit
301b49b63f
|
@ -1,5 +1,7 @@
|
||||||
package mount
|
package mount
|
||||||
|
|
||||||
|
import "sync/atomic"
|
||||||
|
|
||||||
type WriterPattern struct {
|
type WriterPattern struct {
|
||||||
isSequentialCounter int64
|
isSequentialCounter int64
|
||||||
lastWriteStopOffset int64
|
lastWriteStopOffset int64
|
||||||
|
@ -20,18 +22,19 @@ func NewWriterPattern(chunkSize int64) *WriterPattern {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
|
func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
|
||||||
if rp.lastWriteStopOffset == offset {
|
lastOffset := atomic.SwapInt64(&rp.lastWriteStopOffset, offset+int64(size))
|
||||||
if rp.isSequentialCounter < ModeChangeLimit {
|
counter := atomic.LoadInt64(&rp.isSequentialCounter)
|
||||||
rp.isSequentialCounter++
|
if lastOffset == offset {
|
||||||
|
if counter < ModeChangeLimit {
|
||||||
|
atomic.AddInt64(&rp.isSequentialCounter, 1)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if rp.isSequentialCounter > -ModeChangeLimit {
|
if counter > -ModeChangeLimit {
|
||||||
rp.isSequentialCounter--
|
atomic.AddInt64(&rp.isSequentialCounter, -1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rp.lastWriteStopOffset = offset + int64(size)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *WriterPattern) IsSequentialMode() bool {
|
func (rp *WriterPattern) IsSequentialMode() bool {
|
||||||
return rp.isSequentialCounter >= 0
|
return atomic.LoadInt64(&rp.isSequentialCounter) >= 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue