From 4018929613622d0c27abc9aeefe4e435a9916313 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 12:48:53 -0800 Subject: [PATCH] test(storage): cover device-dependent data sync policy --- .../core/storage/device_sync_linux.go | 7 ++ .../core/storage/device_sync_linux_test.go | 92 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 seaweed-block/core/storage/device_sync_linux.go create mode 100644 seaweed-block/core/storage/device_sync_linux_test.go diff --git a/seaweed-block/core/storage/device_sync_linux.go b/seaweed-block/core/storage/device_sync_linux.go new file mode 100644 index 0000000000..c88d81e959 --- /dev/null +++ b/seaweed-block/core/storage/device_sync_linux.go @@ -0,0 +1,7 @@ +//go:build linux + +package storage + +func deviceDataSyncMode(root string, major, minor uint32) (string, string) { + return "fdatasync", "unconditional Linux default" +} diff --git a/seaweed-block/core/storage/device_sync_linux_test.go b/seaweed-block/core/storage/device_sync_linux_test.go new file mode 100644 index 0000000000..abf2c9034e --- /dev/null +++ b/seaweed-block/core/storage/device_sync_linux_test.go @@ -0,0 +1,92 @@ +//go:build linux + +package storage + +import ( + "os" + "path/filepath" + "testing" +) + +func TestDeviceDataSyncPolicy(t *testing.T) { + for _, tc := range []struct { + name, cache, want string + partition, dm, mixed bool + }{ + {name: "write-back", cache: "write back\n", want: "fsync"}, + {name: "write-through", cache: "write through\n", want: "fdatasync"}, + {name: "missing", want: "fsync"}, + {name: "unknown", cache: "unknown\n", want: "fsync"}, + {name: "partition", cache: "write through\n", want: "fdatasync", partition: true}, + {name: "dm-through", cache: "write through\n", want: "fdatasync", dm: true}, + {name: "dm-mixed", cache: "write through\n", want: "fsync", dm: true, mixed: true}, + {name: "dm-missing", want: "fsync", dm: true}, + } { + t.Run(tc.name, func(t *testing.T) { + root := t.TempDir() + put := func(path, value string) { + t.Helper() + path = filepath.Join(root, path) + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(value), 0644); err != nil { + t.Fatal(err) + } + } + link := func(target, path string) { + t.Helper() + path = filepath.Join(root, path) + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(root, target), path); err != nil { + t.Fatal(err) + } + } + disk := "devices/pci/block/nvme0n1" + if tc.cache != "" { + put(disk+"/queue/write_cache", tc.cache) + } + device := disk + if tc.partition { + put(disk+"/nvme0n1p1/partition", "1\n") + device = disk + "/nvme0n1p1" + } + if tc.dm { + device = "devices/virtual/block/dm-0" + put(device+"/dm/name", "volume") + // A virtual queue's advertised policy is not a backing-device proof. + put(device+"/queue/write_cache", "write through\n") + link(disk, device+"/slaves/nvme0n1") + if tc.mixed { + put("devices/pci/block/nvme1n1/queue/write_cache", "write back\n") + link("devices/pci/block/nvme1n1", device+"/slaves/nvme1n1") + } + } + if tc.cache != "" || tc.dm { + link(device, "dev/block/8:1") + } + mode, reason := deviceDataSyncMode(root, 8, 1) + if mode != tc.want || reason == "" { + t.Fatalf("mode=%s reason=%q, want %s", mode, reason, tc.want) + } + }) + } +} + +func TestExplicitDataSyncPolicyDoesNotInspectDevice(t *testing.T) { + f, err := os.CreateTemp(t.TempDir(), "volume") + if err != nil { + t.Fatal(err) + } + f.Close() // Discovery would fail; explicit policy must not inspect the fd. + 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) + } + }) + } +}