mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
Merge pull request #571 from seaweedfs/feat/wal-record-decode
storage: export a read-only decode of one v3 WAL record
This commit is contained in:
commit
b9ea626c09
2 files changed
+93
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user