Merge pull request #879 from seaweedfs/test/v4b-space-baseline

storage: a SPACE baseline at the 1 TiB shape and barrier benchmarks at 1 and 16 TiB
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-12-01 06:30:39 -08:00
commit 42ba1b9cd0
1 file changed
+199
@@ -0,0 +1,199 @@
package storage
import (
"context"
"crypto/sha256"
"fmt"
"os"
"runtime"
"strings"
"testing"
)
// heapNow is the live heap after two GCs.
func heapNow() uint64 {
runtime.GC()
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
return m.HeapAlloc
}
// 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: canonicalKey(owner, ownerID, i, sum), SHA256: sum, Encoding: "full"}
}
return h
}
// 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, owner)
f.mu.Unlock()
before := heapNow()
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() }) // also on an attach failure: no worker or lock outlives the test
cfg := ctConfig(1)
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)
runtime.KeepAlive(f)
if err := layer.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, 32 and the real 1024 blocks a chunk agree
// within 10%; the 1024 store is a 64 GiB sparse file), so a store of
// one-block chunks stands for a 1 TiB volume of 4 MiB chunks with the same
// count. The map's coverage entry is wider at 1024 blocks (144 B against
// 17), but it is the on-disk page format: the attached heap holds each
// chunk's coverage as a pointer, not the entry. (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, 1024} {
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), %.0f B (1024)", chunks, geo[1], geo[8], geo[32], geo[1024])
for _, cb := range []int{8, 32, 1024} {
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)
}
}
// syntheticScanLayer is a layer over a store of S chunks with no dirt: the
// barrier's candidate scan (captureCandidates) visits every chunk's map
// entry. The store is the scan's inputs only (an empty dirty map and S idle
// map entries), not a file: the scan's own cost at 16 TiB without 16 GiB of
// test volume.
func syntheticScanLayer(chunks int) *chunkLayerImpl {
s := &WALStore{dm: newDirtyMap(64), v3: &walStoreV3State{}}
s.v3.mapEntries = newLiveMapEntries(make([]chunkLSNEntryV3, chunks), mapEntrySnapshot{})
return &chunkLayerImpl{s: s, chunkBlocks: 1, numChunks: uint64(chunks), absent: map[uint64]bool{}, filledClean: map[uint64]filledMark{}, localBlocks: map[uint64]bool{}}
}
// The barrier's O(S) work today (V4-B B1/B2): the idle candidate scan and the
// copy of the flat map, at 1 TiB (262,144 chunks) and 16 TiB (4,194,304).
func BenchmarkCutBarrierScan(b *testing.B) {
for _, n := range []int{262144, 4194304} {
b.Run(fmt.Sprintf("chunks=%d", n), func(b *testing.B) {
l := syntheticScanLayer(n)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if c := l.captureCandidates(); len(c) != 0 {
b.Fatalf("an idle scan named %d candidates", len(c))
}
}
})
}
}
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, "v1")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if c := copyHead(h); len(c.Chunks) != int(n) {
b.Fatal("short copy")
}
}
})
}
}