mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
fix(storage): choose data sync from backing device cache policy
This commit is contained in:
1 parent
4018929613
commit
d14b66d0a6
6 files changed
+146
-12
No files matched your search
@@ -7,12 +7,12 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Data-only sync applies to WAL/extent data on Linux by default. Descriptor slots,
|
||||
// chunk maps, creation and directory operations retain full fsync.
|
||||
// Data-only sync applies to WAL/extent data. Descriptor slots, chunk maps,
|
||||
// creation and directory operations retain full fsync.
|
||||
func configuredDataSyncMode() (string, error) {
|
||||
mode := os.Getenv("SWBLOCK_SYNC")
|
||||
if mode == "" {
|
||||
mode = defaultDataFileSyncMode
|
||||
return "", nil
|
||||
}
|
||||
switch mode {
|
||||
case "fsync":
|
||||
@@ -24,15 +24,27 @@ func configuredDataSyncMode() (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func newDataFileSync(f *os.File) (func() error, error) {
|
||||
func selectedDataSyncMode(f *os.File) (string, string, error) {
|
||||
mode, err := configuredDataSyncMode()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
reason := "explicit SWBLOCK_SYNC override"
|
||||
if mode == "" {
|
||||
mode, reason = defaultDataSyncMode(f)
|
||||
}
|
||||
return mode, reason, nil
|
||||
}
|
||||
|
||||
func newDataFileSync(f *os.File) (func() error, error) {
|
||||
mode, reason, err := selectedDataSyncMode(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("storage: WAL/extent sync=%s file=%s reason=%s", mode, f.Name(), reason)
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,6 @@ import (
|
||||
// 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 {
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
const defaultDataFileSyncMode = "fsync"
|
||||
func defaultDataSyncMode(*os.File) (string, string) {
|
||||
return "fsync", "device cache detection requires Linux"
|
||||
}
|
||||
|
||||
func dataSyncSupported() error {
|
||||
return errors.New("storage: fdatasync/prezero experiment requires Linux")
|
||||
|
||||
@@ -2,6 +2,80 @@
|
||||
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func defaultDataSyncMode(f *os.File) (string, string) {
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return "fsync", "device stat unavailable: " + err.Error()
|
||||
}
|
||||
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
return "fsync", "device stat unavailable"
|
||||
}
|
||||
return deviceDataSyncMode("/sys", unix.Major(uint64(stat.Dev)), unix.Minor(uint64(stat.Dev)))
|
||||
}
|
||||
|
||||
// Resolve the mounted filesystem's device, including partition parents and
|
||||
// stacked devices. Only an entirely known write-through backing set opts in.
|
||||
func deviceDataSyncMode(root string, major, minor uint32) (string, string) {
|
||||
return "fdatasync", "unconditional Linux default"
|
||||
path := filepath.Join(root, "dev", "block", fmt.Sprintf("%d:%d", major, minor))
|
||||
if err := requireWriteThrough(path, make(map[string]bool), 0); err != nil {
|
||||
return "fsync", fmt.Sprintf("device=%d:%d %v", major, minor, err)
|
||||
}
|
||||
return "fdatasync", fmt.Sprintf("device=%d:%d all backing devices write through", major, minor)
|
||||
}
|
||||
|
||||
func requireWriteThrough(path string, visiting map[string]bool, depth int) error {
|
||||
if depth > 8 {
|
||||
return fmt.Errorf("backing device depth exceeded")
|
||||
}
|
||||
path, err := filepath.EvalSymlinks(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("device unavailable: %w", err)
|
||||
}
|
||||
if visiting[path] {
|
||||
return fmt.Errorf("backing device cycle at %s", path)
|
||||
}
|
||||
visiting[path] = true
|
||||
defer delete(visiting, path)
|
||||
if _, err := os.Stat(filepath.Join(path, "partition")); err == nil {
|
||||
return requireWriteThrough(filepath.Dir(path), visiting, depth+1)
|
||||
} else if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("partition identity unavailable: %w", err)
|
||||
}
|
||||
slaves, err := os.ReadDir(filepath.Join(path, "slaves"))
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("backing devices unavailable: %w", err)
|
||||
}
|
||||
if len(slaves) > 0 {
|
||||
for _, slave := range slaves {
|
||||
if err := requireWriteThrough(filepath.Join(path, "slaves", slave.Name()), visiting, depth+1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// A dm queue alone does not establish the physical cache policy.
|
||||
if _, err := os.Stat(filepath.Join(path, "dm")); err == nil {
|
||||
return fmt.Errorf("dm backing devices unavailable")
|
||||
} else if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("dm identity unavailable: %w", err)
|
||||
}
|
||||
cache, err := os.ReadFile(filepath.Join(path, "queue", "write_cache"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("write_cache unavailable: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(string(cache)) != "write through" {
|
||||
return fmt.Errorf("device=%s write_cache=%q", filepath.Base(path), strings.TrimSpace(string(cache)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ package storage
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -84,9 +85,56 @@ func TestExplicitDataSyncPolicyDoesNotInspectDevice(t *testing.T) {
|
||||
for _, mode := range []string{"fsync", "fdatasync"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
t.Setenv("SWBLOCK_SYNC", mode)
|
||||
if _, err := newDataFileSync(f); err != nil {
|
||||
t.Fatal(err)
|
||||
got, reason, err := selectedDataSyncMode(f)
|
||||
if err != nil || got != mode || !strings.Contains(reason, "override") {
|
||||
t.Fatalf("explicit policy = %s %q %v", got, reason, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeviceDataSyncMalformedTopology(t *testing.T) {
|
||||
for _, shape := range []string{"empty-dm", "cycle", "unreadable-cache"} {
|
||||
t.Run(shape, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
device := filepath.Join(root, "dev/block/8:1")
|
||||
if err := os.MkdirAll(device, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
switch shape {
|
||||
case "empty-dm":
|
||||
if err := os.Mkdir(filepath.Join(device, "dm"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "cycle":
|
||||
if err := os.Mkdir(filepath.Join(device, "slaves"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(device, filepath.Join(device, "slaves/self")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "unreadable-cache":
|
||||
if err := os.MkdirAll(filepath.Join(device, "queue/write_cache"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
mode, reason := deviceDataSyncMode(root, 8, 1)
|
||||
if mode != "fsync" || reason == "" {
|
||||
t.Fatalf("unsafe fallback: %s %q", mode, reason)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultDataSyncUnknownFileFallsBack(t *testing.T) {
|
||||
t.Setenv("SWBLOCK_SYNC", "")
|
||||
f, err := os.CreateTemp(t.TempDir(), "volume")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
mode, reason, err := selectedDataSyncMode(f)
|
||||
if err != nil || mode != "fsync" || !strings.Contains(reason, "unavailable") {
|
||||
t.Fatalf("fallback: %s %q %v", mode, reason, err)
|
||||
}
|
||||
}
|
||||
@@ -563,7 +563,7 @@ Notes:
|
||||
|
||||
| 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 |
|
||||
| `INV-WAL-DATA-SYNC-001` | At volume open, Linux WAL/extent sync selects fdatasync only when every resolved backing device advertises write through; write back or unknown/unreadable device policy selects fsync. `SWBLOCK_SYNC=fsync|fdatasync` explicitly overrides detection (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 | `TestDeviceDataSyncPolicy`, `TestExplicitDataSyncPolicyDoesNotInspectDevice`, `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
|
||||
|
||||
|
||||
Reference in new issue
Block a user