From 0ecfbd330daf2357ed1fa346b00e6f3b8b3da724 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 18:30:57 -0800 Subject: [PATCH] storage: export a read-only decode of one v3 WAL record DecodeWALRecordV3 wraps the decoder recovery uses (same geometry, size and CRC checks and errors) and returns LSN, type, LBA, length and the whole record size; WALRecordHeaderBytesV3 is the header the codec adds. For read-only tools that analyse crash images; recovery does not use it. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/storage/wal_record_v3.go | 34 +++++++++++ .../core/storage/wal_record_v3_test.go | 59 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 seaweed-block/core/storage/wal_record_v3.go create mode 100644 seaweed-block/core/storage/wal_record_v3_test.go diff --git a/seaweed-block/core/storage/wal_record_v3.go b/seaweed-block/core/storage/wal_record_v3.go new file mode 100644 index 0000000000..21a66b49c1 --- /dev/null +++ b/seaweed-block/core/storage/wal_record_v3.go @@ -0,0 +1,34 @@ +package storage + +// WALRecordV3 is the checked header of one impl-3 WAL record, for read-only +// tools (crash-image analysis, diagnostics). Recovery does not use it. +type WALRecordV3 struct { + LSN uint64 + Type uint8 + LBA uint64 + Length uint32 // the payload bytes the record covers + Size uint64 // the whole record, header included + Padding bool +} + +// WALRecordHeaderBytesV3 is what a record adds to its payload (prefix, CRC32C +// and EntrySize): the same constant the encoder and decoder use. +const WALRecordHeaderBytesV3 = walEntryHeaderSizeV3 + +// DecodeWALRecordV3 decodes the record at the start of buf with the decoder +// recovery uses: the same geometry, size and CRC checks and errors. It adds +// no parsing of its own; nonce, cursor and lap admission stay the scanner's. +func DecodeWALRecordV3(buf []byte, blockSize uint32, numBlocks uint64) (WALRecordV3, error) { + e, err := decodeWALEntryV3(buf, blockSize, numBlocks) + if err != nil { + return WALRecordV3{}, err + } + return WALRecordV3{ + LSN: e.LSN, + Type: e.Type, + LBA: e.LBA, + Length: e.Length, + Size: uint64(walEntryHeaderSizeV3 + len(e.Data)), + Padding: e.Type == walEntryPadding, + }, nil +} diff --git a/seaweed-block/core/storage/wal_record_v3_test.go b/seaweed-block/core/storage/wal_record_v3_test.go new file mode 100644 index 0000000000..a81efe2301 --- /dev/null +++ b/seaweed-block/core/storage/wal_record_v3_test.go @@ -0,0 +1,59 @@ +package storage + +import ( + "bytes" + "errors" + "testing" +) + +// DecodeWALRecordV3 must agree with the internal decoder field for field on +// valid records and fail exactly when it fails, with the same error. +func TestDecodeWALRecordV3AgreesWithTheInternalDecoder(t *testing.T) { + var nonce [16]byte + copy(nonce[:], "0123456789abcdef") + write := walEntryV3{LSN: 21, Type: walEntryWrite, LBA: 3, Length: v3TestBlockSize, DurableLSN: 20, + LogicalStart: 4096, Nonce: nonce, Data: bytes.Repeat([]byte{0xA5}, int(v3TestBlockSize))} + pad := walEntryV3{Type: walEntryPadding, Nonce: nonce, Length: 512, Data: make([]byte, 512)} + for name, e := range map[string]walEntryV3{"write": write, "barrier": v3TestBarrier(), "padding": pad} { + t.Run(name, func(t *testing.T) { + b, err := encodeWALEntryV3(e, v3TestBlockSize, v3TestNumBlocks) + if err != nil { + t.Fatalf("fixture not encodable: %v", err) + } + in, ierr := decodeWALEntryV3(b, v3TestBlockSize, v3TestNumBlocks) + out, oerr := DecodeWALRecordV3(b, v3TestBlockSize, v3TestNumBlocks) + if ierr != nil || oerr != nil { + t.Fatalf("valid record: internal %v, exported %v", ierr, oerr) + } + want := WALRecordV3{LSN: in.LSN, Type: in.Type, LBA: in.LBA, Length: in.Length, + Size: uint64(len(b)), Padding: in.Type == walEntryPadding} + if out != want { + t.Fatalf("exported %+v, internal decode gives %+v", out, want) + } + }) + } + b, err := encodeWALEntryV3(write, v3TestBlockSize, v3TestNumBlocks) + if err != nil { + t.Fatal(err) + } + if got := uint64(len(b)); got != uint64(WALRecordHeaderBytesV3)+uint64(v3TestBlockSize) { + t.Fatalf("a %d-byte write's record is %d bytes, want header %d + payload", v3TestBlockSize, got, WALRecordHeaderBytesV3) + } + // every single-byte corruption and every truncation: both decoders refuse, with the same error + for i := 0; i < len(b); i++ { + c := append([]byte(nil), b...) + c[i] ^= 0x40 + _, ierr := decodeWALEntryV3(c, v3TestBlockSize, v3TestNumBlocks) + _, oerr := DecodeWALRecordV3(c, v3TestBlockSize, v3TestNumBlocks) + if (ierr == nil) != (oerr == nil) || ierr != nil && !errors.Is(oerr, ierr) && oerr.Error() != ierr.Error() { + t.Fatalf("byte %d corrupted: internal %v, exported %v", i, ierr, oerr) + } + } + for n := 0; n < len(b); n += 97 { + _, ierr := decodeWALEntryV3(b[:n], v3TestBlockSize, v3TestNumBlocks) + _, oerr := DecodeWALRecordV3(b[:n], v3TestBlockSize, v3TestNumBlocks) + if ierr == nil || oerr == nil || oerr.Error() != ierr.Error() { + t.Fatalf("truncated to %d: internal %v, exported %v", n, ierr, oerr) + } + } +}