2022-02-14 14:50:44 +08:00
|
|
|
package page_writer
|
|
|
|
|
|
|
|
import (
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/mem"
|
2022-02-14 14:50:44 +08:00
|
|
|
"os"
|
2022-04-03 16:08:25 +08:00
|
|
|
"sync"
|
2022-02-14 14:50:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ = PageChunk(&SwapFileChunk{})
|
|
|
|
)
|
|
|
|
|
|
|
|
type ActualChunkIndex int
|
|
|
|
|
|
|
|
type SwapFile struct {
|
2022-04-03 16:08:25 +08:00
|
|
|
dir string
|
|
|
|
file *os.File
|
|
|
|
logicToActualChunkIndex map[LogicChunkIndex]ActualChunkIndex
|
|
|
|
logicToActualChunkIndexLock sync.Mutex
|
|
|
|
chunkSize int64
|
|
|
|
freeActualChunkList []ActualChunkIndex
|
2022-02-14 14:50:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type SwapFileChunk struct {
|
2022-08-22 14:26:06 +08:00
|
|
|
sync.RWMutex
|
2022-02-14 14:50:44 +08:00
|
|
|
swapfile *SwapFile
|
|
|
|
usage *ChunkWrittenIntervalList
|
|
|
|
logicChunkIndex LogicChunkIndex
|
|
|
|
actualChunkIndex ActualChunkIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSwapFile(dir string, chunkSize int64) *SwapFile {
|
|
|
|
return &SwapFile{
|
|
|
|
dir: dir,
|
|
|
|
file: nil,
|
|
|
|
logicToActualChunkIndex: make(map[LogicChunkIndex]ActualChunkIndex),
|
|
|
|
chunkSize: chunkSize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (sf *SwapFile) FreeResource() {
|
|
|
|
if sf.file != nil {
|
|
|
|
sf.file.Close()
|
|
|
|
os.Remove(sf.file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sf *SwapFile) NewTempFileChunk(logicChunkIndex LogicChunkIndex) (tc *SwapFileChunk) {
|
|
|
|
if sf.file == nil {
|
|
|
|
var err error
|
|
|
|
sf.file, err = os.CreateTemp(sf.dir, "")
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("create swap file: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-04-03 16:08:25 +08:00
|
|
|
sf.logicToActualChunkIndexLock.Lock()
|
|
|
|
defer sf.logicToActualChunkIndexLock.Unlock()
|
2022-02-14 14:50:44 +08:00
|
|
|
actualChunkIndex, found := sf.logicToActualChunkIndex[logicChunkIndex]
|
|
|
|
if !found {
|
2022-03-14 09:17:35 +08:00
|
|
|
if len(sf.freeActualChunkList) > 0 {
|
|
|
|
actualChunkIndex = sf.freeActualChunkList[0]
|
|
|
|
sf.freeActualChunkList = sf.freeActualChunkList[1:]
|
|
|
|
} else {
|
|
|
|
actualChunkIndex = ActualChunkIndex(len(sf.logicToActualChunkIndex))
|
|
|
|
}
|
2022-02-14 14:50:44 +08:00
|
|
|
sf.logicToActualChunkIndex[logicChunkIndex] = actualChunkIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SwapFileChunk{
|
|
|
|
swapfile: sf,
|
|
|
|
usage: newChunkWrittenIntervalList(),
|
|
|
|
logicChunkIndex: logicChunkIndex,
|
|
|
|
actualChunkIndex: actualChunkIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SwapFileChunk) FreeResource() {
|
2022-04-03 16:08:25 +08:00
|
|
|
sc.swapfile.logicToActualChunkIndexLock.Lock()
|
|
|
|
defer sc.swapfile.logicToActualChunkIndexLock.Unlock()
|
|
|
|
|
2022-08-22 14:26:06 +08:00
|
|
|
sc.Lock()
|
|
|
|
defer sc.Unlock()
|
|
|
|
|
2022-03-14 09:17:35 +08:00
|
|
|
sc.swapfile.freeActualChunkList = append(sc.swapfile.freeActualChunkList, sc.actualChunkIndex)
|
|
|
|
delete(sc.swapfile.logicToActualChunkIndex, sc.logicChunkIndex)
|
2022-02-14 14:50:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SwapFileChunk) WriteDataAt(src []byte, offset int64) (n int) {
|
2022-08-22 14:26:06 +08:00
|
|
|
sc.Lock()
|
|
|
|
defer sc.Unlock()
|
|
|
|
|
2022-02-14 14:50:44 +08:00
|
|
|
innerOffset := offset % sc.swapfile.chunkSize
|
|
|
|
var err error
|
|
|
|
n, err = sc.swapfile.file.WriteAt(src, int64(sc.actualChunkIndex)*sc.swapfile.chunkSize+innerOffset)
|
|
|
|
if err == nil {
|
|
|
|
sc.usage.MarkWritten(innerOffset, innerOffset+int64(n))
|
|
|
|
} else {
|
|
|
|
glog.Errorf("failed to write swap file %s: %v", sc.swapfile.file.Name(), err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SwapFileChunk) ReadDataAt(p []byte, off int64) (maxStop int64) {
|
2022-08-22 14:26:06 +08:00
|
|
|
sc.RLock()
|
|
|
|
defer sc.RUnlock()
|
|
|
|
|
2022-02-14 14:50:44 +08:00
|
|
|
chunkStartOffset := int64(sc.logicChunkIndex) * sc.swapfile.chunkSize
|
|
|
|
for t := sc.usage.head.next; t != sc.usage.tail; t = t.next {
|
|
|
|
logicStart := max(off, chunkStartOffset+t.StartOffset)
|
|
|
|
logicStop := min(off+int64(len(p)), chunkStartOffset+t.stopOffset)
|
|
|
|
if logicStart < logicStop {
|
|
|
|
actualStart := logicStart - chunkStartOffset + int64(sc.actualChunkIndex)*sc.swapfile.chunkSize
|
|
|
|
if _, err := sc.swapfile.file.ReadAt(p[logicStart-off:logicStop-off], actualStart); err != nil {
|
|
|
|
glog.Errorf("failed to reading swap file %s: %v", sc.swapfile.file.Name(), err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
maxStop = max(maxStop, logicStop)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SwapFileChunk) IsComplete() bool {
|
2022-08-22 14:26:06 +08:00
|
|
|
sc.RLock()
|
|
|
|
defer sc.RUnlock()
|
2022-02-14 14:50:44 +08:00
|
|
|
return sc.usage.IsComplete(sc.swapfile.chunkSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SwapFileChunk) WrittenSize() int64 {
|
2022-08-22 14:26:06 +08:00
|
|
|
sc.RLock()
|
|
|
|
defer sc.RUnlock()
|
2022-02-14 14:50:44 +08:00
|
|
|
return sc.usage.WrittenSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SwapFileChunk) SaveContent(saveFn SaveToStorageFunc) {
|
|
|
|
if saveFn == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-08-22 14:26:06 +08:00
|
|
|
sc.Lock()
|
|
|
|
defer sc.Unlock()
|
|
|
|
|
2022-02-14 14:50:44 +08:00
|
|
|
for t := sc.usage.head.next; t != sc.usage.tail; t = t.next {
|
|
|
|
data := mem.Allocate(int(t.Size()))
|
|
|
|
sc.swapfile.file.ReadAt(data, t.StartOffset+int64(sc.actualChunkIndex)*sc.swapfile.chunkSize)
|
|
|
|
reader := util.NewBytesReader(data)
|
|
|
|
saveFn(reader, int64(sc.logicChunkIndex)*sc.swapfile.chunkSize+t.StartOffset, t.Size(), func() {
|
|
|
|
})
|
|
|
|
mem.Free(data)
|
|
|
|
}
|
2022-08-22 14:26:06 +08:00
|
|
|
|
2022-02-14 14:50:44 +08:00
|
|
|
sc.usage = newChunkWrittenIntervalList()
|
|
|
|
}
|