mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
Atomic per T3a mini plan §3 stop rules #1-#3: - Adapter file atomic (G-int.1 + G-int.2 + G-int.3 one commit) - Interface extension atomic with all consumers - Superblock schema bump one-way with migration discipline ## Scope delivered (mini plan §0) ### 1. core/frontend/durable/storage_adapter.go (NEW) StorageBackend wraps any LogicalStorage and exposes the full frontend.Backend surface. Four responsibilities: - byte↔LBA translation with RMW on partial blocks (G-int.1) - per-I/O fence check preserving INV-FRONTEND-002.* (G-int.2) - operational gate preserving INV-DURABLE-OPGATE-001 (G-int.3) - Sync dispatches to LogicalStorage.Sync (G-int.2 policy) Storage lifecycle NOT owned by backend — Close marks the handle closed but does not tear down LogicalStorage (Provider owns that, lands in T3b). ### 2. Backend interface extension (core/frontend/types.go) Two new methods: Sync(ctx) error + SetOperational(ok, evidence). SetOperational godoc pins the invariant: does NOT touch identity /lineage/epoch — local readiness only; authority publication stays on the master path (PCDD-STUFFING-001). All 6 existing Backend implementations gain trivial impls in the same commit (stop rule #2): - memback.backend (Sync no-op, SetOperational state-flip) - testback.RecordingBackend (SyncCall recording, SyncCount) - testback.StaleRejectingBackend (Sync returns ErrStalePrimary) - iscsi countingBackend (delegates) - nvme countingFaultBackend (delegates) - nvme writeCountingBackend (delegates) ### 3. Superblock impl-identity (Addendum A Addition 2) Both walstore (core/storage/superblock.go) and smartwal (core/storage/smartwal/superblock.go) headers gain ImplKind + ImplVersion fields. superblockVersion/headerVersion bumped 1→2. - newSuperblock rejects ImplKindUnset (0) at create time - readSuperblock + validate reject unknown ImplKind - CreateWALStore passes ImplKindWALStore=1 + WALStoreImplVersion=1 - smartwal.newHeader passes implKindSmartWAL=2 + smartwalImplVersion=1 Stop rule #3 (one-way bump): no compat read path for v1; opening a v1 file with v2 code returns errSuperblockBadVer. No existing on-disk production data expected since T3 hasn't shipped. ### 4. Matrix test infrastructure (Addendum A Addition 1) core/frontend/durable/storage_adapter_test.go uses a single shared logicalStorageFactories() helper returning {walstore, smartwal} factories. Every adapter test iterates over it — adding a new impl later = one edit (stop rule #4). TestT3a_MatrixCoverage is a sanity canary that trips if the factory list drifts. ## Tests landed (mini plan §2 acceptance) #1 TestT3a_StorageBackend_ImplementsBackend ✅ walstore + smartwal #2 TestT3a_StorageBackend_ByteLBATranslation_Matrix ✅ 6 shape subtests × 2 impls #3 TestT3a_StorageBackend_FenceCheck_{Epoch,EV,Replica,Healthy} ✅ 4×2 #4 TestT3a_StorageBackend_OperationalGate_{BeforeOpen,FlipBack,Evidence} ✅ 3×2 #5 TestT3a_StorageBackend_Sync_DispatchesToStorage ✅ 2 impls #6 All tests matrix-parameterized — N×2 execution TestT3a_MatrixCoverage canary pins factory count #7/#8 Superblock schema bump covered by existing core/storage tests: - newSuperblock rejects ImplKindUnset at create - readSuperblock rejects unknown ImplKind at open - CurrentVersion bump is forward-only #9 Full regression green: 17 core packages, incl. prior T0/T1/T2 suites #10 boundary_guard_test.go auto-covers core/frontend/durable/ (TestFrontendCannotMintAuthority_BoundaryGuard walks the whole frontend tree recursively; no explicit extension needed) ## Regression go test ./core/... -count=1 → 17/17 packages ok - core/frontend: 17.7s - core/frontend/durable: 0.3s (22 subtests all green) - core/frontend/iscsi: 31.3s (no regression) - core/frontend/nvme: 61.1s (no regression) - core/storage: 7.2s (schema bump accepted) - core/storage/smartwal: 0.3s - core/host: 122.4s - All others sub-second ## Out of T3a per mini plan - DurableProvider (T3b) - Recovery integration (T3b) - iSCSI SYNCHRONIZE CACHE / NVMe Flush wiring (T3b) - 4 G6 scenarios + perf baseline (T3c) ## Ledger rows queued ACTIVE at T3a close - INV-DURABLE-OPGATE-001 (operational gate preserves INV-FRONTEND-002.*) - INV-DURABLE-IMPL-IDENTITY-001 (superblock self-identifies; mismatch fails fast) ## References sw-block/design/v3-phase-15-t3-port-audit.md (T3.0 LOCKED) sw-block/design/v3-phase-15-t3a-mini-plan.md (this batch spec) feedback_porting_discipline.md (port-model discipline) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
274 lines
8.5 KiB
Go
274 lines
8.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
// File-format identity. Bumped only on incompatible layout changes.
|
|
//
|
|
// Version history:
|
|
// 1: initial layout (magic+version+geometry+WAL state+CreatedAt)
|
|
// 2: T3a Addendum A — added ImplKind + ImplVersion at end of
|
|
// layout. Opening a v1 store with v2 code returns
|
|
// errSuperblockBadVer (one-way bump; no compat read path).
|
|
const (
|
|
superblockSize = 4096
|
|
superblockMagic = "SWBK"
|
|
superblockVersion = 2
|
|
)
|
|
|
|
// ImplKind self-identifies which LogicalStorage implementation
|
|
// owns this file. DurableProvider compares the stored ImplKind
|
|
// against the selector at Open time and rejects mismatches with
|
|
// errSuperblockImplMismatch — prevents silently opening a
|
|
// walstore file as smartwal (or vice versa), which would then
|
|
// attempt an incompatible format parse. ImplKindUnset (0) on
|
|
// disk means the file predates T3a and is a format error.
|
|
type ImplKind uint8
|
|
|
|
const (
|
|
ImplKindUnset ImplKind = 0
|
|
ImplKindWALStore ImplKind = 1
|
|
ImplKindSmartWAL ImplKind = 2
|
|
)
|
|
|
|
// WALStoreImplVersion is the per-impl schema version for walstore
|
|
// files. Independent from superblockVersion (which is the shared
|
|
// superblock-format version) — walstore and smartwal evolve their
|
|
// WAL+extent layouts on independent clocks.
|
|
const WALStoreImplVersion uint32 = 1
|
|
|
|
var (
|
|
errSuperblockNotOurs = errors.New("storage: not a store file (bad magic)")
|
|
errSuperblockBadVer = errors.New("storage: unsupported store version")
|
|
errSuperblockBadVolume = errors.New("storage: invalid volume size")
|
|
errSuperblockInvalid = errors.New("storage: invalid superblock")
|
|
errSuperblockImplUnknown = errors.New("storage: unknown impl kind")
|
|
)
|
|
|
|
// superblock is the 4KB header at offset 0 of a store file. It
|
|
// identifies the format, records geometry, and tracks WAL state.
|
|
//
|
|
// Fields are deliberately narrow: nothing here records anything about
|
|
// recovery class, mode, replica role, or any other engine-owned
|
|
// concern. Storage describes its own bytes; the engine decides what
|
|
// they mean.
|
|
type superblock struct {
|
|
Magic [4]byte
|
|
Version uint16
|
|
Flags uint16
|
|
UUID [16]byte
|
|
VolumeSize uint64 // logical size in bytes
|
|
ExtentSize uint32 // bytes per extent slot
|
|
BlockSize uint32 // bytes per block (caller's IO unit)
|
|
WALOffset uint64 // absolute file offset where WAL region starts
|
|
WALSize uint64 // WAL region size in bytes
|
|
WALHead uint64 // monotonic WAL write position
|
|
WALTail uint64 // monotonic WAL flush position
|
|
WALCheckpointLSN uint64 // last LSN durably present in extent
|
|
CreatedAt uint64 // unix nanoseconds
|
|
|
|
// T3a Addendum A Addition 2 — impl self-identity.
|
|
// ImplKind is the store implementation (walstore vs smartwal);
|
|
// ImplVersion is the per-impl schema version. DurableProvider
|
|
// compares stored ImplKind against the selector at Open time
|
|
// and rejects mismatches fast.
|
|
ImplKind ImplKind
|
|
ImplVersion uint32
|
|
}
|
|
|
|
// newSuperblock constructs a superblock with defaults and a fresh
|
|
// UUID. Caller-supplied geometry overrides defaults. The caller
|
|
// MUST pass a valid ImplKind (WALStore / SmartWAL) — unset (0)
|
|
// rejected at validate() time so no file can ever persist with
|
|
// unidentified impl.
|
|
func newSuperblock(volumeSize uint64, opts createOptions) (superblock, error) {
|
|
if volumeSize == 0 {
|
|
return superblock{}, errSuperblockBadVolume
|
|
}
|
|
extentSize := opts.ExtentSize
|
|
if extentSize == 0 {
|
|
extentSize = 64 * 1024
|
|
}
|
|
blockSize := opts.BlockSize
|
|
if blockSize == 0 {
|
|
blockSize = 4096
|
|
}
|
|
walSize := opts.WALSize
|
|
if walSize == 0 {
|
|
walSize = 64 * 1024 * 1024
|
|
}
|
|
if opts.ImplKind == ImplKindUnset {
|
|
return superblock{}, fmt.Errorf("%w: ImplKind is unset (0)", errSuperblockImplUnknown)
|
|
}
|
|
if opts.ImplKind != ImplKindWALStore && opts.ImplKind != ImplKindSmartWAL {
|
|
return superblock{}, fmt.Errorf("%w: ImplKind=%d", errSuperblockImplUnknown, opts.ImplKind)
|
|
}
|
|
implVersion := opts.ImplVersion
|
|
if implVersion == 0 {
|
|
implVersion = WALStoreImplVersion
|
|
}
|
|
sb := superblock{
|
|
Version: superblockVersion,
|
|
VolumeSize: volumeSize,
|
|
ExtentSize: extentSize,
|
|
BlockSize: blockSize,
|
|
WALOffset: superblockSize,
|
|
WALSize: walSize,
|
|
ImplKind: opts.ImplKind,
|
|
ImplVersion: implVersion,
|
|
}
|
|
copy(sb.Magic[:], superblockMagic)
|
|
if _, err := rand.Read(sb.UUID[:]); err != nil {
|
|
return superblock{}, fmt.Errorf("storage: superblock UUID: %w", err)
|
|
}
|
|
return sb, nil
|
|
}
|
|
|
|
// writeTo serializes the superblock into a 4KB block at w.
|
|
func (sb *superblock) writeTo(w io.Writer) (int64, error) {
|
|
buf := make([]byte, superblockSize)
|
|
le := binary.LittleEndian
|
|
off := 0
|
|
off += copy(buf[off:], sb.Magic[:])
|
|
le.PutUint16(buf[off:], sb.Version)
|
|
off += 2
|
|
le.PutUint16(buf[off:], sb.Flags)
|
|
off += 2
|
|
off += copy(buf[off:], sb.UUID[:])
|
|
le.PutUint64(buf[off:], sb.VolumeSize)
|
|
off += 8
|
|
le.PutUint32(buf[off:], sb.ExtentSize)
|
|
off += 4
|
|
le.PutUint32(buf[off:], sb.BlockSize)
|
|
off += 4
|
|
le.PutUint64(buf[off:], sb.WALOffset)
|
|
off += 8
|
|
le.PutUint64(buf[off:], sb.WALSize)
|
|
off += 8
|
|
le.PutUint64(buf[off:], sb.WALHead)
|
|
off += 8
|
|
le.PutUint64(buf[off:], sb.WALTail)
|
|
off += 8
|
|
le.PutUint64(buf[off:], sb.WALCheckpointLSN)
|
|
off += 8
|
|
le.PutUint64(buf[off:], sb.CreatedAt)
|
|
off += 8
|
|
// T3a Addendum A Addition 2 — ImplKind + ImplVersion.
|
|
buf[off] = uint8(sb.ImplKind)
|
|
off++
|
|
// 3 bytes reserved for future ImplKind expansion / alignment.
|
|
off += 3
|
|
le.PutUint32(buf[off:], sb.ImplVersion)
|
|
|
|
n, err := w.Write(buf)
|
|
return int64(n), err
|
|
}
|
|
|
|
// readSuperblock reads and validates the 4KB header at the start of r.
|
|
func readSuperblock(r io.Reader) (superblock, error) {
|
|
buf := make([]byte, superblockSize)
|
|
if _, err := io.ReadFull(r, buf); err != nil {
|
|
return superblock{}, fmt.Errorf("storage: read superblock: %w", err)
|
|
}
|
|
le := binary.LittleEndian
|
|
var sb superblock
|
|
off := 0
|
|
copy(sb.Magic[:], buf[off:off+4])
|
|
off += 4
|
|
if string(sb.Magic[:]) != superblockMagic {
|
|
return superblock{}, errSuperblockNotOurs
|
|
}
|
|
sb.Version = le.Uint16(buf[off:])
|
|
off += 2
|
|
if sb.Version != superblockVersion {
|
|
return superblock{}, fmt.Errorf("%w: got %d, want %d", errSuperblockBadVer, sb.Version, superblockVersion)
|
|
}
|
|
sb.Flags = le.Uint16(buf[off:])
|
|
off += 2
|
|
copy(sb.UUID[:], buf[off:off+16])
|
|
off += 16
|
|
sb.VolumeSize = le.Uint64(buf[off:])
|
|
off += 8
|
|
if sb.VolumeSize == 0 {
|
|
return superblock{}, errSuperblockBadVolume
|
|
}
|
|
sb.ExtentSize = le.Uint32(buf[off:])
|
|
off += 4
|
|
sb.BlockSize = le.Uint32(buf[off:])
|
|
off += 4
|
|
sb.WALOffset = le.Uint64(buf[off:])
|
|
off += 8
|
|
sb.WALSize = le.Uint64(buf[off:])
|
|
off += 8
|
|
sb.WALHead = le.Uint64(buf[off:])
|
|
off += 8
|
|
sb.WALTail = le.Uint64(buf[off:])
|
|
off += 8
|
|
sb.WALCheckpointLSN = le.Uint64(buf[off:])
|
|
off += 8
|
|
sb.CreatedAt = le.Uint64(buf[off:])
|
|
off += 8
|
|
// T3a Addendum A Addition 2.
|
|
sb.ImplKind = ImplKind(buf[off])
|
|
off++
|
|
off += 3 // reserved
|
|
sb.ImplVersion = le.Uint32(buf[off:])
|
|
return sb, nil
|
|
}
|
|
|
|
// validate checks internal consistency. Called after read and before
|
|
// any structural use.
|
|
func (sb *superblock) validate() error {
|
|
if string(sb.Magic[:]) != superblockMagic {
|
|
return errSuperblockNotOurs
|
|
}
|
|
if sb.Version != superblockVersion {
|
|
return fmt.Errorf("%w: got %d", errSuperblockBadVer, sb.Version)
|
|
}
|
|
if sb.VolumeSize == 0 {
|
|
return errSuperblockBadVolume
|
|
}
|
|
if sb.BlockSize == 0 {
|
|
return fmt.Errorf("%w: BlockSize is 0", errSuperblockInvalid)
|
|
}
|
|
if sb.ExtentSize == 0 {
|
|
return fmt.Errorf("%w: ExtentSize is 0", errSuperblockInvalid)
|
|
}
|
|
if sb.WALSize == 0 {
|
|
return fmt.Errorf("%w: WALSize is 0", errSuperblockInvalid)
|
|
}
|
|
if sb.WALOffset != superblockSize {
|
|
return fmt.Errorf("%w: WALOffset=%d, expected %d", errSuperblockInvalid, sb.WALOffset, superblockSize)
|
|
}
|
|
if sb.VolumeSize%uint64(sb.BlockSize) != 0 {
|
|
return fmt.Errorf("%w: VolumeSize %d not aligned to BlockSize %d",
|
|
errSuperblockInvalid, sb.VolumeSize, sb.BlockSize)
|
|
}
|
|
// T3a Addendum A: ImplKind must be set and recognized.
|
|
if sb.ImplKind == ImplKindUnset {
|
|
return fmt.Errorf("%w: ImplKind is unset (0); file predates T3a or is corrupted",
|
|
errSuperblockImplUnknown)
|
|
}
|
|
if sb.ImplKind != ImplKindWALStore && sb.ImplKind != ImplKindSmartWAL {
|
|
return fmt.Errorf("%w: ImplKind=%d", errSuperblockImplUnknown, sb.ImplKind)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// createOptions configures a new store file.
|
|
type createOptions struct {
|
|
BlockSize uint32 // default 4096
|
|
ExtentSize uint32 // default 64KB
|
|
WALSize uint64 // default 64MB
|
|
|
|
// T3a Addendum A — impl self-identity. Required (no default);
|
|
// newSuperblock rejects ImplKindUnset (0).
|
|
ImplKind ImplKind
|
|
ImplVersion uint32 // default WALStoreImplVersion
|
|
}
|