mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* filer: fold only runs of small chunks that lie end to end #12462 folds every chunk under 1 MiB into manifests, 256 at a time in list order. Two things go wrong when those chunks are not plain appends: - A manifest's chunks are not compacted again. A small chunk that a later write covers stays stored and referenced after the write hid it. - weed mount's bloat check sums chunk sizes and counts a manifest by its span. A manifest over chunks that are not next to each other spans bytes other chunks hold, so the file looks bloated and the flush reads it back and stores it again, whole. With weed mount on a local cluster, a writer that appends 300-byte records and rewrites a 4 KiB header at the front after each one (open, two pwrites, close): after 766 records the mount logged "2.0x chunk bloat detected ... (2 chunks, 475684 stored vs 233896 content), merging" and rewrote the file, and did it again at 2.5x (1164184 stored vs 463396 content); 1,600 records cost two whole-file rewrites. Each rewrite reads and stores the whole file, and the next one comes a few hundred flushes later. Random small writes fold their overwritten pages the same way. Only small chunks that no other chunk overlaps now fold, and only in runs that lie end to end, so a manifest spans exactly the bytes it holds. Appends make such runs and fold as before. Chunks between full-size ones, chunks that overlap others and chunks with a hole between them stay flat, as they did before #12462. What is left of a run stays flat until the run grows. * filer: size the small-chunk fold's result by its input CodeQL alert 446 (go/allocation-size-overflow) flagged the capacity len(inputChunks)-len(folded)+len(manifests). It cannot overflow, but the input's length bounds the result anyway: each manifest takes the place of SmallChunkManifestBatch chunks.
63 lines
2.4 KiB
Go
63 lines
2.4 KiB
Go
package mount
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
)
|
|
|
|
// fakeManifestSaver stands in for wfs.saveDataAsChunk when a test folds chunks:
|
|
// each saved manifest gets a fresh file id.
|
|
type fakeManifestSaver struct{ saved uint64 }
|
|
|
|
func (s *fakeManifestSaver) save(reader io.Reader, name string, offset int64, tsNs int64, expectedDataSize uint64) (*filer_pb.FileChunk, error) {
|
|
if _, err := io.Copy(io.Discard, reader); err != nil {
|
|
return nil, err
|
|
}
|
|
s.saved++
|
|
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 9, FileKey: s.saved, Cookie: 1}, ModifiedTsNs: tsNs}, nil
|
|
}
|
|
|
|
// A writer that appends records and rewrites a header at the front after each
|
|
// one, as formats with an index at the start of the file do. Each flush
|
|
// compacts, checks for bloat, then folds, as flushMetadataToFiler does. A
|
|
// header chunk folded into a manifest is never compacted away, and a manifest
|
|
// over the header and the appends spans the whole file, so folding them
|
|
// together makes the file look bloated after a few folds and the flush reads
|
|
// the whole file back. Only the appends may fold.
|
|
func TestSmallChunkFoldLeavesHeaderRewritesOutOfBloatCheck(t *testing.T) {
|
|
const header = 4096
|
|
saver := &fakeManifestSaver{}
|
|
var key uint64
|
|
next := func(offset int64, size uint64) *filer_pb.FileChunk {
|
|
key++
|
|
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: key, Cookie: 1}, Offset: offset, Size: size, ModifiedTsNs: int64(key)}
|
|
}
|
|
|
|
chunks := []*filer_pb.FileChunk{next(0, header)}
|
|
end := int64(header)
|
|
for i := 0; i < 4*filer.SmallChunkManifestBatch; i++ {
|
|
chunks = append(chunks, next(end, 300), next(0, header))
|
|
end += 300
|
|
|
|
manifests, flat := filer.SeparateManifestChunks(chunks)
|
|
compacted, _, err := filer.CompactFileChunks(context.Background(), nil, flat)
|
|
require.NoError(t, err)
|
|
total, size, bloated := shouldMergeChunks(compacted, manifests)
|
|
require.False(t, bloated, "flush %d: %d stored for %d bytes", i, total, size)
|
|
folded, err := filer.MaybeManifestize(saver.save, nil, compacted)
|
|
require.NoError(t, err)
|
|
chunks = append(folded, manifests...)
|
|
}
|
|
|
|
manifests, flat := filer.SeparateManifestChunks(chunks)
|
|
require.Len(t, manifests, 4, "the appends fold")
|
|
require.Less(t, len(flat), filer.SmallChunkManifestBatch+1)
|
|
require.Equal(t, uint64(end), filer.TotalSize(chunks))
|
|
}
|