diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 6a890b43ee..5df3ea7d18 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -1891,7 +1891,9 @@ func (l *chunkLayerImpl) persistPresenceEach(each func(uint64)) error { hook() } if fd := l.s.fd; fd != nil { - if err := fd.Sync(); err != nil { + // The store's fsync rule: refused once a fault is latched, and a + // failure latches it, so a failed fsync stays failed. + if err := l.s.syncFile(nil, fd.Sync); err != nil { return fmt.Errorf("storage: chunk presence: fsync volume: %w", err) } } diff --git a/seaweed-block/core/storage/chunk_presence_fsync_test.go b/seaweed-block/core/storage/chunk_presence_fsync_test.go new file mode 100644 index 0000000000..6dad41ec88 --- /dev/null +++ b/seaweed-block/core/storage/chunk_presence_fsync_test.go @@ -0,0 +1,95 @@ +package storage + +import ( + "errors" + "os" + "path/filepath" + "testing" +) + +// presenceLayer attaches a chunk layer whose presence has changed since it +// was last saved (one chunk read-filled), so the next save writes it. +func presenceLayer(t *testing.T) (*WALStore, *chunkLayerImpl) { + t.Helper() + path := filepath.Join(t.TempDir(), "v.wal") + s, err := CreateWALStoreWithOptions(path, ctNumBlocks, ctBlockSize, WALStoreOptions{ChunkBlocks: ctChunkBlocks}) + if err != nil { + t.Fatal(err) + } + s.DisableAutoFlushForRecoveryTest() + f := newFakeChunkRemote() + seed, err := attachChunkLayer(s, f, ctConfig(1)) + if err != nil { + t.Fatal(err) + } + if _, err := s.Write(0, makeBlock(ctBlockSize, 0x42)); err != nil { + t.Fatal(err) + } + if _, err := seed.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + if err := seed.Close(); err != nil { + t.Fatal(err) + } + if err := s.Close(); err != nil { + t.Fatal(err) + } + s, err = OpenWALStoreWithOptions(path, WALStoreOptions{ChunkBlocks: ctChunkBlocks}) + if err != nil { + t.Fatal(err) + } + s.DisableAutoFlushForRecoveryTest() + cfg := ctConfig(2) + cfg.FreshFromHead = true + l, err := attachChunkLayer(s, f, cfg) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = l.Close(); _ = s.Close() }) + if _, err := s.Read(0); err != nil { // read-fill: the presence changes + t.Fatal(err) + } + impl := l.(*chunkLayerImpl) + if impl.presenceVer == impl.savedPresVer { + t.Fatal("setup: the read fill did not change the presence") + } + return s, impl +} + +// A presence save after an earlier fsync of the store failed is refused: the +// fault is sticky, so the save never reports its filled bytes as on disk. +func TestAPresenceSaveIsRefusedAfterAFailedFsync(t *testing.T) { + s, l := presenceLayer(t) + _ = s.noteFsyncFailure(errors.New("an earlier fsync of the store failed")) + saved := l.savedPresVer + if err := l.persistPresence(); !errors.Is(err, ErrFsyncFailed) { + t.Fatalf("presence save after a failed fsync: %v; want ErrFsyncFailed", err) + } + if l.savedPresVer != saved { + t.Fatal("the presence was recorded as saved after a failed fsync") + } +} + +// A presence save whose own fsync fails latches the store's fault: later +// syncs are refused, as after any other failed fsync of the store's file. +func TestAPresenceSavesFailedFsyncStaysFailed(t *testing.T) { + s, l := presenceLayer(t) + gone, err := os.Open(s.fd.Name()) + if err != nil { + t.Fatal(err) + } + _ = gone.Close() + real := s.fd + s.fd = gone // its Sync fails: the file is closed + err = l.persistPresence() + s.fd = real + if err == nil { + t.Fatal("setup: the presence save's fsync did not fail") + } + if s.stickyIOFault() == nil { + t.Fatal("a failed presence fsync left no fault latched") + } + if _, err := s.Sync(); !errors.Is(err, ErrFsyncFailed) { + t.Fatalf("a store sync after the failed presence fsync: %v; want ErrFsyncFailed", err) + } +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index e78f6bc219..683c3e8505 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -580,7 +580,7 @@ The "Machine / transition" column refers to Part A. Notes under a table record w | `INV-WAL-CHECKPOINT-SNAPSHOT-001` | A checkpoint never passes a synced write that is not yet in the extent. | M1 write lifecycle: InExtent → Checkpointed | `core/storage/flusher.go` `flushOnceInternal` | `TestWALStoreV3_FlushMaterializationBoundToRecoveryLifetime` (`core/storage/walstore_map_recovery_lifetime_v3_test.go`); `TestNativeReplacementCheckpointSIGKILL` (`core/storage/legacy_checkpoint_replacement_v3_linux_test.go`) | `WALRecovery` `I1` | ACTIVE | | `INV-WAL-FRONTIER-GAP-001` | A durable BASE anchor and its contiguous synced install-lane suffix survive recovery; an unpersisted frontier jump is not admitted. | M1 Recovering | `core/storage/walstore_v3.go` `recoverV3` | `TestBaseInstallV3CompleteServesTheImageAcrossACrash` (`core/storage/base_install_v3_test.go`) | `WALRecovery` `I1` | ACTIVE | | `INV-WAL-CKPT-META-FAIL-001` | A failed checkpoint metadata write neither publishes the new checkpoint nor releases WAL space. | M1 write lifecycle: InExtent → Checkpointed (refused) | `core/storage/walstore_map_checkpoint_v3.go` `publishCheckpointV3` | `TestNativeReplacementCallerPublicationFaults` (`core/storage/legacy_replacement_v3_test.go`) | — | ACTIVE | -| `INV-WAL-FSYNC-STICKY-001` | After the first failed `fsync` of the store file (WAL, extent or superblock), the open store refuses every later `Sync`, write and checkpoint without calling `fsync` again; only a reopen and `Recover` clear it. | M1 Serving → FaultLatched | `core/storage/walstore.go` `noteFsyncFailure`, `syncFile`, `stickyIOFault` | `TestNativeReplacementDataFsyncFailureIsSticky` (`core/storage/legacy_replacement_v3_test.go`); `TestNativeReplacementCallerPublicationFaults` (`core/storage/legacy_replacement_v3_test.go`); `TestWALStoreV3SyncRechecksTheFaultAtTheAckBoundary` (`core/storage/walstore_v3_test.go`) | — | ACTIVE | +| `INV-WAL-FSYNC-STICKY-001` | After the first failed `fsync` of the store file (WAL, extent, superblock, or the chunk layer's presence save), the open store refuses every later `Sync`, write, checkpoint and presence save without calling `fsync` again; only a reopen and `Recover` clear it. | M1 Serving → FaultLatched | `core/storage/walstore.go` `noteFsyncFailure`, `syncFile`, `stickyIOFault` | `TestNativeReplacementDataFsyncFailureIsSticky` (`core/storage/legacy_replacement_v3_test.go`); `TestNativeReplacementCallerPublicationFaults` (`core/storage/legacy_replacement_v3_test.go`); `TestWALStoreV3SyncRechecksTheFaultAtTheAckBoundary` (`core/storage/walstore_v3_test.go`); `TestAPresenceSaveIsRefusedAfterAFailedFsync`, `TestAPresenceSavesFailedFsyncStaysFailed` (`core/storage/chunk_presence_fsync_test.go`) | — | ACTIVE | | `INV-WAL-APPEND-STICKY-001` | A WAL append whose write failed latches the store fault; writers already queued behind it are refused. | M1 Serving → FaultLatched | `core/storage/walstore_v3.go` `latchV3`, `appendWriteV3` | `TestNativeReplacementQueuedApplyAfterFailedAppend` (`core/storage/legacy_replacement_v3_test.go`); `TestWALStoreV3AppendWriteErrorLatchesTheStore` (`core/storage/walstore_v3_test.go`) | — | ACTIVE | | `INV-WAL-SYNC-FRONTIER-001` | `Sync` never reports an uncovered write durable: it returns the global synced frontier, which is at least the caller's own write and is covered by a completed `fsync`. It may include later writes whose covering `fsync` completed before the caller resumed. | M1 write lifecycle: Appended → Synced | `core/storage/walstore.go` `WALStore.Sync` | `TestWALStore_SyncDoesNotClaimConcurrentWrite` (`core/storage/walstore_test.go`); `TestNativeReplacementSyncCallerReturnOrdering` (`core/storage/legacy_sync_replacement_v3_test.go`) | — | ACTIVE | | `INV-WAL-TYPED-FAILCLOSED-001` | A corrupt replay at startup is reported as `wal_corrupt` at once and keeps local readiness blocked. | M1 Recovering → RecoveryFailed | `cmd/blockvolume/durable_startup.go` `openAndRecoverDurable`; `core/host/volume/recovery_watchdog.go` `LocalRecoveryHealthReason` | `TestDurableStartup_RealProviderCorruptReplayIsWALCorrupt` (`cmd/blockvolume/durable_startup_test.go`); `TestLocalRecoveryFailure_CorruptWALReportsWALCorruptAtOnce` (`core/host/volume/recovery_typed_reason_test.go`) | — | ACTIVE |