2022-01-22 16:06:53 +00:00
|
|
|
package page_writer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util/mem"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-22 16:08:01 +00:00
|
|
|
_ = PageChunk(&SwapFileChunk{})
|
2022-01-22 16:06:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ActualChunkIndex int
|
|
|
|
|
|
|
|
type SwapFile struct {
|
|
|
|
dir string
|
|
|
|
file *os.File
|
|
|
|
logicToActualChunkIndex map[LogicChunkIndex]ActualChunkIndex
|
|
|
|
chunkSize int64
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:08:01 +00:00
|
|
|
type SwapFileChunk struct {
|
2022-01-22 16:06:53 +00: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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:08:01 +00:00
|
|
|
func (sf *SwapFile) NewTempFileChunk(logicChunkIndex LogicChunkIndex) (tc *SwapFileChunk) {
|
2022-01-22 16:06:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actualChunkIndex, found := sf.logicToActualChunkIndex[logicChunkIndex]
|
|
|
|
if !found {
|
|
|
|
actualChunkIndex = ActualChunkIndex(len(sf.logicToActualChunkIndex))
|
|
|
|
sf.logicToActualChunkIndex[logicChunkIndex] = actualChunkIndex
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:08:01 +00:00
|
|
|
return &SwapFileChunk{
|
2022-01-22 16:06:53 +00:00
|
|
|
swapfile: sf,
|
|
|
|
usage: newChunkWrittenIntervalList(),
|
|
|
|
logicChunkIndex: logicChunkIndex,
|
|
|
|
actualChunkIndex: actualChunkIndex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:08:01 +00:00
|
|
|
func (sc *SwapFileChunk) FreeResource() {
|
2022-01-22 16:06:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 16:08:01 +00:00
|
|
|
func (sc *SwapFileChunk) WriteDataAt(src []byte, offset int64) (n int) {
|
|
|
|
innerOffset := offset % sc.swapfile.chunkSize
|
2022-01-22 16:06:53 +00:00
|
|
|
var err error
|
2022-01-22 16:08:01 +00:00
|
|
|
n, err = sc.swapfile.file.WriteAt(src, int64(sc.actualChunkIndex)*sc.swapfile.chunkSize+innerOffset)
|
2022-01-22 16:06:53 +00:00
|
|
|
if err == nil {
|
2022-01-22 16:08:01 +00:00
|
|
|
sc.usage.MarkWritten(innerOffset, innerOffset+int64(n))
|
2022-01-22 16:06:53 +00:00
|
|
|
} else {
|
2022-01-22 16:08:01 +00:00
|
|
|
glog.Errorf("failed to write swap file %s: %v", sc.swapfile.file.Name(), err)
|
2022-01-22 16:06:53 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:08:01 +00:00
|
|
|
func (sc *SwapFileChunk) ReadDataAt(p []byte, off int64) (maxStop int64) {
|
|
|
|
chunkStartOffset := int64(sc.logicChunkIndex) * sc.swapfile.chunkSize
|
|
|
|
for t := sc.usage.head.next; t != sc.usage.tail; t = t.next {
|
2022-01-22 16:06:53 +00:00
|
|
|
logicStart := max(off, chunkStartOffset+t.StartOffset)
|
|
|
|
logicStop := min(off+int64(len(p)), chunkStartOffset+t.stopOffset)
|
|
|
|
if logicStart < logicStop {
|
2022-01-22 16:08:01 +00:00
|
|
|
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)
|
2022-01-22 16:06:53 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
maxStop = max(maxStop, logicStop)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-22 16:08:01 +00:00
|
|
|
func (sc *SwapFileChunk) IsComplete() bool {
|
|
|
|
return sc.usage.IsComplete(sc.swapfile.chunkSize)
|
2022-01-22 16:06:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 16:08:01 +00:00
|
|
|
func (sc *SwapFileChunk) SaveContent(saveFn SaveToStorageFunc) {
|
2022-01-22 16:06:53 +00:00
|
|
|
if saveFn == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-01-22 16:08:01 +00:00
|
|
|
for t := sc.usage.head.next; t != sc.usage.tail; t = t.next {
|
2022-01-22 16:06:53 +00:00
|
|
|
data := mem.Allocate(int(t.Size()))
|
2022-01-22 16:08:01 +00:00
|
|
|
sc.swapfile.file.ReadAt(data, t.StartOffset+int64(sc.actualChunkIndex)*sc.swapfile.chunkSize)
|
2022-01-22 16:06:53 +00:00
|
|
|
reader := util.NewBytesReader(data)
|
2022-01-22 16:08:01 +00:00
|
|
|
saveFn(reader, int64(sc.logicChunkIndex)*sc.swapfile.chunkSize+t.StartOffset, t.Size(), func() {
|
2022-01-22 16:06:53 +00:00
|
|
|
})
|
|
|
|
mem.Free(data)
|
|
|
|
}
|
2022-01-24 02:30:53 +00:00
|
|
|
sc.usage = newChunkWrittenIntervalList()
|
2022-01-22 16:06:53 +00:00
|
|
|
}
|