2018-05-28 19:30:17 +00:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-01-23 07:00:04 +00:00
|
|
|
"io"
|
2019-02-15 17:59:22 +00:00
|
|
|
"sync"
|
2018-07-22 00:39:10 +00:00
|
|
|
"time"
|
2018-05-28 19:30:17 +00:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-07-22 00:39:10 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-05-28 19:30:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContinuousDirtyPages struct {
|
2020-02-25 06:28:45 +00:00
|
|
|
intervals *ContinuousIntervals
|
|
|
|
f *File
|
|
|
|
lock sync.Mutex
|
|
|
|
collection string
|
|
|
|
replication string
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 08:21:21 +00:00
|
|
|
func newDirtyPages(file *File) *ContinuousDirtyPages {
|
|
|
|
return &ContinuousDirtyPages{
|
2020-01-23 07:00:04 +00:00
|
|
|
intervals: &ContinuousIntervals{},
|
|
|
|
f: file,
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
2018-05-29 08:21:21 +00:00
|
|
|
}
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2018-12-28 11:27:48 +00:00
|
|
|
func (pages *ContinuousDirtyPages) releaseResource() {
|
2018-11-15 06:48:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 10:14:40 +00:00
|
|
|
var counter = int32(0)
|
|
|
|
|
2020-02-26 06:23:59 +00:00
|
|
|
func (pages *ContinuousDirtyPages) AddPage(offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
|
2018-05-31 05:02:21 +00:00
|
|
|
|
2018-09-07 20:11:43 +00:00
|
|
|
pages.lock.Lock()
|
|
|
|
defer pages.lock.Unlock()
|
|
|
|
|
2020-08-16 02:55:28 +00:00
|
|
|
glog.V(5).Infof("%s AddPage [%d,%d) of %d bytes", pages.f.fullpath(), offset, offset+int64(len(data)), pages.f.entry.Attributes.FileSize)
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2019-01-01 10:14:40 +00:00
|
|
|
if len(data) > int(pages.f.wfs.option.ChunkSizeLimit) {
|
2018-05-31 05:09:24 +00:00
|
|
|
// this is more than what buffer can hold.
|
2020-02-26 06:23:59 +00:00
|
|
|
return pages.flushAndSave(offset, data)
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 21:01:11 +00:00
|
|
|
pages.intervals.AddInterval(data, offset)
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2020-01-23 07:00:04 +00:00
|
|
|
var chunk *filer_pb.FileChunk
|
|
|
|
var hasSavedData bool
|
|
|
|
|
|
|
|
if pages.intervals.TotalSize() > pages.f.wfs.option.ChunkSizeLimit {
|
2020-02-26 05:50:12 +00:00
|
|
|
chunk, hasSavedData, err = pages.saveExistingLargestPageToStorage()
|
2020-01-23 07:00:04 +00:00
|
|
|
if hasSavedData {
|
|
|
|
chunks = append(chunks, chunk)
|
2018-09-10 09:39:41 +00:00
|
|
|
}
|
2018-09-07 20:11:43 +00:00
|
|
|
}
|
2018-09-10 09:21:57 +00:00
|
|
|
|
2018-05-28 19:30:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 06:23:59 +00:00
|
|
|
func (pages *ContinuousDirtyPages) flushAndSave(offset int64, data []byte) (chunks []*filer_pb.FileChunk, err error) {
|
2018-09-10 09:21:57 +00:00
|
|
|
|
|
|
|
var chunk *filer_pb.FileChunk
|
2020-01-23 07:00:04 +00:00
|
|
|
var newChunks []*filer_pb.FileChunk
|
2018-09-10 09:21:57 +00:00
|
|
|
|
|
|
|
// flush existing
|
2020-02-26 05:50:12 +00:00
|
|
|
if newChunks, err = pages.saveExistingPagesToStorage(); err == nil {
|
2020-01-23 07:00:04 +00:00
|
|
|
if newChunks != nil {
|
|
|
|
chunks = append(chunks, newChunks...)
|
2018-09-10 09:21:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush the new page
|
2020-02-26 05:50:12 +00:00
|
|
|
if chunk, err = pages.saveToStorage(bytes.NewReader(data), offset, int64(len(data))); err == nil {
|
2018-09-10 09:21:57 +00:00
|
|
|
if chunk != nil {
|
2020-03-26 07:08:14 +00:00
|
|
|
glog.V(4).Infof("%s/%s flush big request [%d,%d) to %s", pages.f.dir.FullPath(), pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.FileId)
|
2018-09-10 09:21:57 +00:00
|
|
|
chunks = append(chunks, chunk)
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-26 07:08:14 +00:00
|
|
|
glog.V(0).Infof("%s/%s failed to flush2 [%d,%d): %v", pages.f.dir.FullPath(), pages.f.Name, chunk.Offset, chunk.Offset+int64(chunk.Size), err)
|
2018-09-10 09:21:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (pages *ContinuousDirtyPages) FlushToStorage() (chunks []*filer_pb.FileChunk, err error) {
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2018-09-07 20:11:43 +00:00
|
|
|
pages.lock.Lock()
|
|
|
|
defer pages.lock.Unlock()
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
return pages.saveExistingPagesToStorage()
|
2020-01-23 07:00:04 +00:00
|
|
|
}
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (pages *ContinuousDirtyPages) saveExistingPagesToStorage() (chunks []*filer_pb.FileChunk, err error) {
|
2020-01-23 07:00:04 +00:00
|
|
|
|
|
|
|
var hasSavedData bool
|
|
|
|
var chunk *filer_pb.FileChunk
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
chunk, hasSavedData, err = pages.saveExistingLargestPageToStorage()
|
2020-01-23 07:00:04 +00:00
|
|
|
if !hasSavedData {
|
|
|
|
return chunks, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
chunks = append(chunks, chunk)
|
|
|
|
} else {
|
|
|
|
return
|
2018-05-28 21:32:16 +00:00
|
|
|
}
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
2020-01-23 07:00:04 +00:00
|
|
|
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (pages *ContinuousDirtyPages) saveExistingLargestPageToStorage() (chunk *filer_pb.FileChunk, hasSavedData bool, err error) {
|
2018-09-07 20:11:43 +00:00
|
|
|
|
2020-01-23 07:00:04 +00:00
|
|
|
maxList := pages.intervals.RemoveLargestIntervalLinkedList()
|
|
|
|
if maxList == nil {
|
|
|
|
return nil, false, nil
|
2018-09-07 20:11:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 16:32:47 +00:00
|
|
|
fileSize := int64(pages.f.entry.Attributes.FileSize)
|
2020-04-09 06:12:37 +00:00
|
|
|
for {
|
2020-08-15 16:32:47 +00:00
|
|
|
chunkSize := min(maxList.Size(), fileSize-maxList.Offset())
|
|
|
|
chunk, err = pages.saveToStorage(maxList.ToReader(), maxList.Offset(), chunkSize)
|
2020-04-09 06:12:37 +00:00
|
|
|
if err == nil {
|
|
|
|
hasSavedData = true
|
2020-08-16 02:55:28 +00:00
|
|
|
glog.V(4).Infof("saveToStorage %s %s [%d,%d) of %d bytes", pages.f.fullpath(), chunk.GetFileIdString(), maxList.Offset(), maxList.Offset()+chunkSize, fileSize)
|
2020-04-10 04:43:26 +00:00
|
|
|
return
|
2020-04-09 06:12:37 +00:00
|
|
|
} else {
|
2020-08-15 16:32:47 +00:00
|
|
|
glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), maxList.Offset(), maxList.Offset()+chunkSize, err)
|
2020-04-10 04:43:26 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2020-04-09 06:12:37 +00:00
|
|
|
}
|
2020-01-23 07:00:04 +00:00
|
|
|
}
|
2020-01-21 04:21:01 +00:00
|
|
|
|
2018-05-31 05:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) (*filer_pb.FileChunk, error) {
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2020-02-25 06:28:45 +00:00
|
|
|
dir, _ := pages.f.fullpath().DirAndName()
|
|
|
|
|
2020-08-15 16:32:47 +00:00
|
|
|
reader = io.LimitReader(reader, size)
|
2020-07-20 00:59:43 +00:00
|
|
|
chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(dir)(reader, pages.f.Name, offset)
|
2018-05-28 19:30:17 +00:00
|
|
|
if err != nil {
|
2020-07-20 00:59:43 +00:00
|
|
|
return nil, err
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
2020-07-20 00:59:43 +00:00
|
|
|
pages.collection, pages.replication = collection, replication
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2020-07-20 00:59:43 +00:00
|
|
|
return chunk, nil
|
2018-05-28 19:30:17 +00:00
|
|
|
|
|
|
|
}
|
2018-05-29 08:21:21 +00:00
|
|
|
|
2020-08-15 16:32:47 +00:00
|
|
|
func maxUint64(x, y uint64) uint64 {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2018-05-29 08:21:21 +00:00
|
|
|
func max(x, y int64) int64 {
|
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
2020-01-22 21:42:03 +00:00
|
|
|
func min(x, y int64) int64 {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2020-08-17 18:12:10 +00:00
|
|
|
func (pages *ContinuousDirtyPages) ReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64) {
|
2020-01-22 23:38:25 +00:00
|
|
|
|
|
|
|
pages.lock.Lock()
|
|
|
|
defer pages.lock.Unlock()
|
|
|
|
|
2020-08-17 18:12:10 +00:00
|
|
|
return pages.intervals.ReadDataAt(data, startOffset)
|
2020-01-22 21:42:03 +00:00
|
|
|
|
|
|
|
}
|