2022-02-14 06:50:44 +00:00
|
|
|
package page_writer
|
|
|
|
|
|
|
|
import (
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/mem"
|
2022-02-14 06:50:44 +00:00
|
|
|
"os"
|
2022-04-03 08:08:25 +00:00
|
|
|
"sync"
|
2022-02-14 06:50:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ = PageChunk(&SwapFileChunk{})
|
|
|
|
)
|
|
|
|
|
|
|
|
type ActualChunkIndex int
|
|
|
|
|
|
|
|
type SwapFile struct {
|
2023-01-03 07:20:45 +00:00
|
|
|
dir string
|
|
|
|
file *os.File
|
|
|
|
chunkSize int64
|
|
|
|
chunkTrackingLock sync.Mutex
|
|
|
|
activeChunkCount int
|
|
|
|
freeActualChunkList []ActualChunkIndex
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SwapFileChunk struct {
|
2022-08-22 06:26:06 +00:00
|
|
|
sync.RWMutex
|
2022-02-14 06:50:44 +00:00
|
|
|
swapfile *SwapFile
|
|
|
|
usage *ChunkWrittenIntervalList
|
|
|
|
logicChunkIndex LogicChunkIndex
|
|
|
|
actualChunkIndex ActualChunkIndex
|
2023-01-03 07:20:45 +00:00
|
|
|
activityScore *ActivityScore
|
|
|
|
//memChunk *MemChunk
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSwapFile(dir string, chunkSize int64) *SwapFile {
|
|
|
|
return &SwapFile{
|
2023-01-03 07:20:45 +00:00
|
|
|
dir: dir,
|
|
|
|
file: nil,
|
|
|
|
chunkSize: chunkSize,
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func (sf *SwapFile) FreeResource() {
|
|
|
|
if sf.file != nil {
|
|
|
|
sf.file.Close()
|
|
|
|
os.Remove(sf.file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
func (sf *SwapFile) NewSwapFileChunk(logicChunkIndex LogicChunkIndex) (tc *SwapFileChunk) {
|
2022-02-14 06:50:44 +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
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
sf.chunkTrackingLock.Lock()
|
|
|
|
defer sf.chunkTrackingLock.Unlock()
|
|
|
|
|
|
|
|
sf.activeChunkCount++
|
|
|
|
|
|
|
|
// assign a new physical chunk
|
|
|
|
var actualChunkIndex ActualChunkIndex
|
|
|
|
if len(sf.freeActualChunkList) > 0 {
|
|
|
|
actualChunkIndex = sf.freeActualChunkList[0]
|
|
|
|
sf.freeActualChunkList = sf.freeActualChunkList[1:]
|
|
|
|
} else {
|
|
|
|
actualChunkIndex = ActualChunkIndex(sf.activeChunkCount)
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
swapFileChunk := &SwapFileChunk{
|
2022-02-14 06:50:44 +00:00
|
|
|
swapfile: sf,
|
|
|
|
usage: newChunkWrittenIntervalList(),
|
|
|
|
logicChunkIndex: logicChunkIndex,
|
|
|
|
actualChunkIndex: actualChunkIndex,
|
2023-01-03 07:20:45 +00:00
|
|
|
activityScore: NewActivityScore(),
|
|
|
|
// memChunk: NewMemChunk(logicChunkIndex, sf.chunkSize),
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
|
|
|
|
// println(logicChunkIndex, "|", "++++", swapFileChunk.actualChunkIndex, swapFileChunk, sf)
|
|
|
|
return swapFileChunk
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SwapFileChunk) FreeResource() {
|
2022-04-03 08:08:25 +00:00
|
|
|
|
2022-08-22 06:26:06 +00:00
|
|
|
sc.Lock()
|
|
|
|
defer sc.Unlock()
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
sc.swapfile.chunkTrackingLock.Lock()
|
|
|
|
defer sc.swapfile.chunkTrackingLock.Unlock()
|
|
|
|
|
2022-03-14 01:17:35 +00:00
|
|
|
sc.swapfile.freeActualChunkList = append(sc.swapfile.freeActualChunkList, sc.actualChunkIndex)
|
2023-01-03 07:20:45 +00:00
|
|
|
sc.swapfile.activeChunkCount--
|
|
|
|
// println(sc.logicChunkIndex, "|", "----", sc.actualChunkIndex, sc, sc.swapfile)
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
func (sc *SwapFileChunk) WriteDataAt(src []byte, offset int64, tsNs int64) (n int) {
|
2022-08-22 06:26:06 +00:00
|
|
|
sc.Lock()
|
|
|
|
defer sc.Unlock()
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
// println(sc.logicChunkIndex, "|", tsNs, "write at", offset, len(src), sc.actualChunkIndex)
|
|
|
|
|
2022-02-14 06:50:44 +00:00
|
|
|
innerOffset := offset % sc.swapfile.chunkSize
|
|
|
|
var err error
|
|
|
|
n, err = sc.swapfile.file.WriteAt(src, int64(sc.actualChunkIndex)*sc.swapfile.chunkSize+innerOffset)
|
2023-01-03 07:20:45 +00:00
|
|
|
sc.usage.MarkWritten(innerOffset, innerOffset+int64(n), tsNs)
|
|
|
|
if err != nil {
|
2022-02-14 06:50:44 +00:00
|
|
|
glog.Errorf("failed to write swap file %s: %v", sc.swapfile.file.Name(), err)
|
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
//sc.memChunk.WriteDataAt(src, offset, tsNs)
|
|
|
|
sc.activityScore.MarkWrite()
|
|
|
|
|
2022-02-14 06:50:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
func (sc *SwapFileChunk) ReadDataAt(p []byte, off int64, tsNs int64) (maxStop int64) {
|
2022-08-22 06:26:06 +00:00
|
|
|
sc.RLock()
|
|
|
|
defer sc.RUnlock()
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
// println(sc.logicChunkIndex, "|", tsNs, "read at", off, len(p), sc.actualChunkIndex)
|
|
|
|
|
|
|
|
//memCopy := make([]byte, len(p))
|
|
|
|
//copy(memCopy, p)
|
|
|
|
|
2022-02-14 06:50:44 +00: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 {
|
2023-01-03 07:20:45 +00:00
|
|
|
if t.TsNs >= tsNs {
|
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
println("read old data2", tsNs-t.TsNs, "ns")
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
//sc.memChunk.ReadDataAt(memCopy, off, tsNs)
|
|
|
|
//if bytes.Compare(memCopy, p) != 0 {
|
|
|
|
// println("read wrong data from swap file", off, sc.logicChunkIndex)
|
|
|
|
//}
|
|
|
|
|
|
|
|
sc.activityScore.MarkRead()
|
|
|
|
|
2022-02-14 06:50:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *SwapFileChunk) IsComplete() bool {
|
2022-08-22 06:26:06 +00:00
|
|
|
sc.RLock()
|
|
|
|
defer sc.RUnlock()
|
2022-02-14 06:50:44 +00:00
|
|
|
return sc.usage.IsComplete(sc.swapfile.chunkSize)
|
|
|
|
}
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
func (sc *SwapFileChunk) ActivityScore() int64 {
|
|
|
|
return sc.activityScore.ActivityScore()
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 06:50:39 +00:00
|
|
|
func (sc *SwapFileChunk) WrittenSize() int64 {
|
|
|
|
sc.RLock()
|
|
|
|
defer sc.RUnlock()
|
|
|
|
return sc.usage.WrittenSize()
|
|
|
|
}
|
|
|
|
|
2022-02-14 06:50:44 +00:00
|
|
|
func (sc *SwapFileChunk) SaveContent(saveFn SaveToStorageFunc) {
|
2023-01-03 07:20:45 +00:00
|
|
|
sc.RLock()
|
|
|
|
defer sc.RUnlock()
|
|
|
|
|
2022-02-14 06:50:44 +00:00
|
|
|
if saveFn == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-01-03 07:20:45 +00:00
|
|
|
// println(sc.logicChunkIndex, "|", "save")
|
2022-02-14 06:50:44 +00:00
|
|
|
for t := sc.usage.head.next; t != sc.usage.tail; t = t.next {
|
|
|
|
data := mem.Allocate(int(t.Size()))
|
2023-01-03 07:20:45 +00:00
|
|
|
n, _ := sc.swapfile.file.ReadAt(data, t.StartOffset+int64(sc.actualChunkIndex)*sc.swapfile.chunkSize)
|
|
|
|
if n > 0 {
|
|
|
|
reader := util.NewBytesReader(data[:n])
|
|
|
|
saveFn(reader, int64(sc.logicChunkIndex)*sc.swapfile.chunkSize+t.StartOffset, int64(n), t.TsNs, func() {
|
|
|
|
})
|
|
|
|
}
|
2022-02-14 06:50:44 +00:00
|
|
|
mem.Free(data)
|
|
|
|
}
|
2022-08-22 06:26:06 +00:00
|
|
|
|
2022-02-14 06:50:44 +00:00
|
|
|
}
|