diff --git a/seaweed-block/core/authority/authority.go b/seaweed-block/core/authority/authority.go index cb28f1b74d..fce81f1169 100644 --- a/seaweed-block/core/authority/authority.go +++ b/seaweed-block/core/authority/authority.go @@ -618,8 +618,9 @@ func (p *Publisher) applyWithWorkspaceMove(ask AssignmentAsk, grant *workspaceGr p.mu.Unlock() if issuedWorkspace != nil { // The store may have written the issuing record before - // failing (e.g. a directory sync after the rename): the - // move's outcome is unknown, not refused. + // failing (a failure after its rename): the move's outcome + // is unknown, not refused. The file store fail-stops on a + // directory fsync failure there instead (exit 74). return fmt.Errorf("authority: durable Put of a workspace move issue failed: %w: %w", err, ErrWorkspaceOutcomeUncertain) } return fmt.Errorf("authority: durable Put failed, rolling back mint: %w", err) 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 f34cd50981..80e3276f10 100644 --- a/seaweed-block/core/authority/authority_store_dir_fsync_test.go +++ b/seaweed-block/core/authority/authority_store_dir_fsync_test.go @@ -5,6 +5,8 @@ import ( "fmt" "os" "os/exec" + "path/filepath" + "reflect" "runtime" "strings" "testing" @@ -72,23 +74,80 @@ func TestPutFailStopsWhenTheDirectoryFsyncFailsAfterRename(t *testing.T) { !strings.Contains(string(out), "directory "+step+": injected directory "+step+" 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() - recs, err := s.Load() - if err != nil { + // The restart reloads whatever survived. The child stopped after the + // rename without a power loss, so the record is on disk: the reload + // must return exactly it, with no skipped (corrupt) record. + if err := reloadExactly(dir, failStopRecord(kind)); 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) - } }) } } +// reloadExactly reopens the store at dir and returns nil only if it loads +// exactly want and skips nothing. +func reloadExactly(dir string, want DurableRecord) error { + s, err := NewFileAuthorityStore(dir) + if err != nil { + return err + } + defer s.Close() + recs, skips, err := s.LoadWithSkips() + switch { + case err != nil: + return err + case len(skips) != 0: + return fmt.Errorf("%d skipped record(s): %v", len(skips), skips) + case len(recs) != 1: + return fmt.Errorf("%d records, want exactly the written one", len(recs)) + case !reflect.DeepEqual(recs[0], want): + return fmt.Errorf("reloaded %+v, want %+v", recs[0], want) + } + return nil +} + +// The reload check refuses each way the restart could mislead it: a corrupt +// record (skipped by Load), no record, a different record. +func TestReloadExactlyRefusesCorruptMissingOrDifferent(t *testing.T) { + want := failStopRecord("assignment") + put := func(t *testing.T, r DurableRecord) string { + dir := t.TempDir() + s, err := NewFileAuthorityStore(dir) + if err != nil { + t.Fatal(err) + } + if err := s.Put(r); err != nil { + t.Fatal(err) + } + s.Close() + return dir + } + if err := reloadExactly(put(t, want), want); err != nil { + t.Fatalf("control: the written record must reload: %v", err) + } + if reloadExactly(t.TempDir(), want) == nil { + t.Fatal("an empty store passed") + } + other := want + other.Epoch = 8 + if reloadExactly(put(t, other), want) == nil { + t.Fatal("a different record passed") + } + dir := put(t, want) + ents, err := os.ReadDir(dir) + if err != nil || len(ents) == 0 { + t.Fatalf("store dir: %v %d", err, len(ents)) + } + for _, e := range ents { + if err := os.WriteFile(filepath.Join(dir, e.Name()), []byte("corrupt"), 0o644); err != nil { + t.Fatal(err) + } + } + if reloadExactly(dir, want) == nil { + t.Fatal("a corrupt record passed") + } +} + // A close error after a successful directory fsync changes nothing on disk: // the rename is durable, so Put reports the record written (telling the // caller "failed" would make two truths). diff --git a/seaweed-block/core/authority/workspace_lease_horizon_test.go b/seaweed-block/core/authority/workspace_lease_horizon_test.go index ce19c81325..9cbf2570bf 100644 --- a/seaweed-block/core/authority/workspace_lease_horizon_test.go +++ b/seaweed-block/core/authority/workspace_lease_horizon_test.go @@ -351,7 +351,7 @@ func TestWorkspaceAmbiguousExtensionLatchesUncertainty(t *testing.T) { first := p.WorkspaceState("v").Lease.ExpiresAtNS hs.setAfter(func(r DurableRecord) error { if isExtension(r, "v", first) { - return errors.New("injected: directory sync failed after rename") + return errors.New("injected: failure after the rename") } return nil }) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index f494f56987..ee5b217e86 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -730,7 +730,7 @@ Notes: - `INV-LIFECYCLE-STORE-DURABLE-001` open gaps: Store-directory ancestor sync on Open is best-effort: failures are logged and do not block startup; a new directory entry may not survive an immediate crash when an ancestor cannot be synced. Record-file and store-directory sync after a put remain mandatory. `NodeInventoryStore.RemoveNode` does not fsync its directory after deletion, so a removed node can reappear after a crash. A failed put may still be on disk: if directory fsync fails after rename, restart can load the new record. - `INV-AUTH-PERSIST-BEFORE-PUBLISH-001`: a failure before the rename leaves the old record and is reported failed (the in-memory rollback test); a directory open or fsync failure after the rename leaves the outcome unknown and fail-stops the process, for every record type, so the restart reloads whatever survived and callers re-read. A workspace volume's uncertainty latch (`INV-AUTH-UNCERTAIN-001`) stays for its other causes. -- `INV-AUTH-UNCERTAIN-001`: The cited test asserts the refusal after an unknown-outcome lease extension (rename landed, directory sync failed): the next renewal, a revocation and an endpoint change are all refused. The TLA+ configuration `S5_red_noUncertain` violates `UniqueEpoch` without it. A Revoking lease still gets its bounded drain reply, min(horizon, deadline), never extended: see M2 any (workspace) → Uncertain. A failed move on an uncertain volume is answered as an unknown outcome before any admission refusal, the request's own cancellation or deadline stays typed, and an issued move's replay still returns its stored grant: see M2 Uncertain → Uncertain (move answer). The renewal path reads uncertainty from the lease view (`INV-LEASE-VIEW-001`), not from `persistWorkspaceLocked`. +- `INV-AUTH-UNCERTAIN-001`: The cited test asserts the refusal after an unknown-outcome lease extension (rename landed, then a failure; the file store fail-stops on a directory fsync failure instead, `INV-AUTH-PERSIST-BEFORE-PUBLISH-001`): the next renewal, a revocation and an endpoint change are all refused. The TLA+ configuration `S5_red_noUncertain` violates `UniqueEpoch` without it. A Revoking lease still gets its bounded drain reply, min(horizon, deadline), never extended: see M2 any (workspace) → Uncertain. A failed move on an uncertain volume is answered as an unknown outcome before any admission refusal, the request's own cancellation or deadline stays typed, and an issued move's replay still returns its stored grant: see M2 Uncertain → Uncertain (move answer). The renewal path reads uncertainty from the lease view (`INV-LEASE-VIEW-001`), not from `persistWorkspaceLocked`. - `INV-WS-LEASE-HORIZON-001`: The forced-barrier argument (a renewal that reads its clock after the barrier has read the deadline is refused) assumes nondecreasing master time. That is an assumption of the current `WorkspaceTakeover` model, whose `Tick` and `SlowTick` actions only advance `now`; it is not a consequence of `time.Now` carrying a monotonic reading, because persisted `UnixNano` deadlines are compared as wall-clock values. The existing deadline check stays alongside the horizon. The model still renews with `exp = deadline = now + TTL`; the horizon/grant split is a pending specification update (Part C rule 2). - `INV-WS-LEASE-HORIZON-001`, `INV-WS-LEASE-HORIZON-MONO-001`: a move issue that replaces the lease with the new holder's (a clean handoff, or a forced move past the barrier) is its ordered successor and ends the scope of these rows. If that issue write is persisted ambiguously (it may have landed while memory has not published it), the volume is uncertain and no further grant at the old epoch is issued (`INV-AUTH-UNCERTAIN-001`), so no disk expiry is claimed for it. - `INV-LEASE-VIEW-001`, `INV-LEASE-VIEW-OWNED-001`: writers refresh the view under the publisher lock, then the lease lock, at every change of a derive input; the live renewal path takes only the lease lock (`INV-WS-RENEW-NONBLOCKING-001`). A move issue publishes the derived view and the new holder's initial deadline in one lease-lock section, never lowering a deadline a renewal raised: see M2 Moved → epoch issued (test `TestWorkspaceIssuePublishNeverLowersARenewedDeadline`, `core/authority/workspace_issue_publish_test.go`).