aboutsummaryrefslogtreecommitdiff
path: root/weed/filer/reader_pattern.go
diff options
context:
space:
mode:
authorPatrick Schmidt <patrick.schmidt@innogames.com>2022-08-27 01:41:37 +0200
committerGitHub <noreply@github.com>2022-08-26 16:41:37 -0700
commit5df105b1f94b8776d18159ae213da39299e2ea37 (patch)
tree947bd9517ff31f6927dcaed3019c117179073a48 /weed/filer/reader_pattern.go
parentf5156cf3a8d08622574865532ef5139537384b40 (diff)
downloadseaweedfs-5df105b1f94b8776d18159ae213da39299e2ea37.tar.xz
seaweedfs-5df105b1f94b8776d18159ae213da39299e2ea37.zip
Fix a few data races when reading files in mount (#3527)
Diffstat (limited to 'weed/filer/reader_pattern.go')
-rw-r--r--weed/filer/reader_pattern.go20
1 files changed, 13 insertions, 7 deletions
diff --git a/weed/filer/reader_pattern.go b/weed/filer/reader_pattern.go
index e32f7fc2d..b0906e99f 100644
--- a/weed/filer/reader_pattern.go
+++ b/weed/filer/reader_pattern.go
@@ -1,5 +1,9 @@
package filer
+import (
+ "sync/atomic"
+)
+
type ReaderPattern struct {
isSequentialCounter int64
lastReadStopOffset int64
@@ -18,18 +22,20 @@ func NewReaderPattern() *ReaderPattern {
}
func (rp *ReaderPattern) MonitorReadAt(offset int64, size int) {
- if rp.lastReadStopOffset == offset {
- if rp.isSequentialCounter < ModeChangeLimit {
- rp.isSequentialCounter++
+ lastOffset := atomic.SwapInt64(&rp.lastReadStopOffset, offset+int64(size))
+ counter := atomic.LoadInt64(&rp.isSequentialCounter)
+
+ if lastOffset == offset {
+ if counter < ModeChangeLimit {
+ atomic.AddInt64(&rp.isSequentialCounter, 1)
}
} else {
- if rp.isSequentialCounter > -ModeChangeLimit {
- rp.isSequentialCounter--
+ if counter > -ModeChangeLimit {
+ atomic.AddInt64(&rp.isSequentialCounter, -1)
}
}
- rp.lastReadStopOffset = offset + int64(size)
}
func (rp *ReaderPattern) IsRandomMode() bool {
- return rp.isSequentialCounter < 0
+ return atomic.LoadInt64(&rp.isSequentialCounter) < 0
}