2018-05-29 03:30:17 +08:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-10-15 14:28:03 +08:00
|
|
|
"io"
|
2020-10-21 17:16:21 +08:00
|
|
|
"runtime"
|
2020-10-15 14:28:03 +08:00
|
|
|
"sync"
|
2020-10-16 02:08:45 +08:00
|
|
|
"time"
|
2020-10-21 17:16:21 +08:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
concurrentWriterLimit = runtime.NumCPU()
|
2018-05-29 03:30:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContinuousDirtyPages struct {
|
2020-10-17 03:54:23 +08:00
|
|
|
intervals *ContinuousIntervals
|
|
|
|
f *File
|
|
|
|
writeWaitGroup sync.WaitGroup
|
|
|
|
chunkSaveErrChan chan error
|
|
|
|
chunkSaveErrChanClosed bool
|
2020-10-21 17:16:21 +08:00
|
|
|
lastErr error
|
2020-10-17 03:54:23 +08:00
|
|
|
lock sync.Mutex
|
|
|
|
collection string
|
|
|
|
replication string
|
2018-05-29 03:30:17 +08:00
|
|
|
}
|
|
|
|
|
2018-05-29 16:21:21 +08:00
|
|
|
func newDirtyPages(file *File) *ContinuousDirtyPages {
|
2020-10-21 17:16:21 +08:00
|
|
|
dirtyPages := &ContinuousDirtyPages{
|
2020-10-15 14:28:03 +08:00
|
|
|
intervals: &ContinuousIntervals{},
|
|
|
|
f: file,
|
2020-10-21 17:16:21 +08:00
|
|
|
chunkSaveErrChan: make(chan error, concurrentWriterLimit),
|
2018-05-29 03:30:17 +08:00
|
|
|
}
|
2020-10-21 17:16:21 +08:00
|
|
|
go func() {
|
|
|
|
for t := range dirtyPages.chunkSaveErrChan {
|
|
|
|
if t != nil {
|
|
|
|
dirtyPages.lastErr = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return dirtyPages
|
2018-05-29 16:21:21 +08:00
|
|
|
}
|
2018-05-29 03:30:17 +08:00
|
|
|
|
2020-10-15 14:28:03 +08:00
|
|
|
func (pages *ContinuousDirtyPages) AddPage(offset int64, data []byte) {
|
2018-05-31 13:02:21 +08:00
|
|
|
|
2020-08-31 11:12:04 +08:00
|
|
|
glog.V(4).Infof("%s AddPage [%d,%d) of %d bytes", pages.f.fullpath(), offset, offset+int64(len(data)), pages.f.entry.Attributes.FileSize)
|
2018-05-29 03:30:17 +08:00
|
|
|
|
2019-01-01 18:14:40 +08:00
|
|
|
if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
|
2018-05-31 13:09:24 +08:00
|
|
|
// this is more than what buffer can hold.
|
2020-10-15 14:28:03 +08:00
|
|
|
pages.flushAndSave(offset, data)
|
2018-05-29 03:30:17 +08:00
|
|
|
}
|
|
|
|
|
2020-01-27 05:01:11 +08:00
|
|
|
pages.intervals.AddInterval(data, offset)
|
2018-05-29 03:30:17 +08:00
|
|
|
|
2020-01-23 15:00:04 +08:00
|
|
|
if pages.intervals.TotalSize() > pages.f.wfs.option.ChunkSizeLimit {
|
2020-10-15 14:28:03 +08:00
|
|
|
pages.saveExistingLargestPageToStorage()
|
2018-09-08 04:11:43 +08:00
|
|
|
}
|
2018-09-10 17:21:57 +08:00
|
|
|
|
2018-05-29 03:30:17 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-15 14:28:03 +08:00
|
|
|
func (pages *ContinuousDirtyPages) flushAndSave(offset int64, data []byte) {
|
2018-09-10 17:21:57 +08:00
|
|
|
|
|
|
|
// flush existing
|
2020-10-15 14:28:03 +08:00
|
|
|
pages.saveExistingPagesToStorage()
|
2018-09-10 17:21:57 +08:00
|
|
|
|
|
|
|
// flush the new page
|
2020-10-15 14:28:03 +08:00
|
|
|
pages.saveToStorage(bytes.NewReader(data), offset, int64(len(data)))
|
2018-09-10 17:21:57 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-15 14:28:03 +08:00
|
|
|
func (pages *ContinuousDirtyPages) saveExistingPagesToStorage() {
|
|
|
|
for pages.saveExistingLargestPageToStorage() {
|
2018-05-29 03:30:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 14:28:03 +08:00
|
|
|
func (pages *ContinuousDirtyPages) saveExistingLargestPageToStorage() (hasSavedData bool) {
|
2018-09-08 04:11:43 +08:00
|
|
|
|
2020-01-23 15:00:04 +08:00
|
|
|
maxList := pages.intervals.RemoveLargestIntervalLinkedList()
|
|
|
|
if maxList == nil {
|
2020-10-15 14:28:03 +08:00
|
|
|
return false
|
2018-09-08 04:11:43 +08:00
|
|
|
}
|
|
|
|
|
2020-08-16 00:32:47 +08:00
|
|
|
fileSize := int64(pages.f.entry.Attributes.FileSize)
|
2020-10-15 14:28:03 +08:00
|
|
|
|
|
|
|
chunkSize := min(maxList.Size(), fileSize-maxList.Offset())
|
|
|
|
if chunkSize == 0 {
|
|
|
|
return false
|
2020-01-23 15:00:04 +08:00
|
|
|
}
|
2020-01-21 12:21:01 +08:00
|
|
|
|
2020-10-15 14:28:03 +08:00
|
|
|
pages.saveToStorage(maxList.ToReader(), maxList.Offset(), chunkSize)
|
|
|
|
|
|
|
|
return true
|
2018-05-31 13:02:21 +08:00
|
|
|
}
|
|
|
|
|
2020-10-15 14:28:03 +08:00
|
|
|
func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) {
|
2018-05-29 03:30:17 +08:00
|
|
|
|
2020-10-17 03:54:23 +08:00
|
|
|
if pages.chunkSaveErrChanClosed {
|
|
|
|
pages.chunkSaveErrChan = make(chan error, 8)
|
|
|
|
pages.chunkSaveErrChanClosed = false
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:08:45 +08:00
|
|
|
mtime := time.Now().UnixNano()
|
2020-10-15 14:28:03 +08:00
|
|
|
pages.writeWaitGroup.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer pages.writeWaitGroup.Done()
|
2020-02-25 14:28:45 +08:00
|
|
|
|
2020-10-15 14:28:03 +08:00
|
|
|
dir, _ := pages.f.fullpath().DirAndName()
|
2018-05-29 03:30:17 +08:00
|
|
|
|
2020-10-15 14:28:03 +08:00
|
|
|
reader = io.LimitReader(reader, size)
|
|
|
|
chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(dir)(reader, pages.f.Name, offset)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), offset, offset+size, err)
|
|
|
|
pages.chunkSaveErrChan <- err
|
|
|
|
return
|
|
|
|
}
|
2020-10-16 02:08:45 +08:00
|
|
|
chunk.Mtime = mtime
|
2020-10-15 14:28:03 +08:00
|
|
|
pages.collection, pages.replication = collection, replication
|
|
|
|
pages.f.addChunks([]*filer_pb.FileChunk{chunk})
|
2020-10-21 17:17:40 +08:00
|
|
|
glog.V(3).Infof("%s saveToStorage [%d,%d)", pages.f.fullpath(), offset, offset+size)
|
2020-10-15 14:28:03 +08:00
|
|
|
}()
|
2018-05-29 03:30:17 +08:00
|
|
|
}
|
2018-05-29 16:21:21 +08:00
|
|
|
|
|
|
|
func max(x, y int64) int64 {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
2020-01-23 05:42:03 +08:00
|
|
|
func min(x, y int64) int64 {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2020-08-18 02:12:10 +08:00
|
|
|
func (pages *ContinuousDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
|
|
|
|
return pages.intervals.ReadDataAt(data, startOffset)
|
2020-01-23 05:42:03 +08:00
|
|
|
}
|