mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
Fence: O_DIRECT only on the store's own file; qualify the direct path
The O_DIRECT descriptor is used only when it is the same file as the store's descriptor (os.SameFile); a replaced path is never written. The tests qualify an aligned O_DIRECT write in their directory and fail on Linux when it is refused, and the flush-fault test registers its cleanup first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
25d5c605d6
commit
2fe1b06d1b
3 files changed
+116
-19
No files matched your search
@@ -1,27 +1,48 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// directSupported says whether the filesystem holding path takes O_DIRECT
|
||||
// fence writes (Linux, not tmpfs).
|
||||
func directSupported(path string) bool {
|
||||
f, err := openFenceDirect(path)
|
||||
if err != nil {
|
||||
// directEligible says whether the filesystem holding dir takes an aligned
|
||||
// O_DIRECT write (not only the open: some filesystems refuse at write
|
||||
// time). On Linux an ineligible test directory fails the test, so the
|
||||
// O_DIRECT path cannot pass by falling back unnoticed, unless
|
||||
// SWBLOCK_TEST_NO_ODIRECT=1 names a filesystem known to refuse it.
|
||||
func directEligible(t *testing.T, dir string) bool {
|
||||
t.Helper()
|
||||
if runtime.GOOS != "linux" {
|
||||
return false
|
||||
}
|
||||
_ = f.Close()
|
||||
return true
|
||||
probe := filepath.Join(dir, "odirect-probe")
|
||||
if err := os.WriteFile(probe, make([]byte, 64<<10), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(probe)
|
||||
ok := false
|
||||
if f, err := openFenceDirect(probe); err == nil {
|
||||
_, werr := f.WriteAt(alignedZeros(fenceDirectAlign), 0)
|
||||
ok = werr == nil
|
||||
_ = f.Close()
|
||||
}
|
||||
if !ok && os.Getenv("SWBLOCK_TEST_NO_ODIRECT") != "1" {
|
||||
t.Fatalf("the test directory %s refuses an aligned O_DIRECT write: run on a filesystem that takes it, or set SWBLOCK_TEST_NO_ODIRECT=1", dir)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// The fence runs in 1 MiB slices, and where the filesystem allows it each
|
||||
// slice's aligned bytes bypass the page cache (O_DIRECT), so a slice never
|
||||
// waits behind other writers' writeback.
|
||||
func TestWALStoreV3FenceSlicesAreSmallAndBypassThePageCache(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "volume")
|
||||
dir := t.TempDir()
|
||||
eligible := directEligible(t, dir)
|
||||
path := filepath.Join(dir, "volume")
|
||||
const walSize = 3<<20 + 1<<19
|
||||
s, err := createMappedWALStoreV3ForTest(path, 4096, 4096, walSize, WModeLagged, 64)
|
||||
if err != nil {
|
||||
@@ -45,10 +66,10 @@ func TestWALStoreV3FenceSlicesAreSmallAndBypassThePageCache(t *testing.T) {
|
||||
}
|
||||
}
|
||||
gotZeroed, gotDirect := s.fenceZeroBytes.Load()-zeroed, s.fenceDirectBytes.Load()-direct
|
||||
if directSupported(path) && (gotDirect == 0 || gotDirect > gotZeroed) {
|
||||
if eligible && (gotDirect == 0 || gotDirect > gotZeroed) {
|
||||
t.Fatalf("fenced %d bytes, %d of them O_DIRECT: want the aligned bytes written O_DIRECT", gotZeroed, gotDirect)
|
||||
}
|
||||
if !directSupported(path) && gotDirect != 0 {
|
||||
if !eligible && gotDirect != 0 {
|
||||
t.Fatalf("%d bytes written O_DIRECT where the filesystem refuses it", gotDirect)
|
||||
}
|
||||
}
|
||||
@@ -58,12 +79,20 @@ func TestWALStoreV3FenceSlicesAreSmallAndBypassThePageCache(t *testing.T) {
|
||||
// completed fence), latches the store, and the next open's recovery fences
|
||||
// it again.
|
||||
func TestWALStoreV3FenceSliceFlushFaultAfterADirectWriteIsNotCounted(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "volume")
|
||||
dir := t.TempDir()
|
||||
eligible := directEligible(t, dir)
|
||||
path := filepath.Join(dir, "volume")
|
||||
const walSize = 3<<20 + 1<<19
|
||||
s, err := createMappedWALStoreV3ForTest(path, 4096, 4096, walSize, WModeLagged, 64)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
closed := false
|
||||
t.Cleanup(func() {
|
||||
if !closed {
|
||||
_ = s.Close()
|
||||
}
|
||||
})
|
||||
s.DisableAutoFlushForRecoveryTest()
|
||||
writeSynced(t, s, 1, 1)
|
||||
flushErr := errors.New("injected device flush failure")
|
||||
@@ -79,7 +108,7 @@ func TestWALStoreV3FenceSliceFlushFaultAfterADirectWriteIsNotCounted(t *testing.
|
||||
if _, err := s.Recover(); !errors.Is(err, flushErr) {
|
||||
t.Fatalf("recovery over a failing slice flush: %v", err)
|
||||
}
|
||||
if directSupported(path) && writtenDirect == 0 {
|
||||
if eligible && writtenDirect == 0 {
|
||||
t.Fatal("the faulted slice was not written O_DIRECT")
|
||||
}
|
||||
if got := s.fenceZeroBytes.Load() - zeroed; got != 0 || hooked != 0 {
|
||||
@@ -92,6 +121,7 @@ func TestWALStoreV3FenceSliceFlushFaultAfterADirectWriteIsNotCounted(t *testing.
|
||||
t.Fatal("a failed slice flush did not latch the store")
|
||||
}
|
||||
s.testBeforeFenceFsync = nil
|
||||
closed = true
|
||||
_ = s.Close() // latched: the close may report the fault
|
||||
r := mustReopenV3(t, path)
|
||||
defer r.Close()
|
||||
@@ -99,3 +129,52 @@ func TestWALStoreV3FenceSliceFlushFaultAfterADirectWriteIsNotCounted(t *testing.
|
||||
t.Fatalf("the reopened store's recovery fenced %d bytes, want the unflushed slice fenced again", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The O_DIRECT descriptor is used only on the very file the store holds: if
|
||||
// the store's path now names another file (replaced after the open), the
|
||||
// fence writes through the store's own descriptor and the other file is
|
||||
// never touched. The control is the test above (same file: O_DIRECT used).
|
||||
func TestWALStoreV3FenceNeverWritesAFileThatReplacedTheStorePath(t *testing.T) {
|
||||
if runtime.GOOS != "linux" {
|
||||
t.Skip("the O_DIRECT path is Linux only; elsewhere the fence never reopens the path")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
directEligible(t, dir)
|
||||
path := filepath.Join(dir, "volume")
|
||||
const walSize = 3<<20 + 1<<19
|
||||
s, err := createMappedWALStoreV3ForTest(path, 4096, 4096, walSize, WModeLagged, 64)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = s.Close() })
|
||||
s.DisableAutoFlushForRecoveryTest()
|
||||
writeSynced(t, s, 1, 1)
|
||||
info, err := s.fd.Stat()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Rename(path, path+".held"); err != nil { // the store keeps its descriptor
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreign := bytes.Repeat([]byte{0xAB}, int(info.Size()))
|
||||
if err := os.WriteFile(path, foreign, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
zeroed, direct := s.fenceZeroBytes.Load(), s.fenceDirectBytes.Load()
|
||||
if _, err := s.Recover(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := s.fenceDirectBytes.Load() - direct; got != 0 {
|
||||
t.Fatalf("%d fence bytes written O_DIRECT through a path that names another file", got)
|
||||
}
|
||||
if s.fenceZeroBytes.Load()-zeroed == 0 {
|
||||
t.Fatal("the fence did not run on the store's own descriptor")
|
||||
}
|
||||
after, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(after, foreign) {
|
||||
t.Fatal("the fence wrote the file that replaced the store's path")
|
||||
}
|
||||
}
|
||||
@@ -837,12 +837,9 @@ func (s *WALStore) fenceV3(E, floor, walSize uint64, sameNonce bool, walOffset u
|
||||
}
|
||||
}
|
||||
zero := alignedZeros(64 * 1024)
|
||||
var direct *os.File // nil: every fence write goes through the page cache
|
||||
if s.path != "" {
|
||||
if f, err := openFenceDirect(s.path); err == nil {
|
||||
direct = f
|
||||
defer func() { _ = direct.Close() }()
|
||||
}
|
||||
direct := s.openFenceDirectV3() // nil: every fence write goes through the page cache
|
||||
if direct != nil {
|
||||
defer func() { _ = direct.Close() }()
|
||||
}
|
||||
for _, r := range ranges {
|
||||
for done := uint64(0); done < r.Len; {
|
||||
@@ -881,6 +878,27 @@ func (s *WALStore) fenceV3(E, floor, walSize uint64, sameNonce bool, walOffset u
|
||||
return nil
|
||||
}
|
||||
|
||||
// openFenceDirectV3 opens the store's file again for O_DIRECT fence writes,
|
||||
// or returns nil (the page-cache path). The descriptor is used only when it
|
||||
// is the very file the store holds open (same device and inode as s.fd): a
|
||||
// path that was replaced or that resolves elsewhere is never written.
|
||||
func (s *WALStore) openFenceDirectV3() *os.File {
|
||||
if s.path == "" || s.fd == nil {
|
||||
return nil
|
||||
}
|
||||
f, err := openFenceDirect(s.path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
owned, err1 := s.fd.Stat()
|
||||
opened, err2 := f.Stat()
|
||||
if err1 != nil || err2 != nil || !os.SameFile(owned, opened) {
|
||||
_ = f.Close()
|
||||
return nil
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// writeFenceSliceV3 writes zeros over [off, off+n): the fenceDirectAlign-
|
||||
// aligned middle through direct (O_DIRECT) when it is open, the unaligned
|
||||
// edges, or all of it without direct, through the page cache. A direct
|
||||
|
||||
@@ -750,7 +750,7 @@ The "Machine / transition" column refers to Part A. Notes under a table record w
|
||||
| `INV-WAL-V3-W-CHECKPOINT-CLAMP-001` | A v3 checkpoint never exceeds the acknowledged W; entries the flusher wrote to the extent above the published checkpoint stay retry work, so a later Sync and flusher cycle advance the checkpoint and map coverage together. | M1 write lifecycle: InExtent → Checkpointed (v3) | `core/storage/walstore_v3.go` `WALStore.persistCheckpointV3`; `core/storage/walstore_map_checkpoint_v3.go` `WALStore.persistOwnedMapCheckpointV3`; `core/storage/flusher.go` `flushOnceInternal` | `TestWALStoreV3FlusherCheckpointIsClampedToTheAcknowledgedW`, `TestWALStoreV3FlusherWritesTheExtentButHoldsTheCheckpointUntilTheMap` (`core/storage/walstore_v3_test.go`); `TestWALStoreV3_FlushedProgressRetriesAfterLaterSync` (`core/storage/walstore_flushed_progress_v3_test.go`) | — | STUB |
|
||||
| `INV-WAL-V3-W-SETWMODE-001` | `SetWMode` first syncs every write admitted before it under the old mode, then publishes the new mode while holding the v3 leader role, so no Sync batch acknowledges under a mode other than the descriptor's. | M1 write lifecycle: Appended → Synced (v3) | `core/storage/walstore_v3.go` `WALStore.SetWMode`, `WALStore.syncWALBatchV3Internal` | `TestWALStoreV3SetWModeWaitsForAHeldBatch`, `TestWALStoreV3NoBatchAcksUnderAStaleModeAfterSetWMode`, `TestWALStoreV3SetWModeCrashCases` (`core/storage/walstore_v3_test.go`) | — | DORMANT |
|
||||
| `INV-WAL-V3-W-RECOVER-STRADDLE-001` | No v3 W publication or Sync acknowledgement straddles a recovery: a Sync whose completion sees a changed recovery generation is refused, and recovery sets the acknowledged W to its recovered prefix R. | M1 Recovering → Serving | `core/storage/walstore_v3.go` `WALStore.recoverV3`, `WALStore.refreshW`, `WALStore.syncWALBatchV3Internal`; `core/storage/walstore.go` `WALStore.syncWithPhases` | `TestWALStoreV3SameOpenRecoverResetsTheAcknowledgedFrontier`, `TestWALStoreV3RecoverWaitsForABatchAckAndNothingRevivesTheCut`, `TestWALStoreV3OuterSyncCompletionIsRefusedAcrossARecovery`, `TestWALStoreV3RefreshDuringRecoverWaitsAndPublishesNothing` (`core/storage/walstore_v3_test.go`) | — | STUB |
|
||||
| `INV-WAL-V3-FENCE-SLICE-001` | The fence zero-writes its range in 1 MiB slices, aligned bytes O_DIRECT where allowed, each fdatasynced and only then counted as progress; a recovery skips the fence only when the same range was fenced and fsynced in this open by the same writer with no WAL write since. | M1 Recovering → Serving (v3) | `core/storage/walstore_v3.go` `WALStore.fenceV3`; `core/storage/wal_io_v3.go` `writeCountV3` | `TestWALStoreV3FenceCountsAndSkipsARepeat`, `TestWALStoreV3SameOpenRecoveryAfterWritesStillFences`, `TestWALStoreV3FenceSlicesCountAfterEachSync`, `TestWALStoreV3FenceSliceFailureCountsOnlyCompletedSlices` (`core/storage/walstore_fence_slice_v3_test.go`); `TestWALStoreV3FenceSlicesAreSmallAndBypassThePageCache`, `TestWALStoreV3FenceSliceFlushFaultAfterADirectWriteIsNotCounted` (`core/storage/walstore_fence_direct_v3_test.go`) | `WALFormat` `NoResurrection` | ACTIVE |
|
||||
| `INV-WAL-V3-FENCE-SLICE-001` | The fence zero-writes its range in 1 MiB slices, aligned bytes O_DIRECT where allowed, each fdatasynced and only then counted as progress; a recovery skips the fence only when the same range was fenced and fsynced in this open by the same writer with no WAL write since. | M1 Recovering → Serving (v3) | `core/storage/walstore_v3.go` `WALStore.fenceV3`; `core/storage/wal_io_v3.go` `writeCountV3` | `TestWALStoreV3FenceCountsAndSkipsARepeat`, `TestWALStoreV3SameOpenRecoveryAfterWritesStillFences`, `TestWALStoreV3FenceSlicesCountAfterEachSync`, `TestWALStoreV3FenceSliceFailureCountsOnlyCompletedSlices` (`core/storage/walstore_fence_slice_v3_test.go`); `TestWALStoreV3FenceSlicesAreSmallAndBypassThePageCache`, `TestWALStoreV3FenceSliceFlushFaultAfterADirectWriteIsNotCounted`, `TestWALStoreV3FenceNeverWritesAFileThatReplacedTheStorePath` (`core/storage/walstore_fence_direct_v3_test.go`) | `WALFormat` `NoResurrection` | ACTIVE |
|
||||
| `INV-WAL-V3-FENCE-001` | Before any append after a v3 recovery, the store zeroes the logical WAL interval [E, floor + WALSize), floor being the both-slot same-nonce reuse floor, fsyncs it through the leaf, and only then publishes retainedHead = E. | M1 Recovering → Serving (v3) | `core/storage/walstore_v3.go` `WALStore.recoverV3`, `WALStore.fenceV3`; `core/storage/recovery_v3.go` `fenceRangesV3` | `TestWALStoreV3DroppedRecordsNeverResurfaceAfterASecondCrash`, `TestWALStoreV3NoFenceResurrectsInBothModes` (`core/storage/walstore_v3_test.go`) | `WALFormat` `NoResurrection` | STUB |
|
||||
| `INV-WAL-V3-FENCE-EXACT-001` | The v3 drop fence changes only the bytes of [E, floor + WALSize) mapped modulo WALSize, in at most two ranges split at the physical wrap; no byte before E or outside the interval changes. | M1 Recovering → Serving (v3) | `core/storage/recovery_v3.go` `fenceRangesV3`; `core/storage/walstore_v3.go` `WALStore.fenceV3` | `TestWALStoreV3FenceTouchesNoByteBeforeTheHead`, `TestWALStoreV3RecoversAHistoryEndingAtTheLastRepresentableCursor` (`core/storage/walstore_v3_test.go`); `TestRecoveryV3FenceRangesAreExact` (`core/storage/recovery_v3_test.go`) | — | STUB |
|
||||
| `INV-WAL-V3-NO-RESURRECTION-001` | A WAL record dropped by one v3 recovery is never replayed by a later recovery, also after a second crash. | M1 Recovering → Serving (v3) | `core/storage/walstore_v3.go` `WALStore.recoverV3`, `WALStore.fenceV3`; `core/storage/wal_io_v3.go` `scanWALV3Accepted` | `TestWALStoreV3DroppedRecordsNeverResurfaceAfterASecondCrash`, `TestWALStoreV3NoFenceResurrectsInBothModes` (`core/storage/walstore_v3_test.go`) | `WALFormat` `NoResurrection` | STUB |
|
||||
|
||||
Reference in new issue
Block a user