From 7638cf2f660eece322fec7ddeee9275d92ae0dfe Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 25 Nov 2026 13:07:45 -0800 Subject: [PATCH] mount: keep the data past a sparse file's first hole when merging chunks (#12978) * mount: merge each run of a sparse file's data, not just the run before its first hole The chunk stream reader ends at a hole, so the merge stopped there and replaced every chunk with ones covering only the data before the first hole. Read and save each contiguous run of visible data on its own, and leave the holes between runs unwritten. Co-Authored-By: Claude Opus 5.5 * filer: skip the small-chunk merge for a sparse file The merge reads the chunks through the stream reader to its end, which stops at a hole, and keeps only the chunks before its start offset, so an append to a sparse file lost the data past the first hole after it. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- weed/mount/merge_quiet_pages_test.go | 49 +++++++++++++++++++ weed/mount/weedfs_file_sync.go | 24 ++++----- .../filer_server_handlers_write_merge.go | 14 ++++++ .../filer_server_handlers_write_merge_test.go | 30 ++++++++++++ 4 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 weed/server/filer_server_handlers_write_merge_test.go diff --git a/weed/mount/merge_quiet_pages_test.go b/weed/mount/merge_quiet_pages_test.go index 8d6bb1ca5b..776617206b 100644 --- a/weed/mount/merge_quiet_pages_test.go +++ b/weed/mount/merge_quiet_pages_test.go @@ -544,3 +544,52 @@ func TestFlushMergeRacesWithWritesAndReads(t *testing.T) { t.Fatal("the filer's entry does not hold what was written") } } + +// A merge of a sparse file must keep the data past each hole, and leave the +// holes unwritten. +func TestFlushMergeKeepsDataPastAHole(t *testing.T) { + var chunks []*filer_pb.FileChunk + vol := &proxyVolume{stored: map[string][]byte{}} + // Overlapping 256-byte chunks over [0,300) and [512,856), a hole between. + for i, offset := range []int64{0, 22, 44, 512, 534, 556, 578, 600} { + c := &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: uint64(i + 1), Cookie: 1}, Offset: offset, Size: 256, ModifiedTsNs: int64(i + 1)} + b := make([]byte, 256) + x := uint32(i+1) * 2654435761 + for j := range b { + x = x*1103515245 + 12345 + b[j] = byte(x >> 16) + } + vol.stored[c.GetFileIdString()] = b + chunks = append(chunks, c) + } + const fileSize = 856 + want := make([]byte, fileSize) + for _, c := range chunks { + copy(want[c.Offset:], vol.stored[c.GetFileIdString()]) + } + base := &filer_pb.Entry{Name: "file", Attributes: &filer_pb.FuseAttributes{ + Inode: 42, Uid: 2000, Gid: 20, FileMode: 0644, FileSize: fileSize, Mtime: 10, + }, Chunks: chunks, CommitTsNs: 100} + + w, fh, _ := openProxyHandle(t, vol, base, vol) + lock := w.fhLockTable.AcquireLock("test flush", fh.fh, util.ExclusiveLock) + fh.dirtyMetadata = true + w.fhLockTable.ReleaseLock(fh.fh, lock) + if err := w.flushMetadataToFiler(context.Background(), fh, "/dir", "file", 0, 0, nil, &contentPublication{writes: fh.pageWrites.Load()}); err != nil { + t.Fatalf("flush: %v", err) + } + + entry, got := vol.committed(t) + if _, merges := vol.counts(); merges != 1 { + t.Fatalf("the flush did not merge the bloated file: %v", entry.Chunks) + } + if !bytes.Equal(got, want) { + t.Fatalf("the merge changed the file's content: [0,300) kept %v, hole zero %v, [512,856) kept %v", + bytes.Equal(got[:300], want[:300]), bytes.Equal(got[300:512], want[300:512]), bytes.Equal(got[512:], want[512:])) + } + for _, c := range entry.Chunks { + if c.Offset < 512 && c.Offset+int64(c.Size) > 300 { + t.Errorf("merged chunk [%d,%d) writes into the hole [300,512)", c.Offset, c.Offset+int64(c.Size)) + } + } +} diff --git a/weed/mount/weedfs_file_sync.go b/weed/mount/weedfs_file_sync.go index c6a3e31411..61903cfa03 100644 --- a/weed/mount/weedfs_file_sync.go +++ b/weed/mount/weedfs_file_sync.go @@ -5,7 +5,6 @@ import ( "context" "errors" "fmt" - "io" "math" "sort" "syscall" @@ -850,13 +849,20 @@ func (wfs *WFS) maybeMergeChunks(fileFullPath util.FullPath, compactedChunks []* chunkSize = int64(fileSize) } + // The reader ends at a hole, so read each run of data on its own and + // leave the holes between runs unwritten. var newChunks []*filer_pb.FileChunk - var offset int64 buf := make([]byte, chunkSize) - - for { - n, readErr := io.ReadFull(reader, buf) - if n > 0 { + for i := 0; i < len(visibles); { + start, stop := visibles[i].StartOffset, visibles[i].StopOffset + for i++; i < len(visibles) && visibles[i].StartOffset == stop; i++ { + stop = visibles[i].StopOffset + } + for offset := start; offset < stop; { + n := int(min(int64(len(buf)), stop-offset)) + if _, readErr := reader.ReadAt(buf[:n], offset); readErr != nil { + return nil, fmt.Errorf("read [%d,%d): %w", offset, offset+int64(n), readErr) + } chunk, uploadErr := saveFunc(bytes.NewReader(buf[:n]), "", offset, oldestVisibleTsNs(visibles, offset, offset+int64(n)), uint64(n)) if uploadErr != nil { return nil, uploadErr @@ -864,12 +870,6 @@ func (wfs *WFS) maybeMergeChunks(fileFullPath util.FullPath, compactedChunks []* newChunks = append(newChunks, chunk) offset += int64(n) } - if readErr != nil { - if readErr == io.EOF || readErr == io.ErrUnexpectedEOF { - break - } - return nil, readErr - } } glog.V(0).Infof("merged %s: %d chunks -> %d chunks", fileFullPath, len(compactedChunks)+len(manifestChunks), len(newChunks)) diff --git a/weed/server/filer_server_handlers_write_merge.go b/weed/server/filer_server_handlers_write_merge.go index a167c1a856..31af5c773d 100644 --- a/weed/server/filer_server_handlers_write_merge.go +++ b/weed/server/filer_server_handlers_write_merge.go @@ -45,6 +45,20 @@ func (fs *FilerServer) maybeMergeChunks(ctx context.Context, so *operation.Stora return inputChunks, nil } + // The merge reads to the end of the stream, which stops at a hole. + visibles, err := filer.NonOverlappingVisibleIntervals(ctx, fs.lookupFileId, inputChunks, 0, math.MaxInt64) + if err != nil { + return nil, err + } + var stop int64 + for x := visibles.Front(); x != nil; x = x.Next { + if x.StartOffset != stop { + glog.V(3).InfofCtx(ctx, "Skipping chunk merge for a sparse file") + return inputChunks, nil + } + stop = x.StopOffset + } + return fs.mergeChunks(ctx, so, inputChunks, minOffset) } diff --git a/weed/server/filer_server_handlers_write_merge_test.go b/weed/server/filer_server_handlers_write_merge_test.go new file mode 100644 index 0000000000..7168464a24 --- /dev/null +++ b/weed/server/filer_server_handlers_write_merge_test.go @@ -0,0 +1,30 @@ +package weed_server + +import ( + "context" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/operation" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" +) + +func TestMaybeMergeChunksSkipsSparseFile(t *testing.T) { + fs := &FilerServer{option: &FilerOption{MaxMB: 4}} + var chunks []*filer_pb.FileChunk + offset := int64(0) + for i := 0; i < MergeChunkMinCount+1; i++ { + if i == MergeChunkMinCount/2 { + offset += 1 << 20 + } + chunks = append(chunks, &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: uint64(i + 1), Cookie: 1}, Offset: offset, Size: 1024, ModifiedTsNs: int64(i + 1)}) + offset += 1024 + } + + merged, err := fs.maybeMergeChunks(context.Background(), &operation.StorageOption{}, chunks) + if err != nil { + t.Fatal(err) + } + if len(merged) != len(chunks) { + t.Fatalf("merged a sparse file into %d chunks, want its %d chunks unchanged", len(merged), len(chunks)) + } +}