diff --git a/seaweed-block/core/frontend/chunkstore/manifest.go b/seaweed-block/core/frontend/chunkstore/manifest.go index 01781b4b06..9a4708655d 100644 --- a/seaweed-block/core/frontend/chunkstore/manifest.go +++ b/seaweed-block/core/frontend/chunkstore/manifest.go @@ -90,14 +90,19 @@ func (r *ChunkRemote) PrepareManifest(ctx context.Context, base, next storage.Ch data []byte } var uploads []upload - for group, chunks := range groups { - old := map[uint64]storage.ChunkRef{} - for idx, ref := range base.Chunks { - if idx/per == group { - ref.Key = qualifyManifestKey(owner, ref.Key) - old[idx] = ref - } + // Partition the base once, rather than scanning every base chunk for each + // segment group. Keep the same qualified-key comparison for segment reuse. + baseGroups := map[uint64]map[uint64]storage.ChunkRef{} + for idx, ref := range base.Chunks { + group := idx / per + if baseGroups[group] == nil { + baseGroups[group] = map[uint64]storage.ChunkRef{} } + ref.Key = qualifyManifestKey(owner, ref.Key) + baseGroups[group][idx] = ref + } + for group, chunks := range groups { + old := baseGroups[group] if previous, ok := base.Segments[group]; ok && base.ChunkBytes == chunkBytes && reflect.DeepEqual(chunks, old) { next.Segments[group] = previous continue diff --git a/seaweed-block/core/frontend/chunkstore/manifest_scaling_test.go b/seaweed-block/core/frontend/chunkstore/manifest_scaling_test.go new file mode 100644 index 0000000000..dd093d6c84 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/manifest_scaling_test.go @@ -0,0 +1,36 @@ +package chunkstore + +import ( + "context" + "fmt" + "testing" + + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// Metadata-only scaling probe: one chunk per 1-GiB segment, all unchanged. +// No volume data allocation or remote I/O is included. Report ns/op and allocs; +// the old per-group scan visits groups*groups base references. +func BenchmarkPrepareManifestUnchangedGroups(b *testing.B) { + for _, n := range []int{256, 1024, 4096} { + b.Run(fmt.Sprint(n), func(b *testing.B) { + r, err := NewChunkRemote(newFakeFiler(), newFakeBlobs(), "/chunks/v1") + if err != nil { + b.Fatal(err) + } + base := storage.ChunkHead{Chunks: map[uint64]storage.ChunkRef{}, Segments: map[uint64]storage.ChunkSegment{}, ChunkBytes: 1 << 30, ManifestChunkCount: n} + for i := 0; i < n; i++ { + base.Chunks[uint64(i)] = storage.ChunkRef{Key: fmt.Sprintf("@v1/chunk/%d", i)} + base.Segments[uint64(i)] = storage.ChunkSegment{Key: fmt.Sprintf("@v1/seg/%d", i), Owner: "v1", Count: 1} + } + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + next, ids, err := r.PrepareManifest(context.Background(), base, base, 1<<30) + if err != nil || len(ids) != 0 || len(next.Segments) != n { + b.Fatalf("unchanged segment reuse: %v, intents=%v", err, ids) + } + } + }) + } +} diff --git a/seaweed-block/scripts/check-manifest-scaling.py b/seaweed-block/scripts/check-manifest-scaling.py new file mode 100644 index 0000000000..1ad129ccb7 --- /dev/null +++ b/seaweed-block/scripts/check-manifest-scaling.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +"""Check three-sample manifest benchmark scaling; no device or host access. + +Run go test with -bench BenchmarkPrepareManifestUnchangedGroups -benchtime=20x +-count=3, save stdout, then pass that file to this checker. A 16x metadata +scale must grow less than 64x on the same serial runner. This is an explicit +qualification benchmark, not a wall-clock CI unit test. +""" +import re +import statistics +import sys +from pathlib import Path + +if len(sys.argv) != 2: + raise SystemExit("usage: check-manifest-scaling.py BENCHMARK_LOG") +samples = {256: [], 1024: [], 4096: []} +for count, ns in re.findall( + r"BenchmarkPrepareManifestUnchangedGroups/(256|1024|4096)-\d+\s+\d+\s+(\d+) ns/op", + Path(sys.argv[1]).read_text(encoding="utf-8-sig"), +): + samples[int(count)].append(int(ns)) +if any(len(values) != 3 for values in samples.values()): + raise SystemExit("need exactly three samples per group count") +medians = {count: statistics.median(values) for count, values in samples.items()} +if min(medians.values()) <= 0: + raise SystemExit("benchmark durations must be positive") +growth = medians[4096] / medians[256] +print("median_ns", medians, "growth_4096_over_256", growth, "limit", 64) +raise SystemExit(0 if growth < 64 else 1)