mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
* mount: keep delayed events about an earlier file off the next file at its name A writer that publishes a status file by writing NNNN.json.tmp-PID, fsyncing it and renaming it over NNNN.json recreates the tmp name every few seconds. Without -writebackCache a mount applies its own events from the metadata subscription after the local apply (the events of a rename stream carry the response time, not the log position, so the dedup ring does not match them), and their invalidations reach whatever file sits at the path by then. With about 200 such writers in one directory, two of these events failed a round of the next file: - the rename of round k moved round k+1's tmp file to NNNN.json. Its handle published under that name and was refused (EIO); the retry wrote into NNNN.json, and the rename of the tmp name failed with ENOENT. - the create of round k became the entry and base of round k+1's file, and every flush of the round was refused (EIO). Both hit the new file in the window in which Create had put its handle in the map but not yet marked it an unpublished create: it did that afterwards, holding no lock, so the event found a clean handle with a base and took it for a published file. On master the second event, which carries the inode number the creating mount stored, makes the handle give the name up instead (replacedByAnotherFile) and marks it deleted: its writes are never published, and close returns success. - AcquireCreatedFileHandle puts a new handle in the state the create leaves it in before the handle is in the map. An existing handle, whose inode number outlived an earlier file, is installed under its lock. - An invalidation carries which file its event is about: the inode number the file's creator stored and its creation time (hard links excluded). An open handle ignores a rename of another file, and any other event about an earlier one (another number, created before the file held). A later file at the name still reaches the handle. - MovePath takes the inode number a rename event reports. A source that maps to another inode, while the table knows the renamed file by that number, holds another file and stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: judge another file by its creation time only for this mount's own events A peer's file can take the name with an older creation time: renamed in from another name, or created on a host whose clock runs behind. Dropping its event left the name on the replaced file's inode. A peer's rename can also name a file by a stored inode number another file here holds, when two mounts made the same path within one second, and was refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: a delete of another file at the name leaves the open handle alone Like a rename, a delete names the file it removed. A peer's delayed delete of an earlier file at the name, logged by a filer other than the one behind the handle's version, marked the held file deleted and dropped its writes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
218 lines
8.3 KiB
Go
218 lines
8.3 KiB
Go
//go:build !freebsd
|
|
|
|
package mount
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/seaweedfs/go-fuse/v2/fuse"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/mount/meta_cache"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
)
|
|
|
|
// peerUnlink removes name on the filer the way another mount does and
|
|
// delivers the delete event this mount's subscription receives.
|
|
func peerUnlink(t *testing.T, wfs *WFS, fake *hardLinkFiler, name string) {
|
|
t.Helper()
|
|
old := fake.stored(name)
|
|
if _, err := fake.DeleteEntry(context.Background(), &filer_pb.DeleteEntryRequest{Directory: "/", Name: name}); err != nil {
|
|
t.Fatalf("peer delete %s: %v", name, err)
|
|
}
|
|
fake.mu.Lock()
|
|
fake.stamp++
|
|
ts := fake.stamp
|
|
fake.mu.Unlock()
|
|
del := &filer_pb.SubscribeMetadataResponse{Directory: "/", TsNs: ts, EventNotification: &filer_pb.EventNotification{OldEntry: old}}
|
|
if err := wfs.metaCache.ApplyMetadataResponse(context.Background(), del, meta_cache.SubscriberMetadataResponseApplyOptions); err != nil {
|
|
t.Fatalf("apply peer delete: %v", err)
|
|
}
|
|
wfs.metaCache.WaitForEntryInvalidations()
|
|
}
|
|
|
|
// Another mount removes one of the two names of a file open here. The handle
|
|
// keeps publishing through the other name, over the link count and ctime the
|
|
// filer stored there, and the removed name stays removed.
|
|
func TestPeerUnlinkedAliasKeepsOpenHandlePublishing(t *testing.T) {
|
|
for _, removed := range []string{"tmp", "final"} {
|
|
for _, dirty := range []bool{false, true} {
|
|
t.Run(removed+map[bool]string{false: "/clean", true: "/dirty"}[dirty], func(t *testing.T) {
|
|
wfs, fake, fh, inode := openLinkTestFile(t)
|
|
linkInRoot(t, wfs, inode, "final")
|
|
chunks := 2
|
|
if dirty {
|
|
fh.UpdateEntry(func(e *filer_pb.Entry) {
|
|
e.Chunks = append(e.Chunks, linkHandleChunk(9, 100))
|
|
e.Attributes.FileSize = 200
|
|
})
|
|
fh.dirtyMetadata = true
|
|
chunks = 3
|
|
}
|
|
survivor := map[string]string{"tmp": "final", "final": "tmp"}[removed]
|
|
peerUnlink(t, wfs, fake, removed)
|
|
if fh.isDeleted {
|
|
t.Fatal("a peer unlinking one of two names marked the open handle deleted")
|
|
}
|
|
unlinked := fake.stored(survivor).Attributes
|
|
if code := appendAndFlush(t, wfs, fh, 2); code != fuse.OK {
|
|
t.Fatalf("flush after the peer unlink: %v", code)
|
|
}
|
|
wantStored(t, fake, survivor, chunks, 1)
|
|
if got := fake.stored(survivor).Attributes; got.Ctime != unlinked.Ctime || got.CtimeNs != unlinked.CtimeNs {
|
|
t.Fatalf("the flush moved the ctime from the unlink's %d.%09d back to %d.%09d", unlinked.Ctime, unlinked.CtimeNs, got.Ctime, got.CtimeNs)
|
|
}
|
|
if fake.stored(removed) != nil {
|
|
t.Fatalf("the flush recreated %s", removed)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// The surviving name was found by a lookup, not linked by this mount.
|
|
func TestPeerUnlinkedAliasKeepsLookedUpSurvivor(t *testing.T) {
|
|
wfs, fake, fh, inode := openLinkTestFile(t)
|
|
linkInRoot(t, wfs, inode, "final")
|
|
wfs.inodeToPath.RemovePath("/final", nil)
|
|
wfs.inodeToPath.Lookup("/final", 1, false, true, inode, true)
|
|
peerUnlink(t, wfs, fake, "tmp")
|
|
if fh.isDeleted {
|
|
t.Fatal("a peer unlinking the open name of a file with a looked-up second name marked the handle deleted")
|
|
}
|
|
if code := appendAndFlush(t, wfs, fh, 2); code != fuse.OK {
|
|
t.Fatalf("flush after the peer unlink: %v", code)
|
|
}
|
|
wantStored(t, fake, "final", 2, 1)
|
|
if fake.stored("tmp") != nil {
|
|
t.Fatal("the flush recreated tmp")
|
|
}
|
|
}
|
|
|
|
// With no other name known here, the handle stops rather than recreating the
|
|
// removed one.
|
|
func TestPeerUnlinkedAliasWithUnknownSurvivorStops(t *testing.T) {
|
|
wfs, fake, fh, inode := openLinkTestFile(t)
|
|
linkInRoot(t, wfs, inode, "final")
|
|
wfs.inodeToPath.RemovePath("/final", nil)
|
|
peerUnlink(t, wfs, fake, "tmp")
|
|
if !fh.isDeleted {
|
|
t.Fatal("a handle with no known surviving name was left publishing")
|
|
}
|
|
if code := appendAndFlush(t, wfs, fh, 2); code != fuse.OK {
|
|
t.Fatalf("flush: %v", code)
|
|
}
|
|
if fake.stored("tmp") != nil {
|
|
t.Fatal("the flush recreated tmp")
|
|
}
|
|
}
|
|
|
|
// The kernel keeps the nodeid of a closed hard link after a peer unlinks the
|
|
// name it was opened by, and the next open must reach the surviving name.
|
|
func TestPeerUnlinkedAliasReopensClosedInode(t *testing.T) {
|
|
for _, removed := range []string{"tmp", "final"} {
|
|
t.Run(removed, func(t *testing.T) {
|
|
wfs, fake, fh, inode := openLinkTestFile(t)
|
|
wfs.openMtimeCache = map[uint64][2]int64{}
|
|
linkInRoot(t, wfs, inode, "final")
|
|
wfs.ReleaseHandle(fh.fh)
|
|
if _, found := wfs.fhMap.FindFileHandle(inode); found {
|
|
t.Fatal("handle still open")
|
|
}
|
|
peerUnlink(t, wfs, fake, removed)
|
|
survivor := map[string]string{"tmp": "/final", "final": "/tmp"}[removed]
|
|
in := &fuse.OpenIn{InHeader: fuse.InHeader{NodeId: inode, Caller: fuse.Caller{Owner: fuse.Owner{Uid: 1000, Gid: 20}}}, Flags: uint32(os.O_RDONLY)}
|
|
out := &fuse.OpenOut{}
|
|
if code := wfs.Open(make(chan struct{}), in, out); code != fuse.OK {
|
|
t.Fatalf("open after the peer unlinked %s: %v", removed, code)
|
|
}
|
|
reopened := wfs.GetHandle(FileHandleId(out.Fh))
|
|
t.Cleanup(func() { wfs.ReleaseHandle(reopened.fh) })
|
|
if got := reopened.FullPath(); string(got) != survivor {
|
|
t.Fatalf("reopened at %s, want %s", got, survivor)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// This mount renames another name of the closed file onto the one a peer
|
|
// unlinked before the peer's delete arrives, so that name is live again.
|
|
func TestDelayedPeerUnlinkKeepsRenamedOntoName(t *testing.T) {
|
|
wfs := newInvalidateTestWFS(t)
|
|
wfs.inodeToPath.MarkChildrenCached("/")
|
|
ctx := context.Background()
|
|
link := func(name string) *filer_pb.Entry {
|
|
return &filer_pb.Entry{Name: name, HardLinkId: []byte("link"), HardLinkCounter: 3, Attributes: &filer_pb.FuseAttributes{Inode: 42, FileMode: 0o600}}
|
|
}
|
|
for _, name := range []string{"tmp", "final", "third"} {
|
|
if err := wfs.metaCache.InsertEntry(ctx, filer.FromPbEntry("/", link(name)), 1); err != nil {
|
|
t.Fatalf("insert %s: %v", name, err)
|
|
}
|
|
}
|
|
inode := wfs.inodeToPath.Lookup("/tmp", 1, false, true, 42, true)
|
|
wfs.inodeToPath.AddPath(inode, "/final")
|
|
wfs.inodeToPath.AddPath(inode, "/third")
|
|
|
|
rename := &filer_pb.SubscribeMetadataResponse{Directory: "/", TsNs: 20, EventNotification: &filer_pb.EventNotification{OldEntry: link("final"), NewEntry: link("tmp"), NewParentPath: "/"}}
|
|
if err := wfs.applyLocalMetadataEvent(ctx, rename); err != nil {
|
|
t.Fatalf("apply local rename: %v", err)
|
|
}
|
|
wfs.inodeToPath.MovePath("/final", "/tmp", 0)
|
|
del := &filer_pb.SubscribeMetadataResponse{Directory: "/", TsNs: 10, EventNotification: &filer_pb.EventNotification{OldEntry: link("tmp")}}
|
|
if err := wfs.metaCache.ApplyMetadataResponse(ctx, del, meta_cache.SubscriberMetadataResponseApplyOptions); err != nil {
|
|
t.Fatalf("apply peer delete: %v", err)
|
|
}
|
|
wfs.metaCache.WaitForEntryInvalidations()
|
|
|
|
if got, found := wfs.inodeToPath.GetInode("/tmp"); !found || got != inode {
|
|
t.Fatalf("/tmp maps to %d (%v), want %d", got, found, inode)
|
|
}
|
|
}
|
|
|
|
// A peer removes one name, then this mount unlinks the last one.
|
|
func TestPeerThenLocalUnlinkKeepsHandleDeleted(t *testing.T) {
|
|
wfs, fake, fh, inode := openLinkTestFile(t)
|
|
linkInRoot(t, wfs, inode, "final")
|
|
peerUnlink(t, wfs, fake, "final")
|
|
unlinkInRoot(t, wfs, "tmp")
|
|
if !fh.isDeleted {
|
|
t.Fatal("the handle of a file with no names left is not marked deleted")
|
|
}
|
|
if code := appendAndFlush(t, wfs, fh, 2); code != fuse.OK {
|
|
t.Fatalf("flush after the last unlink: %v", code)
|
|
}
|
|
for _, name := range []string{"tmp", "final"} {
|
|
if fake.stored(name) != nil {
|
|
t.Fatalf("the flush recreated %s", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPeerUnlinkLastNameKeepsHandleDeleted(t *testing.T) {
|
|
wfs, fake, fh, _ := openLinkTestFile(t)
|
|
peerUnlink(t, wfs, fake, "tmp")
|
|
if !fh.isDeleted {
|
|
t.Fatal("a peer unlinking the only name did not mark the handle deleted")
|
|
}
|
|
}
|
|
|
|
// The periodic metadata flush publishes through the survivor as well.
|
|
func TestPeerUnlinkedAliasKeepsPeriodicFlushPublishing(t *testing.T) {
|
|
wfs, fake, fh, inode := openLinkTestFile(t)
|
|
linkInRoot(t, wfs, inode, "final")
|
|
peerUnlink(t, wfs, fake, "tmp")
|
|
fh.UpdateEntry(func(e *filer_pb.Entry) {
|
|
e.Chunks = append(e.Chunks, linkHandleChunk(2, 100))
|
|
e.Attributes.FileSize = 200
|
|
})
|
|
fh.dirtyMetadata = true
|
|
if err := wfs.flushFileMetadata(fh); err != nil {
|
|
t.Fatalf("periodic flush after the peer unlink: %v", err)
|
|
}
|
|
wantStored(t, fake, "final", 2, 1)
|
|
if fake.stored("tmp") != nil {
|
|
t.Fatal("the periodic flush recreated tmp")
|
|
}
|
|
}
|