From 76692a5ef7bec78b95ec7da5510a46ed672a8eb7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 23 Nov 2026 22:02:21 -0800 Subject: [PATCH] test: guard that the flusher sync and the WAL sync are one whole-file sync A source-level test checks that the store opener binds exactly one data sync for its file and that both the flusher's extent sync and the WAL sync use it (a range sync, a second descriptor, or a WAL sync that no longer calls the shared sync fails it). The checkpoint bound over a superseded synced record rests on this. The crash test helpers also close the owned-map file of a crashed store, so the crash tests' directory cleanup no longer fails where an open file cannot be deleted. Co-Authored-By: Claude Opus 5.5 --- .../core/storage/chunk_fake_remote_test.go | 12 ++ .../storage/sync_covers_wal_guard_test.go | 128 ++++++++++++++++++ seaweed-block/docs/design/invariants.md | 4 +- 3 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 seaweed-block/core/storage/sync_covers_wal_guard_test.go diff --git a/seaweed-block/core/storage/chunk_fake_remote_test.go b/seaweed-block/core/storage/chunk_fake_remote_test.go index 417724798a..9151a70438 100644 --- a/seaweed-block/core/storage/chunk_fake_remote_test.go +++ b/seaweed-block/core/storage/chunk_fake_remote_test.go @@ -727,6 +727,7 @@ func crashableStore(t *testing.T) (*WALStore, string, func()) { if err := s.fd.Close(); err != nil { t.Fatal(err) } + crashOwnedMap(s) } } @@ -753,6 +754,7 @@ func reopen(t *testing.T, path string) (*WALStore, func()) { if err := s.fd.Close(); err != nil { t.Fatal(err) } + crashOwnedMap(s) } } @@ -1133,3 +1135,13 @@ func waitReconciledForTest(t *testing.T, layer chunkLayer) { time.Sleep(time.Millisecond) } } + +// crashOwnedMap closes the owned-map file of a crashed store without +// persisting anything, as the process's exit would; otherwise its handle +// stays open and a platform that refuses to delete an open file fails the +// test's directory cleanup. +func crashOwnedMap(s *WALStore) { + if s.v3 != nil && s.v3.ownedMap != nil && s.v3.ownedMap.fd != nil { + _ = s.v3.ownedMap.fd.Close() + } +} diff --git a/seaweed-block/core/storage/sync_covers_wal_guard_test.go b/seaweed-block/core/storage/sync_covers_wal_guard_test.go new file mode 100644 index 0000000000..d299b8ca14 --- /dev/null +++ b/seaweed-block/core/storage/sync_covers_wal_guard_test.go @@ -0,0 +1,128 @@ +package storage + +import ( + "go/ast" + "go/parser" + "go/token" + "os" + "path/filepath" + "strings" + "testing" +) + +// INV-WAL-FLUSH-SYNC-COVERS-WAL-001, checked in the source: the flusher's +// extent sync and the WAL sync are the same whole-file sync of the store's +// one descriptor. The checkpoint bound rests on it (a checkpoint may pass a +// synced record superseded by an unsynced newer one only because the +// flusher's sync makes the newer record durable too). In the package's +// non-test Go files: +// - the function that sets syncExtentFile binds exactly one data sync +// (bindDataFileSync of the store file) and no other function sets it; +// - every assignment to syncExtentFile assigns exactly that result; +// - every assignment to syncWALFile is a function that calls that result. +// +// A range sync, a second descriptor for the WAL or the extent, or a WAL sync +// that no longer calls the shared sync fails here first. +func TestSyncCoversWALGuard(t *testing.T) { + files, err := filepath.Glob("*.go") + if err != nil { + t.Fatal(err) + } + fset := token.NewFileSet() + binds, extentOK, extentBad, walOK, walBad := 0, 0, []string{}, 0, []string{} + shared := "" // the variable bindDataFileSync's sync result is bound to + type asg struct { + field string + rhs ast.Expr + pos token.Position + } + var asgs []asg + for _, f := range files { + if strings.HasSuffix(f, "_test.go") { + continue + } + src, err := os.ReadFile(f) + if err != nil { + t.Fatal(err) + } + file, err := parser.ParseFile(fset, f, src, 0) + if err != nil { + t.Fatal(err) + } + for _, decl := range file.Decls { + fn, ok := decl.(*ast.FuncDecl) + if !ok || fn.Body == nil { + continue + } + var fnBinds []string + setsExtent := false + ast.Inspect(fn.Body, func(n ast.Node) bool { + a, ok := n.(*ast.AssignStmt) + if !ok { + return true + } + for i, rhs := range a.Rhs { + if call, ok := rhs.(*ast.CallExpr); ok { + if id, ok := call.Fun.(*ast.Ident); ok && id.Name == "bindDataFileSync" && len(a.Lhs) > 0 { + if lhs, ok := a.Lhs[0].(*ast.Ident); ok { + fnBinds = append(fnBinds, lhs.Name) + } + } + } + if i < len(a.Lhs) { + if sel, ok := a.Lhs[i].(*ast.SelectorExpr); ok && (sel.Sel.Name == "syncExtentFile" || sel.Sel.Name == "syncWALFile") { + asgs = append(asgs, asg{sel.Sel.Name, rhs, fset.Position(a.Pos())}) + if sel.Sel.Name == "syncExtentFile" { + setsExtent = true + } + } + } + } + return true + }) + if setsExtent { + binds += len(fnBinds) + if len(fnBinds) > 0 { + shared = fnBinds[0] + } + } + } + } + if binds != 1 || shared == "" { + t.Fatalf("the function setting syncExtentFile binds %d data syncs (first bound to %q); want exactly one: one descriptor, one sync", binds, shared) + } + callsShared := func(e ast.Expr) bool { + found := false + ast.Inspect(e, func(n ast.Node) bool { + if c, ok := n.(*ast.CallExpr); ok { + if id, ok := c.Fun.(*ast.Ident); ok && id.Name == shared { + found = true + } + } + return !found + }) + return found + } + for _, a := range asgs { + switch a.field { + case "syncExtentFile": + if id, ok := a.rhs.(*ast.Ident); ok && id.Name == shared { + extentOK++ + } else { + extentBad = append(extentBad, a.pos.String()) + } + case "syncWALFile": + if fl, ok := a.rhs.(*ast.FuncLit); ok && callsShared(fl) { + walOK++ + } else { + walBad = append(walBad, a.pos.String()) + } + } + } + if extentOK == 0 || len(extentBad) > 0 { + t.Fatalf("syncExtentFile assigned the shared sync %q %d times; other assignments at %v: the flusher's sync must be the WAL's whole-file sync", shared, extentOK, extentBad) + } + if walOK == 0 || len(walBad) > 0 { + t.Fatalf("syncWALFile assigned a function calling %q %d times; other assignments at %v", shared, walOK, walBad) + } +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 2878cca9bb..750081d60b 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -1018,12 +1018,12 @@ Notes: | `INV-WAL-DATA-SYNC-STATUS-001` | The data sync mode chosen at open and its reason are logged and exposed in durable status. | M1 Create / Sync / Checkpoint | `data_sync.go` `bindDataFileSync`, `DataSyncPolicy`; `core/frontend/durable/storage_adapter.go` status | `TestDurableProvider_DataSyncStatus` (`core/frontend/durable/data_sync_status_test.go`) | — | ACTIVE | | `INV-WAL-ALLOCATION-001` | `SWBLOCK_ALLOCATION` selects allocation at create: sparse, the default, or prezero; fallocate is refused. `SWBLOCK_PREZERO=1` is an alias of prezero. | M1 Create / Sync / Checkpoint | `walstore_v3.go` Create | `TestDefaultCreatedVolumeStaysSparse`, `TestFallocateAllocationIsRefused` (`core/storage/data_sync_linux_test.go`) | — | ACTIVE | | `INV-WAL-PREZERO-001` | Prezero writes every byte at create and fsyncs; a file opened as initialized is refused when it has holes or unwritten extent mappings. | M1 Create / Sync / Checkpoint | `walstore_v3.go` Create | `TestPrezeroCreatesInitializedVolume`, `TestInitializedFileRejectsSparseAndUnwritten` (`core/storage/data_sync_linux_test.go`) | — | ACTIVE | -| `INV-WAL-FLUSH-SYNC-COVERS-WAL-001` | WAL and extent data live in one store file, and the WAL sync and the flusher's extent sync are the same whole-file fsync or fdatasync of that one descriptor, never a range sync, so every flusher sync also makes the WAL durable. | M1 Serving → Serving (checkpoint) | `core/storage/walstore.go` `openInitializedV3`; `core/storage/data_sync.go` `bindDataFileSync`; `core/storage/flusher.go` `flushOnceInternal` | `TestFlusherCheckpointOverASupersededSyncedRecord` (`core/storage/flusher_superseded_record_test.go`; its control models an extent-only flusher sync and loses X) | — | ACTIVE | +| `INV-WAL-FLUSH-SYNC-COVERS-WAL-001` | WAL and extent data live in one store file, and the WAL sync and the flusher's extent sync are the same whole-file fsync or fdatasync of that one descriptor, never a range sync, so every flusher sync also makes the WAL durable. | M1 Serving → Serving (checkpoint) | `core/storage/walstore.go` `openInitializedV3`; `core/storage/data_sync.go` `bindDataFileSync`; `core/storage/flusher.go` `flushOnceInternal` | `TestFlusherCheckpointOverASupersededSyncedRecord` (`core/storage/flusher_superseded_record_test.go`; its control models an extent-only flusher sync and loses X); `TestSyncCoversWALGuard` (`core/storage/sync_covers_wal_guard_test.go`: the store opener binds one data sync, and the flusher sync and the WAL sync both use it) | — | ACTIVE | - `INV-WAL-DATA-SYNC-001` and the rows split from it: enforced by reading, with no test of their own: the mode is not reselected during the life of an open volume; descriptor, map, create and directory durability always use fsync, whatever the data sync mode; an existing file is never prezeroed. Notes: -- `INV-WAL-FLUSH-SYNC-COVERS-WAL-001`: the one-file, one-descriptor, no-range-sync clause is enforced by reading; `TestFlusherCheckpointOverASupersededSyncedRecord` shows why it is load-bearing (a checkpoint over a superseded synced record is safe only if the flusher's sync made the newer record durable; its control, an extent-only sync, loses the older record). No test checks that the WAL sync and the extent sync are the same call. +- `INV-WAL-FLUSH-SYNC-COVERS-WAL-001`: the one-file, one-descriptor, no-range-sync clause is enforced by reading; `TestFlusherCheckpointOverASupersededSyncedRecord` shows why it is load-bearing (a checkpoint over a superseded synced record is safe only if the flusher's sync made the newer record durable; its control, an extent-only sync, loses the older record). `TestSyncCoversWALGuard` checks in the source that the WAL sync and the flusher's extent sync are the one data sync the store opener binds for its file. ## Part C. Change rules