mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
Merge pull request #888 from seaweedfs/test/v11-golden-bytes
storage: golden bytes for the v1.1 record, pass cursor and coverage map
This commit is contained in:
commit
4063be8def
8 files changed
+238
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user