mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
phase169: prove segmented WAL format recovery
This commit is contained in:
1 parent
c3dd8a8e22
commit
5f69697b2a
6 files changed
+1135
-1
No files matched your search
@@ -59,6 +59,46 @@ Open chooses the highest valid generation. A damaged latest header may fall
|
||||
back to the prior valid generation; a torn or CRC-invalid committed record
|
||||
fails with typed `storage.ErrWALIntegrityFault`.
|
||||
|
||||
### Phase 169 Segmented Format Candidate
|
||||
|
||||
Phase 169 adds an internal format proof before changing the Store:
|
||||
|
||||
```text
|
||||
[64-byte segment header]
|
||||
[N x 32-byte entry descriptors]
|
||||
[N contiguous fixed-block payloads]
|
||||
```
|
||||
|
||||
The segment header records the version, total bytes, entry count, block size,
|
||||
first/last LSN, monotonic segment sequence, table-and-payload CRC, and header
|
||||
CRC. Each descriptor records one LSN, LBA, write flag, canonical payload
|
||||
offset/size, data CRC, and descriptor-plus-data CRC.
|
||||
|
||||
The initial bounds are 256 entries and 1 MiB of payload per segment. Decode
|
||||
validates the bounds before allocation, requires contiguous increasing LSNs,
|
||||
and rejects non-canonical offsets, invalid LBAs, unsupported flags, and every
|
||||
CRC mismatch. Repeated writes to the same LBA are valid and retain LSN order.
|
||||
|
||||
Segment commitment is not inferred from bytes that happen to exist in the
|
||||
file. Recovery receives a trusted manifest from the future dual-header
|
||||
protocol: committed bytes, segment count, first segment sequence, and
|
||||
first/last committed LSN:
|
||||
|
||||
- malformed or incomplete bytes inside that boundary fail closed;
|
||||
- physical bytes after the boundary are an uncommitted tail and are ignored;
|
||||
- the first and last segment must match the trusted logical anchors;
|
||||
- adjacent committed segments must have consecutive segment sequences and LSN
|
||||
ranges.
|
||||
|
||||
The scanner streams one bounded segment at a time into a callback. It does not
|
||||
retain payload memory proportional to the committed WAL length, and callers
|
||||
must not publish accumulated recovery state until the complete manifest
|
||||
validates.
|
||||
|
||||
The codec and committed-prefix scanner are not yet selected by
|
||||
`CreateStore`/`OpenStore`. This separation is deliberate: D1 proves format and
|
||||
recovery behavior before D2 adds a bounded group-commit owner.
|
||||
|
||||
## Write And Publication
|
||||
|
||||
The store allocates global LSN and lane sequence under one short metadata lock.
|
||||
|
||||
@@ -0,0 +1,382 @@
|
||||
package parallelwal
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
segmentHeaderSize = 64
|
||||
segmentEntryHeaderSize = 32
|
||||
segmentMagic = "PWSG"
|
||||
segmentVersion = 1
|
||||
maxSegmentEntries = 256
|
||||
maxSegmentPayloadBytes = 1 << 20
|
||||
segmentHeaderCRCOffset = 60
|
||||
segmentEntriesCRCOffset = 48
|
||||
)
|
||||
|
||||
var (
|
||||
errBadSegment = errors.New("parallelwal: invalid segment")
|
||||
errSegmentBounds = errors.New("parallelwal: segment exceeds bounds")
|
||||
)
|
||||
|
||||
type segmentHeader struct {
|
||||
Size uint32
|
||||
EntryCount uint32
|
||||
BlockSize uint32
|
||||
FirstLSN uint64
|
||||
LastLSN uint64
|
||||
Sequence uint64
|
||||
}
|
||||
|
||||
type walSegment struct {
|
||||
Sequence uint64
|
||||
Records []walRecord
|
||||
}
|
||||
|
||||
// segmentRecoveryWindow is the trusted manifest that a future dual-header
|
||||
// generation will persist. CommittedBytes alone cannot prove that the logical
|
||||
// prefix starts and ends at the expected sequence and LSN.
|
||||
type segmentRecoveryWindow struct {
|
||||
CommittedBytes int64
|
||||
SegmentCount uint64
|
||||
FirstSequence uint64
|
||||
FirstLSN uint64
|
||||
LastLSN uint64
|
||||
}
|
||||
|
||||
func (w segmentRecoveryWindow) validate() error {
|
||||
if w.CommittedBytes < 0 {
|
||||
return fmt.Errorf("%w: negative committed bytes %d", errBadSegment, w.CommittedBytes)
|
||||
}
|
||||
if w.CommittedBytes == 0 {
|
||||
if w.SegmentCount != 0 || w.FirstSequence != 0 || w.FirstLSN != 0 || w.LastLSN != 0 {
|
||||
return fmt.Errorf("%w: non-empty manifest for empty committed prefix", errBadSegment)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if w.SegmentCount == 0 || w.FirstSequence == 0 || w.FirstLSN == 0 || w.LastLSN < w.FirstLSN {
|
||||
return fmt.Errorf("%w: invalid recovery window %+v", errBadSegment, w)
|
||||
}
|
||||
if w.SegmentCount > uint64(w.CommittedBytes/segmentHeaderSize) {
|
||||
return fmt.Errorf("%w: segment count %d exceeds committed bytes %d",
|
||||
errBadSegment, w.SegmentCount, w.CommittedBytes)
|
||||
}
|
||||
if w.FirstSequence > ^uint64(0)-(w.SegmentCount-1) {
|
||||
return fmt.Errorf("%w: segment sequence range overflows", errBadSegment)
|
||||
}
|
||||
if w.LastLSN-w.FirstLSN+1 < w.SegmentCount {
|
||||
return fmt.Errorf("%w: LSN range [%d,%d] cannot contain %d non-empty segments",
|
||||
errBadSegment, w.FirstLSN, w.LastLSN, w.SegmentCount)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func segmentEncodedSize(entryCount, blockSize uint32) (uint32, error) {
|
||||
if entryCount == 0 || entryCount > maxSegmentEntries {
|
||||
return 0, fmt.Errorf("%w: entry count %d", errSegmentBounds, entryCount)
|
||||
}
|
||||
if blockSize == 0 || blockSize > maxSegmentPayloadBytes {
|
||||
return 0, fmt.Errorf("%w: block size %d", errSegmentBounds, blockSize)
|
||||
}
|
||||
payloadBytes := uint64(entryCount) * uint64(blockSize)
|
||||
if payloadBytes > maxSegmentPayloadBytes {
|
||||
return 0, fmt.Errorf("%w: payload bytes %d", errSegmentBounds, payloadBytes)
|
||||
}
|
||||
total := uint64(segmentHeaderSize) +
|
||||
uint64(entryCount)*segmentEntryHeaderSize +
|
||||
payloadBytes
|
||||
if total > uint64(^uint32(0)) {
|
||||
return 0, fmt.Errorf("%w: encoded bytes %d", errSegmentBounds, total)
|
||||
}
|
||||
return uint32(total), nil
|
||||
}
|
||||
|
||||
func encodeSegment(sequence uint64, records []walRecord, blockSize, numBlocks uint32) ([]byte, error) {
|
||||
if sequence == 0 {
|
||||
return nil, fmt.Errorf("%w: sequence must be positive", errBadSegment)
|
||||
}
|
||||
if len(records) == 0 || uint64(len(records)) > uint64(maxSegmentEntries) {
|
||||
return nil, fmt.Errorf("%w: entry count %d", errSegmentBounds, len(records))
|
||||
}
|
||||
entryCount := uint32(len(records))
|
||||
totalSize, err := segmentEncodedSize(entryCount, blockSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
firstLSN := records[0].LSN
|
||||
if firstLSN == 0 || firstLSN > ^uint64(0)-uint64(entryCount-1) {
|
||||
return nil, fmt.Errorf("%w: invalid first LSN %d", errBadSegment, firstLSN)
|
||||
}
|
||||
if numBlocks == 0 {
|
||||
return nil, fmt.Errorf("%w: numBlocks must be positive", errBadSegment)
|
||||
}
|
||||
|
||||
buf := make([]byte, totalSize)
|
||||
payloadStart := segmentHeaderSize + int(entryCount)*segmentEntryHeaderSize
|
||||
for i, rec := range records {
|
||||
wantLSN := firstLSN + uint64(i)
|
||||
if rec.LSN != wantLSN {
|
||||
return nil, fmt.Errorf("%w: entry %d LSN=%d want=%d",
|
||||
errBadSegment, i, rec.LSN, wantLSN)
|
||||
}
|
||||
if rec.LBA >= numBlocks {
|
||||
return nil, fmt.Errorf("%w: entry %d LBA=%d numBlocks=%d",
|
||||
errBadSegment, i, rec.LBA, numBlocks)
|
||||
}
|
||||
if rec.Flags != flagWrite {
|
||||
return nil, fmt.Errorf("%w: entry %d flags=%d", errBadSegment, i, rec.Flags)
|
||||
}
|
||||
if len(rec.Data) != int(blockSize) {
|
||||
return nil, fmt.Errorf("%w: entry %d payload=%d blockSize=%d",
|
||||
errBadSegment, i, len(rec.Data), blockSize)
|
||||
}
|
||||
|
||||
entryOffset := segmentHeaderSize + i*segmentEntryHeaderSize
|
||||
payloadOffset := payloadStart + i*int(blockSize)
|
||||
entry := buf[entryOffset : entryOffset+segmentEntryHeaderSize]
|
||||
payload := buf[payloadOffset : payloadOffset+int(blockSize)]
|
||||
copy(payload, rec.Data)
|
||||
binary.LittleEndian.PutUint64(entry[0:8], rec.LSN)
|
||||
binary.LittleEndian.PutUint32(entry[8:12], rec.LBA)
|
||||
binary.LittleEndian.PutUint16(entry[12:14], rec.Flags)
|
||||
binary.LittleEndian.PutUint32(entry[16:20], uint32(payloadOffset))
|
||||
binary.LittleEndian.PutUint32(entry[20:24], blockSize)
|
||||
binary.LittleEndian.PutUint32(entry[24:28], crc32.ChecksumIEEE(payload))
|
||||
entryCRC := crc32.NewIEEE()
|
||||
_, _ = entryCRC.Write(entry[:28])
|
||||
_, _ = entryCRC.Write(payload)
|
||||
binary.LittleEndian.PutUint32(entry[28:32], entryCRC.Sum32())
|
||||
}
|
||||
|
||||
copy(buf[0:4], segmentMagic)
|
||||
binary.LittleEndian.PutUint16(buf[4:6], segmentVersion)
|
||||
binary.LittleEndian.PutUint16(buf[6:8], segmentHeaderSize)
|
||||
binary.LittleEndian.PutUint32(buf[8:12], totalSize)
|
||||
binary.LittleEndian.PutUint32(buf[12:16], entryCount)
|
||||
binary.LittleEndian.PutUint32(buf[16:20], blockSize)
|
||||
binary.LittleEndian.PutUint64(buf[24:32], firstLSN)
|
||||
binary.LittleEndian.PutUint64(buf[32:40], firstLSN+uint64(entryCount-1))
|
||||
binary.LittleEndian.PutUint64(buf[40:48], sequence)
|
||||
binary.LittleEndian.PutUint32(buf[segmentEntriesCRCOffset:52],
|
||||
crc32.ChecksumIEEE(buf[segmentHeaderSize:]))
|
||||
binary.LittleEndian.PutUint32(buf[segmentHeaderCRCOffset:64],
|
||||
crc32.ChecksumIEEE(buf[:segmentHeaderCRCOffset]))
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func decodeSegmentHeader(buf []byte, expectedBlockSize uint32) (segmentHeader, error) {
|
||||
if len(buf) < segmentHeaderSize {
|
||||
return segmentHeader{}, fmt.Errorf("%w: truncated header size=%d", errBadSegment, len(buf))
|
||||
}
|
||||
buf = buf[:segmentHeaderSize]
|
||||
if string(buf[0:4]) != segmentMagic {
|
||||
return segmentHeader{}, fmt.Errorf("%w: bad magic", errBadSegment)
|
||||
}
|
||||
if got := binary.LittleEndian.Uint16(buf[4:6]); got != segmentVersion {
|
||||
return segmentHeader{}, fmt.Errorf("%w: version=%d", errBadSegment, got)
|
||||
}
|
||||
if got := binary.LittleEndian.Uint16(buf[6:8]); got != segmentHeaderSize {
|
||||
return segmentHeader{}, fmt.Errorf("%w: header size=%d", errBadSegment, got)
|
||||
}
|
||||
if got, want := binary.LittleEndian.Uint32(buf[segmentHeaderCRCOffset:64]),
|
||||
crc32.ChecksumIEEE(buf[:segmentHeaderCRCOffset]); got != want {
|
||||
return segmentHeader{}, fmt.Errorf("%w: header CRC got=%08x want=%08x", errBadSegment, got, want)
|
||||
}
|
||||
if flags := binary.LittleEndian.Uint32(buf[20:24]); flags != 0 {
|
||||
return segmentHeader{}, fmt.Errorf("%w: header flags=%d", errBadSegment, flags)
|
||||
}
|
||||
if reserved := binary.LittleEndian.Uint64(buf[52:60]); reserved != 0 {
|
||||
return segmentHeader{}, fmt.Errorf("%w: reserved=%d", errBadSegment, reserved)
|
||||
}
|
||||
h := segmentHeader{
|
||||
Size: binary.LittleEndian.Uint32(buf[8:12]),
|
||||
EntryCount: binary.LittleEndian.Uint32(buf[12:16]),
|
||||
BlockSize: binary.LittleEndian.Uint32(buf[16:20]),
|
||||
FirstLSN: binary.LittleEndian.Uint64(buf[24:32]),
|
||||
LastLSN: binary.LittleEndian.Uint64(buf[32:40]),
|
||||
Sequence: binary.LittleEndian.Uint64(buf[40:48]),
|
||||
}
|
||||
if h.BlockSize != expectedBlockSize {
|
||||
return segmentHeader{}, fmt.Errorf("%w: block size=%d want=%d",
|
||||
errBadSegment, h.BlockSize, expectedBlockSize)
|
||||
}
|
||||
wantSize, err := segmentEncodedSize(h.EntryCount, h.BlockSize)
|
||||
if err != nil {
|
||||
return segmentHeader{}, err
|
||||
}
|
||||
if h.Size != wantSize {
|
||||
return segmentHeader{}, fmt.Errorf("%w: size=%d want=%d", errBadSegment, h.Size, wantSize)
|
||||
}
|
||||
if h.Sequence == 0 || h.FirstLSN == 0 ||
|
||||
h.FirstLSN > ^uint64(0)-uint64(h.EntryCount-1) ||
|
||||
h.LastLSN != h.FirstLSN+uint64(h.EntryCount-1) {
|
||||
return segmentHeader{}, fmt.Errorf("%w: sequence=%d LSN range=[%d,%d] entries=%d",
|
||||
errBadSegment, h.Sequence, h.FirstLSN, h.LastLSN, h.EntryCount)
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func decodeSegment(buf []byte, blockSize, numBlocks uint32) (walSegment, error) {
|
||||
h, err := decodeSegmentHeader(buf, blockSize)
|
||||
if err != nil {
|
||||
return walSegment{}, err
|
||||
}
|
||||
if len(buf) != int(h.Size) {
|
||||
return walSegment{}, fmt.Errorf("%w: bytes=%d want=%d", errBadSegment, len(buf), h.Size)
|
||||
}
|
||||
if numBlocks == 0 {
|
||||
return walSegment{}, fmt.Errorf("%w: numBlocks must be positive", errBadSegment)
|
||||
}
|
||||
if got, want := binary.LittleEndian.Uint32(buf[segmentEntriesCRCOffset:52]),
|
||||
crc32.ChecksumIEEE(buf[segmentHeaderSize:]); got != want {
|
||||
return walSegment{}, fmt.Errorf("%w: entries CRC got=%08x want=%08x", errBadSegment, got, want)
|
||||
}
|
||||
|
||||
records := make([]walRecord, h.EntryCount)
|
||||
payloadStart := segmentHeaderSize + int(h.EntryCount)*segmentEntryHeaderSize
|
||||
for i := range records {
|
||||
entryOffset := segmentHeaderSize + i*segmentEntryHeaderSize
|
||||
entry := buf[entryOffset : entryOffset+segmentEntryHeaderSize]
|
||||
wantPayloadOffset := payloadStart + i*int(blockSize)
|
||||
payloadOffset := binary.LittleEndian.Uint32(entry[16:20])
|
||||
payloadSize := binary.LittleEndian.Uint32(entry[20:24])
|
||||
if payloadOffset != uint32(wantPayloadOffset) || payloadSize != blockSize {
|
||||
return walSegment{}, fmt.Errorf("%w: entry %d payload offset/size=%d/%d want=%d/%d",
|
||||
errBadSegment, i, payloadOffset, payloadSize, wantPayloadOffset, blockSize)
|
||||
}
|
||||
payloadEnd := uint64(payloadOffset) + uint64(payloadSize)
|
||||
if payloadEnd > uint64(len(buf)) {
|
||||
return walSegment{}, fmt.Errorf("%w: entry %d payload end=%d size=%d",
|
||||
errBadSegment, i, payloadEnd, len(buf))
|
||||
}
|
||||
payload := buf[int(payloadOffset):int(payloadEnd)]
|
||||
if got, want := crc32.ChecksumIEEE(payload), binary.LittleEndian.Uint32(entry[24:28]); got != want {
|
||||
return walSegment{}, fmt.Errorf("%w: entry %d data CRC got=%08x want=%08x",
|
||||
errBadSegment, i, got, want)
|
||||
}
|
||||
entryCRC := crc32.NewIEEE()
|
||||
_, _ = entryCRC.Write(entry[:28])
|
||||
_, _ = entryCRC.Write(payload)
|
||||
if got, want := entryCRC.Sum32(), binary.LittleEndian.Uint32(entry[28:32]); got != want {
|
||||
return walSegment{}, fmt.Errorf("%w: entry %d CRC got=%08x want=%08x",
|
||||
errBadSegment, i, got, want)
|
||||
}
|
||||
lsn := binary.LittleEndian.Uint64(entry[0:8])
|
||||
if want := h.FirstLSN + uint64(i); lsn != want {
|
||||
return walSegment{}, fmt.Errorf("%w: entry %d LSN=%d want=%d",
|
||||
errBadSegment, i, lsn, want)
|
||||
}
|
||||
lba := binary.LittleEndian.Uint32(entry[8:12])
|
||||
if lba >= numBlocks {
|
||||
return walSegment{}, fmt.Errorf("%w: entry %d LBA=%d numBlocks=%d",
|
||||
errBadSegment, i, lba, numBlocks)
|
||||
}
|
||||
flags := binary.LittleEndian.Uint16(entry[12:14])
|
||||
if flags != flagWrite || binary.LittleEndian.Uint16(entry[14:16]) != 0 {
|
||||
return walSegment{}, fmt.Errorf("%w: entry %d flags/reserved=%d/%d",
|
||||
errBadSegment, i, flags, binary.LittleEndian.Uint16(entry[14:16]))
|
||||
}
|
||||
records[i] = walRecord{
|
||||
LSN: lsn,
|
||||
LBA: lba,
|
||||
Flags: flags,
|
||||
Data: append([]byte(nil), payload...),
|
||||
}
|
||||
}
|
||||
return walSegment{Sequence: h.Sequence, Records: records}, nil
|
||||
}
|
||||
|
||||
// scanCommittedSegments scans only the trusted committed window. Physical
|
||||
// bytes after the window are an uncommitted tail and are intentionally
|
||||
// ignored; malformed bytes or manifest disagreement inside it fail closed.
|
||||
// Callers must not publish state accumulated by visit until this returns nil.
|
||||
func scanCommittedSegments(
|
||||
f *os.File,
|
||||
window segmentRecoveryWindow,
|
||||
blockSize, numBlocks uint32,
|
||||
visit func(walSegment) error,
|
||||
) error {
|
||||
if err := window.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
st, err := f.Stat()
|
||||
if err != nil {
|
||||
return fmt.Errorf("parallelwal: stat segment file: %w", err)
|
||||
}
|
||||
if window.CommittedBytes > st.Size() {
|
||||
return fmt.Errorf("%w: committed bytes=%d file size=%d",
|
||||
errBadSegment, window.CommittedBytes, st.Size())
|
||||
}
|
||||
|
||||
var offset int64
|
||||
var segmentCount uint64
|
||||
var lastLSN uint64
|
||||
for offset < window.CommittedBytes {
|
||||
if segmentCount >= window.SegmentCount {
|
||||
return fmt.Errorf("%w: committed bytes contain more than %d segments",
|
||||
errBadSegment, window.SegmentCount)
|
||||
}
|
||||
if window.CommittedBytes-offset < segmentHeaderSize {
|
||||
return fmt.Errorf("%w: committed tail has %d header bytes",
|
||||
errBadSegment, window.CommittedBytes-offset)
|
||||
}
|
||||
headerBytes := make([]byte, segmentHeaderSize)
|
||||
if n, err := f.ReadAt(headerBytes, offset); err != nil || n != len(headerBytes) {
|
||||
return fmt.Errorf("%w: read header at %d: n=%d err=%v", errBadSegment, offset, n, err)
|
||||
}
|
||||
h, err := decodeSegmentHeader(headerBytes, blockSize)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parallelwal: decode segment at %d: %w", offset, err)
|
||||
}
|
||||
if int64(h.Size) > window.CommittedBytes-offset {
|
||||
return fmt.Errorf("%w: committed segment at %d size=%d remaining=%d",
|
||||
errBadSegment, offset, h.Size, window.CommittedBytes-offset)
|
||||
}
|
||||
segmentBytes := make([]byte, h.Size)
|
||||
if n, err := f.ReadAt(segmentBytes, offset); err != nil || n != len(segmentBytes) {
|
||||
return fmt.Errorf("%w: read segment at %d: n=%d err=%v", errBadSegment, offset, n, err)
|
||||
}
|
||||
segment, err := decodeSegment(segmentBytes, blockSize, numBlocks)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parallelwal: decode segment at %d: %w", offset, err)
|
||||
}
|
||||
wantSequence := window.FirstSequence + segmentCount
|
||||
if segment.Sequence != wantSequence {
|
||||
return fmt.Errorf("%w: segment sequence=%d want=%d",
|
||||
errBadSegment, segment.Sequence, wantSequence)
|
||||
}
|
||||
wantFirstLSN := window.FirstLSN
|
||||
if segmentCount != 0 {
|
||||
if lastLSN == ^uint64(0) {
|
||||
return fmt.Errorf("%w: LSN range overflows", errBadSegment)
|
||||
}
|
||||
wantFirstLSN = lastLSN + 1
|
||||
}
|
||||
if firstLSN := segment.Records[0].LSN; firstLSN != wantFirstLSN {
|
||||
return fmt.Errorf("%w: segment first LSN=%d want=%d",
|
||||
errBadSegment, firstLSN, wantFirstLSN)
|
||||
}
|
||||
lastLSN = segment.Records[len(segment.Records)-1].LSN
|
||||
if lastLSN > window.LastLSN {
|
||||
return fmt.Errorf("%w: segment last LSN=%d exceeds manifest=%d",
|
||||
errBadSegment, lastLSN, window.LastLSN)
|
||||
}
|
||||
if visit != nil {
|
||||
if err := visit(segment); err != nil {
|
||||
return fmt.Errorf("parallelwal: visit segment %d: %w", segment.Sequence, err)
|
||||
}
|
||||
}
|
||||
segmentCount++
|
||||
offset += int64(h.Size)
|
||||
}
|
||||
if segmentCount != window.SegmentCount || lastLSN != window.LastLSN {
|
||||
return fmt.Errorf("%w: recovered segments/lastLSN=%d/%d want=%d/%d",
|
||||
errBadSegment, segmentCount, lastLSN, window.SegmentCount, window.LastLSN)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,567 @@
|
||||
package parallelwal
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"hash/crc32"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func segmentTestRecord(lsn uint64, lba uint32, fill byte, blockSize uint32) walRecord {
|
||||
return walRecord{
|
||||
LSN: lsn,
|
||||
LBA: lba,
|
||||
Flags: flagWrite,
|
||||
Data: testBlock(fill, int(blockSize)),
|
||||
}
|
||||
}
|
||||
|
||||
func resealTestSegment(buf []byte) {
|
||||
binary.LittleEndian.PutUint32(buf[segmentEntriesCRCOffset:52],
|
||||
crc32.ChecksumIEEE(buf[segmentHeaderSize:]))
|
||||
binary.LittleEndian.PutUint32(buf[segmentHeaderCRCOffset:64],
|
||||
crc32.ChecksumIEEE(buf[:segmentHeaderCRCOffset]))
|
||||
}
|
||||
|
||||
func resealTestSegmentEntry(buf []byte, index int, blockSize uint32) {
|
||||
count := int(binary.LittleEndian.Uint32(buf[12:16]))
|
||||
entryOffset := segmentHeaderSize + index*segmentEntryHeaderSize
|
||||
payloadOffset := segmentHeaderSize + count*segmentEntryHeaderSize + index*int(blockSize)
|
||||
entry := buf[entryOffset : entryOffset+segmentEntryHeaderSize]
|
||||
payload := buf[payloadOffset : payloadOffset+int(blockSize)]
|
||||
entryCRC := crc32.NewIEEE()
|
||||
_, _ = entryCRC.Write(entry[:28])
|
||||
_, _ = entryCRC.Write(payload)
|
||||
binary.LittleEndian.PutUint32(entry[28:32], entryCRC.Sum32())
|
||||
resealTestSegment(buf)
|
||||
}
|
||||
|
||||
func TestSegmentEncodeDecodeAllowsOrderedSameLBAWrites(t *testing.T) {
|
||||
const (
|
||||
blockSize = uint32(512)
|
||||
numBlocks = uint32(16)
|
||||
)
|
||||
encoded, err := encodeSegment(7, []walRecord{
|
||||
segmentTestRecord(11, 3, 0x11, blockSize),
|
||||
segmentTestRecord(12, 3, 0x22, blockSize),
|
||||
segmentTestRecord(13, 8, 0x33, blockSize),
|
||||
}, blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := decodeSegment(encoded, blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded.Sequence != 7 || len(decoded.Records) != 3 {
|
||||
t.Fatalf("decoded sequence/records=%d/%d", decoded.Sequence, len(decoded.Records))
|
||||
}
|
||||
for i, rec := range decoded.Records {
|
||||
if rec.LSN != uint64(11+i) {
|
||||
t.Fatalf("record %d LSN=%d", i, rec.LSN)
|
||||
}
|
||||
}
|
||||
if decoded.Records[0].LBA != 3 || decoded.Records[1].LBA != 3 ||
|
||||
decoded.Records[0].Data[0] != 0x11 || decoded.Records[1].Data[0] != 0x22 {
|
||||
t.Fatal("same-LBA writes lost order or payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSegmentFormatGoldenVector(t *testing.T) {
|
||||
const goldenHex = "505753470100400064000000010000000400000000000000050000000000000005000000000000000300000000000000966002fb0000000000000000bc06d2c205000000000000000200000001000000600000000400000000b98ae092979dfc10203040"
|
||||
golden, err := hex.DecodeString(goldenHex)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encoded, err := encodeSegment(3, []walRecord{{
|
||||
LSN: 5, LBA: 2, Flags: flagWrite, Data: []byte{0x10, 0x20, 0x30, 0x40},
|
||||
}}, 4, 8)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(encoded) != string(golden) {
|
||||
t.Fatalf("encoded segment changed:\ngot %x\nwant %x", encoded, golden)
|
||||
}
|
||||
decoded, err := decodeSegment(golden, 4, 8)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded.Sequence != 3 || len(decoded.Records) != 1 ||
|
||||
decoded.Records[0].LSN != 5 || decoded.Records[0].LBA != 2 ||
|
||||
string(decoded.Records[0].Data) != string([]byte{0x10, 0x20, 0x30, 0x40}) {
|
||||
t.Fatalf("decoded golden=%+v", decoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSegmentEncodeRejectsInvalidGeometryAndOrder(t *testing.T) {
|
||||
const blockSize = uint32(512)
|
||||
tests := []struct {
|
||||
name string
|
||||
sequence uint64
|
||||
records []walRecord
|
||||
}{
|
||||
{name: "zero sequence", records: []walRecord{segmentTestRecord(1, 0, 1, blockSize)}},
|
||||
{name: "empty", sequence: 1},
|
||||
{
|
||||
name: "non-contiguous LSN",
|
||||
sequence: 1,
|
||||
records: []walRecord{
|
||||
segmentTestRecord(1, 0, 1, blockSize),
|
||||
segmentTestRecord(3, 1, 2, blockSize),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid LBA",
|
||||
sequence: 1,
|
||||
records: []walRecord{segmentTestRecord(1, 8, 1, blockSize)},
|
||||
},
|
||||
{
|
||||
name: "invalid flags",
|
||||
sequence: 1,
|
||||
records: []walRecord{{
|
||||
LSN: 1, LBA: 0, Flags: 9, Data: testBlock(1, int(blockSize)),
|
||||
}},
|
||||
},
|
||||
{
|
||||
name: "invalid payload",
|
||||
sequence: 1,
|
||||
records: []walRecord{{
|
||||
LSN: 1, LBA: 0, Flags: flagWrite, Data: []byte{1},
|
||||
}},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if _, err := encodeSegment(tc.sequence, tc.records, blockSize, 8); err == nil {
|
||||
t.Fatal("encode unexpectedly succeeded")
|
||||
}
|
||||
})
|
||||
}
|
||||
if _, err := segmentEncodedSize(maxSegmentEntries+1, blockSize); !errors.Is(err, errSegmentBounds) {
|
||||
t.Fatalf("entry bound error=%v", err)
|
||||
}
|
||||
if _, err := segmentEncodedSize(2, maxSegmentPayloadBytes); !errors.Is(err, errSegmentBounds) {
|
||||
t.Fatalf("payload bound error=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSegmentDecodeRejectsCorruptionAndBounds(t *testing.T) {
|
||||
const (
|
||||
blockSize = uint32(512)
|
||||
numBlocks = uint32(8)
|
||||
)
|
||||
base, err := encodeSegment(1, []walRecord{
|
||||
segmentTestRecord(1, 0, 0x11, blockSize),
|
||||
segmentTestRecord(2, 1, 0x22, blockSize),
|
||||
}, blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
clone := func() []byte { return append([]byte(nil), base...) }
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func([]byte) []byte
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "truncated header",
|
||||
mutate: func(buf []byte) []byte {
|
||||
return buf[:segmentHeaderSize-1]
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bad header CRC",
|
||||
mutate: func(buf []byte) []byte {
|
||||
buf[24] ^= 0xff
|
||||
return buf
|
||||
},
|
||||
want: "header CRC",
|
||||
},
|
||||
{
|
||||
name: "bad entries CRC",
|
||||
mutate: func(buf []byte) []byte {
|
||||
buf[segmentHeaderSize+14] ^= 0xff
|
||||
return buf
|
||||
},
|
||||
want: "entries CRC",
|
||||
},
|
||||
{
|
||||
name: "bad entry CRC",
|
||||
mutate: func(buf []byte) []byte {
|
||||
buf[segmentHeaderSize+28] ^= 0xff
|
||||
resealTestSegment(buf)
|
||||
return buf
|
||||
},
|
||||
want: "entry 0 CRC",
|
||||
},
|
||||
{
|
||||
name: "duplicate LSN",
|
||||
mutate: func(buf []byte) []byte {
|
||||
second := segmentHeaderSize + segmentEntryHeaderSize
|
||||
binary.LittleEndian.PutUint64(buf[second:second+8], 1)
|
||||
resealTestSegmentEntry(buf, 1, blockSize)
|
||||
return buf
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid LBA",
|
||||
mutate: func(buf []byte) []byte {
|
||||
binary.LittleEndian.PutUint32(buf[segmentHeaderSize+8:segmentHeaderSize+12], numBlocks)
|
||||
resealTestSegmentEntry(buf, 0, blockSize)
|
||||
return buf
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "non-canonical payload offset",
|
||||
mutate: func(buf []byte) []byte {
|
||||
binary.LittleEndian.PutUint32(buf[segmentHeaderSize+16:segmentHeaderSize+20], 0)
|
||||
resealTestSegmentEntry(buf, 0, blockSize)
|
||||
return buf
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "bad data CRC",
|
||||
mutate: func(buf []byte) []byte {
|
||||
payloadStart := segmentHeaderSize + 2*segmentEntryHeaderSize
|
||||
buf[payloadStart] ^= 0xff
|
||||
resealTestSegmentEntry(buf, 0, blockSize)
|
||||
return buf
|
||||
},
|
||||
want: "entry 0 data CRC",
|
||||
},
|
||||
{
|
||||
name: "truncated payload",
|
||||
mutate: func(buf []byte) []byte {
|
||||
return buf[:len(buf)-1]
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "oversized count before allocation",
|
||||
mutate: func(buf []byte) []byte {
|
||||
binary.LittleEndian.PutUint32(buf[12:16], ^uint32(0))
|
||||
binary.LittleEndian.PutUint32(buf[segmentHeaderCRCOffset:64],
|
||||
crc32.ChecksumIEEE(buf[:segmentHeaderCRCOffset]))
|
||||
return buf
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := decodeSegment(tc.mutate(clone()), blockSize, numBlocks)
|
||||
if err == nil {
|
||||
t.Fatal("decode unexpectedly succeeded")
|
||||
}
|
||||
if !errors.Is(err, errBadSegment) && !errors.Is(err, errSegmentBounds) {
|
||||
t.Fatalf("decode error=%v", err)
|
||||
}
|
||||
if tc.want != "" && !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatalf("decode error=%v want substring %q", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoverCommittedSegmentsIgnoresOnlyUncommittedTail(t *testing.T) {
|
||||
const (
|
||||
blockSize = uint32(512)
|
||||
numBlocks = uint32(8)
|
||||
)
|
||||
first, err := encodeSegment(9,
|
||||
[]walRecord{segmentTestRecord(10, 0, 0x10, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := encodeSegment(10, []walRecord{
|
||||
segmentTestRecord(11, 1, 0x11, blockSize),
|
||||
segmentTestRecord(12, 1, 0x12, blockSize),
|
||||
}, blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
third, err := encodeSegment(11,
|
||||
[]walRecord{segmentTestRecord(13, 2, 0x13, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
path := filepath.Join(t.TempDir(), "segments.bin")
|
||||
physical := append(append(append([]byte(nil), first...), second...), third[:len(third)/2]...)
|
||||
if err := os.WriteFile(path, physical, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
committedBytes := int64(len(first) + len(second))
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var segments []walSegment
|
||||
window := segmentRecoveryWindow{
|
||||
CommittedBytes: committedBytes,
|
||||
SegmentCount: 2,
|
||||
FirstSequence: 9,
|
||||
FirstLSN: 10,
|
||||
LastLSN: 12,
|
||||
}
|
||||
err = scanCommittedSegments(f, window, blockSize, numBlocks, func(segment walSegment) error {
|
||||
segments = append(segments, segment)
|
||||
return nil
|
||||
})
|
||||
_ = f.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(segments) != 2 {
|
||||
t.Fatalf("recovered segment count=%d", len(segments))
|
||||
}
|
||||
if len(segments[0].Records) != 1 || len(segments[1].Records) != 2 {
|
||||
t.Fatalf("recovered segment shape=%v", []int{len(segments[0].Records), len(segments[1].Records)})
|
||||
}
|
||||
if got := segments[1].Records[1]; got.LSN != 12 || got.LBA != 1 || got.Data[0] != 0x12 {
|
||||
t.Fatalf("last recovered record=%+v", got)
|
||||
}
|
||||
|
||||
f, err = os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tornWindow := segmentRecoveryWindow{
|
||||
CommittedBytes: int64(len(physical)),
|
||||
SegmentCount: 3,
|
||||
FirstSequence: 9,
|
||||
FirstLSN: 10,
|
||||
LastLSN: 13,
|
||||
}
|
||||
err = scanCommittedSegments(f, tornWindow, blockSize, numBlocks, nil)
|
||||
_ = f.Close()
|
||||
if !errors.Is(err, errBadSegment) {
|
||||
t.Fatalf("committed torn tail error=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoverCommittedSegmentsFailsClosedOnCommittedCorruption(t *testing.T) {
|
||||
const (
|
||||
blockSize = uint32(512)
|
||||
numBlocks = uint32(8)
|
||||
)
|
||||
first, err := encodeSegment(1,
|
||||
[]walRecord{segmentTestRecord(1, 0, 0x10, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := encodeSegment(2,
|
||||
[]walRecord{segmentTestRecord(2, 1, 0x20, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
all := append(append([]byte(nil), first...), second...)
|
||||
all[len(first)+segmentHeaderSize+segmentEntryHeaderSize] ^= 0xff
|
||||
path := filepath.Join(t.TempDir(), "segments.bin")
|
||||
if err := os.WriteFile(path, all, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
window := segmentRecoveryWindow{
|
||||
CommittedBytes: int64(len(all)),
|
||||
SegmentCount: 2,
|
||||
FirstSequence: 1,
|
||||
FirstLSN: 1,
|
||||
LastLSN: 2,
|
||||
}
|
||||
err = scanCommittedSegments(f, window, blockSize, numBlocks, nil)
|
||||
_ = f.Close()
|
||||
if !errors.Is(err, errBadSegment) {
|
||||
t.Fatalf("committed corruption error=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoverCommittedSegmentsRejectsSequenceAndLSNGaps(t *testing.T) {
|
||||
const (
|
||||
blockSize = uint32(512)
|
||||
numBlocks = uint32(8)
|
||||
)
|
||||
first, err := encodeSegment(1,
|
||||
[]walRecord{segmentTestRecord(1, 0, 0x10, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
sequence uint64
|
||||
firstLSN uint64
|
||||
want string
|
||||
}{
|
||||
{name: "sequence gap", sequence: 3, firstLSN: 2, want: "segment sequence"},
|
||||
{name: "LSN gap", sequence: 2, firstLSN: 3, want: "segment first LSN"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
second, err := encodeSegment(tc.sequence,
|
||||
[]walRecord{segmentTestRecord(tc.firstLSN, 1, 0x20, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "segments.bin")
|
||||
all := append(append([]byte(nil), first...), second...)
|
||||
if err := os.WriteFile(path, all, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
window := segmentRecoveryWindow{
|
||||
CommittedBytes: int64(len(all)),
|
||||
SegmentCount: 2,
|
||||
FirstSequence: 1,
|
||||
FirstLSN: 1,
|
||||
LastLSN: tc.firstLSN,
|
||||
}
|
||||
err = scanCommittedSegments(f, window, blockSize, numBlocks, nil)
|
||||
_ = f.Close()
|
||||
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatalf("recovery error=%v want substring %q", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanCommittedSegmentsRequiresManifestAnchors(t *testing.T) {
|
||||
const (
|
||||
blockSize = uint32(512)
|
||||
numBlocks = uint32(8)
|
||||
)
|
||||
first, err := encodeSegment(1,
|
||||
[]walRecord{segmentTestRecord(1, 0, 0x10, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := encodeSegment(2,
|
||||
[]walRecord{segmentTestRecord(2, 1, 0x20, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
window segmentRecoveryWindow
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "missing first segment",
|
||||
data: second,
|
||||
window: segmentRecoveryWindow{
|
||||
CommittedBytes: int64(len(second)),
|
||||
SegmentCount: 1,
|
||||
FirstSequence: 1,
|
||||
FirstLSN: 1,
|
||||
LastLSN: 1,
|
||||
},
|
||||
want: "segment sequence=2 want=1",
|
||||
},
|
||||
{
|
||||
name: "durable frontier not reached",
|
||||
data: first,
|
||||
window: segmentRecoveryWindow{
|
||||
CommittedBytes: int64(len(first)),
|
||||
SegmentCount: 1,
|
||||
FirstSequence: 1,
|
||||
FirstLSN: 1,
|
||||
LastLSN: 2,
|
||||
},
|
||||
want: "segments/lastLSN=1/1 want=1/2",
|
||||
},
|
||||
{
|
||||
name: "segment count not reached",
|
||||
data: first,
|
||||
window: segmentRecoveryWindow{
|
||||
CommittedBytes: int64(len(first)),
|
||||
SegmentCount: 2,
|
||||
FirstSequence: 1,
|
||||
FirstLSN: 1,
|
||||
LastLSN: 2,
|
||||
},
|
||||
want: "segments/lastLSN=1/1 want=2/2",
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "segments.bin")
|
||||
if err := os.WriteFile(path, tc.data, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = scanCommittedSegments(f, tc.window, blockSize, numBlocks, nil)
|
||||
_ = f.Close()
|
||||
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatalf("scan error=%v want substring %q", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestScanCommittedSegmentsReplaysSameLBAInLSNOrder(t *testing.T) {
|
||||
const (
|
||||
blockSize = uint32(512)
|
||||
numBlocks = uint32(8)
|
||||
)
|
||||
first, err := encodeSegment(4,
|
||||
[]walRecord{segmentTestRecord(20, 3, 0x20, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := encodeSegment(5,
|
||||
[]walRecord{segmentTestRecord(21, 3, 0x21, blockSize)},
|
||||
blockSize, numBlocks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
all := append(append([]byte(nil), first...), second...)
|
||||
path := filepath.Join(t.TempDir(), "segments.bin")
|
||||
if err := os.WriteFile(path, all, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
latest := make(map[uint32]walRecord)
|
||||
window := segmentRecoveryWindow{
|
||||
CommittedBytes: int64(len(all)),
|
||||
SegmentCount: 2,
|
||||
FirstSequence: 4,
|
||||
FirstLSN: 20,
|
||||
LastLSN: 21,
|
||||
}
|
||||
err = scanCommittedSegments(f, window, blockSize, numBlocks, func(segment walSegment) error {
|
||||
for _, record := range segment.Records {
|
||||
latest[record.LBA] = record
|
||||
}
|
||||
return nil
|
||||
})
|
||||
_ = f.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := latest[3]; got.LSN != 21 || got.Data[0] != 0x21 {
|
||||
t.Fatalf("latest same-LBA record=%+v", got)
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ Block's own LSN, recovery, replication, and frontend contracts.
|
||||
2. Every LSN appears in exactly one committed segment or receives a terminal
|
||||
error.
|
||||
3. Segment decode validates geometry, bounds, header CRC, entry CRC, monotonic
|
||||
LSN order, and duplicate LSN/LBA metadata before replay.
|
||||
and duplicate LSN metadata, and invalid LBA metadata before replay.
|
||||
4. Torn or corrupt committed segments fail closed; an uncommitted tail is
|
||||
ignored only under an explicit tail rule.
|
||||
5. The publication frontier advances only through the contiguous successful
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
# Phase 169 D1 Segment Format And Recovery QA
|
||||
|
||||
Validate the internal segmented WAL codec and committed-prefix recovery proof.
|
||||
This is a local engine gate: it does not install Kubernetes resources, build
|
||||
images, or select the candidate in the product.
|
||||
|
||||
## Source
|
||||
|
||||
Use the exact Phase 169 D1 commit on branch
|
||||
`phase169-segmented-wal-group-commit`.
|
||||
|
||||
## Run
|
||||
|
||||
On a Linux host with Go and CGO enabled:
|
||||
|
||||
```bash
|
||||
cd /tmp/seaweed_block
|
||||
bash scripts/run-phase169-segment-format-gate.sh /tmp/seaweed_block
|
||||
cat results/phase169-segment-format-gate/phase169-segment-format-summary.txt
|
||||
```
|
||||
|
||||
If the shared source directory is stale, sync an exact clean archive of the D1
|
||||
commit first. Do not run against uncommitted files or a moving shared tree.
|
||||
|
||||
## Required Evidence
|
||||
|
||||
The gate must exit zero and the summary must contain:
|
||||
|
||||
```text
|
||||
clean_encode_decode=pass
|
||||
invalid_geometry_and_order=pass
|
||||
corruption_and_bounds=pass
|
||||
uncommitted_tail_rule=pass
|
||||
committed_corruption_fail_closed=pass
|
||||
cross_segment_sequence_and_lsn=pass
|
||||
trusted_manifest_anchors=pass
|
||||
same_lba_recovery_order=pass
|
||||
frozen_format_vector=pass
|
||||
storage_regression=pass
|
||||
parallelwal_race=pass
|
||||
windows_compile=pass
|
||||
product_selector_added=false
|
||||
segment_version=1
|
||||
segment_max_entries=256
|
||||
segment_max_payload_bytes=1048576
|
||||
committed_corruption_policy=fail_closed
|
||||
uncommitted_tail_policy=ignore_after_trusted_boundary
|
||||
phase169_segment_format_status=ok
|
||||
```
|
||||
|
||||
Inspect the individual logs rather than relying only on the summary. Confirm
|
||||
that each line follows a real test/build command and that the corruption tests
|
||||
mutate encoded bytes before resealing only the outer checksum needed to reach
|
||||
the intended inner validation.
|
||||
|
||||
## Boundary
|
||||
|
||||
- Same-LBA writes in one segment are valid and retain LSN order.
|
||||
- Duplicate or non-contiguous LSNs are invalid.
|
||||
- The decoder must reject size/count/payload bounds before allocating from
|
||||
untrusted fields.
|
||||
- A malformed committed segment fails closed.
|
||||
- A physical torn tail is ignored only when it is beyond the trusted committed
|
||||
byte boundary.
|
||||
- The trusted manifest anchors the first segment sequence, first LSN, final
|
||||
durable LSN, and exact segment count.
|
||||
- Recovery streams bounded segments and later same-LBA writes win only after
|
||||
the full manifest validates.
|
||||
- No product selector or existing Store format changes are allowed in D1.
|
||||
|
||||
Write the result to
|
||||
`internal/docs/qa-assignments/phase169-d1-segment-format-recovery-qa-signoff.md`.
|
||||
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT="${1:-$(pwd)}"
|
||||
ARTIFACT_DIR="${SW_BLOCK_ARTIFACT_DIR:-${ROOT}/results/phase169-segment-format-gate}"
|
||||
SUMMARY="${ARTIFACT_DIR}/phase169-segment-format-summary.txt"
|
||||
|
||||
mkdir -p "${ARTIFACT_DIR}"
|
||||
: >"${SUMMARY}"
|
||||
|
||||
write_summary() {
|
||||
echo "$*" | tee -a "${SUMMARY}" >/dev/null
|
||||
}
|
||||
|
||||
run_test() {
|
||||
local name="$1"
|
||||
local pattern="$2"
|
||||
go test ./core/storage/parallelwal -run "${pattern}" -count=20 \
|
||||
>"${ARTIFACT_DIR}/${name}.log" 2>&1
|
||||
write_summary "${name}=pass"
|
||||
}
|
||||
|
||||
write_summary "phase169_segment_format_status=running"
|
||||
write_summary "scope=internal_format_and_committed_prefix_recovery"
|
||||
|
||||
cd "${ROOT}"
|
||||
|
||||
run_test "clean_encode_decode" '^TestSegmentEncodeDecodeAllowsOrderedSameLBAWrites$'
|
||||
run_test "invalid_geometry_and_order" '^TestSegmentEncodeRejectsInvalidGeometryAndOrder$'
|
||||
run_test "corruption_and_bounds" '^TestSegmentDecodeRejectsCorruptionAndBounds$'
|
||||
run_test "uncommitted_tail_rule" '^TestRecoverCommittedSegmentsIgnoresOnlyUncommittedTail$'
|
||||
run_test "committed_corruption_fail_closed" '^TestRecoverCommittedSegmentsFailsClosedOnCommittedCorruption$'
|
||||
run_test "cross_segment_sequence_and_lsn" '^TestRecoverCommittedSegmentsRejectsSequenceAndLSNGaps$'
|
||||
run_test "trusted_manifest_anchors" '^TestScanCommittedSegmentsRequiresManifestAnchors$'
|
||||
run_test "same_lba_recovery_order" '^TestScanCommittedSegmentsReplaysSameLBAInLSNOrder$'
|
||||
run_test "frozen_format_vector" '^TestSegmentFormatGoldenVector$'
|
||||
|
||||
go test ./core/storage/parallelwal ./core/storage ./core/recovery ./core/frontend/durable -count=1 \
|
||||
>"${ARTIFACT_DIR}/storage-regression.log" 2>&1
|
||||
write_summary "storage_regression=pass"
|
||||
|
||||
if [[ "$(go env CGO_ENABLED)" == "1" ]]; then
|
||||
go test -race ./core/storage/parallelwal -count=1 \
|
||||
>"${ARTIFACT_DIR}/parallelwal-race.log" 2>&1
|
||||
write_summary "parallelwal_race=pass"
|
||||
else
|
||||
write_summary "parallelwal_race=not_run_cgo_disabled"
|
||||
fi
|
||||
|
||||
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 \
|
||||
go test -c ./core/storage/parallelwal \
|
||||
-o "${ARTIFACT_DIR}/parallelwal-windows.test.exe" \
|
||||
>"${ARTIFACT_DIR}/windows-compile.log" 2>&1
|
||||
write_summary "windows_compile=pass"
|
||||
|
||||
if grep -R -n --include='*.go' --include='*.yaml' --include='*.tpl' \
|
||||
-E 'segmented-walstore|segment-walstore' \
|
||||
cmd charts core/storage >/dev/null 2>&1; then
|
||||
echo "Phase 169 D1 unexpectedly added a product selector" >&2
|
||||
exit 1
|
||||
fi
|
||||
write_summary "product_selector_added=false"
|
||||
|
||||
grep -Eq 'segmentVersion[[:space:]]*=[[:space:]]*1' core/storage/parallelwal/segment.go
|
||||
grep -Eq 'maxSegmentEntries[[:space:]]*=[[:space:]]*256' core/storage/parallelwal/segment.go
|
||||
grep -Eq 'maxSegmentPayloadBytes[[:space:]]*=[[:space:]]*1[[:space:]]*<<[[:space:]]*20' \
|
||||
core/storage/parallelwal/segment.go
|
||||
write_summary "segment_version=1"
|
||||
write_summary "segment_max_entries=256"
|
||||
write_summary "segment_max_payload_bytes=1048576"
|
||||
write_summary "committed_corruption_policy=fail_closed"
|
||||
write_summary "uncommitted_tail_policy=ignore_after_trusted_boundary"
|
||||
write_summary "phase169_segment_format_status=ok"
|
||||
Reference in new issue
Block a user