avoid concurrent access to map object

fix https://github.com/chrislusf/seaweedfs/issues/2866
This commit is contained in:
chrislu 2022-04-03 01:08:25 -07:00
parent 6a2bcd03aa
commit e8d7bb42e2

View file

@ -5,6 +5,7 @@ import (
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
"github.com/chrislusf/seaweedfs/weed/util/mem" "github.com/chrislusf/seaweedfs/weed/util/mem"
"os" "os"
"sync"
) )
var ( var (
@ -14,11 +15,12 @@ var (
type ActualChunkIndex int type ActualChunkIndex int
type SwapFile struct { type SwapFile struct {
dir string dir string
file *os.File file *os.File
logicToActualChunkIndex map[LogicChunkIndex]ActualChunkIndex logicToActualChunkIndex map[LogicChunkIndex]ActualChunkIndex
chunkSize int64 logicToActualChunkIndexLock sync.Mutex
freeActualChunkList []ActualChunkIndex chunkSize int64
freeActualChunkList []ActualChunkIndex
} }
type SwapFileChunk struct { type SwapFileChunk struct {
@ -52,6 +54,8 @@ func (sf *SwapFile) NewTempFileChunk(logicChunkIndex LogicChunkIndex) (tc *SwapF
return nil return nil
} }
} }
sf.logicToActualChunkIndexLock.Lock()
defer sf.logicToActualChunkIndexLock.Unlock()
actualChunkIndex, found := sf.logicToActualChunkIndex[logicChunkIndex] actualChunkIndex, found := sf.logicToActualChunkIndex[logicChunkIndex]
if !found { if !found {
if len(sf.freeActualChunkList) > 0 { if len(sf.freeActualChunkList) > 0 {
@ -72,6 +76,9 @@ func (sf *SwapFile) NewTempFileChunk(logicChunkIndex LogicChunkIndex) (tc *SwapF
} }
func (sc *SwapFileChunk) FreeResource() { func (sc *SwapFileChunk) FreeResource() {
sc.swapfile.logicToActualChunkIndexLock.Lock()
defer sc.swapfile.logicToActualChunkIndexLock.Unlock()
sc.swapfile.freeActualChunkList = append(sc.swapfile.freeActualChunkList, sc.actualChunkIndex) sc.swapfile.freeActualChunkList = append(sc.swapfile.freeActualChunkList, sc.actualChunkIndex)
delete(sc.swapfile.logicToActualChunkIndex, sc.logicChunkIndex) delete(sc.swapfile.logicToActualChunkIndex, sc.logicChunkIndex)
} }