aboutsummaryrefslogtreecommitdiff
path: root/weed/filer
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
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')
-rw-r--r--weed/filer/reader_cache.go45
-rw-r--r--weed/filer/reader_pattern.go20
2 files changed, 39 insertions, 26 deletions
diff --git a/weed/filer/reader_cache.go b/weed/filer/reader_cache.go
index eb2308758..89db04eb0 100644
--- a/weed/filer/reader_cache.go
+++ b/weed/filer/reader_cache.go
@@ -3,6 +3,7 @@ package filer
import (
"fmt"
"sync"
+ "sync/atomic"
"time"
"github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
@@ -20,17 +21,17 @@ type ReaderCache struct {
type SingleChunkCacher struct {
sync.Mutex
- parent *ReaderCache
- chunkFileId string
- data []byte
- err error
- cipherKey []byte
- isGzipped bool
- chunkSize int
- shouldCache bool
- wg sync.WaitGroup
- cacheStartedCh chan struct{}
- completedTime time.Time
+ parent *ReaderCache
+ chunkFileId string
+ data []byte
+ err error
+ cipherKey []byte
+ isGzipped bool
+ chunkSize int
+ shouldCache bool
+ wg sync.WaitGroup
+ cacheStartedCh chan struct{}
+ completedTimeNew int64
}
func newReaderCache(limit int, chunkCache chunk_cache.ChunkCache, lookupFileIdFn wdclient.LookupFileIdFunctionType) *ReaderCache {
@@ -50,13 +51,17 @@ func (rc *ReaderCache) MaybeCache(chunkViews []*ChunkView) {
rc.Lock()
defer rc.Unlock()
+ if len(rc.downloaders) >= rc.limit {
+ return
+ }
+
for _, chunkView := range chunkViews {
if _, found := rc.downloaders[chunkView.FileId]; found {
continue
}
if len(rc.downloaders) >= rc.limit {
- // if still no slots, return
+ // abort when slots are filled
return
}
@@ -74,27 +79,28 @@ func (rc *ReaderCache) MaybeCache(chunkViews []*ChunkView) {
func (rc *ReaderCache) ReadChunkAt(buffer []byte, fileId string, cipherKey []byte, isGzipped bool, offset int64, chunkSize int, shouldCache bool) (int, error) {
rc.Lock()
- defer rc.Unlock()
+
if cacher, found := rc.downloaders[fileId]; found {
if n, err := cacher.readChunkAt(buffer, offset); n != 0 && err == nil {
+ rc.Unlock()
return n, err
}
}
if shouldCache || rc.lookupFileIdFn == nil {
n, err := rc.chunkCache.ReadChunkAt(buffer, fileId, uint64(offset))
if n > 0 {
+ rc.Unlock()
return n, err
}
}
// clean up old downloaders
if len(rc.downloaders) >= rc.limit {
- oldestFid, oldestTime := "", time.Now()
+ oldestFid, oldestTime := "", time.Now().Unix()
for fid, downloader := range rc.downloaders {
- if !downloader.completedTime.IsZero() {
- if downloader.completedTime.Before(oldestTime) {
- oldestFid, oldestTime = fid, downloader.completedTime
- }
+ completedTime := atomic.LoadInt64(&downloader.completedTimeNew)
+ if completedTime > 0 && completedTime < oldestTime {
+ oldestFid, oldestTime = fid, completedTime
}
}
if oldestFid != "" {
@@ -110,6 +116,7 @@ func (rc *ReaderCache) ReadChunkAt(buffer []byte, fileId string, cipherKey []byt
go cacher.startCaching()
<-cacher.cacheStartedCh
rc.downloaders[fileId] = cacher
+ rc.Unlock()
return cacher.readChunkAt(buffer, offset)
}
@@ -172,7 +179,7 @@ func (s *SingleChunkCacher) startCaching() {
if s.shouldCache {
s.parent.chunkCache.SetChunk(s.chunkFileId, s.data)
}
- s.completedTime = time.Now()
+ atomic.StoreInt64(&s.completedTimeNew, time.Now().Unix())
return
}
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
}