From 191a26da506de9da0e6899965bc29eee1ce3b555 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 06:00:04 -0800 Subject: [PATCH] storage: the SPACE baseline charges the layer for its own canonical keys; its controls The layer's HEAD now comes from a remote whose GetHead allocates its own key strings (as a real remote's decode does), so the keys the layer keeps are counted; the keys are in the layout's canonical form (114 bytes at the 1 TiB chunk count). TestSpaceMeasurementControls shows a shallow copy would be undercounted by its keys, that the per-chunk heap does not depend on a chunk's block count (1, 8 and 32 blocks a chunk agree), and that key length moves it about one byte a key byte. Co-Authored-By: Claude Opus 5.5 --- .../core/storage/chunk_space_baseline_test.go | 144 ++++++++++++++---- 1 file changed, 117 insertions(+), 27 deletions(-) diff --git a/seaweed-block/core/storage/chunk_space_baseline_test.go b/seaweed-block/core/storage/chunk_space_baseline_test.go index ded5e56123..935cdc3615 100644 --- a/seaweed-block/core/storage/chunk_space_baseline_test.go +++ b/seaweed-block/core/storage/chunk_space_baseline_test.go @@ -1,10 +1,12 @@ package storage import ( + "context" "crypto/sha256" "fmt" "os" "runtime" + "strings" "testing" ) @@ -17,49 +19,137 @@ func heapNow() uint64 { return m.HeapAlloc } -// fullHead names every chunk of an S-chunk volume, with references shaped -// as the layout remote's (an owner-qualified key, the chunk's SHA-256). -func fullHead(chunks uint64) chunkHead { +// canonicalKey is a chunk reference's engine key in the layout remote's form +// (chunkstore.LayoutKey): "layout:" + owner + ":" + the owner's 32-hex +// VOLUME id + ":" + the chunk's name (index "-" the 64-hex SHA-256). +func canonicalKey(owner, ownerID string, index uint64, sum [32]byte) string { + return fmt.Sprintf("layout:%s:%s:%d-%x", owner, ownerID, index, sum) +} + +// fullHead names every chunk of an S-chunk volume with canonical references. +func fullHead(chunks uint64, owner string) chunkHead { + const ownerID = "309016fbc5b31531e1e7c44585851c7d" h := chunkHead{Gen: 1, Epoch: 1, Holder: "host-a", Chunks: make(map[uint64]chunkRef, chunks)} for i := uint64(0); i < chunks; i++ { sum := sha256.Sum256([]byte(fmt.Sprint(i))) - h.Chunks[i] = chunkRef{Key: fmt.Sprintf("@v1-0011223344556677/chunks/%d/%x", i, sum[:8]), SHA256: sum, Encoding: "full"} + h.Chunks[i] = chunkRef{Key: canonicalKey(owner, ownerID, i, sum), SHA256: sum, Encoding: "full"} } return h } -// The SPACE baseline (V4-B PR 4's red, r2.736/r2.777): the holder's heap per -// chunk for a 1 TiB-shaped volume (262,144 chunks; one-block chunks here, the -// same count), every chunk named by HEAD, measured after GC once the layer is -// attached: the store's and the layer's retained heap, the remote's copy of -// HEAD excluded (it exists before the first reading). It also reports one -// flat map's own cost. With CHUNK_SPACE_RED=1 it asserts the target, at most -// 200 B per chunk, which fails until PR 4 drops the flat map. -func TestSpacePerChunkAt1TiBShape(t *testing.T) { - const chunks = 262144 +// deepHead copies a HEAD with every key string newly allocated. +func deepHead(h chunkHead) chunkHead { + out := copyHead(h) + for i, r := range out.Chunks { + r.Key = strings.Clone(r.Key) + out.Chunks[i] = r + } + return out +} + +// ownedHeadRemote answers GetHead with a HEAD whose key strings are its own +// (allocated at the call), as a real remote's decode does: a layer that +// keeps them is charged for them, never for the fake's backing. +type ownedHeadRemote struct{ *fakeChunkRemote } + +func (r ownedHeadRemote) GetHead(ctx context.Context) (chunkHead, error) { + h, err := r.fakeChunkRemote.GetHead(ctx) + return deepHead(h), err +} + +func (r ownedHeadRemote) CASHeadDelta(ctx context.Context, e chunkHeadVersion, n chunkHead, c map[uint64]*chunkRef) (uint64, error) { + return casDeltaVia(ctx, r.GetHead, r.CASHead, e, n, c) +} + +// heldPerChunk attaches a layer to a store of chunks x chunkBlocks blocks +// whose HEAD names every chunk (keys of the owner's length) and reports the +// heap the store and layer hold after attach, per chunk, after GC. The +// fake's own HEAD exists before the first reading; the layer's copy has its +// own keys (ownedHeadRemote). +func heldPerChunk(t *testing.T, chunks uint64, chunkBlocks int, owner string) float64 { + t.Helper() f := newFakeChunkRemote() f.mu.Lock() - f.head = fullHead(chunks) + f.head = fullHead(chunks, owner) f.mu.Unlock() - m0 := heapNow() - oneMap := fullHead(chunks) - flat := float64(heapNow()-m0) / chunks - runtime.KeepAlive(oneMap) - oneMap = chunkHead{} before := heapNow() - s, err := createMappedWALStoreV3ForTest(t.TempDir()+"/mapped", chunks, 4096, v3TestWAL, WModeStrict, 1) + s, err := createMappedWALStoreV3ForTest(t.TempDir()+"/mapped", uint32(chunks)*uint32(chunkBlocks), 4096, v3TestWAL, WModeStrict, uint64(chunkBlocks)) if err != nil { t.Fatal(err) } s.DisableAutoFlushForRecoveryTest() - t.Cleanup(func() { s.Close() }) cfg := ctConfig(1) - cfg.ChunkBlocks, cfg.Bitmap = 1, nil - layer := mustAttach(t, s, f, cfg) - per := float64(int64(heapNow())-int64(before)) / chunks + cfg.ChunkBlocks, cfg.Bitmap = uint32(chunkBlocks), nil + layer := mustAttach(t, s, ownedHeadRemote{f}, cfg) + per := float64(int64(heapNow())-int64(before)) / float64(chunks) runtime.KeepAlive(layer) - t.Logf("SPACE at 1 TiB shape (%d chunks, v1.1 format %v): %.0f B/chunk held after attach; one flat map alone is %.0f B/chunk", - chunks, chunkV11Format, per, flat) + runtime.KeepAlive(f) + if err := layer.Close(); err != nil { + t.Fatal(err) + } + if err := s.Close(); err != nil { + t.Fatal(err) + } + return per +} + +// The measurement's own controls. (1) A retained copy is charged with its +// keys only when it owns them: a deep copy of a HEAD costs its keys more +// than a shallow one (copyHead shares them), so a layer charged by this +// method for a shallow copy would be undercounted, which ownedHeadRemote +// prevents. (2) The per-chunk heap depends on the chunk count, not on the +// chunk's block count (1, 8 and 32 blocks a chunk agree within 10%), so a +// store of one-block chunks stands for a 1 TiB volume of 4 MiB chunks with +// the same count. (3) Key length moves it by about one byte a key byte. +func TestSpaceMeasurementControls(t *testing.T) { + const chunks = 16384 + h := fullHead(chunks, "v1") + m0 := heapNow() + shallow := copyHead(h) + sh := heapNow() - m0 + m1 := heapNow() + deep := deepHead(h) + dp := heapNow() - m1 + runtime.KeepAlive(shallow) + runtime.KeepAlive(deep) + keyLen := len(h.Chunks[0].Key) + if float64(dp-sh)/chunks < float64(keyLen)*0.8 { + t.Fatalf("a deep copy costs %d B/chunk over a shallow one, want about the key's %d", (dp-sh)/chunks, keyLen) + } + geo := map[int]float64{} + for _, cb := range []int{1, 8, 32} { + geo[cb] = heldPerChunk(t, chunks, cb, "v1") + } + t.Logf("held per chunk at %d chunks: %.0f B (1 block/chunk), %.0f B (8), %.0f B (32)", chunks, geo[1], geo[8], geo[32]) + for _, cb := range []int{8, 32} { + if d := geo[cb]/geo[1] - 1; d > 0.10 || d < -0.10 { + t.Fatalf("held per chunk depends on the chunk's block count: %.0f B at 1 block, %.0f B at %d", geo[1], geo[cb], cb) + } + } + long := strings.Repeat("v", 101) // a 100-byte longer owner name + longer := heldPerChunk(t, chunks, 1, long) + t.Logf("held per chunk with keys 100 B longer: %.0f B (+%.0f)", longer, longer-geo[1]) + if longer-geo[1] < 60 || longer-geo[1] > 160 { + t.Fatalf("keys 100 B longer moved the held heap by %.0f B/chunk, want about 100", longer-geo[1]) + } +} + +// The SPACE baseline (V4-B PR 4's red, r2.736/r2.777): the holder's heap per +// chunk for a 1 TiB volume's chunk count (262,144; one-block chunks, which +// TestSpaceMeasurementControls shows stand for 4 MiB ones), every chunk +// named by HEAD with canonical keys owned by the layer, after GC once the +// layer is attached. With CHUNK_SPACE_RED=1 it asserts the target, at most +// 200 B per chunk, which fails until PR 4 drops the flat map. +func TestSpacePerChunkAt1TiBShape(t *testing.T) { + const chunks = 262144 + m0 := heapNow() + oneMap := fullHead(chunks, "v1") + flat := float64(heapNow()-m0) / chunks + runtime.KeepAlive(oneMap) + oneMap = chunkHead{} + per := heldPerChunk(t, chunks, 1, "v1") + t.Logf("SPACE at 1 TiB's chunk count (%d chunks, v1.1 format %v, canonical %d-byte keys): %.0f B/chunk held after attach; one flat map with its keys is %.0f B/chunk", + chunks, chunkV11Format, len(canonicalKey("v1", "309016fbc5b31531e1e7c44585851c7d", chunks-1, [32]byte{})), per, flat) if os.Getenv("CHUNK_SPACE_RED") == "1" && per > 200 { t.Fatalf("SPACE red: %.0f B/chunk, want at most 200", per) } @@ -95,7 +185,7 @@ func BenchmarkCutBarrierScan(b *testing.B) { func BenchmarkCopyHead(b *testing.B) { for _, n := range []uint64{262144, 4194304} { b.Run(fmt.Sprintf("chunks=%d", n), func(b *testing.B) { - h := fullHead(n) + h := fullHead(n, "v1") b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ {