mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
Merge pull request #286 from seaweedfs/experiment/wal-fdatasync
fix(storage): default Linux data sync to fdatasync with sparse creation
This commit is contained in:
commit
a1c28e6d99
9 files changed
+668
-4
No files matched your search
@@ -0,0 +1,102 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Data-only sync applies to WAL/extent data on Linux by default. Descriptor slots,
|
||||
// chunk maps, creation and directory operations retain full fsync.
|
||||
func configuredDataSyncMode() (string, error) {
|
||||
mode := os.Getenv("SWBLOCK_SYNC")
|
||||
if mode == "" {
|
||||
mode = defaultDataFileSyncMode
|
||||
}
|
||||
switch mode {
|
||||
case "fsync":
|
||||
return mode, nil
|
||||
case "fdatasync":
|
||||
return mode, dataSyncSupported()
|
||||
default:
|
||||
return "", fmt.Errorf("storage: invalid SWBLOCK_SYNC %q", mode)
|
||||
}
|
||||
}
|
||||
|
||||
func newDataFileSync(f *os.File) (func() error, error) {
|
||||
mode, err := configuredDataSyncMode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if mode == "fsync" {
|
||||
return f.Sync, nil
|
||||
}
|
||||
log.Printf("storage: WAL/extent sync=fdatasync file=%s", f.Name())
|
||||
return func() error { return fdatasyncFile(f) }, nil
|
||||
}
|
||||
|
||||
// Configuration errors must not create or initialize a volume file.
|
||||
func validateNewStorePolicy() error {
|
||||
if _, err := configuredDataSyncMode(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := configuredAllocationMode()
|
||||
return err
|
||||
}
|
||||
|
||||
func configuredAllocationMode() (string, error) {
|
||||
mode := os.Getenv("SWBLOCK_ALLOCATION")
|
||||
prezero := os.Getenv("SWBLOCK_PREZERO")
|
||||
if prezero != "" && prezero != "0" && prezero != "1" {
|
||||
return "", fmt.Errorf("storage: invalid SWBLOCK_PREZERO %q", prezero)
|
||||
}
|
||||
if prezero == "1" {
|
||||
if mode != "" && mode != "prezero" {
|
||||
return "", fmt.Errorf("storage: SWBLOCK_PREZERO conflicts with SWBLOCK_ALLOCATION %q", mode)
|
||||
}
|
||||
mode = "prezero"
|
||||
}
|
||||
switch mode {
|
||||
case "", "sparse":
|
||||
return "sparse", nil
|
||||
case "fallocate":
|
||||
return "", fmt.Errorf("storage: fallocate allocation is disabled; use sparse or explicit prezero")
|
||||
case "prezero":
|
||||
return mode, dataSyncSupported()
|
||||
default:
|
||||
return "", fmt.Errorf("storage: invalid SWBLOCK_ALLOCATION %q", mode)
|
||||
}
|
||||
}
|
||||
|
||||
// Only newly-created files reach this hook; existing bytes are never rewritten.
|
||||
func prezeroNewStore(f *os.File, size int64) error {
|
||||
mode, err := configuredAllocationMode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if mode == "sparse" {
|
||||
return nil
|
||||
}
|
||||
zero := make([]byte, 1<<20)
|
||||
for off := int64(0); off < size; {
|
||||
b := zero[:min(int64(len(zero)), size-off)]
|
||||
n, err := f.WriteAt(b, off)
|
||||
if err != nil {
|
||||
return fmt.Errorf("storage: prezero: %w", err)
|
||||
}
|
||||
if n != len(b) {
|
||||
return io.ErrShortWrite
|
||||
}
|
||||
off += int64(n)
|
||||
}
|
||||
// Allocation/size/extent metadata must be fully durable before the check.
|
||||
if err := f.Sync(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := verifyInitializedFile(f); err != nil {
|
||||
return fmt.Errorf("storage: prezero verification: %w", err)
|
||||
}
|
||||
log.Printf("storage: prezero FIEMAP verified initialized file=%s bytes=%d", f.Name(), size)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
//go:build linux
|
||||
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/sys/unix"
|
||||
"os"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// FIEMAP with SYNC resolves delayed allocation before inspecting the mapping.
|
||||
// Reject holes, unknown/delayed mappings and unwritten extents; fallocate alone
|
||||
// does not satisfy the initialized-file experiment's precondition.
|
||||
const defaultDataFileSyncMode = "fdatasync"
|
||||
|
||||
func verifyInitializedFile(f *os.File) error { return verifyAllocatedFile(f, true) }
|
||||
|
||||
func verifyAllocatedFile(f *os.File, written bool) error {
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
type extent struct {
|
||||
Logical, Physical, Length uint64
|
||||
Reserved [2]uint64
|
||||
Flags uint32
|
||||
Reserved2 [3]uint32
|
||||
}
|
||||
type mapping struct {
|
||||
Start, Length uint64
|
||||
Flags, Mapped, Count, Reserved uint32
|
||||
Extents [128]extent
|
||||
}
|
||||
end := uint64(info.Size())
|
||||
var covered uint64
|
||||
for covered < end {
|
||||
m := mapping{Start: covered, Length: end - covered, Flags: 1, Count: 128}
|
||||
_, _, errno := unix.Syscall(unix.SYS_IOCTL, f.Fd(), 0xc020660b, uintptr(unsafe.Pointer(&m)))
|
||||
if errno != 0 {
|
||||
return fmt.Errorf("storage: FIEMAP: %w", errno)
|
||||
}
|
||||
if m.Mapped == 0 || m.Mapped > m.Count {
|
||||
return fmt.Errorf("storage: uninitialized file at %d", covered)
|
||||
}
|
||||
for _, e := range m.Extents[:m.Mapped] {
|
||||
if e.Logical > covered || e.Length == 0 || e.Logical+e.Length < e.Logical || e.Flags&(2|4) != 0 || (written && e.Flags&0x800 != 0) {
|
||||
return fmt.Errorf("storage: hole or unwritten extent at %d (logical=%d flags=%#x)", covered, e.Logical, e.Flags)
|
||||
}
|
||||
next := e.Logical + e.Length
|
||||
if next <= covered {
|
||||
return fmt.Errorf("storage: FIEMAP made no progress")
|
||||
}
|
||||
covered = next
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func dataSyncSupported() error { return nil }
|
||||
func fdatasyncFile(f *os.File) error { return unix.Fdatasync(int(f.Fd())) }
|
||||
|
||||
func fallocateFile(f *os.File, size int64) error { return unix.Fallocate(int(f.Fd()), 0, 0, size) }
|
||||
@@ -0,0 +1,303 @@
|
||||
//go:build linux
|
||||
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"golang.org/x/sys/unix"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPrezeroCreatesInitializedVolume(t *testing.T) {
|
||||
t.Setenv("SWBLOCK_ALLOCATION", "")
|
||||
t.Setenv("SWBLOCK_SYNC", "fdatasync")
|
||||
t.Setenv("SWBLOCK_PREZERO", "1")
|
||||
path := filepath.Join(t.TempDir(), "volume")
|
||||
s, e := CreateWALStore(path, 32, 4096)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer s.Close()
|
||||
f, e := os.Open(path)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer f.Close()
|
||||
if e = verifyInitializedFile(f); e != nil {
|
||||
t.Fatalf("prezero must eliminate holes and unwritten extents: %v", e)
|
||||
}
|
||||
if _, e = s.Recover(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
b, e := s.Read(31)
|
||||
if e != nil || !bytes.Equal(b, make([]byte, 4096)) {
|
||||
t.Fatalf("new extent bytes: %v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializedFileRejectsSparseAndUnwritten(t *testing.T) {
|
||||
f, e := os.CreateTemp(t.TempDir(), "volume")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer f.Close()
|
||||
if e = f.Truncate(1 << 20); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = verifyInitializedFile(f); e == nil {
|
||||
t.Fatal("sparse file accepted")
|
||||
}
|
||||
if e = unix.Fallocate(int(f.Fd()), 0, 0, 1<<20); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = verifyInitializedFile(f); e == nil {
|
||||
t.Fatal("unwritten extents accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFdatasyncAcknowledgedWritesSurviveSIGKILL(t *testing.T) {
|
||||
if path := os.Getenv("SWBLOCK_TEST_SYNC_CRASH"); path != "" {
|
||||
if os.Getenv("SWBLOCK_TEST_SYNC_CHILD_FAILURE") == "1" {
|
||||
_ = os.Stdout.Close()
|
||||
time.Sleep(30 * time.Millisecond)
|
||||
fmt.Fprintln(os.Stderr, "SYNC_CHILD_FAILURE_SETTLED")
|
||||
os.Exit(7)
|
||||
}
|
||||
opts := WALStoreOptions{}
|
||||
if os.Getenv("SWBLOCK_TEST_STRICT") == "1" {
|
||||
opts.WMode = WModeStrict
|
||||
}
|
||||
s, e := CreateWALStoreWithOptions(path, 32, 4096, opts)
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if os.Getenv("SWBLOCK_PREZERO") == "0" && verifyInitializedFile(s.fd) == nil {
|
||||
panic("sparse crash arm was silently prezeroed")
|
||||
}
|
||||
s.DisableAutoFlushForRecoveryTest()
|
||||
if _, e = s.Recover(); e != nil {
|
||||
panic(e)
|
||||
}
|
||||
for _, lba := range []uint32{0, 13, 31} {
|
||||
if _, e = s.Write(lba, bytes.Repeat([]byte{byte(lba + 17)}, 4096)); e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
if n, e := s.Sync(); e != nil || n < 3 {
|
||||
panic(fmt.Sprintf("Sync=%d %v", n, e))
|
||||
}
|
||||
if os.Getenv("SWBLOCK_TEST_EXTENT") == "1" {
|
||||
if e = s.flusher.flushOnce(); e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
fmt.Println("SYNC_ACKNOWLEDGED")
|
||||
for {
|
||||
time.Sleep(time.Hour)
|
||||
}
|
||||
}
|
||||
for _, strict := range []string{"0", "1"} {
|
||||
for _, prezero := range []string{"0", "1"} {
|
||||
for _, extent := range []string{"0", "1"} {
|
||||
t.Run("strict"+strict+"/prezero"+prezero+"/extent"+extent, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "volume")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestFdatasyncAcknowledgedWritesSurviveSIGKILL$")
|
||||
cmd.Env = append(os.Environ(), "SWBLOCK_ALLOCATION=", "SWBLOCK_SYNC=fdatasync", "SWBLOCK_PREZERO="+prezero, "SWBLOCK_TEST_SYNC_CRASH="+path, "SWBLOCK_TEST_EXTENT="+extent, "SWBLOCK_TEST_STRICT="+strict)
|
||||
out, e := cmd.StdoutPipe()
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if e = cmd.Start(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
var waitOnce sync.Once
|
||||
var waitErr error
|
||||
wait := func() { waitOnce.Do(func() { waitErr = cmd.Wait() }) }
|
||||
defer func() { _ = cmd.Process.Kill(); wait() }()
|
||||
scan := bufio.NewScanner(out)
|
||||
ready := false
|
||||
for scan.Scan() {
|
||||
if scan.Text() == "SYNC_ACKNOWLEDGED" {
|
||||
ready = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ready {
|
||||
wait()
|
||||
t.Fatalf("no sync ACK: scan=%v wait=%v %s", scan.Err(), waitErr, stderr.String())
|
||||
}
|
||||
if e = cmd.Process.Kill(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
wait()
|
||||
exit, ok := waitErr.(*exec.ExitError)
|
||||
if !ok {
|
||||
t.Fatalf("not killed: %v", waitErr)
|
||||
}
|
||||
status, ok := exit.Sys().(syscall.WaitStatus)
|
||||
if !ok || status.Signal() != syscall.SIGKILL {
|
||||
t.Fatalf("wrong exit: %v", waitErr)
|
||||
}
|
||||
t.Setenv("SWBLOCK_ALLOCATION", "")
|
||||
t.Setenv("SWBLOCK_SYNC", "fdatasync")
|
||||
t.Setenv("SWBLOCK_PREZERO", prezero)
|
||||
s, e := OpenWALStore(path)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer s.Close()
|
||||
if _, e = s.Recover(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for _, lba := range []uint32{0, 13, 31} {
|
||||
b, e := s.Read(lba)
|
||||
if e != nil || !bytes.Equal(b, bytes.Repeat([]byte{byte(lba + 17)}, 4096)) {
|
||||
t.Fatalf("acknowledged block%d differs: %v", lba, e)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Run under strace with the sync environment unset/set to verify the
|
||||
// syscall boundary independently of the selected function's Go identity.
|
||||
func TestDataSyncSyscallProbe(t *testing.T) {
|
||||
opts := WALStoreOptions{}
|
||||
if os.Getenv("SWBLOCK_TEST_STRICT") == "1" {
|
||||
opts.WMode = WModeStrict
|
||||
}
|
||||
s, e := CreateWALStoreWithOptions(filepath.Join(t.TempDir(), "volume"), 32, 4096, opts)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer s.Close()
|
||||
s.DisableAutoFlushForRecoveryTest()
|
||||
if _, e = s.Recover(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.Write(7, bytes.Repeat([]byte{0x6d}, 4096)); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.Sync(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = s.flusher.flushOnce(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
b, e := s.Read(7)
|
||||
if e != nil || !bytes.Equal(b, bytes.Repeat([]byte{0x6d}, 4096)) {
|
||||
t.Fatalf("readback: %v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFallocateAllocationIsRefused(t *testing.T) {
|
||||
t.Setenv("SWBLOCK_PREZERO", "0")
|
||||
t.Setenv("SWBLOCK_ALLOCATION", "fallocate")
|
||||
s, err := CreateWALStore(filepath.Join(t.TempDir(), "volume"), 32, 4096)
|
||||
if err == nil {
|
||||
s.Close()
|
||||
t.Fatal("fallocate allocation must be refused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultCreatedVolumeStaysSparse(t *testing.T) {
|
||||
t.Setenv("SWBLOCK_SYNC", "")
|
||||
t.Setenv("SWBLOCK_PREZERO", "")
|
||||
t.Setenv("SWBLOCK_ALLOCATION", "")
|
||||
path := filepath.Join(t.TempDir(), "volume")
|
||||
s, err := CreateWALStore(path, 262144, 4096)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stat := info.Sys().(*syscall.Stat_t)
|
||||
if allocated := stat.Blocks * 512; allocated >= info.Size()/4 {
|
||||
t.Fatalf("default volume lost sparse allocation: allocated=%d size=%d", allocated, info.Size())
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidCreatePolicyLeavesNoFileAndAllowsRetry(t *testing.T) {
|
||||
for _, tc := range []struct{ name, sync, allocation, prezero string }{
|
||||
{"sync-before-prezero", "bad", "prezero", "1"},
|
||||
{"allocation", "fdatasync", "bad", "0"},
|
||||
{"fallocate", "fdatasync", "fallocate", "0"},
|
||||
{"prezero", "fdatasync", "", "bad"},
|
||||
{"conflict", "fdatasync", "sparse", "1"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Setenv("SWBLOCK_SYNC", tc.sync)
|
||||
t.Setenv("SWBLOCK_ALLOCATION", tc.allocation)
|
||||
t.Setenv("SWBLOCK_PREZERO", tc.prezero)
|
||||
path := filepath.Join(t.TempDir(), "volume")
|
||||
s, err := CreateWALStore(path, 32, 4096)
|
||||
if err == nil {
|
||||
s.Close()
|
||||
t.Fatal("invalid policy accepted")
|
||||
}
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Fatalf("invalid policy touched volume before refusal: %v", err)
|
||||
}
|
||||
t.Setenv("SWBLOCK_SYNC", "fdatasync")
|
||||
t.Setenv("SWBLOCK_ALLOCATION", "sparse")
|
||||
t.Setenv("SWBLOCK_PREZERO", "0")
|
||||
s, err = CreateWALStore(path, 32, 4096)
|
||||
if err != nil {
|
||||
t.Fatalf("corrected retry failed: %v", err)
|
||||
}
|
||||
s.Close()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncCrashFailureDiagnosticsWaitForStderr(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestFdatasyncAcknowledgedWritesSurviveSIGKILL$")
|
||||
cmd.Env = append(os.Environ(), "SWBLOCK_TEST_SYNC_CRASH=", "SWBLOCK_TEST_SYNC_CHILD_FAILURE=1", "SWBLOCK_ALLOCATION=", "SWBLOCK_PREZERO=0")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil || ctx.Err() != nil {
|
||||
t.Fatalf("wrong failed-child outcome: %v %v", err, ctx.Err())
|
||||
}
|
||||
if !strings.Contains(string(out), "SYNC_CHILD_FAILURE_SETTLED") || strings.Contains(string(out), "WARNING: DATA RACE") {
|
||||
t.Fatalf("failure diagnostics read before child settlement: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncControlsIgnoreAmbientAllocation(t *testing.T) {
|
||||
for _, tc := range []struct{ name, test, allocation string }{
|
||||
{"prezero", "TestPrezeroCreatesInitializedVolume", "sparse"},
|
||||
{"crash-matrix", "TestFdatasyncAcknowledgedWritesSurviveSIGKILL", "prezero"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^"+tc.test+"$")
|
||||
cmd.Env = append(os.Environ(), "SWBLOCK_TEST_SYNC_CRASH=", "SWBLOCK_TEST_SYNC_CHILD_FAILURE=", "SWBLOCK_ALLOCATION="+tc.allocation, "SWBLOCK_PREZERO=0")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("ambient allocation contaminated control: %v: %s", err, out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//go:build !linux
|
||||
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
const defaultDataFileSyncMode = "fsync"
|
||||
|
||||
func dataSyncSupported() error {
|
||||
return errors.New("storage: fdatasync/prezero experiment requires Linux")
|
||||
}
|
||||
func fdatasyncFile(*os.File) error { return dataSyncSupported() }
|
||||
func verifyInitializedFile(*os.File) error { return dataSyncSupported() }
|
||||
|
||||
func fallocateFile(*os.File, int64) error { return dataSyncSupported() }
|
||||
func verifyAllocatedFile(*os.File, bool) error { return dataSyncSupported() }
|
||||
@@ -0,0 +1,148 @@
|
||||
//go:build linux
|
||||
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// External dm-log-writes harness calls prepare/write, replays only through
|
||||
// the post-sync ACK mark onto a fresh device, then calls verify. Without that
|
||||
// block-layer harness this helper is not a power-loss test.
|
||||
func TestDataSyncPowerLossReplayHelper(t *testing.T) {
|
||||
root := os.Getenv("SWBLOCK_REPLAY_ROOT")
|
||||
if root == "" {
|
||||
t.Skip("requires isolated dm-log-writes replay harness")
|
||||
}
|
||||
phase, kind := os.Getenv("SWBLOCK_REPLAY_PHASE"), os.Getenv("SWBLOCK_REPLAY_CASE")
|
||||
path := filepath.Join(root, "probe")
|
||||
payload := bytes.Repeat([]byte{0x7d}, 4096)
|
||||
if kind == "wal" {
|
||||
switch phase {
|
||||
case "prepare":
|
||||
s, e := CreateWALStore(path, 32, 4096)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = s.Close(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
case "write":
|
||||
s, e := OpenWALStore(path)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
s.DisableAutoFlushForRecoveryTest()
|
||||
if _, e = s.Recover(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if _, e = s.Write(13, payload); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if os.Getenv("SWBLOCK_REPLAY_DROP_SYNC") != "1" {
|
||||
if _, e = s.Sync(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
// Deliberately no Close: only the explicit Sync precedes the ACK mark.
|
||||
case "verify":
|
||||
s, e := OpenWALStore(path)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer s.Close()
|
||||
if _, e = s.Recover(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
b, e := s.Read(13)
|
||||
if e != nil || !bytes.Equal(b, payload) {
|
||||
t.Fatalf("REPLAY_BYTES_MISMATCH WAL: %v", e)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("unknown replay phase %q", phase)
|
||||
}
|
||||
} else {
|
||||
switch phase {
|
||||
case "prepare":
|
||||
f, e := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer f.Close()
|
||||
switch kind {
|
||||
case "written":
|
||||
if _, e = f.WriteAt(make([]byte, 1<<20), 0); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
case "unwritten":
|
||||
if e = fallocateFile(f, 1<<20); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
case "hole":
|
||||
if e = f.Truncate(1 << 20); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
case "growth":
|
||||
if _, e = f.WriteAt(make([]byte, 4096), 0); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("unknown replay case %q", kind)
|
||||
}
|
||||
if e = f.Sync(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
case "write":
|
||||
f, e := os.OpenFile(path, os.O_RDWR, 0)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer f.Close()
|
||||
if _, e = f.WriteAt(payload, 128<<10); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if os.Getenv("SWBLOCK_REPLAY_DROP_SYNC") != "1" {
|
||||
if kind == "growth" {
|
||||
e = f.Sync()
|
||||
} else {
|
||||
var sync func() error
|
||||
sync, e = newDataFileSync(f)
|
||||
if e == nil {
|
||||
e = sync()
|
||||
}
|
||||
}
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
case "verify":
|
||||
f, e := os.Open(path)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer f.Close()
|
||||
b := make([]byte, 4096)
|
||||
n, e := f.ReadAt(b, 128<<10)
|
||||
if e != nil || n != len(b) || !bytes.Equal(b, payload) {
|
||||
t.Fatalf("REPLAY_BYTES_MISMATCH %s: n=%d err=%v", kind, n, e)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("unknown replay phase %q", phase)
|
||||
}
|
||||
}
|
||||
if phase == "prepare" {
|
||||
d, e := os.Open(root)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
defer d.Close()
|
||||
if e = d.Sync(); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
fmt.Printf("REPLAY_%s_OK %s\n", phase, kind)
|
||||
}
|
||||
@@ -479,7 +479,13 @@ func (w *walWriter) logicalTailValue() uint64 {
|
||||
}
|
||||
|
||||
// fsync forces buffered data to durable storage.
|
||||
func (w *walWriter) fsync() error { return w.fd.Sync() }
|
||||
func (w *walWriter) fsync() error {
|
||||
sync, err := newDataFileSync(w.fd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return sync()
|
||||
}
|
||||
|
||||
// usedFraction returns the fraction of the WAL region currently in
|
||||
// use, in the range [0, 1]. Used by the admission controller to
|
||||
|
||||
@@ -209,6 +209,9 @@ func CreateWALStoreWithOptions(path string, numBlocks uint32, blockSize int, opt
|
||||
// Frozen constructor for legacy-format regression fixtures; public Create
|
||||
// never reaches it after activation. Removed from production in row 14a.
|
||||
func createLegacyWALStoreWithOptions(path string, numBlocks uint32, blockSize int, opts WALStoreOptions) (*WALStore, error) {
|
||||
if err := validateNewStorePolicy(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if blockSize == 0 {
|
||||
blockSize = DefaultBlockSize
|
||||
}
|
||||
@@ -254,7 +257,11 @@ func createLegacyWALStoreWithOptions(path string, numBlocks uint32, blockSize in
|
||||
_ = f.Close()
|
||||
return nil, fmt.Errorf("storage: fsync after create: %w", err)
|
||||
}
|
||||
return openInitializedWithMetadata(path, f, &sb, metadata)
|
||||
s, err := openInitializedWithMetadata(path, f, &sb, metadata)
|
||||
if err != nil {
|
||||
_ = f.Close()
|
||||
}
|
||||
return s, err
|
||||
}
|
||||
|
||||
// OpenWALStore opens an existing store file WITHOUT recovering it.
|
||||
@@ -309,6 +316,10 @@ func openInitializedWithMetadata(path string, f *os.File, sb *superblock, metada
|
||||
// init hook that runs before the committer and flusher goroutines start
|
||||
// (the v3 store attaches its state there).
|
||||
func openInitializedInternal(path string, f *os.File, sb *superblock, metadata BlockMetadata, init func(*WALStore) error) (*WALStore, error) {
|
||||
dataSync, err := newDataFileSync(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dm := newDirtyMap(64) // 64 shards is plenty for Phase 07 demo workloads
|
||||
|
||||
s := &WALStore{
|
||||
@@ -333,9 +344,9 @@ func openInitializedInternal(path string, f *os.File, sb *superblock, metadata B
|
||||
s.walMutation.Store(1)
|
||||
s.syncWALFile = func() error {
|
||||
s.walSyncCalls.Add(1)
|
||||
return f.Sync()
|
||||
return dataSync()
|
||||
}
|
||||
s.syncExtentFile = f.Sync
|
||||
s.syncExtentFile = dataSync
|
||||
if init != nil {
|
||||
if err := init(s); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -231,6 +231,9 @@ func createWALStoreV3MapAtCursorForTest(path string, numBlocks, blockSize uint32
|
||||
}
|
||||
|
||||
func createWALStoreV3Configured(path string, numBlocks, blockSize uint32, walSize uint64, mode WMode, cursor, chunkBlocks uint64, opts WALStoreOptions) (*WALStore, error) {
|
||||
if err := validateNewStorePolicy(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d := descriptorV3{
|
||||
ImplKind: ImplKindWALStore, ImplVersion: walStoreImplVersion3,
|
||||
VolumeSize: uint64(numBlocks) * uint64(blockSize), ExtentSize: 64 * 1024, BlockSize: blockSize,
|
||||
@@ -269,6 +272,9 @@ func createWALStoreV3Configured(path string, numBlocks, blockSize uint32, walSiz
|
||||
return nil, err
|
||||
}
|
||||
err = fd.Truncate(totalSize)
|
||||
if err == nil {
|
||||
err = prezeroNewStore(fd, totalSize)
|
||||
}
|
||||
if err == nil && chunkBlocks != 0 {
|
||||
var m *chunkLSNMapV3
|
||||
m, err = createChunkLSNMapV3(path+".chunkmap", d, chunkBlocks)
|
||||
|
||||
@@ -559,6 +559,12 @@ Notes:
|
||||
|---|---|---|---|---|---|---|
|
||||
| `INV-NVME-H2C-RANGE-001` | An H2CData range exceeding the expected transfer size is refused before slicing, including a uint32 offset-plus-length overflow; an exact-bound payload is preserved. | Frontend H2C receive validation; no storage state transition | `core/frontend/nvme/session.go` `recvH2CData` | `TestRecvH2CDataRangeBounds` (`core/frontend/nvme/h2c_bounds_test.go`) | — | ACTIVE |
|
||||
|
||||
### Experimental data-file synchronization
|
||||
|
||||
| ID | Invariant | Machine / transition | Enforced in | Tests | TLA+ | Status |
|
||||
|---|---|---|---|---|---|---|
|
||||
| `INV-WAL-DATA-SYNC-001` | Linux WAL/extent sync defaults to fdatasync; `SWBLOCK_SYNC=fsync` explicitly restores full data-file sync (other platforms retain fsync). Descriptor/map/create/directory durability still uses fsync. `SWBLOCK_ALLOCATION=sparse|prezero` selects allocation at create; sparse is the default and fallocate is refused. `SWBLOCK_PREZERO=1` aliases prezero, which explicitly writes every byte only at create, fsyncs, then refuses holes/unwritten FIEMAP mappings. Existing files are never prezeroed. | M1 Create / Sync / Checkpoint | `data_sync.go`, `data_sync_linux.go`; `walstore_v3.go` Create; `walstore.go` data sync seams | `TestPrezeroCreatesInitializedVolume`, `TestInitializedFileRejectsSparseAndUnwritten`, `TestFallocateAllocationIsRefused`, `TestDefaultCreatedVolumeStaysSparse`, `TestFdatasyncAcknowledgedWritesSurviveSIGKILL`, `TestDataSyncSyscallProbe` (`core/storage/data_sync_linux_test.go`) | Same abstract durable sync action; syscall/extent initialization and process-crash versus power-loss limits are outside the model | ACTIVE |
|
||||
|
||||
## Part C. Change rules
|
||||
|
||||
How a fix or a feature is calibrated against this model:
|
||||
|
||||
Reference in new issue
Block a user