SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.
Go blob-storagecloud-drivedistributed-file-systemdistributed-storageerasure-codingfusehadoop-hdfshdfskuberneteslancelancedbobject-storageposixs3s3-storageseaweedfstiered-file-systemvector-search
write to disk during random writes, limiting total disk spaces used
chrisluf2f68f675efc
diff --git a/weed/mount/dirty_pages_chunked.go b/weed/mount/dirty_pages_chunked.goindex a76c6dd61a..e0d7640707 100644--- a/weed/mount/dirty_pages_chunked.go+++ b/weed/mount/dirty_pages_chunked.go@@ -38,11 +38,11 @@ func newMemoryChunkPages(fh *FileHandle, chunkSize int64) *ChunkedDirtyPages {return dirtyPages}-func (pages *ChunkedDirtyPages) AddPage(offset int64, data []byte) {+func (pages *ChunkedDirtyPages) AddPage(offset int64, data []byte, isSequential bool) {pages.hasWrites = trueglog.V(4).Infof("%v memory AddPage [%d, %d)", pages.fh.fh, offset, offset+int64(len(data)))- pages.uploadPipeline.SaveDataAt(data, offset)+ pages.uploadPipeline.SaveDataAt(data, offset, isSequential)return}diff --git a/weed/mount/page_writer.go b/weed/mount/page_writer.goindex ffcaa63986..016c4841ac 100644--- a/weed/mount/page_writer.go+++ b/weed/mount/page_writer.go@@ -6,10 +6,11 @@ import ()type PageWriter struct {- fh *FileHandle- collection string- replication string- chunkSize int64+ fh *FileHandle+ collection string+ replication string+ chunkSize int64+ writerPattern *WriterPatternrandomWriter page_writer.DirtyPages}@@ -20,28 +21,29 @@ var (func newPageWriter(fh *FileHandle, chunkSize int64) *PageWriter {pw := &PageWriter{- fh: fh,- chunkSize: chunkSize,- randomWriter: newMemoryChunkPages(fh, chunkSize),+ fh: fh,+ chunkSize: chunkSize,+ writerPattern: NewWriterPattern(chunkSize),+ randomWriter: newMemoryChunkPages(fh, chunkSize),}return pw}-func (pw *PageWriter) AddPage(offset int64, data []byte) {+func (pw *PageWriter) AddPage(offset int64, data []byte, isSequentail bool) {glog.V(4).Infof("%v AddPage [%d, %d)", pw.fh.fh, offset, offset+int64(len(data)))chunkIndex := offset / pw.chunkSizefor i := chunkIndex; len(data) > 0; i++ {writeSize := min(int64(len(data)), (i+1)*pw.chunkSize-offset)- pw.addToOneChunk(i, offset, data[:writeSize])+ pw.addToOneChunk(i, offset, data[:writeSize], isSequentail)offset += writeSizedata = data[writeSize:]}}-func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte) {- pw.randomWriter.AddPage(offset, data)+func (pw *PageWriter) addToOneChunk(chunkIndex, offset int64, data []byte, isSequential bool) {+ pw.randomWriter.AddPage(offset, data, isSequential)}func (pw *PageWriter) FlushData() error {diff --git a/weed/mount/page_writer/dirty_pages.go b/weed/mount/page_writer/dirty_pages.goindex 25b747fad9..c16cee47af 100644--- a/weed/mount/page_writer/dirty_pages.go+++ b/weed/mount/page_writer/dirty_pages.go@@ -1,7 +1,7 @@package page_writertype DirtyPages interface {- AddPage(offset int64, data []byte)+ AddPage(offset int64, data []byte, isSequential bool)FlushData() errorReadDirtyDataAt(data []byte, startOffset int64) (maxStop int64)GetStorageOptions() (collection, replication string)diff --git a/weed/mount/page_writer/page_chunk_swapfile.go b/weed/mount/page_writer/page_chunk_swapfile.goindex 4865576291..c70f8c5a18 100644--- a/weed/mount/page_writer/page_chunk_swapfile.go+++ b/weed/mount/page_writer/page_chunk_swapfile.go@@ -18,6 +18,7 @@ type SwapFile struct {file *os.FilelogicToActualChunkIndex map[LogicChunkIndex]ActualChunkIndexchunkSize int64+ freeActualChunkList []ActualChunkIndex}type SwapFileChunk struct {@@ -53,7 +54,12 @@ func (sf *SwapFile) NewTempFileChunk(logicChunkIndex LogicChunkIndex) (tc *SwapF}actualChunkIndex, found := sf.logicToActualChunkIndex[logicChunkIndex]if !found {- actualChunkIndex = ActualChunkIndex(len(sf.logicToActualChunkIndex))+ if len(sf.freeActualChunkList) > 0 {+ actualChunkIndex = sf.freeActualChunkList[0]+ sf.freeActualChunkList = sf.freeActualChunkList[1:]+ } else {+ actualChunkIndex = ActualChunkIndex(len(sf.logicToActualChunkIndex))+ }sf.logicToActualChunkIndex[logicChunkIndex] = actualChunkIndex}@@ -66,6 +72,8 @@ func (sf *SwapFile) NewTempFileChunk(logicChunkIndex LogicChunkIndex) (tc *SwapF}func (sc *SwapFileChunk) FreeResource() {+ sc.swapfile.freeActualChunkList = append(sc.swapfile.freeActualChunkList, sc.actualChunkIndex)+ delete(sc.swapfile.logicToActualChunkIndex, sc.logicChunkIndex)}func (sc *SwapFileChunk) WriteDataAt(src []byte, offset int64) (n int) {diff --git a/weed/mount/page_writer/upload_pipeline.go b/weed/mount/page_writer/upload_pipeline.goindex 190076a2b6..2286cdf007 100644--- a/weed/mount/page_writer/upload_pipeline.go+++ b/weed/mount/page_writer/upload_pipeline.go@@ -55,7 +55,7 @@ func NewUploadPipeline(writers *util.LimitedConcurrentExecutor, chunkSize int64,}}-func (up *UploadPipeline) SaveDataAt(p []byte, off int64) (n int) {+func (up *UploadPipeline) SaveDataAt(p []byte, off int64, isSequential bool) (n int) {up.writableChunksLock.Lock()defer up.writableChunksLock.Unlock()@@ -63,13 +63,8 @@ func (up *UploadPipeline) SaveDataAt(p []byte, off int64) (n int) {pageChunk, found := up.writableChunks[logicChunkIndex]if !found {- if atomic.LoadInt64(&memChunkCounter) > 4*int64(up.bufferChunkLimit) {- // if total number of chunks is over 4 times of per file buffer count limit- pageChunk = up.swapFile.NewTempFileChunk(logicChunkIndex)- } else if len(up.writableChunks) < up.bufferChunkLimit {- // if current file chunks is still under the per file buffer count limit- pageChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)- } else {+ if len(up.writableChunks) > up.bufferChunkLimit {+ // if current file chunks is over the per file buffer count limitfullestChunkIndex, fullness := LogicChunkIndex(-1), int64(0)for lci, mc := range up.writableChunks {chunkFullness := mc.WrittenSize()@@ -81,7 +76,13 @@ func (up *UploadPipeline) SaveDataAt(p []byte, off int64) (n int) {up.moveToSealed(up.writableChunks[fullestChunkIndex], fullestChunkIndex)delete(up.writableChunks, fullestChunkIndex)// fmt.Printf("flush chunk %d with %d bytes written\n", logicChunkIndex, fullness)+ }+ if isSequential &&+ len(up.writableChunks) < up.bufferChunkLimit &&+ atomic.LoadInt64(&memChunkCounter) < 4*int64(up.bufferChunkLimit) {pageChunk = NewMemChunk(logicChunkIndex, up.ChunkSize)+ } else {+ pageChunk = up.swapFile.NewTempFileChunk(logicChunkIndex)}up.writableChunks[logicChunkIndex] = pageChunk}diff --git a/weed/mount/page_writer/upload_pipeline_test.go b/weed/mount/page_writer/upload_pipeline_test.goindex 63b60faaf3..f130c97c11 100644--- a/weed/mount/page_writer/upload_pipeline_test.go+++ b/weed/mount/page_writer/upload_pipeline_test.go@@ -31,7 +31,7 @@ func writeRange(uploadPipeline *UploadPipeline, startOff, stopOff int64) {p := make([]byte, 4)for i := startOff / 4; i < stopOff/4; i += 4 {util.Uint32toBytes(p, uint32(i))- uploadPipeline.SaveDataAt(p, i)+ uploadPipeline.SaveDataAt(p, i, false)}}diff --git a/weed/mount/page_writer_pattern.go b/weed/mount/page_writer_pattern.gonew file mode 100644index 0000000000..665056b363--- /dev/null+++ b/weed/mount/page_writer_pattern.go@@ -0,0 +1,44 @@+package mount++type WriterPattern struct {+ isStreaming bool+ lastWriteOffset int64+ chunkSize int64+}++// For streaming write: only cache the first chunk+// For random write: fall back to temp file approach+// writes can only change from streaming mode to non-streaming mode++func NewWriterPattern(chunkSize int64) *WriterPattern {+ return &WriterPattern{+ isStreaming: true,+ lastWriteOffset: -1,+ chunkSize: chunkSize,+ }+}++func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {+ if rp.lastWriteOffset > offset {+ rp.isStreaming = false+ }+ if rp.lastWriteOffset == -1 {+ if offset != 0 {+ rp.isStreaming = false+ }+ }+ rp.lastWriteOffset = offset+}++func (rp *WriterPattern) IsStreamingMode() bool {+ return rp.isStreaming+}++func (rp *WriterPattern) IsRandomMode() bool {+ return !rp.isStreaming+}++func (rp *WriterPattern) Reset() {+ rp.isStreaming = true+ rp.lastWriteOffset = -1+}diff --git a/weed/mount/weedfs_file_write.go b/weed/mount/weedfs_file_write.goindex f71e27335e..d146807527 100644--- a/weed/mount/weedfs_file_write.go+++ b/weed/mount/weedfs_file_write.go@@ -43,6 +43,8 @@ func (wfs *WFS) Write(cancel <-chan struct{}, in *fuse.WriteIn, data []byte) (wrreturn 0, fuse.ENOENT}+ fh.dirtyPages.writerPattern.MonitorWriteAt(int64(in.Offset), int(in.Size))+fh.Lock()defer fh.Unlock()@@ -56,7 +58,7 @@ func (wfs *WFS) Write(cancel <-chan struct{}, in *fuse.WriteIn, data []byte) (wrentry.Attributes.FileSize = uint64(max(offset+int64(len(data)), int64(entry.Attributes.FileSize)))// glog.V(4).Infof("%v write [%d,%d) %d", fh.f.fullpath(), req.Offset, req.Offset+int64(len(req.Data)), len(req.Data))- fh.dirtyPages.AddPage(offset, data)+ fh.dirtyPages.AddPage(offset, data, fh.dirtyPages.writerPattern.IsStreamingMode())written = uint32(len(data))