mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
feat(sw-block): sw-block migrate-v3, the offline v2 -> v3 descriptor rewrite (row 13b)
DESIGN r17.4 section 10.1. The tool (cmd/sw-block/migrate.go) runs the steps, each durable before the next: the read-only exhaustive scan and the geometry check (a refusal changes no byte), the atomic operator backup of the v2 superblock, the initial map sidecar (generation 1, every chunk UNKNOWN, covered = the v2 checkpoint), the prepared slot B at 4096, slot A at phase 0 (the commit point), slot B at phase 0, then the v2 chunk bitmap retirement, and prints the new descriptor hash. A rerun decides from the slot bytes alone (the 10.1 table): a v2 A restarts with a full rescan; a committed A finishes the rollover and the retirement; a committed B only the retirement; a prepared B with an invalid A refuses with ErrMigrationIncomplete; neither valid refuses. It never reads or restores the backup. core/storage/migrate_v3.go is the tool-only export: it reuses the row-1 slot codec and the row-6 map codec (no second encoder), keeps the extent base (new WALSize = old - 4096), and opens or serves nothing. Controls through the actual CLI entry and, tagged, the actual v3 open: a clean migration (extent unchanged, backup, retirement, the old binary refuses, an idempotent rerun, v3 serves the v2 blocks with an all-UNKNOWN map), refusals changing nothing, a crash after each step, the old binary after preparation, a partly and wholly overwritten prepared B, and the backup never restored. Mutants: no clean scan, commit before prepare, retire before the commit, no rollover on rerun, an unrefused incomplete state, a clean initial map. Not yet: the shared advisory lock (12b) and committed raw v2 fixtures (13c). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
54191e188d
commit
7c6a29d809
7 files changed
+933
No files matched your search
@@ -77,6 +77,9 @@ func run(args []string, stdout, stderr io.Writer) int {
|
||||
fmt.Fprintln(stdout, buildinfo.Version("sw-block"))
|
||||
return ops.VolumeStatusExitOK
|
||||
}
|
||||
if args[0] == "migrate-v3" {
|
||||
return runMigrateV3(args[1:], stdout, stderr)
|
||||
}
|
||||
if args[0] != "ops" {
|
||||
fmt.Fprintf(stderr, "sw-block: unknown command %q\n", args[0])
|
||||
usage(stderr)
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/cmd/sw-block/internal/v2legacy"
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// `sw-block migrate-v3` (format bump v3, DESIGN r17.4 section 10.1; plan row
|
||||
// 13b): the offline, in-place v2/impl1 -> v3/impl3 descriptor rewrite of a
|
||||
// cleanly closed volume whose WAL is fully flushed. No record is converted
|
||||
// and no extent byte is touched. Every step is durable before the next.
|
||||
|
||||
// errMigrateNotClean is DESIGN 10.1's ErrMigrateNotClean.
|
||||
var errMigrateNotClean = errors.New("migrate-v3: the volume is not cleanly flushed: open with the old binary, flush, close cleanly, retry")
|
||||
|
||||
// errMigrateNeitherSlot: neither offset 0 nor offset 4096 holds a valid
|
||||
// slot or v2 superblock; nothing is changed.
|
||||
var errMigrateNeitherSlot = errors.New("migrate-v3: no valid v2 superblock or v3 descriptor slot; nothing changed")
|
||||
|
||||
// migrateStepHook, when set by a test, runs after each durable step with
|
||||
// its name; returning an error stops the tool there (a simulated crash).
|
||||
var migrateStepHook func(step string) error
|
||||
|
||||
func runMigrateV3(args []string, stdout, stderr io.Writer) int {
|
||||
fs := flag.NewFlagSet("migrate-v3", flag.ContinueOnError)
|
||||
fs.SetOutput(stderr)
|
||||
chunkBlocks := fs.Uint64("chunk-blocks", 0, "the provider's chunk size in blocks (the map geometry); required")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return 2
|
||||
}
|
||||
if fs.NArg() != 1 || *chunkBlocks == 0 {
|
||||
fmt.Fprintln(stderr, "usage: sw-block migrate-v3 --chunk-blocks N <volume-file>")
|
||||
return 2
|
||||
}
|
||||
if err := migrateV3(fs.Arg(0), *chunkBlocks, stdout); err != nil {
|
||||
fmt.Fprintln(stderr, err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func migrateV3(path string, chunkBlocks uint64, out io.Writer) error {
|
||||
f, err := os.OpenFile(path, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
head := make([]byte, 2*storage.MigrationSlotSize)
|
||||
if _, err := f.ReadAt(head, 0); err != nil {
|
||||
return fmt.Errorf("migrate-v3: read the descriptor region: %w", err)
|
||||
}
|
||||
a, b := head[:storage.MigrationSlotSize], head[storage.MigrationSlotSize:]
|
||||
sb, v2err := v2legacy.ReadSuperblock(a)
|
||||
switch {
|
||||
case v2err == nil:
|
||||
// Not committed: restart from step 1 with a full rescan (the old
|
||||
// binary may have run since; nothing earlier is trusted).
|
||||
return migrateFromV2(f, path, sb, chunkBlocks, out)
|
||||
case errors.Is(v2err, v2legacy.ErrImplUnsupported):
|
||||
return fmt.Errorf("migrate-v3: %w (an impl-2 volume stays on its old binary)", v2err)
|
||||
}
|
||||
stA, genA := storage.ClassifyMigrationSlotV3(a)
|
||||
stB, _ := storage.ClassifyMigrationSlotV3(b)
|
||||
switch {
|
||||
case stA == storage.MigrationSlotCommitted:
|
||||
// Committed. Finish step 6 if B was never rewritten, then step 7.
|
||||
if genA == 2 && stB != storage.MigrationSlotCommitted {
|
||||
rb, err := storage.RolloverSlotV3(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := pwriteSync(f, rb, storage.MigrationSlotBOffset); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return retireC11(path)
|
||||
case stB == storage.MigrationSlotCommitted:
|
||||
// Committed and possibly in use: only step 7.
|
||||
return retireC11(path)
|
||||
case stB == storage.MigrationSlotPrepared:
|
||||
// A torn step 5 or an old-binary run after preparation followed by
|
||||
// damage to A: the bytes cannot tell; never guess.
|
||||
return fmt.Errorf("migrate-v3: %w; nothing changed (an operator decision, using the backup)", storage.ErrMigrationIncomplete)
|
||||
default:
|
||||
return errMigrateNeitherSlot
|
||||
}
|
||||
}
|
||||
|
||||
func migrateFromV2(f *os.File, path string, sb v2legacy.Superblock, chunkBlocks uint64, out io.Writer) error {
|
||||
// Step 1: precondition 3, read-only and before any write.
|
||||
if sb.WALHead != sb.WALTail {
|
||||
return fmt.Errorf("%w (WALHead %d != WALTail %d)", errMigrateNotClean, sb.WALHead, sb.WALTail)
|
||||
}
|
||||
wal := make([]byte, sb.WALSize)
|
||||
if _, err := f.ReadAt(wal, int64(sb.WALOffset)); err != nil {
|
||||
return fmt.Errorf("migrate-v3: read the v2 WAL: %w", err)
|
||||
}
|
||||
if found := v2legacy.ScanClean(wal, sb.WALCheckpointLSN); len(found) > 0 {
|
||||
return fmt.Errorf("%w (%d unflushed record(s); first at WAL offset %d: %s)", errMigrateNotClean, len(found), found[0].Offset, found[0].Reason)
|
||||
}
|
||||
src := storage.MigrationSourceV3{
|
||||
UUID: sb.UUID, VolumeSize: sb.VolumeSize, ExtentSize: sb.ExtentSize, BlockSize: sb.BlockSize,
|
||||
OldWALOffset: sb.WALOffset, OldWALSize: sb.WALSize, CreatedAt: sb.CreatedAt,
|
||||
CheckpointLSN: sb.WALCheckpointLSN, ChunkBlocks: chunkBlocks,
|
||||
}
|
||||
if err := storage.ValidateMigrationV3(src); err != nil { // precondition 4
|
||||
return err
|
||||
}
|
||||
if err := step("scanned"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Step 2: the atomic operator backup of the v2 superblock.
|
||||
raw := make([]byte, v2legacy.SuperblockSize)
|
||||
if _, err := f.ReadAt(raw, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writeFileAtomic(path+".v2-superblock.bak", raw); err != nil {
|
||||
return fmt.Errorf("migrate-v3: backup: %w", err)
|
||||
}
|
||||
if err := step("backup"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Step 3: the initial map sidecar (all UNKNOWN), durable.
|
||||
plan, err := storage.PrepareMigrationV3(path+".chunkmap", src)
|
||||
if err != nil {
|
||||
return fmt.Errorf("migrate-v3: initial map: %w", err)
|
||||
}
|
||||
if err := syncDirOf(path); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := step("map"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Step 4: the prepared slot B, over the old WAL's first 4 KiB.
|
||||
if err := pwriteSync(f, plan.PreparedB, storage.MigrationSlotBOffset); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := step("prepared"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Step 5: slot A, phase 0: the commit point.
|
||||
if err := pwriteSync(f, plan.CommittedA, storage.MigrationSlotAOffset); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := step("committed"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Step 6: slot B rewritten at phase 0 (two-slot redundancy).
|
||||
if err := pwriteSync(f, plan.RolloverB, storage.MigrationSlotBOffset); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := step("rollover"); err != nil {
|
||||
return err
|
||||
}
|
||||
// Step 7: retire the v2 C11 bitmap, only after the commit.
|
||||
if err := retireC11(path); err != nil {
|
||||
return err
|
||||
}
|
||||
// Step 8.
|
||||
fmt.Fprintf(out, "migrated %s to v3: descriptor sha256 %s; backups left in place\n", path, hex.EncodeToString(plan.DescriptorHash[:]))
|
||||
return nil
|
||||
}
|
||||
|
||||
func step(name string) error {
|
||||
if h := migrateStepHook; h != nil {
|
||||
return h(name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func pwriteSync(f *os.File, b []byte, off int64) error {
|
||||
if _, err := f.WriteAt(b, off); err != nil {
|
||||
return fmt.Errorf("migrate-v3: write at %d: %w", off, err)
|
||||
}
|
||||
if err := f.Sync(); err != nil {
|
||||
return fmt.Errorf("migrate-v3: fsync: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeFileAtomic writes b to name via name.tmp, fsync, rename and a
|
||||
// directory fsync; a leftover .tmp from an earlier run is replaced.
|
||||
func writeFileAtomic(name string, b []byte) error {
|
||||
tmp := name + ".tmp"
|
||||
if err := os.Remove(tmp); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
t, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := t.Write(b); err != nil {
|
||||
t.Close()
|
||||
return err
|
||||
}
|
||||
if err := t.Sync(); err != nil {
|
||||
t.Close()
|
||||
return err
|
||||
}
|
||||
if err := t.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(tmp, name); err != nil {
|
||||
return err
|
||||
}
|
||||
return syncDirOf(name)
|
||||
}
|
||||
|
||||
// retireC11 is step 7: rename a v2 .chunkbitmap sidecar to
|
||||
// <file>.v2-chunkbitmap.bak and fsync the directory. Absent is done.
|
||||
func retireC11(path string) error {
|
||||
err := os.Rename(path+".chunkbitmap", path+".v2-chunkbitmap.bak")
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("migrate-v3: retire the v2 chunk bitmap: %w", err)
|
||||
}
|
||||
return syncDirOf(path)
|
||||
}
|
||||
|
||||
// syncDirOf fsyncs the directory holding path. A directory cannot be
|
||||
// fsynced on Windows; there it is a no-op, as in the provider.
|
||||
func syncDirOf(path string) error {
|
||||
if runtime.GOOS == "windows" {
|
||||
return nil
|
||||
}
|
||||
d, err := os.Open(filepath.Dir(path))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer d.Close()
|
||||
return d.Sync()
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//go:build swblock_format_v3_test
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// Row 13b through the actual v3 open (tag swblock_format_v3_test).
|
||||
|
||||
// A migrated volume opens as v3 and serves the v2 blocks unchanged; its
|
||||
// map is the initial one (all UNKNOWN, covered = the v2 checkpoint); a
|
||||
// v3 write and a reopen work. Mutant: an initial map with clean entries
|
||||
// fails here.
|
||||
func TestMigrateV3VolumeOpensAsV3(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
if code, msg := migrate(t, path); code != 0 {
|
||||
t.Fatalf("migrate: %s", msg)
|
||||
}
|
||||
s, err := storage.OpenMappedWALStoreV3Test(path)
|
||||
if err != nil {
|
||||
t.Fatalf("v3 open of a migrated volume: %v", err)
|
||||
}
|
||||
if _, err := s.Recover(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
covered, allUnknown := storage.MapCoverageV3Test(s)
|
||||
if !allUnknown || covered != s.CheckpointLSN() {
|
||||
t.Fatalf("initial map: covered %d (checkpoint %d), all UNKNOWN %v", covered, s.CheckpointLSN(), allUnknown)
|
||||
}
|
||||
for lba := uint32(0); lba < 4; lba++ {
|
||||
got, err := s.Read(lba)
|
||||
if err != nil || !bytes.Equal(got, migBlk(0x10+byte(lba))) {
|
||||
t.Fatalf("LBA %d after migration: err %v", lba, err)
|
||||
}
|
||||
}
|
||||
if _, err := s.Write(7, migBlk(0x77)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := storage.OpenMappedWALStoreV3Test(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer r.Close()
|
||||
if _, err := r.Recover(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, _ := r.Read(7); !bytes.Equal(got, migBlk(0x77)) {
|
||||
t.Fatal("a v3 write after migration did not survive a reopen")
|
||||
}
|
||||
}
|
||||
|
||||
// A prepared-only volume (stopped before the commit, A then damaged) is
|
||||
// refused by v3 open with ErrMigrationIncomplete, as by the tool.
|
||||
func TestMigrateV3PreparedOnlyRefusedByV3Open(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
migrateStepHook = func(s string) error {
|
||||
if s == "prepared" {
|
||||
return errors.New("stop")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
migrate(t, path)
|
||||
migrateStepHook = nil
|
||||
damageSlotA(t, path)
|
||||
if s, err := storage.OpenMappedWALStoreV3Test(path); !errors.Is(err, storage.ErrMigrationIncomplete) {
|
||||
if s != nil {
|
||||
_ = s.Close()
|
||||
}
|
||||
t.Fatalf("v3 open of a prepared-only volume: err = %v, want ErrMigrationIncomplete", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,391 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/cmd/sw-block/internal/v2legacy"
|
||||
"github.com/seaweedfs/seaweed-block/core/storage"
|
||||
)
|
||||
|
||||
// Plan row 13b controls (DESIGN r17.4 section 10.1; owner claude03), all
|
||||
// through the actual CLI entry (run).
|
||||
|
||||
const migBlock = 4096
|
||||
|
||||
func migBlk(b byte) []byte { return bytes.Repeat([]byte{b}, migBlock) }
|
||||
|
||||
// v2Volume creates a real v2/impl1 volume with 4 written blocks; clean
|
||||
// closes it (flushing), otherwise it is left synced but unflushed.
|
||||
func v2Volume(t *testing.T, clean bool) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "vol")
|
||||
s, err := storage.CreateWALStore(path, 64, migBlock)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !clean {
|
||||
s.DisableAutoFlushForRecoveryTest()
|
||||
}
|
||||
for lba := uint32(0); lba < 4; lba++ {
|
||||
if _, err := s.Write(lba, migBlk(0x10+byte(lba))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := s.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if clean {
|
||||
if err := s.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func migrate(t *testing.T, path string) (int, string) {
|
||||
t.Helper()
|
||||
var out, errb bytes.Buffer
|
||||
code := run([]string{"migrate-v3", "--chunk-blocks", "4", path}, &out, &errb)
|
||||
return code, out.String() + errb.String()
|
||||
}
|
||||
|
||||
// snapshotFiles reads the volume and every sidecar next to it.
|
||||
func snapshotFiles(t *testing.T, path string) map[string][]byte {
|
||||
t.Helper()
|
||||
got := map[string][]byte{}
|
||||
matches, _ := filepath.Glob(path + "*")
|
||||
for _, m := range matches {
|
||||
b, err := os.ReadFile(m)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got[filepath.Base(m)] = b
|
||||
}
|
||||
return got
|
||||
}
|
||||
|
||||
func sameFiles(a, b map[string][]byte) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for k, v := range a {
|
||||
if !bytes.Equal(v, b[k]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// A clean v2 volume migrates: slot A committed (slotGen 2), slot B rolled
|
||||
// over (slotGen 3), every extent byte unchanged, the backup holds the old
|
||||
// superblock, a v2 chunk bitmap is retired, the old binary now refuses the
|
||||
// file, and a rerun changes no byte.
|
||||
func TestMigrateV3CleanVolume(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
if err := os.WriteFile(path+".chunkbitmap", []byte("c11"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sb, err := v2legacy.ReadSuperblock(before)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
extentBase := sb.WALOffset + sb.WALSize
|
||||
if code, msg := migrate(t, path); code != 0 || !strings.Contains(msg, "descriptor sha256") {
|
||||
t.Fatalf("migrate: exit %d: %s", code, msg)
|
||||
}
|
||||
after, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(after[extentBase:], before[extentBase:]) {
|
||||
t.Fatal("the migration changed extent bytes")
|
||||
}
|
||||
if st, gen := storage.ClassifyMigrationSlotV3(after[:4096]); st != storage.MigrationSlotCommitted || gen != 2 {
|
||||
t.Fatalf("slot A: state %d gen %d, want committed gen 2", st, gen)
|
||||
}
|
||||
if st, gen := storage.ClassifyMigrationSlotV3(after[4096:8192]); st != storage.MigrationSlotCommitted || gen != 3 {
|
||||
t.Fatalf("slot B: state %d gen %d, want committed gen 3", st, gen)
|
||||
}
|
||||
if bak, err := os.ReadFile(path + ".v2-superblock.bak"); err != nil || !bytes.Equal(bak, before[:4096]) {
|
||||
t.Fatalf("backup: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(path + ".chunkbitmap"); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatal("the v2 chunk bitmap was not retired")
|
||||
}
|
||||
if _, err := os.Stat(path + ".v2-chunkbitmap.bak"); err != nil {
|
||||
t.Fatalf("retired bitmap: %v", err)
|
||||
}
|
||||
if s, err := storage.OpenWALStore(path); err == nil {
|
||||
_ = s.Close()
|
||||
t.Fatal("the old binary opened a migrated volume")
|
||||
}
|
||||
snap := snapshotFiles(t, path)
|
||||
if code, msg := migrate(t, path); code != 0 {
|
||||
t.Fatalf("rerun: exit %d: %s", code, msg)
|
||||
}
|
||||
if !sameFiles(snap, snapshotFiles(t, path)) {
|
||||
t.Fatal("a rerun on a committed volume changed bytes")
|
||||
}
|
||||
}
|
||||
|
||||
// Refusals change no byte of the volume or any sidecar: an unflushed WAL
|
||||
// (ErrMigrateNotClean with the operator remedy), an impl-2 volume, and a
|
||||
// file with neither a v2 superblock nor a valid v3 slot. Mutant: skipping
|
||||
// the clean scan migrates the unflushed volume and fails here.
|
||||
func TestMigrateV3RefusalsChangeNothing(t *testing.T) {
|
||||
t.Run("unflushed WAL", func(t *testing.T) {
|
||||
path := v2Volume(t, false)
|
||||
snap := snapshotFiles(t, path)
|
||||
code, msg := migrate(t, path)
|
||||
if code == 0 || !strings.Contains(msg, "open with the old binary, flush, close cleanly, retry") {
|
||||
t.Fatalf("exit %d: %s", code, msg)
|
||||
}
|
||||
if !sameFiles(snap, snapshotFiles(t, path)) {
|
||||
t.Fatal("a refused migration changed bytes")
|
||||
}
|
||||
})
|
||||
t.Run("impl 2", func(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
f, err := os.OpenFile(path, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.WriteAt([]byte{2, 0, 0, 0}, 92); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
snap := snapshotFiles(t, path)
|
||||
if code, _ := migrate(t, path); code == 0 || !sameFiles(snap, snapshotFiles(t, path)) {
|
||||
t.Fatal("an impl-2 volume was not refused unchanged")
|
||||
}
|
||||
})
|
||||
t.Run("neither valid", func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "junk")
|
||||
if err := os.WriteFile(path, bytes.Repeat([]byte{0xEE}, 3*4096), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snap := snapshotFiles(t, path)
|
||||
if code, _ := migrate(t, path); code == 0 || !sameFiles(snap, snapshotFiles(t, path)) {
|
||||
t.Fatal("neither-valid was not refused unchanged")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A crash after each step is resumable from the slot bytes alone: before
|
||||
// the commit the rerun restarts with a full rescan; after it, the rerun
|
||||
// only finishes the rollover and the bitmap retirement.
|
||||
func TestMigrateV3CrashAfterEachStepIsResumable(t *testing.T) {
|
||||
for _, stop := range []string{"scanned", "backup", "map", "prepared", "committed", "rollover"} {
|
||||
t.Run(stop, func(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
if err := os.WriteFile(path+".chunkbitmap", []byte("c11"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
migrateStepHook = func(s string) error {
|
||||
if s == stop {
|
||||
return errors.New("simulated crash after " + s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
code, _ := migrate(t, path)
|
||||
migrateStepHook = nil
|
||||
if code == 0 {
|
||||
t.Fatal("the simulated crash did not stop the tool")
|
||||
}
|
||||
// Before the commit point (step 5) slot A is still the v2
|
||||
// superblock and the v2 bitmap is not retired; after it A is a
|
||||
// committed v3 slot.
|
||||
crashed, _ := os.ReadFile(path)
|
||||
_, v2err := v2legacy.ReadSuperblock(crashed[:4096])
|
||||
_, bitmapErr := os.Stat(path + ".chunkbitmap")
|
||||
if committed := stop == "committed" || stop == "rollover"; committed {
|
||||
if st, _ := storage.ClassifyMigrationSlotV3(crashed[:4096]); st != storage.MigrationSlotCommitted {
|
||||
t.Fatalf("crash after %s: slot A state %d, want committed", stop, st)
|
||||
}
|
||||
} else if v2err != nil || bitmapErr != nil {
|
||||
t.Fatalf("crash after %s: A still v2 = %v, bitmap present = %v; want both before the commit", stop, v2err == nil, bitmapErr == nil)
|
||||
}
|
||||
if code, msg := migrate(t, path); code != 0 {
|
||||
t.Fatalf("rerun after a crash at %s: exit %d: %s", stop, code, msg)
|
||||
}
|
||||
raw, _ := os.ReadFile(path)
|
||||
if st, _ := storage.ClassifyMigrationSlotV3(raw[:4096]); st != storage.MigrationSlotCommitted {
|
||||
t.Fatalf("after the rerun slot A state %d, want committed", st)
|
||||
}
|
||||
if st, _ := storage.ClassifyMigrationSlotV3(raw[4096:8192]); st != storage.MigrationSlotCommitted {
|
||||
t.Fatalf("after the rerun slot B state %d, want committed", st)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The old binary runs after preparation (A is still v2, so it opens): the
|
||||
// rerun finds A still v2 and restarts with a full rescan; it refuses while
|
||||
// the old binary's writes are unflushed and migrates once it flushed and
|
||||
// closed. A prepared B with a damaged A is ambiguous and refused with
|
||||
// ErrMigrationIncomplete, writing nothing.
|
||||
func TestMigrateV3OldBinaryAfterPreparation(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
migrateStepHook = func(s string) error {
|
||||
if s == "prepared" {
|
||||
return errors.New("stop after preparation")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
migrate(t, path)
|
||||
migrateStepHook = nil
|
||||
s, err := storage.OpenWALStore(path)
|
||||
if err != nil {
|
||||
t.Fatalf("the old binary refused a prepared-only volume: %v", err)
|
||||
}
|
||||
s.DisableAutoFlushForRecoveryTest()
|
||||
if _, err := s.Write(9, migBlk(0x99)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snap := snapshotFiles(t, path)
|
||||
if code, msg := migrate(t, path); code == 0 || !strings.Contains(msg, "not cleanly flushed") {
|
||||
t.Fatalf("rerun over the old binary's unflushed write: exit %d: %s", code, msg)
|
||||
}
|
||||
if !sameFiles(snap, snapshotFiles(t, path)) {
|
||||
t.Fatal("a refused rerun changed bytes")
|
||||
}
|
||||
_ = s.Close() // stopped without a flush: still not clean
|
||||
if code, _ := migrate(t, path); code == 0 {
|
||||
t.Fatal("the rerun migrated a volume the old binary left unflushed")
|
||||
}
|
||||
// The operator remedy: open with the old binary, flush, close cleanly.
|
||||
s2, err := storage.OpenWALStore(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s2.Recover(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s2.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code, msg := migrate(t, path); code != 0 {
|
||||
t.Fatalf("rerun after the old binary closed cleanly: exit %d: %s", code, msg)
|
||||
}
|
||||
|
||||
t.Run("prepared B with a damaged A", func(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
migrateStepHook = func(s string) error {
|
||||
if s == "prepared" {
|
||||
return errors.New("stop after preparation")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
migrate(t, path)
|
||||
migrateStepHook = nil
|
||||
f, _ := os.OpenFile(path, os.O_RDWR, 0)
|
||||
_, _ = f.WriteAt(bytes.Repeat([]byte{0xEE}, 4096), 0)
|
||||
f.Close()
|
||||
snap := snapshotFiles(t, path)
|
||||
code, msg := migrate(t, path)
|
||||
if code == 0 || !strings.Contains(msg, storage.ErrMigrationIncomplete.Error()) || !sameFiles(snap, snapshotFiles(t, path)) {
|
||||
t.Fatalf("exit %d: %s", code, msg)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// The tool never reads or restores its backup: a committed volume reruns
|
||||
// to success with the backup damaged and its slots untouched.
|
||||
func TestMigrateV3NeverRestoresTheBackup(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
if code, msg := migrate(t, path); code != 0 {
|
||||
t.Fatalf("migrate: %s", msg)
|
||||
}
|
||||
if err := os.WriteFile(path+".v2-superblock.bak", []byte("damaged"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, _ := os.ReadFile(path)
|
||||
if code, msg := migrate(t, path); code != 0 {
|
||||
t.Fatalf("rerun: %s", msg)
|
||||
}
|
||||
now, _ := os.ReadFile(path)
|
||||
if !bytes.Equal(raw, now) {
|
||||
t.Fatal("a rerun changed the committed volume")
|
||||
}
|
||||
}
|
||||
|
||||
// The old v2 WAL starts at offset 4096, over the prepared slot B, so an
|
||||
// old-binary run can overwrite B partly or wholly (codex03 r8). Either way B
|
||||
// is an invalid slot, never prepared or committed: with A still v2 the rerun
|
||||
// restarts and migrates; with A damaged too, neither slot is valid and the
|
||||
// rerun refuses with no byte changed.
|
||||
func TestMigrateV3OldWALOverwritesThePreparedSlot(t *testing.T) {
|
||||
for _, n := range []int{100, 4096} {
|
||||
for _, damageA := range []bool{false, true} {
|
||||
name := map[int]string{100: "partial", 4096: "full"}[n]
|
||||
if damageA {
|
||||
name += " with A damaged"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
path := v2Volume(t, true)
|
||||
migrateStepHook = func(s string) error {
|
||||
if s == "prepared" {
|
||||
return errors.New("stop after preparation")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
migrate(t, path)
|
||||
migrateStepHook = nil
|
||||
f, err := os.OpenFile(path, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.WriteAt(bytes.Repeat([]byte{0xA5}, n), 4096); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if damageA {
|
||||
if _, err := f.WriteAt(bytes.Repeat([]byte{0xEE}, 4096), 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
f.Close()
|
||||
raw, _ := os.ReadFile(path)
|
||||
if st, _ := storage.ClassifyMigrationSlotV3(raw[4096:8192]); st != storage.MigrationSlotInvalid {
|
||||
t.Fatalf("an overwritten B classifies as %d, want invalid", st)
|
||||
}
|
||||
snap := snapshotFiles(t, path)
|
||||
code, msg := migrate(t, path)
|
||||
if damageA {
|
||||
if code == 0 || !sameFiles(snap, snapshotFiles(t, path)) {
|
||||
t.Fatalf("neither valid: exit %d, bytes changed: %s", code, msg)
|
||||
}
|
||||
return
|
||||
}
|
||||
if code != 0 {
|
||||
t.Fatalf("rerun with A still v2: exit %d: %s", code, msg)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func damageSlotA(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
f, err := os.OpenFile(path, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := f.WriteAt(bytes.Repeat([]byte{0xEE}, 4096), 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -58,3 +58,17 @@ func CrashReopenV3Test(s *WALStore, path string) (*WALStore, error) {
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// MapCoverageV3Test reports the bound map root's covered checkpoint and
|
||||
// whether every chunk entry is UNKNOWN (the row 13 initial-map control).
|
||||
func MapCoverageV3Test(s *WALStore) (covered uint64, allUnknown bool) {
|
||||
s.v3.pub.descMu.Lock()
|
||||
defer s.v3.pub.descMu.Unlock()
|
||||
allUnknown = true
|
||||
for _, e := range s.v3.pub.mapCurrent.entries {
|
||||
if !e.Unknown {
|
||||
allUnknown = false
|
||||
}
|
||||
}
|
||||
return s.v3.pub.cur.MapCoveredCheckpoint, allUnknown
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Tool-only exports for the offline migration `sw-block migrate-v3` (format
|
||||
// bump v3, DESIGN r17.4 section 10.1; plan row 13b). They reuse the row-1
|
||||
// slot codec and the row-6 map codec; there is no second encoder. Nothing
|
||||
// here opens, recovers or serves a store, and the v3 open code calls none of
|
||||
// it.
|
||||
|
||||
var (
|
||||
// ErrMigrateGeometry: the v2 geometry cannot be expressed as v3 without
|
||||
// moving data (DESIGN 10.1 precondition 4).
|
||||
ErrMigrateGeometry = errors.New("storage: v2 geometry cannot be migrated in place to v3")
|
||||
)
|
||||
|
||||
// MigrationSlotAOffset and MigrationSlotBOffset are the v3 slot positions.
|
||||
const (
|
||||
MigrationSlotAOffset = slotAOffsetV3
|
||||
MigrationSlotBOffset = slotBOffsetV3
|
||||
MigrationSlotSize = slotSizeV3
|
||||
// migratedWALMinV3 is the provisional impl-3 minimum WAL size for a
|
||||
// migrated volume: at least 64 KiB and two single-block records.
|
||||
migratedWALMinV3 = 64 * 1024
|
||||
)
|
||||
|
||||
// MigrationSourceV3 is what the tool read from the v2 superblock.
|
||||
type MigrationSourceV3 struct {
|
||||
UUID [16]byte
|
||||
VolumeSize uint64
|
||||
ExtentSize uint32
|
||||
BlockSize uint32
|
||||
OldWALOffset uint64 // must be 4096
|
||||
OldWALSize uint64
|
||||
CreatedAt uint64
|
||||
CheckpointLSN uint64
|
||||
ChunkBlocks uint64 // the map's chunk geometry (the provider's)
|
||||
}
|
||||
|
||||
// MigrationPlanV3 is the v3 image of a migration: the descriptor slot
|
||||
// images in their write order and the new descriptor's hash.
|
||||
type MigrationPlanV3 struct {
|
||||
PreparedB []byte // step 4: slot B, slotGen 1, MigrationPhase prepared
|
||||
CommittedA []byte // step 5: slot A, slotGen 2, phase 0 (the commit point)
|
||||
RolloverB []byte // step 6: slot B, slotGen 3, phase 0
|
||||
// DescriptorHash is the SHA-256 of CommittedA, printed at step 8.
|
||||
DescriptorHash [32]byte
|
||||
}
|
||||
|
||||
// migrationDescriptorV3 is the v3 descriptor for src (DESIGN 10.1 step 4):
|
||||
// WALOffset 8192 and WALSize = old WALSize - 4096, so the extent base
|
||||
// WALOffset + WALSize is unchanged; CheckpointLSN = W = the v2 checkpoint;
|
||||
// an empty cursor space; BaseState none; WMode C.
|
||||
func migrationDescriptorV3(src MigrationSourceV3) (descriptorV3, error) {
|
||||
if src.OldWALOffset != superblockSize {
|
||||
return descriptorV3{}, fmt.Errorf("%w: v2 WALOffset %d, want %d", ErrMigrateGeometry, src.OldWALOffset, superblockSize)
|
||||
}
|
||||
if src.BlockSize == 0 || src.OldWALSize <= slotSizeV3 {
|
||||
return descriptorV3{}, fmt.Errorf("%w: v2 WAL size %d", ErrMigrateGeometry, src.OldWALSize)
|
||||
}
|
||||
walSize := src.OldWALSize - slotSizeV3
|
||||
if walSize%uint64(src.BlockSize) != 0 || walSize < migratedWALMinV3 ||
|
||||
walSize < 2*(walEntryHeaderSizeV3+uint64(src.BlockSize)) {
|
||||
return descriptorV3{}, fmt.Errorf("%w: v3 WAL size %d is not block aligned or below the minimum", ErrMigrateGeometry, walSize)
|
||||
}
|
||||
if src.CheckpointLSN > counterMaxV3 {
|
||||
return descriptorV3{}, fmt.Errorf("%w: checkpoint %d", ErrMigrateGeometry, src.CheckpointLSN)
|
||||
}
|
||||
d := descriptorV3{
|
||||
ImplKind: ImplKindWALStore, ImplVersion: walStoreImplVersion3, UUID: src.UUID,
|
||||
VolumeSize: src.VolumeSize, ExtentSize: src.ExtentSize, BlockSize: src.BlockSize,
|
||||
WALOffset: walOffsetV3, WALSize: walSize, CreatedAt: src.CreatedAt,
|
||||
CheckpointLSN: src.CheckpointLSN, W: src.CheckpointLSN, WMode: WModeLagged,
|
||||
MapFormatID: mapFormatIDV1, MapGeneration: 1, SlotGen: 1,
|
||||
}
|
||||
if walOffsetV3+walSize != src.OldWALOffset+src.OldWALSize {
|
||||
return descriptorV3{}, fmt.Errorf("%w: the extent base would move", ErrMigrateGeometry)
|
||||
}
|
||||
if _, err := mapShapeForV3(d, src.ChunkBlocks); err != nil {
|
||||
return descriptorV3{}, fmt.Errorf("%w: map geometry: %w", ErrMigrateGeometry, err)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// ValidateMigrationV3 checks precondition 4 (geometry) without writing.
|
||||
func ValidateMigrationV3(src MigrationSourceV3) error {
|
||||
_, err := migrationDescriptorV3(src)
|
||||
return err
|
||||
}
|
||||
|
||||
// PrepareMigrationV3 runs DESIGN 10.1 step 3 and builds the slot images of
|
||||
// steps 4 to 6. It (re)creates the initial map sidecar at mapPath durably:
|
||||
// generation 1, every chunk UNKNOWN with maxWriteLSN 0, coveredCheckpoint =
|
||||
// the v2 checkpoint (a fully flushed WAL is not remote-clean). The slots
|
||||
// carry a fresh random nonce and the map fields of that root. It writes
|
||||
// nothing to the store file.
|
||||
func PrepareMigrationV3(mapPath string, src MigrationSourceV3) (MigrationPlanV3, error) {
|
||||
d, err := migrationDescriptorV3(src)
|
||||
if err != nil {
|
||||
return MigrationPlanV3{}, err
|
||||
}
|
||||
for _, b := range [][]byte{d.Nonce[:], d.MapLineage[:]} {
|
||||
for b2 := b; ; {
|
||||
if _, err := rand.Read(b2); err != nil {
|
||||
return MigrationPlanV3{}, err
|
||||
}
|
||||
if !allZero(b2) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := os.Remove(mapPath); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return MigrationPlanV3{}, err // a rerun rewrites the map (10.1 rerun table)
|
||||
}
|
||||
m, err := createChunkLSNMapV3(mapPath, d, src.ChunkBlocks)
|
||||
if err != nil {
|
||||
return MigrationPlanV3{}, err
|
||||
}
|
||||
shape, err := mapShapeForV3(d, src.ChunkBlocks)
|
||||
if err != nil {
|
||||
_ = m.Close()
|
||||
return MigrationPlanV3{}, err
|
||||
}
|
||||
entries := make([]chunkLSNEntryV3, int(shape.chunks))
|
||||
for i := range entries {
|
||||
entries[i].Unknown = true
|
||||
}
|
||||
c, err := m.Prepare(1, src.CheckpointLSN, entries, nil)
|
||||
if err = errors.Join(err, m.Close()); err != nil {
|
||||
return MigrationPlanV3{}, err
|
||||
}
|
||||
d = c.descriptor(d)
|
||||
d.SlotGen, d.MigrationPhase = 1, MigrationPrepared
|
||||
if err := d.validate(nil); err != nil {
|
||||
return MigrationPlanV3{}, err
|
||||
}
|
||||
var plan MigrationPlanV3
|
||||
plan.PreparedB = d.encode()
|
||||
d.SlotGen, d.MigrationPhase = 2, MigrationNormal
|
||||
if err := d.validate(nil); err != nil {
|
||||
return MigrationPlanV3{}, err
|
||||
}
|
||||
plan.CommittedA = d.encode()
|
||||
plan.DescriptorHash = sha256.Sum256(plan.CommittedA)
|
||||
d.SlotGen = 3
|
||||
plan.RolloverB = d.encode()
|
||||
return plan, nil
|
||||
}
|
||||
|
||||
// RolloverSlotV3 is step 6 for a rerun that finds a committed slot A of
|
||||
// slotGen 2: the same descriptor at slotGen 3, phase 0.
|
||||
func RolloverSlotV3(committedA []byte) ([]byte, error) {
|
||||
d, err := decodeSlotV3(committedA, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if d.MigrationPhase != MigrationNormal || d.SlotGen != 2 {
|
||||
return nil, fmt.Errorf("%w: slot A is not a fresh migration commit", errSuperblockInvalid)
|
||||
}
|
||||
d.SlotGen = 3
|
||||
return d.encode(), nil
|
||||
}
|
||||
|
||||
// MigrationSlotStateV3 classifies one raw v3 slot for the rerun table.
|
||||
type MigrationSlotStateV3 int
|
||||
|
||||
const (
|
||||
MigrationSlotInvalid MigrationSlotStateV3 = iota // not a valid v3 slot
|
||||
MigrationSlotPrepared // valid, MigrationPhase prepared
|
||||
MigrationSlotCommitted // valid and selectable (phase 0)
|
||||
)
|
||||
|
||||
// ClassifyMigrationSlotV3 reports a slot's state and slotGen.
|
||||
func ClassifyMigrationSlotV3(raw []byte) (MigrationSlotStateV3, uint64) {
|
||||
d, err := decodeSlotV3(raw, nil)
|
||||
if err != nil {
|
||||
return MigrationSlotInvalid, 0
|
||||
}
|
||||
if d.MigrationPhase == MigrationPrepared {
|
||||
return MigrationSlotPrepared, d.SlotGen
|
||||
}
|
||||
return MigrationSlotCommitted, d.SlotGen
|
||||
}
|
||||
|
||||
func allZero(b []byte) bool {
|
||||
for _, x := range b {
|
||||
if x != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -292,6 +292,7 @@ The "Machine / transition" column refers to Part A. Notes under a table record w
|
||||
| `INV-WAL-V3-BASE-001` | A v3 BASE install begins only under `installMu` exclusive (draining the old ID's admitted mutations) and only when BaseLSN covers the acknowledged W (published or not yet), the checkpoint and every appended LSN (an unacknowledged record above BaseLSN would leave a hole behind BaseCursor; codex01 row 9a P1-1); otherwise it is refused, writing nothing, with `ErrInstallBaseUncovered` or `ErrInstallSupersedeUncovered`. It publishes installing (BaseLSN, BaseCursor = the writer head P, the ID) into both slots before the first BASE mutation. BASE extent writes and install-lane applies go only through the handle, under `installMu` shared: a handle lives until the next begin (an install epoch), `WriteBase` needs installing under its ID, and an install-lane apply is also admitted after the handle's own completion while installed or retired, because the session's WAL lane can outlive its BaseDone (DESIGN r17.4; `ErrInstallSuperseded` otherwise); the install lane keeps the order and gap rules. Complete validates the ID under `installMu` exclusive, fsyncs the BASE bytes and the suffix, runs the checkpoint's metadata and chunk barriers and publishes installed with retainedTail >= BaseCursor together with a fresh owned-map candidate carrying the BASE writes' UNKNOWN (codex01 row 9a P1-2) and stops serving the superseded pre-BASE records. installing -> none is refused (no abort); installed -> none retires only once the checkpoint covers BaseLSN and both slots carry a tail at or past BaseCursor. | M1 Serving → Installing → Serving (dark) | `core/storage/base_install_v3.go` `WALStore.beginBaseInstallV3`, `installHandleV3.WriteBase`, `installHandleV3.ApplyInstallEntry`, `WALStore.markBaseCompleteV3`; `core/storage/descriptor_v3.go` `baseTransitionAllowedV3`, `retireBaseV3` | `TestBaseInstallV3BeginPublishesInstallingIntoBothSlots`, `TestBaseInstallV3BeginRefusesBelowW`, `TestBaseInstallV3BeginCoversTheAcknowledgedNotYetPublishedW`, `TestBaseInstallV3BeginRefusals`, `TestBaseInstallV3CompleteServesTheImageAcrossACrash`, `TestBaseInstallV3InstallingToNoneIsRefused`, `TestBaseInstallV3RetiresOnlyOnceTheCheckpointCoversBaseLSN`, `TestBaseInstallV3ApplyInstallEntryOrderAndGap`, `TestBaseInstallV3HeldOldWriteDrainsBeforeANewBegin`, `TestBaseInstallV3ParkedOldFrameIsRefusedAfterASupersession`, `TestBaseInstallV3BeginRefusesBelowTheHighestAppendedLSN`, `TestBaseInstallV3CompletePersistsTheMapUnknownOfBaseWrites`, `TestBaseInstallV3BeginRefusesWithAnAttachedChunkLayer`, `TestBaseInstallV3CompletedHandleAppliesUntilTheNextBegin` (`core/storage/base_install_v3_test.go`); `TestBaseInstallV3SupersessionAfterReopenFencesTheOldSuffix`, `TestBaseInstallV3CoveringSupersessionOverADamagedSuffix` (`core/storage/base_install_reopen_v3_test.go`); `TestBaseInstallV3ResumeAfterReopenFencesAndServesTheSuffix`, `TestBaseInstallV3ResumeRefusesADamagedSuffix`, `TestBaseInstallV3EBelowThePersistedHead`, `TestBaseInstallV3SameOpenRecoverLeavesNoStaleResumeCut`, `TestBaseInstallV3SameOpenRecoverKeepsTheLiveInstallServed`, `TestBaseInstallV3SameOpenRecoverOverDamageFailsClosed` (`core/storage/base_install_resume_v3_test.go`); `TestDescriptorV3BaseTransitionTable`; tagged `swblock_format_v3_test`: `TestReceiverV3InstallThroughTheHandle`, `TestReceiverV3HeldOldBaseWriteDrainsBeforeANewSession`, `TestReceiverV3ParkedOldFrameIsRefusedAfterANewSession`, `TestReceiverV3SupersededBaseDoneFailsWithoutAnAck`, `TestReceiverV3WALAfterBaseDoneStillApplies`, `TestReceiverV3WALAfterBaseDoneSurvivesACrash` (`core/recovery/receiver_install_format_v3_test.go`) | `WALFormat` handle and BASE configs (claude05 R17c, pending) | STUB |
|
||||
| `INV-WAL-V3-INSTALL-ACK-001` | While a v3 volume is installing, the logical `Sync` returns exactly (0, `ErrBaseInstalling`), runs no batch and acknowledges no new W, rechecked at its completion. The receiver's durable progress ack is `SyncInstallLog(installID)` on the narrow `InstallLogSyncer` (not on `LogicalStorage`; named only by `core/storage` and `core/recovery`): under `installMu` shared it refuses outside an install (`ErrNotInstalling`) or for another ID (`ErrInstallIDMismatch`), freezes H, fsyncs the WAL and publishes W = max(W, H) with BaseState still installing, always mode A, and acknowledges H only if no fault latched by its final check under `WALStore.mu` (the #172 ack boundary). It attests log durability only. After the session's own completion the receiver acks through the ordinary `Sync`. A v2 store's `SyncInstallLog` is its `Sync`. | M1 Installing (dark) | `core/storage/install_log_v3.go` `WALStore.SyncInstallLog`; `core/storage/walstore.go` `WALStore.Sync`; `core/recovery/receiver.go` `sendAck` | `TestInstallLogV3SyncIsGatedWhileInstalling`, `TestInstallLogV3Refusals`, `TestInstallLogV3IsModeAAndSurvivesACrash`, `TestInstallLogV3AFaultLatchedBeforeTheAckRefusesIt` (`core/storage/install_log_v3_test.go`); `TestInstallLogSyncerIsReceiverOnly` (`core/recovery/install_log_boundary_test.go`); tagged `TestReceiverV3ProgressAckIsTheInstallLog` | `WALFormat` install-log ack (claude05) | STUB |
|
||||
| `INV-WAL-V3-BATCH-001` | A v3 `WriteBatch` of n blocks is one multi-block record (type 0x04) with LSNs first..first+n-1 and one CRC32C, so a crash keeps all of it or none; the writer consumes the whole LSN range, so the next write continues after it. Each block's dirty entry names its data offset inside the record, and every reader (the store read, the flusher, recovery's replay, the install-path replays and the dark offline reader) validates the whole record and the block's LBA, LSN and offset in it. A torn unacknowledged batch is dropped whole; a torn acknowledged one fails closed; a failed batch pwrite latches the store and is never acknowledged. The map coverage follows every block's LSN. | M1 Serving (dark) | `core/storage/walstore_v3.go` `writeBatchV3Locked`, `replayRecordV3`, `readWholeRecordV3`, `blockOfRecordV3`, `flusher.readDirtyRecordV3`; `core/storage/walstore.go` `WriteBatch`; `core/storage/walstore_readonly_v3.go` | `TestWALStoreV3BatchRoundTripAcrossACrash`, `TestWALStoreV3TornBatchIsAllOrNothing`, `TestWALStoreV3CrashMidBatchServesNoneOfIt`, `TestWALStoreV3BatchAckCoversItsLastLSN`, `TestWALStoreV3BatchFlushesEveryBlock`, `TestWALStoreV3ReadOnlyReaderServesBatchBlocks`, `TestWALStoreV3BatchRecordsMapCoverage` (`core/storage/walstore_batch_v3_test.go`) | `WALFormat` batch records (claude05) | STUB |
|
||||
| `INV-WAL-V3-MIGRATE-001` | `sw-block migrate-v3` rewrites only the descriptor of a cleanly closed v2/impl1 volume whose WAL the exhaustive scan proves flushed; the scan and the geometry check run before any write, and a refusal leaves the volume and every sidecar byte-identical. It keeps the extent base (WALSize = old - 4096), writes an atomic operator backup, an all-UNKNOWN initial map (covered = the v2 checkpoint), the prepared slot B, slot A at phase 0 (the commit point), slot B at phase 0, and only then retires the v2 chunk bitmap; each step is durable before the next. A rerun decides from the slot bytes alone: before the commit it restarts with a full rescan; after it, it only finishes the rollover and the retirement; a prepared B with an invalid A refuses with `ErrMigrationIncomplete`, as v3 open does; it never reads or restores the backup. | offline tool | `cmd/sw-block/migrate.go`; `cmd/sw-block/internal/v2legacy`; `core/storage/migrate_v3.go` | `TestMigrateV3CleanVolume`, `TestMigrateV3RefusalsChangeNothing`, `TestMigrateV3CrashAfterEachStepIsResumable`, `TestMigrateV3OldBinaryAfterPreparation`, `TestMigrateV3OldWALOverwritesThePreparedSlot`, `TestMigrateV3NeverRestoresTheBackup` (`cmd/sw-block/migrate_test.go`); tagged `TestMigrateV3VolumeOpensAsV3`, `TestMigrateV3PreparedOnlyRefusedByV3Open` | none (tool) | STUB |
|
||||
| `INV-FE-FUA-SYNC-001` | A FUA write completes only after `Sync` succeeds. | M1 Serving (frontend) | `core/frontend/nvme/io.go` `IOHandler.write`; `core/frontend/ublk/dispatch.go` `handler.serve` | `TestNVMeWriteFUACompletion` (`core/frontend/nvme/fua_test.go`); `TestDispatch_WriteFUASyncsBeforeCompleting` (`core/frontend/ublk/dispatch_test.go`) | — | ACTIVE |
|
||||
| `INV-FE-FLUSH-SYNC-001` | A flush is a `Sync` of the backend, and a failed `Sync` fails the command instead of acknowledging it. | M1 Serving (frontend) | `core/frontend/nvme/io.go` `IOHandler.Handle`; `core/frontend/ublk/dispatch.go` `handler.serve` | `TestT3b_NVMe_Flush_DispatchesToBackend` (`core/frontend/nvme/t3b_flush_test.go`); `TestDispatch_FlushSyncs`, `TestDispatch_BackendFailuresAreReportedNotAcknowledged` (`core/frontend/ublk/dispatch_test.go`) | — | ACTIVE |
|
||||
| `INV-FE-LINEAGE-GATE-001` | Every `Read`, `Write` and `Sync` re-checks `{volume, replica, epoch, endpoint version}` and health against the current projection; any drift returns `ErrStalePrimary`. The volume-ID comparison is code reading: no cited test varies the volume ID. | M3 Primary → Supporting \| Excluded (I/O side) | `core/frontend/durable/storage_adapter.go` `gate`, `lineageCheck` | `TestT3a_StorageBackend_FenceCheck_Epoch`, `TestT3a_StorageBackend_FenceCheck_EV`, `TestT3a_StorageBackend_FenceCheck_Replica`, `TestT3a_StorageBackend_FenceCheck_Healthy` (`core/frontend/durable/storage_adapter_test.go`); `TestG8A_SupersedeFact_FencesOldPrimaryDurableBackend` (`core/host/volume/g8a_stale_fence_test.go`) | — | ACTIVE |
|
||||
@@ -309,6 +310,7 @@ Notes:
|
||||
- `INV-FE-FLUSH-SYNC-001`: The failed-`Sync` case is asserted for ublk; for NVMe/TCP only the dispatch to `Sync` is asserted here.
|
||||
- `INV-WAL-V3-RESET-001`: Format bump v3, plan row 8 (DESIGN 8.6). Dark: no production path calls the reset, and it must not be activated before the reset model (r4.5) passes. No v3 tail release exists yet, so the precondition is CheckpointLSN >= W rather than an empty retained range: retained old-incarnation bytes at or below the checkpoint are preserved in the extent. The map root is unchanged by a reset, so no map page is retired.
|
||||
- `INV-WAL-V3-BASE-001`: Format bump v3, plan row 9a (DESIGN r17.3.1 sections 8.1, 8.2, 8.4, 8.5, 8.7). Dark: only tests reach it. Row 9b makes the seam `BaseInstallPreparer.BeginBaseInstall(baseLSN, installID) (InstallHandle, error)`: the dual-lane `Receiver` begins at SessionStart (BaseLSN = FromLSN, a fresh random ID) and its `RebuildSession` writes, applies and completes only through the handle, propagating the `MarkBaseComplete` error; stores without the seam keep their exact pre-seam behaviour through the lazy legacy handle (parallelwal's staging is now `BaseStager.BeginBaseStage`). Since row 10b the legacy single-lane caller (`core/transport/replica.go`) is on the handle too: the first admitted `MsgRebuildBlock` (or an empty rebuild's `MsgRebuildDone`) begins one install per connection at the lineage's frontier hint under a fresh random ID, blocks go through `WriteBase`, a different hint on the connection is refused, and done completes and then Syncs, closing the connection with no `MsgBarrierResp` when either fails; a reconnect is a superseding install under the coverage rule (tagged controls in `core/transport/replica_install_format_v3_test.go`). Until the chunk layer's side of the v3 install protocol lands (codex03), a v3 begin with an attached chunk layer is refused (`errV3NotYet`), writing nothing; since row 9c the v3 `SeedChunkFrontier` publishes CheckpointLSN = W = map coverage = lsn through the one checkpoint publication (`publishCheckpointV3`: data fsync, metadata and chunk barriers, owned-map candidate, the sole slot publisher under syncMu with the lifetime check), moves the volatile frontier only after it, publishes nothing on failure, and refuses local writes, an install, an attached layer or a store without an owned map; a HEAD restore onto a dark v3 store still refuses in the provider (`errV3RestoreNotYet`) until codex03's v3 FreshFromHead restore lands; since row 11a an installing physical reopen (an installing ID this open did not begin) keeps its recovered bounds (E, R, intact or damaged) without fencing, publishing or admitting; a same-open Recover of this open's live install records none, so its still-valid handle cannot append past a cached cut that a resume would fence from (codex01 row 11b P1); it republishes the live install's trusted records in the dirty map and keeps the volatile map coverage, or latches the store when the recovered cut is short of the live writer, and a covering superseding begin after it fences [E, floor + WALSize) and fsyncs before its two-slot installing publication, anchors at P = max(E, the persisted retainedHead) without lowering retainedHead, and restarts the writer at P (forward only); since row 11b the same-ID resume after a reopen (store level; no current caller resumes, both receivers begin fresh IDs) runs under `installMu` exclusive and appends nothing until the trusted-through-W check (intact suffix and E >= the persisted retainedHead), the fence of [E, floor + WALSize) with fsync and a retainedHead = E publication all held, refusing with `ErrInstallSuffixDamaged` and writing nothing otherwise; the writer continues at E and the recovered install-lane records are replayed. A begin while installed is refused until retirement (8.5 has no installed -> installing edge).
|
||||
- `INV-WAL-V3-MIGRATE-001`: Format bump v3, plan rows 13a and 13b (DESIGN 10.1). Not yet in this row: the exclusive advisory lock that v3 open will share (precondition 1; it lands with the 12b open path), and raw v2 fixtures committed under testdata (today's controls build v2 volumes with the v2 code that row 14 deletes). The minimum migrated WAL size (64 KiB and two single-block records) is provisional.
|
||||
- `INV-WAL-V3-W-001`, `INV-WAL-V3-FENCE-001`: Format bump v3, plan rows 5 and 5b (DESIGN r15 sections 6.1 to 6.6). Since 5b the v3 format runs inside the real `WALStore`, through the shared committer, dirty map, flusher and leaf. It is reachable only through `createWALStoreV3ForTest`/`openWALStoreV3ForTest`. Without the row-7 map candidate the v3 checkpoint and tail do not advance. They are STUBs until activation (row 12b). The test "crash" closes the file without further writes, so the slot-before-records reordering that mode C's lagged W guards against is covered by the composed model (claude05), not by these tests. Installing recovery is row 11.
|
||||
- `INV-WAL-V3-PUBLISH-001`: Format bump v3, plan row 3 (DESIGN r15 sections 4, 5 and 5.1). It is a STUB until the v3 store is wired in (rows 4 and 5) and activated (row 12b). The v2 checkpoint path (`persistCheckpointReleasing`) is unchanged and is retired in row 14a. The map coverage predicates against the root are added by row 7, and the conditions on BASE edges by rows 9 to 11.
|
||||
- `INV-WAL-V3-DESCRIPTOR-001`: Format bump v3, plan row 1 (DESIGN r15 section 2). It is a STUB until the activation commit (plan row 12b): no production open or create path reaches the v3 codec before then. The map-dependency checks (root and pages present and hash-valid) are added by plan row 7. The tests decode vectors from an independent generator (`core/storage/testdata/format_v3/slot/gen_slot_vectors.py`).
|
||||
|
||||
Reference in new issue
Block a user