From f230a1678dfb84d02f857693effe501d3a994bd1 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 11:13:54 -0800 Subject: [PATCH] storage: golden bytes for the v1.1 record, pass cursor and coverage map The chunk record's page and header, the upgrade pass cursor, and the coverage map's two leaves, index page and root are pinned byte for byte: each encodes to its fixture, each fixture decodes to its input, and the fixtures are pinned by SHA-1. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/storage/golden_v11_test.go | 238 ++++++++++++++++++ .../testdata/format_v11/coverage-index.bin | Bin 0 -> 4096 bytes .../testdata/format_v11/coverage-page.bin | Bin 0 -> 4096 bytes .../testdata/format_v11/coverage-page1.bin | Bin 0 -> 4096 bytes .../testdata/format_v11/coverage-root.bin | Bin 0 -> 4096 bytes .../testdata/format_v11/pass-cursor.bin | Bin 0 -> 36 bytes .../testdata/format_v11/record-header.bin | Bin 0 -> 38 bytes .../testdata/format_v11/record-page.bin | Bin 0 -> 215 bytes 8 files changed, 238 insertions(+) create mode 100644 seaweed-block/core/storage/golden_v11_test.go create mode 100644 seaweed-block/core/storage/testdata/format_v11/coverage-index.bin create mode 100644 seaweed-block/core/storage/testdata/format_v11/coverage-page.bin create mode 100644 seaweed-block/core/storage/testdata/format_v11/coverage-page1.bin create mode 100644 seaweed-block/core/storage/testdata/format_v11/coverage-root.bin create mode 100644 seaweed-block/core/storage/testdata/format_v11/pass-cursor.bin create mode 100644 seaweed-block/core/storage/testdata/format_v11/record-header.bin create mode 100644 seaweed-block/core/storage/testdata/format_v11/record-page.bin diff --git a/seaweed-block/core/storage/golden_v11_test.go b/seaweed-block/core/storage/golden_v11_test.go new file mode 100644 index 0000000000..51247acf66 --- /dev/null +++ b/seaweed-block/core/storage/golden_v11_test.go @@ -0,0 +1,238 @@ +package storage + +import ( + "bytes" + "crypto/sha1" + "encoding/hex" + "os" + "path/filepath" + "reflect" + "sort" + "testing" +) + +// The v1.1 store format's own files, pinned byte for byte: the chunk record's +// page and header, the upgrade pass cursor, and the coverage map's leaf page, +// index page and root (MapFormatID 2). Each fixture in testdata/format_v11 is +// what today's encoder writes for the fixed input below; the encoder must +// keep writing it, and the decoder must read it back to the same input. + +const goldenV11FixMessage = "the v1.1 on-disk encoding changed. Before the format is declared, change it on purpose: regenerate the fixtures (SW_GOLDEN_V11_WRITE) and their pins in the same change, and say so in its description. After it is declared, only by the format path (a version bump, and a refusal of the old format first)" + +const goldenV11Dir = "testdata/format_v11" + +// formatV11Pins is the SHA-1 of each fixture in testdata/format_v11 (git's +// blob form is not used, so CRLF checkouts are refused too). A fixture edit +// fails here, so it cannot hide a format change. +var formatV11Pins = map[string]string{ + "record-page.bin": "74501da85ec83195fb6d928069b3a9b535ca64cb", + "record-header.bin": "faae4b24955234b1d20a97aed51917d08205861f", + "pass-cursor.bin": "010637503da3a2d09b5b8ca6ab20ce3870e524c8", + "coverage-page.bin": "cc00ecc220b9cc1bc9f3658d80fe79cb23a7d3b7", + "coverage-page1.bin": "fef154ab0342f443b0e5ff176c1a332f36487a27", + "coverage-index.bin": "e60e9f88f1101294707c5860f4f81a6ac61da121", + "coverage-root.bin": "6fcd8f168a3ae7b5c01bdc1abe1e138d581e9e49", +} + +func goldenV11Hash(b byte) (h [32]byte) { + for i := range h { + h[i] = b + byte(i) + } + return h +} + +// goldenV11Page is a record page of segment 2 with one entry of each kind +// (1 Absent, 2 Partial, 3 Unknown, 4 HashUnpublished, 5 HashPublished), a +// published segment name and a candidate count. +func goldenV11Page() *chunkRecordPage { + p := &chunkRecordPage{Segment: 2, ValidThrough: 77, Candidates: 3, PublishedSegment: "seg-0a1b2c"} + for k := uint8(1); k <= 5; k++ { + p.Entries = append(p.Entries, chunkRecordEntry{State: k, Hash: goldenV11Hash(k * 0x10)}) + } + return p +} + +func goldenV11Header() chunkRecordHeader { + return chunkRecordHeader{Epoch: 5, Chunks: 20001, G: 3} +} + +func goldenV11Cursor() upgradePassCursor { + return upgradePassCursor{Epoch: 5, Next: 10000, Compared: 9000} +} + +// goldenV11Coverage is a coverage map of 2,690 chunks of 1,024 blocks: 97 +// leaf pages, so one index level (fanout 96) under the root. Every chunk is +// at the zero position (7) and unknown except chunk 27, whose entry crosses +// leaves 0 and 1 (LSN 11, its first and last block local). Leaf 0 is written +// at 4096, leaf 1 at 8192, the index page at 12288 and the root at 16384; the +// other leaves are all-default and have zero references. +type goldenV11Map struct { + d descriptorV3 + cb uint64 + shape mapShapeV3 + entries []chunkLSNEntryV3 + page []byte + page1 []byte + index []byte + root []byte +} + +func goldenV11Coverage(t *testing.T) goldenV11Map { + t.Helper() + const cb, chunks = uint64(1024), uint64(2690) + m := goldenV11Map{cb: cb} + m.d = descriptorV3{BlockSize: 4096, VolumeSize: cb * chunks * 4096, MapFormatID: mapFormatIDCoverage, MapGeneration: 3} + m.d.UUID[0], m.d.MapLineage[0] = 1, 2 + var err error + if m.shape, err = mapShapeForV3(m.d, cb); err != nil || m.shape.pages != 97 || coverageTreeHeight(m.shape.pages) != 1 { + t.Fatalf("need 97 leaves under one index level: shape=%+v err=%v", m.shape, err) + } + m.entries = make([]chunkLSNEntryV3, chunks) + for i := range m.entries { + m.entries[i] = chunkLSNEntryV3{LSN: 7, Unknown: true, Local: noChunkLocalBits} + } + m.entries[27] = chunkLSNEntryV3{LSN: 11, Local: noChunkLocalBits.mark(0, cb).mark(1023, cb)} + if m.page, err = encodeCoverageMapPageV3(m.d, 3, 0, cb, m.entries, 7); err != nil || m.page == nil { + t.Fatalf("leaf 0: %v", err) + } + if m.page1, err = encodeCoverageMapPageV3(m.d, 3, 1, cb, m.entries, 7); err != nil || m.page1 == nil { + t.Fatalf("leaf 1: %v", err) + } + leaves := make([]mapPageRefV3, m.shape.pages) + leaves[0] = mapPageRefV3{Offset: 4096, Hash: mapDigestV3(mapPageDomainV3, m.page[:4064])} + leaves[1] = mapPageRefV3{Offset: 8192, Hash: mapDigestV3(mapPageDomainV3, m.page1[:4064])} + var written [][]byte + next := uint64(12288) + write := func(p []byte) (uint64, error) { + written = append(written, append([]byte(nil), p...)) + at := next + next += uint64(len(p)) + return at, nil + } + if m.root, _, err = encodeCoverageTreeRootV3(m.d, 3, cb, 13, leaves, nil, 7, write); err != nil { + t.Fatal(err) + } + if len(written) != 1 { + t.Fatalf("%d index pages written, want 1", len(written)) + } + m.index = written[0] + return m +} + +func goldenV11Files(t *testing.T) map[string][]byte { + t.Helper() + page, err := encodeChunkRecordPage(goldenV11Page()) + if err != nil { + t.Fatal(err) + } + m := goldenV11Coverage(t) + return map[string][]byte{ + "record-page.bin": page, + "record-header.bin": encodeChunkRecordHeader(goldenV11Header()), + "pass-cursor.bin": encodeUpgradePassCursor(goldenV11Cursor()), + "coverage-page.bin": m.page, + "coverage-page1.bin": m.page1, + "coverage-index.bin": m.index, + "coverage-root.bin": m.root, + } +} + +// TestWriteGoldenV11 writes the fixtures when SW_GOLDEN_V11_WRITE names a +// directory; otherwise it is a no-op. +func TestWriteGoldenV11(t *testing.T) { + dir := os.Getenv("SW_GOLDEN_V11_WRITE") + if dir == "" { + t.Skip("set SW_GOLDEN_V11_WRITE to write the v1.1 fixtures") + } + for name, b := range goldenV11Files(t) { + if err := os.WriteFile(filepath.Join(dir, name), b, 0o644); err != nil { + t.Fatal(err) + } + } +} + +// Each v1.1 form encodes to its fixture's bytes. +func TestFormatV11EncodersWriteTheFixtures(t *testing.T) { + for name, got := range goldenV11Files(t) { + want, err := os.ReadFile(filepath.Join(goldenV11Dir, name)) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, want) { + t.Errorf("%s: %s", name, goldenV11FixMessage) + } + } +} + +// Each fixture decodes to the input it was written from. +func TestFormatV11DecodersReadTheFixtures(t *testing.T) { + read := func(name string) []byte { + b, err := os.ReadFile(filepath.Join(goldenV11Dir, name)) + if err != nil { + t.Fatal(err) + } + return b + } + if p, err := decodeChunkRecordPage(read("record-page.bin"), 2); err != nil || !reflect.DeepEqual(p, goldenV11Page()) { + t.Errorf("record-page.bin decodes to %+v, %v", p, err) + } + if h, err := decodeChunkRecordHeader(read("record-header.bin")); err != nil || h != goldenV11Header() { + t.Errorf("record-header.bin decodes to %+v, %v", h, err) + } + if c, err := decodeUpgradePassCursor(read("pass-cursor.bin")); err != nil || c != goldenV11Cursor() { + t.Errorf("pass-cursor.bin decodes to %+v, %v", c, err) + } + m := goldenV11Coverage(t) + f, err := os.Create(filepath.Join(t.TempDir(), "map")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { f.Close() }) + for at, name := range map[int64]string{4096: "coverage-page.bin", 8192: "coverage-page1.bin", 12288: "coverage-index.bin", 16384: "coverage-root.bin"} { + if _, err := f.WriteAt(read(name), at); err != nil { + t.Fatal(err) + } + } + m.d.MapRootLocator = 16384 + got, _, err := readCoverageMapPagesV3(f, m.d, m.cb, m.shape, read("coverage-root.bin"), 20480, nil) + if err != nil { + t.Fatalf("the coverage fixtures do not read back: %v", err) + } + for _, c := range []int{0, 26, 27, 28, len(m.entries) - 1} { + w, g := m.entries[c], got[c] + if g.LSN != w.LSN || g.Unknown != w.Unknown || g.Local.count() != w.Local.count() || g.Local.has(0) != w.Local.has(0) || g.Local.has(1023) != w.Local.has(1023) { + t.Errorf("chunk %d reads back as %+v, want %+v", c, g, w) + } + } +} + +// The fixtures are the pinned files, and no other file is in the directory. +func TestFormatV11FixturesAreThePinnedFiles(t *testing.T) { + entries, err := os.ReadDir(goldenV11Dir) + if err != nil { + t.Fatal(err) + } + var names []string + for _, e := range entries { + names = append(names, e.Name()) + } + sort.Strings(names) + var want []string + for n := range formatV11Pins { + want = append(want, n) + } + sort.Strings(want) + if !reflect.DeepEqual(names, want) { + t.Fatalf("testdata/format_v11 holds %v, pinned %v", names, want) + } + for _, n := range names { + b, err := os.ReadFile(filepath.Join(goldenV11Dir, n)) + if err != nil { + t.Fatal(err) + } + sum := sha1.Sum(b) + if got := hex.EncodeToString(sum[:]); got != formatV11Pins[n] { + t.Errorf("%s: sha1 %s, pinned %s: %s", n, got, formatV11Pins[n], goldenV11FixMessage) + } + } +} diff --git a/seaweed-block/core/storage/testdata/format_v11/coverage-index.bin b/seaweed-block/core/storage/testdata/format_v11/coverage-index.bin new file mode 100644 index 0000000000000000000000000000000000000000..efd9fce0b644b8c469e05163795e11b508b7dc26 GIT binary patch literal 4096 zcmZ?v@ib;)U|?VbVq^dkLk7&q93VRZ#utDvRMtG)|FgeY`2$vaWNf#e1pLZF}Ka^(XqDLE`iHE7MzG#*WgX zAut*OqaiRF0;3@?8UmvsFd71*Aut*OqaiRF0;3@?8UnL z`WX!${2_o}b~OErh7bM_z%M(Ren!Iwe+b~09Zf%@;e$T}@XL;-pV9Ea9|HJgxzVI) o-EUS|^Kk#q{$}M5Y;kX8HrZO5ho5M;=AVBr`I5M!?&l}50DvwN1ONa4 literal 0 HcmV?d00001 diff --git a/seaweed-block/core/storage/testdata/format_v11/coverage-page1.bin b/seaweed-block/core/storage/testdata/format_v11/coverage-page1.bin new file mode 100644 index 0000000000000000000000000000000000000000..462018779b70d72e6b40f22e027dc8ceb1da6bab GIT binary patch literal 4096 zcmZ?v@ik^*U|?VbVq^dkLk7%H4ouwweq>pk>;`r;eK=LpNn+GJ_yc-0d`80ue+b~0 z9Zf%@;e$T}@XL;-pV9Ea9|HJgN7K(}_}~u#{Ia9zXEc29hX8)r(eyJKKKMfbzbv)W m&yKcB6*jUvg_ZQOu5+oyd#TiId*NC2C;Fd3;`8_`(^~)-F%n$> literal 0 HcmV?d00001 diff --git a/seaweed-block/core/storage/testdata/format_v11/coverage-root.bin b/seaweed-block/core/storage/testdata/format_v11/coverage-root.bin new file mode 100644 index 0000000000000000000000000000000000000000..048e418ef89bfa84099e13d5d639caaf8177a21b GIT binary patch literal 4096 zcmZ?v2{LA4U<5)`z=X>8?c z{khjC8IOSK1dr_ki`T4Ow6^aXKe}}=-J|qq2#kinXb6mkz-S1JhQMeDjE2By2#kin yXb6mkz-S1Jh5!*Eu=K$Nj~MGTm$mMz_kGowsgpIqYyT>dOvfmD4z}6lW()ul)gFET literal 0 HcmV?d00001 diff --git a/seaweed-block/core/storage/testdata/format_v11/pass-cursor.bin b/seaweed-block/core/storage/testdata/format_v11/pass-cursor.bin new file mode 100644 index 0000000000000000000000000000000000000000..0e0f06d60d21f2312adf7070cabd1e2379b6c609 GIT binary patch literal 36 ecmWFz4-Ie*4Kie9fB*q?2(6(Eq1mr%?gIc?<^;C@ literal 0 HcmV?d00001 diff --git a/seaweed-block/core/storage/testdata/format_v11/record-header.bin b/seaweed-block/core/storage/testdata/format_v11/record-header.bin new file mode 100644 index 0000000000000000000000000000000000000000..e1a5d8acabb0438036ed6be82986df8b844d4bc6 GIT binary patch literal 38 fcmWFzcMkG!F)(6cU}b;+ML!754COCg_wNt@T&)GT literal 0 HcmV?d00001 diff --git a/seaweed-block/core/storage/testdata/format_v11/record-page.bin b/seaweed-block/core/storage/testdata/format_v11/record-page.bin new file mode 100644 index 0000000000000000000000000000000000000000..5aef1dcbaaa39427d00466242311e441449e7ad7 GIT binary patch literal 215 zcmWFzcMb}0H!xyifB;`8&BVYAWN|SRr>5%~BpN0eC9?wgi~@o}!Xlz#;u4Zl(lWAg z@(PMd$||aA>Kd9_+B&*=`b_8o28Kq)CZ=ZQ7M51lHnw*5%;*9Rj!w=lu5Rugo?hNQ yzJC5J=mG(OLBS!RVc`*xQPDB6aq+C^0ttyp$tkI6=^2?>**Up+`D#ZMo&o^=HZO1h literal 0 HcmV?d00001