mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
* mount: fail the flush when dirty pages cannot be read back from the swap file Dirty pages that weed mount buffers in the swap file (non-sequential writes, or when the memory chunks run out) are read back when they upload. SwapFileChunk.SaveContent ignored the error of that read: an interval it could not read in full was uploaded short, or not at all, and flush, fsync and close still succeeded. The writes the application had been told succeeded became a hole in the stored file, with no error anywhere. A read error on the cache disk, or a swap file truncated by an outside cleanup, is enough. SaveContent now hands the upload a reader that fails with the read error. The upload fails like any other failed chunk upload: the handle records it, rejects further writes, and flush, fsync and close return EIO without publishing the entry. The remaining intervals of that chunk are not saved, since the handle cannot flush cleanly. The chunk, its write budget and its swap slot are released as before. The test in weed/mount cuts the swap file after the write was acknowledged: on master fsync returns OK and the filer is never told. The pipeline tests cover a closed and a truncated swap file, and a swap file cut inside the second of two intervals. SwapFileChunk.ReadDataAt, which serves reads of pages not yet uploaded, still logs a read error and returns what it has read so far; that is left for a follow-up since it needs an error path through the read side. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: require a failed fsync to leave the filer entry unchanged TestFsyncFailsWhenSwapFileCannotBeReadBack accepted any filer entry backed by stored bytes that left out the lost range, so an entry published with the readable interval (the original 256 bytes plus the "A" write) would also have passed. It now records the filer's entry before the writes and requires the same entry and the same 256 bytes after the failed fsync. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
155 lines
6.3 KiB
Go
155 lines
6.3 KiB
Go
//go:build !freebsd
|
|
|
|
package mount
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/go-fuse/v2/fuse"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// openSwapWriteHandle opens /dir/file for writing on a WFS with one writable
|
|
// chunk slot, so a second chunk is buffered in the swap file under swapDir.
|
|
// The file holds 256 'O' bytes on the fake filer.
|
|
func openSwapWriteHandle(t *testing.T, swapDir string) (*WFS, *FileHandle, *g6ConditionalPeer, *fuse.OpenOut) {
|
|
t.Helper()
|
|
base := attributeAppendEntry(1)
|
|
base.CommitTsNs = 100
|
|
peer := &g6ConditionalPeer{entry: proto.Clone(base).(*filer_pb.Entry)}
|
|
peer.blobs = map[string][]byte{base.Chunks[0].GetFileIdString(): bytes.Repeat([]byte{'O'}, 256)}
|
|
|
|
w := newInvalidateTestWFS(t)
|
|
w.option.uniqueCacheDirForWrite = swapDir
|
|
w.option.DefaultPermissions = true
|
|
w.posixLocks = NewPosixLockTable()
|
|
w.option.ConcurrentWriters = 1
|
|
w.concurrentWriters = util.NewLimitedConcurrentExecutor(1)
|
|
startFakeFiler(t, w, peer)
|
|
w.inodeToPath.Lookup("/dir", 1, true, false, 0, false)
|
|
w.inodeToPath.Lookup("/dir/file", 1, false, false, 42, true)
|
|
w.inodeToPath.MarkChildrenCached("/dir")
|
|
if err := w.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/dir", proto.Clone(base).(*filer_pb.Entry)), 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := &fuse.OpenOut{}
|
|
if code := w.Open(nil, &fuse.OpenIn{InHeader: fuse.InHeader{NodeId: 42}, Flags: syscall.O_WRONLY}, out); code != fuse.OK {
|
|
t.Fatal(code)
|
|
}
|
|
fh := w.GetHandle(FileHandleId(out.Fh))
|
|
g6AppendUploader(t, fh, 100, peer)
|
|
|
|
return w, fh, peer, out
|
|
}
|
|
|
|
// A write the swap file cannot buffer fails, and must not grow the file: the
|
|
// next flush would publish that size with a hole where its data should be. A
|
|
// write that buffers only its first chunk is a short write of those bytes, or
|
|
// an error for kernel writeback, which would drop the rest silently.
|
|
func TestFailedWriteDoesNotGrowFile(t *testing.T) {
|
|
t.Run("short write", func(t *testing.T) { testFailedWriteDoesNotGrowFile(t, 0) })
|
|
t.Run("kernel writeback", func(t *testing.T) { testFailedWriteDoesNotGrowFile(t, fuse.WRITE_CACHE) })
|
|
}
|
|
|
|
func testFailedWriteDoesNotGrowFile(t *testing.T, writeFlags uint32) {
|
|
notADir := filepath.Join(t.TempDir(), "file")
|
|
if err := os.WriteFile(notADir, nil, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w, fh, peer, out := openSwapWriteHandle(t, filepath.Join(notADir, "swap"))
|
|
|
|
write := func(offset uint64, size int, value byte) (uint32, fuse.Status) {
|
|
return w.Write(nil, &fuse.WriteIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh, Offset: offset, Size: uint32(size), WriteFlags: writeFlags}, bytes.Repeat([]byte{value}, size))
|
|
}
|
|
if _, code := write(256, 256, 'A'); code != fuse.OK {
|
|
t.Fatalf("buffered write: %v", code)
|
|
}
|
|
// The only writable chunk slot is taken, so this write needs the swap file.
|
|
if _, code := write(4096, 256, 'B'); code == fuse.OK {
|
|
t.Fatal("write through an unusable swap directory succeeded")
|
|
}
|
|
if size := fh.GetEntry().GetEntry().Attributes.FileSize; size != 512 {
|
|
t.Fatalf("file size after the failed write = %d, want 512", size)
|
|
}
|
|
// The first 24 bytes fit the existing chunk; the next chunk needs the swap file.
|
|
n, code := write(1000, 48, 'C')
|
|
if writeFlags == 0 && (code != fuse.OK || n != 24) {
|
|
t.Fatalf("boundary-crossing write = %d, %v; want a short write of 24", n, code)
|
|
}
|
|
if writeFlags != 0 && code == fuse.OK {
|
|
t.Fatalf("boundary-crossing writeback = %d, OK; want an error", n)
|
|
}
|
|
if size := fh.GetEntry().GetEntry().Attributes.FileSize; size != 1024 {
|
|
t.Fatalf("file size after the short write = %d, want 1024", size)
|
|
}
|
|
if code := w.Fsync(nil, &fuse.FsyncIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh}); code != fuse.OK {
|
|
t.Fatal(code)
|
|
}
|
|
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
want := append(bytes.Repeat([]byte{'O'}, 256), bytes.Repeat([]byte{'A'}, 256)...)
|
|
want = append(want, make([]byte, 1000-512)...)
|
|
want = append(want, bytes.Repeat([]byte{'C'}, 24)...)
|
|
if got := g6StoredBytes(t, peer); peer.entry.Attributes.FileSize != 1024 || !bytes.Equal(got, want) {
|
|
t.Fatalf("stored size %d, want 1024 with the original and buffered bytes", peer.entry.Attributes.FileSize)
|
|
}
|
|
}
|
|
|
|
// Writes buffered in the swap file were acknowledged to the application. If the
|
|
// swap file cannot be read back when they upload, fsync must fail like a failed
|
|
// chunk upload and the filer must not get an entry that claims those bytes.
|
|
func TestFsyncFailsWhenSwapFileCannotBeReadBack(t *testing.T) {
|
|
swapDir := t.TempDir()
|
|
w, _, peer, out := openSwapWriteHandle(t, swapDir)
|
|
peer.mu.Lock()
|
|
before := proto.Clone(peer.entry).(*filer_pb.Entry)
|
|
peer.mu.Unlock()
|
|
write := func(offset uint64, value byte) (uint32, fuse.Status) {
|
|
return w.Write(nil, &fuse.WriteIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh, Offset: offset, Size: 256}, bytes.Repeat([]byte{value}, 256))
|
|
}
|
|
if n, code := write(256, 'A'); n != 256 || code != fuse.OK {
|
|
t.Fatalf("buffered write = %d, %v", n, code)
|
|
}
|
|
// The only writable chunk slot is taken, so this write goes to the swap file.
|
|
if n, code := write(4096, 'B'); n != 256 || code != fuse.OK {
|
|
t.Fatalf("swap file write = %d, %v", n, code)
|
|
}
|
|
swapFiles, err := os.ReadDir(swapDir)
|
|
if err != nil || len(swapFiles) != 1 {
|
|
t.Fatalf("swap dir = %v, %v; want one swap file", swapFiles, err)
|
|
}
|
|
// An outside cleanup cuts the swap file after the write was acknowledged.
|
|
if err := os.Truncate(filepath.Join(swapDir, swapFiles[0].Name()), 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
code := w.Fsync(nil, &fuse.FsyncIn{InHeader: fuse.InHeader{NodeId: 42}, Fh: out.Fh})
|
|
if code == fuse.OK {
|
|
t.Fatal("fsync succeeded although acknowledged bytes could not be uploaded")
|
|
}
|
|
t.Logf("fsync = %v", code)
|
|
if _, code := write(8192, 'C'); code == fuse.OK {
|
|
t.Error("write after the failed upload succeeded; the handle can no longer flush cleanly")
|
|
}
|
|
|
|
peer.mu.Lock()
|
|
defer peer.mu.Unlock()
|
|
// The failed fsync publishes nothing: the filer keeps the entry and the 256
|
|
// bytes it had before the writes.
|
|
if !proto.Equal(peer.entry, before) {
|
|
t.Fatalf("a failed fsync changed the filer entry:\n got %v\nwant %v", peer.entry, before)
|
|
}
|
|
if stored := g6StoredBytes(t, peer); !bytes.Equal(stored, bytes.Repeat([]byte{'O'}, 256)) {
|
|
t.Fatalf("filer content = %q, want the original 256 bytes", stored)
|
|
}
|
|
}
|