Files
seaweedfs/weed/mount/weedfs_unpublished_create_test.go
T
Yip LeoandClaude Opus 5.5 e8fef6bdfd mount: fail a read of dirty pages that cannot be read back from the swap file (#13215)
Dirty pages that weed mount buffers in the swap file (non-sequential
writes, or when the memory chunks run out) also serve reads until they
upload. SwapFileChunk.ReadDataAt logged a read error and stopped,
returning only how far it had read; UploadPipeline.MaybeReadDataAt and
DirtyPages.ReadDirtyDataAt passed that offset up. The mount read path
then served the rest of the range from the stored chunks or from zeros,
and the read returned OK. A read error on the cache disk, or a swap file
truncated by an outside cleanup, is enough: the application reads back
bytes it was told were written and gets other bytes, with no error
anywhere. This is the read-side twin of #13198, which made the upload
of those bytes fail.

PageChunk.ReadDataAt, UploadPipeline.MaybeReadDataAt and
DirtyPages.ReadDirtyDataAt now return an error besides the offset. A
swap chunk that cannot read back an interval overlapping the requested
range returns it, wrapping the new ErrDirtyDataUnreadable and the read
error (a short read is io.ErrUnexpectedEOF). Intervals outside the range
are not read, so a read that does not touch the lost bytes still
succeeds. MemChunk cannot fail. The read handler and copy_file_range
turn the error into EIO. Read does not refresh the filer entry and retry
for this error, since the entry has no say over bytes that only exist in
the swap file.

A failed read does not mark the handle failed. Reads of other ranges and
writes keep working, and the upload path already records the failure
when it cannot read the same bytes, so flush, fsync and close return EIO.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 11:58:05 -08:00

183 lines
6.7 KiB
Go

package mount
import (
"bytes"
"context"
"testing"
"time"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/mount/meta_cache"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// openUnpublishedCreate sets up the handle Create leaves for a deferred
// create: dirty, with no base, holding writes the filer has not seen.
func openUnpublishedCreate(t *testing.T, wfs *WFS, path util.FullPath, data []byte) (*FileHandle, uint64) {
t.Helper()
inode := wfs.inodeToPath.Lookup(path, time.Now().Unix(), false, false, 0, true)
_, name := path.DirAndName()
fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, &filer_pb.Entry{
Name: name,
Attributes: &filer_pb.FuseAttributes{FileMode: 0o644, Inode: inode, Crtime: 1, Mtime: 1},
}, 0, 0)
fh.RememberPath(path)
fh.dirtyMetadata = true
fh.baseEntry.Store(nil)
if len(data) > 0 {
if _, err := fh.dirtyPages.AddPage(0, data, true, time.Now().UnixNano()); err != nil {
t.Fatalf("AddPage: %v", err)
}
}
fh.UpdateEntry(func(entry *filer_pb.Entry) {
entry.Attributes.FileSize = uint64(len(data))
})
return fh, inode
}
func applyPeerEvent(t *testing.T, wfs *WFS, resp *filer_pb.SubscribeMetadataResponse) {
t.Helper()
if err := wfs.metaCache.ApplyMetadataResponse(context.Background(), resp, meta_cache.SubscriberMetadataResponseApplyOptions); err != nil {
t.Fatalf("apply event: %v", err)
}
wfs.metaCache.WaitForEntryInvalidations()
}
// requireWritesKept checks the handle still holds its own unflushed writes,
// its own entry and no base, so its flush stays a create-if-not-exists.
func requireWritesKept(t *testing.T, fh *FileHandle, pages *PageWriter, data []byte) {
t.Helper()
if fh.dirtyPages != pages {
t.Fatal("the handle's unflushed writes were discarded")
}
buf := make([]byte, len(data))
if maxStop, err := fh.dirtyPages.ReadDirtyDataAt(buf, 0, time.Now().UnixNano()); err != nil || maxStop != int64(len(data)) || !bytes.Equal(buf, data) {
t.Fatalf("dirty data = %q (maxStop %d, err %v), want %q", buf, maxStop, err, data)
}
if fh.baseEntry.Load() != nil {
t.Fatal("the handle took the peer's entry as its base")
}
if size := fh.GetEntry().GetEntry().Attributes.FileSize; size != uint64(len(data)) {
t.Fatalf("live entry size = %d, want %d", size, len(data))
}
if fh.isDeleted {
t.Fatal("the handle was marked deleted; its flush would be skipped")
}
}
// Two mounts create the same name, each before the other publishes. The
// peer's create reaching this mount must not replace the writes of the file
// this mount created: its flush then publishes nothing and close() succeeds
// although the writes are gone.
func TestPeerCreateKeepsUnpublishedCreateWrites(t *testing.T) {
wfs := newInvalidateTestWFS(t)
fh, inode := openUnpublishedCreate(t, wfs, util.FullPath("/dir/file"), []byte("BBBB"))
pages := fh.dirtyPages
applyPeerEvent(t, wfs, &filer_pb.SubscribeMetadataResponse{
Directory: "/dir",
TsNs: 1500,
EventNotification: &filer_pb.EventNotification{
NewEntry: &filer_pb.Entry{
Name: "file",
Attributes: &filer_pb.FuseAttributes{FileSize: 10, Inode: inode + 1000, Crtime: 2, Mtime: 2},
},
NewParentPath: "/dir",
},
})
requireWritesKept(t, fh, pages, []byte("BBBB"))
}
// The peer deleted its own file at the name; the file this mount created was
// never on the filer and is not unlinked.
func TestPeerDeleteDoesNotUnlinkUnpublishedCreate(t *testing.T) {
wfs := newInvalidateTestWFS(t)
fh, _ := openUnpublishedCreate(t, wfs, util.FullPath("/dir/file"), []byte("BBBB"))
pages := fh.dirtyPages
applyPeerEvent(t, wfs, &filer_pb.SubscribeMetadataResponse{
Directory: "/dir",
TsNs: 1500,
EventNotification: &filer_pb.EventNotification{
OldEntry: &filer_pb.Entry{Name: "file"},
},
})
requireWritesKept(t, fh, pages, []byte("BBBB"))
}
// The peer renamed its own file away from the name; the file this mount
// created stays on its name instead of following the peer's file.
func TestPeerRenameAwayLeavesUnpublishedCreateOnItsName(t *testing.T) {
wfs := newInvalidateTestWFS(t)
path := util.FullPath("/dir/file")
fh, inode := openUnpublishedCreate(t, wfs, path, []byte("BBBB"))
pages := fh.dirtyPages
applyPeerEvent(t, wfs, &filer_pb.SubscribeMetadataResponse{
Directory: "/dir",
TsNs: 1500,
EventNotification: &filer_pb.EventNotification{
OldEntry: &filer_pb.Entry{Name: "file"},
NewEntry: &filer_pb.Entry{Name: "moved", Attributes: &filer_pb.FuseAttributes{FileSize: 10}},
NewParentPath: "/dir",
},
})
requireWritesKept(t, fh, pages, []byte("BBBB"))
if got, status := wfs.inodeToPath.GetPath(inode); status != fuse.OK || got != path {
t.Fatalf("inode resolves to %q (%v), want %s", got, status, path)
}
}
// The peer renamed another file onto the name. That replaces nothing of the
// file this mount created, so its handle is not marked deleted; its flush is
// refused instead of skipped, so the writes are not dropped silently.
func TestPeerRenameOntoUnpublishedCreateDoesNotMarkItDeleted(t *testing.T) {
wfs := newInvalidateTestWFS(t)
fh, _ := openUnpublishedCreate(t, wfs, util.FullPath("/dir/file"), []byte("BBBB"))
pages := fh.dirtyPages
wfs.inodeToPath.Lookup(util.FullPath("/dir/other"), time.Now().Unix(), false, false, 0, true)
applyPeerEvent(t, wfs, &filer_pb.SubscribeMetadataResponse{
Directory: "/dir",
TsNs: 1500,
EventNotification: &filer_pb.EventNotification{
OldEntry: &filer_pb.Entry{Name: "other"},
NewEntry: &filer_pb.Entry{Name: "file", Attributes: &filer_pb.FuseAttributes{FileSize: 10}},
NewParentPath: "/dir",
},
})
requireWritesKept(t, fh, pages, []byte("BBBB"))
}
// Control: a file the filer has is destroyed by a rename over it, and its
// handle must not flush the old content back over what took the name.
func TestPeerRenameOntoPublishedFileMarksItDeleted(t *testing.T) {
wfs := newInvalidateTestWFS(t)
inode := wfs.inodeToPath.Lookup(util.FullPath("/dir/file"), time.Now().Unix(), false, false, 0, true)
fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, &filer_pb.Entry{
Name: "file",
Attributes: &filer_pb.FuseAttributes{FileSize: 200, Inode: inode},
}, 0, 0)
fh.dirtyMetadata = true
wfs.inodeToPath.Lookup(util.FullPath("/dir/other"), time.Now().Unix(), false, false, 0, true)
applyPeerEvent(t, wfs, &filer_pb.SubscribeMetadataResponse{
Directory: "/dir",
TsNs: 1500,
EventNotification: &filer_pb.EventNotification{
OldEntry: &filer_pb.Entry{Name: "other"},
NewEntry: &filer_pb.Entry{Name: "file", Attributes: &filer_pb.FuseAttributes{FileSize: 10}},
NewParentPath: "/dir",
},
})
if !fh.isDeleted {
t.Fatal("a published file replaced by a rename was not marked deleted")
}
}