Files
seaweedfs/weed/server/filer_server_handlers_write_merge.go
T
Chris LuandClaude Opus 5.5 7638cf2f66 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:07:45 -08:00

92 lines
3.0 KiB
Go

package weed_server
import (
"context"
"io"
"math"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/stats"
)
// MergeChunkMinCount matches the small-chunk fold so a merge rewrites them
// before MaybeManifestize hides them in a manifest.
const MergeChunkMinCount int = filer.SmallChunkManifestBatch
func (fs *FilerServer) maybeMergeChunks(ctx context.Context, so *operation.StorageOption, inputChunks []*filer_pb.FileChunk) (mergedChunks []*filer_pb.FileChunk, err error) {
// Don't merge SSE-encrypted chunks to preserve per-chunk metadata
for _, chunk := range inputChunks {
if chunk.GetSseType() != 0 { // Any SSE type (SSE-C or SSE-KMS)
glog.V(3).InfofCtx(ctx, "Skipping chunk merge for SSE-encrypted chunks")
return inputChunks, nil
}
}
// Only merge small chunks more than half of the file
var chunkSize = fs.option.MaxMB * 1024 * 1024
var smallChunk, sumChunk int
var minOffset int64 = math.MaxInt64
for _, chunk := range inputChunks {
if chunk.IsChunkManifest {
continue
}
if chunk.Size < uint64(chunkSize/2) {
smallChunk++
if chunk.Offset < minOffset {
minOffset = chunk.Offset
}
}
sumChunk++
}
if smallChunk < MergeChunkMinCount || smallChunk < sumChunk/2 {
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)
}
func (fs *FilerServer) mergeChunks(ctx context.Context, so *operation.StorageOption, inputChunks []*filer_pb.FileChunk, chunkOffset int64) (mergedChunks []*filer_pb.FileChunk, mergeErr error) {
chunkedFileReader := filer.NewChunkStreamReaderFromFiler(ctx, fs.filer.MasterClient, inputChunks)
_, mergeErr = chunkedFileReader.Seek(chunkOffset, io.SeekCurrent)
if mergeErr != nil {
return nil, mergeErr
}
mergedChunks, _, _, mergeErr, _ = fs.uploadReaderToChunks(ctx, nil, chunkedFileReader, chunkOffset, int32(fs.option.MaxMB*1024*1024), "", "", true, so)
if mergeErr != nil {
return
}
stats.FilerHandlerCounter.WithLabelValues(stats.ChunkMerge).Inc()
for _, chunk := range inputChunks {
if chunk.Offset < chunkOffset || chunk.IsChunkManifest {
mergedChunks = append(mergedChunks, chunk)
}
}
garbage, err := filer.MinusChunks(ctx, fs.lookupFileId, inputChunks, mergedChunks, fs.filer.MasterClient)
if err != nil {
glog.ErrorfCtx(ctx, "Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s",
mergedChunks, inputChunks)
return mergedChunks, err
}
fs.filer.DeleteChunksNotRecursive(garbage)
return
}