From 7c7d1223de7944956c2226ffbe0a9bdec3da0fd2 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 24 Nov 2026 15:31:49 -0800 Subject: [PATCH] test: a directory fsync failure after the rename must fail-stop (red) The child process does one Put with an injected directory fsync failure; it must exit ExitDurabilityUnknown with the typed line and never return to its caller. The parent reloads the store: the record or none. Co-Authored-By: Claude Opus 5.5 --- .../authority_store_dir_fsync_test.go | 83 ++++++++++++++----- .../core/authority/authority_store_file.go | 5 ++ 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/seaweed-block/core/authority/authority_store_dir_fsync_test.go b/seaweed-block/core/authority/authority_store_dir_fsync_test.go index f3ebb22629..4affd1feb0 100644 --- a/seaweed-block/core/authority/authority_store_dir_fsync_test.go +++ b/seaweed-block/core/authority/authority_store_dir_fsync_test.go @@ -2,34 +2,77 @@ package authority import ( "errors" + "fmt" + "os" + "os/exec" "strings" "testing" ) -// A record's rename is durable only after the directory fsync: a Put whose -// directory fsync fails must fail (fail closed), for every record, not only -// for workspace (lease) records. Otherwise the caller believes a write that -// a crash can undo. -func TestPutFailsWhenTheDirectoryFsyncFails(t *testing.T) { - for _, c := range []struct { - name string - rec DurableRecord - }{ - {"assignment record", DurableRecord{VolumeID: "v1", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1, DataAddr: "d", CtrlAddr: "c", WriteSeq: 1}}, - {"workspace record", DurableRecord{VolumeID: "v2", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1, DataAddr: "d", CtrlAddr: "c", WriteSeq: 1, - Workspace: &WorkspaceState{Lease: &WorkspaceLease{ReplicaID: "r1", Epoch: 1}}}}, - } { - t.Run(c.name, func(t *testing.T) { - s, err := NewFileAuthorityStore(t.TempDir()) +const failStopHelperEnv = "SWB_AUTHORITY_FAILSTOP_DIR" + +func failStopRecord(kind string) DurableRecord { + r := DurableRecord{VolumeID: "v1", ReplicaID: "r1", Epoch: 7, EndpointVersion: 1, DataAddr: "d", CtrlAddr: "c", WriteSeq: 1} + if kind == "workspace" { + r.Workspace = &WorkspaceState{Lease: &WorkspaceLease{ReplicaID: "r1", Epoch: 7}} + } + return r +} + +// TestAuthorityStoreFailStopHelper is the child process of +// TestPutFailStopsWhenTheDirectoryFsyncFailsAfterRename: one Put with an +// injected directory fsync failure. Reaching the line after Put means Put +// returned to its caller, which the fail-stop rule forbids. +func TestAuthorityStoreFailStopHelper(t *testing.T) { + dir := os.Getenv(failStopHelperEnv) + if dir == "" { + t.Skip("helper process only") + } + s, err := NewFileAuthorityStore(dir) + if err != nil { + fmt.Println("HELPER-SETUP-FAILED", err) + os.Exit(3) + } + SetDirSyncFaultForTest(func() error { return errors.New("injected directory fsync failure") }) + err = s.Put(failStopRecord(os.Getenv(failStopHelperEnv + "_KIND"))) + fmt.Printf("PUT-RETURNED err=%v\n", err) + os.Exit(0) +} + +// A directory fsync failure after the rename leaves the record's durability +// unknown: blockmaster fail-stops (the conventional answer, PostgreSQL after +// fsyncgate). No caller is ever told "failed" for a record that may survive; +// the restart reloads whatever is on disk. Same rule for every record type. +func TestPutFailStopsWhenTheDirectoryFsyncFailsAfterRename(t *testing.T) { + for _, kind := range []string{"assignment", "workspace"} { + t.Run(kind, func(t *testing.T) { + dir := t.TempDir() + cmd := exec.Command(os.Args[0], "-test.run=^TestAuthorityStoreFailStopHelper$", "-test.count=1") + cmd.Env = append(os.Environ(), failStopHelperEnv+"="+dir, failStopHelperEnv+"_KIND="+kind) + out, err := cmd.CombinedOutput() + var exit *exec.ExitError + if !errors.As(err, &exit) || exit.ExitCode() != ExitDurabilityUnknown { + t.Fatalf("child: err=%v, want exit %d\n%s", err, ExitDurabilityUnknown, out) + } + if strings.Contains(string(out), "PUT-RETURNED") { + t.Fatalf("Put returned to its caller after a failed directory fsync:\n%s", out) + } + if !strings.Contains(string(out), "authority_store: FAIL-STOP: directory fsync after rename") || + !strings.Contains(string(out), "injected directory fsync failure") { + t.Fatalf("missing the typed fail-stop line:\n%s", out) + } + // The restart reloads whatever survived: the new record or none, never an error. + s, err := NewFileAuthorityStore(dir) if err != nil { t.Fatal(err) } defer s.Close() - SetDirSyncFaultForTest(func() error { return errors.New("injected directory fsync failure") }) - defer SetDirSyncFaultForTest(nil) - err = s.Put(c.rec) - if err == nil || !strings.Contains(err.Error(), "injected directory fsync failure") { - t.Fatalf("Put with a failed directory fsync: %v; want it to fail", err) + recs, err := s.Load() + if err != nil { + t.Fatalf("reload after the fail-stop: %v", err) + } + if len(recs) > 1 || (len(recs) == 1 && (recs[0].VolumeID != "v1" || recs[0].Epoch != 7)) { + t.Fatalf("reload after the fail-stop: %+v, want the written record or none", recs) } }) } diff --git a/seaweed-block/core/authority/authority_store_file.go b/seaweed-block/core/authority/authority_store_file.go index e4430d092c..d9c94b67c8 100644 --- a/seaweed-block/core/authority/authority_store_file.go +++ b/seaweed-block/core/authority/authority_store_file.go @@ -31,6 +31,11 @@ func SetPutAfterRenameFaultForTest(hook func(DurableRecord) error) { // every FileAuthorityStore.Put. Tests only. var dirSyncFault atomic.Pointer[func() error] +// ExitDurabilityUnknown is blockmaster's exit code when a record's rename +// may or may not survive a crash (sysexits EX_IOERR). Declared before the +// fail-stop exists so the red test compiles. +const ExitDurabilityUnknown = 74 + // SetDirSyncFaultForTest installs (nil clears) that fault. func SetDirSyncFaultForTest(fault func() error) { if fault == nil {