2018-05-28 19:30:17 +00:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-05-09 22:15:18 +00:00
|
|
|
"fmt"
|
2020-10-15 06:28:03 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
2020-10-15 18:08:45 +00:00
|
|
|
"time"
|
2020-10-21 09:16:21 +00:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-05-28 19:30:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContinuousDirtyPages struct {
|
2021-01-28 23:23:46 +00:00
|
|
|
intervals *ContinuousIntervals
|
|
|
|
f *File
|
2021-05-09 22:22:38 +00:00
|
|
|
writeOnly bool
|
2021-01-28 23:23:46 +00:00
|
|
|
writeWaitGroup sync.WaitGroup
|
|
|
|
chunkAddLock sync.Mutex
|
|
|
|
lastErr error
|
|
|
|
collection string
|
|
|
|
replication string
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 22:33:01 +00:00
|
|
|
func newContinuousDirtyPages(file *File, writeOnly bool) *ContinuousDirtyPages {
|
2020-10-21 09:16:21 +00:00
|
|
|
dirtyPages := &ContinuousDirtyPages{
|
2021-01-28 23:23:46 +00:00
|
|
|
intervals: &ContinuousIntervals{},
|
|
|
|
f: file,
|
2021-05-09 22:22:38 +00:00
|
|
|
writeOnly: writeOnly,
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
2020-10-21 09:16:21 +00:00
|
|
|
return dirtyPages
|
2018-05-29 08:21:21 +00:00
|
|
|
}
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
func (pages *ContinuousDirtyPages) AddPage(offset int64, data []byte) {
|
2018-05-31 05:02:21 +00:00
|
|
|
|
2021-03-10 07:08:38 +00:00
|
|
|
glog.V(4).Infof("%s AddPage [%d,%d)", pages.f.fullpath(), offset, offset+int64(len(data)))
|
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-10-15 06:28:03 +00:00
|
|
|
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-10-22 02:29:51 +00:00
|
|
|
if pages.intervals.TotalSize() >= pages.f.wfs.option.ChunkSizeLimit {
|
2020-10-15 06:28:03 +00:00
|
|
|
pages.saveExistingLargestPageToStorage()
|
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-10-15 06:28:03 +00:00
|
|
|
func (pages *ContinuousDirtyPages) flushAndSave(offset int64, data []byte) {
|
2018-09-10 09:21:57 +00:00
|
|
|
|
|
|
|
// flush existing
|
2020-10-15 06:28:03 +00:00
|
|
|
pages.saveExistingPagesToStorage()
|
2018-09-10 09:21:57 +00:00
|
|
|
|
|
|
|
// flush the new page
|
2020-10-15 06:28:03 +00:00
|
|
|
pages.saveToStorage(bytes.NewReader(data), offset, int64(len(data)))
|
2018-09-10 09:21:57 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 22:15:18 +00:00
|
|
|
func (pages *ContinuousDirtyPages) FlushData() error {
|
|
|
|
|
|
|
|
pages.saveExistingPagesToStorage()
|
|
|
|
pages.writeWaitGroup.Wait()
|
|
|
|
if pages.lastErr != nil {
|
|
|
|
return fmt.Errorf("flush data: %v", pages.lastErr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
func (pages *ContinuousDirtyPages) saveExistingPagesToStorage() {
|
|
|
|
for pages.saveExistingLargestPageToStorage() {
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
func (pages *ContinuousDirtyPages) saveExistingLargestPageToStorage() (hasSavedData bool) {
|
2018-09-07 20:11:43 +00:00
|
|
|
|
2020-01-23 07:00:04 +00:00
|
|
|
maxList := pages.intervals.RemoveLargestIntervalLinkedList()
|
|
|
|
if maxList == nil {
|
2020-10-15 06:28:03 +00:00
|
|
|
return false
|
2018-09-07 20:11:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 07:08:38 +00:00
|
|
|
entry := pages.f.getEntry()
|
|
|
|
if entry == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:59:26 +00:00
|
|
|
fileSize := int64(entry.Attributes.FileSize)
|
2020-10-15 06:28:03 +00:00
|
|
|
|
|
|
|
chunkSize := min(maxList.Size(), fileSize-maxList.Offset())
|
|
|
|
if chunkSize == 0 {
|
|
|
|
return false
|
2020-01-23 07:00:04 +00:00
|
|
|
}
|
2020-01-21 04:21:01 +00:00
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
pages.saveToStorage(maxList.ToReader(), maxList.Offset(), chunkSize)
|
|
|
|
|
|
|
|
return true
|
2018-05-31 05:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
func (pages *ContinuousDirtyPages) saveToStorage(reader io.Reader, offset int64, size int64) {
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2020-10-15 18:08:45 +00:00
|
|
|
mtime := time.Now().UnixNano()
|
2020-10-15 06:28:03 +00:00
|
|
|
pages.writeWaitGroup.Add(1)
|
2020-10-31 04:22:20 +00:00
|
|
|
writer := func() {
|
2020-10-15 06:28:03 +00:00
|
|
|
defer pages.writeWaitGroup.Done()
|
2020-02-25 06:28:45 +00:00
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
reader = io.LimitReader(reader, size)
|
2021-05-09 22:22:38 +00:00
|
|
|
chunk, collection, replication, err := pages.f.wfs.saveDataAsChunk(pages.f.fullpath(), pages.writeOnly)(reader, pages.f.Name, offset)
|
2020-10-15 06:28:03 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("%s saveToStorage [%d,%d): %v", pages.f.fullpath(), offset, offset+size, err)
|
2021-01-28 12:46:37 +00:00
|
|
|
pages.lastErr = err
|
2020-10-15 06:28:03 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-15 18:08:45 +00:00
|
|
|
chunk.Mtime = mtime
|
2020-10-15 06:28:03 +00:00
|
|
|
pages.collection, pages.replication = collection, replication
|
2020-11-03 20:20:41 +00:00
|
|
|
pages.chunkAddLock.Lock()
|
|
|
|
defer pages.chunkAddLock.Unlock()
|
2020-10-15 06:28:03 +00:00
|
|
|
pages.f.addChunks([]*filer_pb.FileChunk{chunk})
|
2020-10-21 09:17:40 +00:00
|
|
|
glog.V(3).Infof("%s saveToStorage [%d,%d)", pages.f.fullpath(), offset, offset+size)
|
2020-10-31 04:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pages.f.wfs.concurrentWriters != nil {
|
|
|
|
pages.f.wfs.concurrentWriters.Execute(writer)
|
|
|
|
} else {
|
|
|
|
go writer()
|
|
|
|
}
|
2018-05-28 19:30:17 +00:00
|
|
|
}
|
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) {
|
|
|
|
return pages.intervals.ReadDataAt(data, startOffset)
|
2020-01-22 21:42:03 +00:00
|
|
|
}
|
2021-05-09 22:28:54 +00:00
|
|
|
|
|
|
|
func (pages *ContinuousDirtyPages) GetStorageOptions() (collection, replication string) {
|
|
|
|
return pages.collection, pages.replication
|
|
|
|
}
|
2021-05-10 00:22:30 +00:00
|
|
|
|
|
|
|
func (pages *ContinuousDirtyPages) SetWriteOnly(writeOnly bool) {
|
|
|
|
if pages.writeOnly {
|
|
|
|
pages.writeOnly = writeOnly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pages *ContinuousDirtyPages) GetWriteOnly() (writeOnly bool) {
|
|
|
|
return pages.writeOnly
|
|
|
|
}
|