mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-19 02:15:02 +01:00
mount: follow a peer unlink to the surviving hard link for closed inodes (#12737)
* mount: drop a peer-unlinked name only while its inode keeps another Move the survivor check from followSurvivingLink into InodeToPath.RemoveAlias so the check and the removal happen under one table lock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: follow a peer unlink to the surviving hard link for closed inodes A peer deleting one name of a hard link only moved an open handle to the surviving name. A closed inode kept the deleted name as its primary path, so the next open by the kernel's nodeid failed with ENOENT. Co-authored-by: skymazer <54621405+skymazer@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: share the cached-entry freshness check of peer invalidations Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep a name a local rename reclaimed before the peer unlink arrived Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: keep a name a local rename or link gave the inode after an older peer delete The cached-entry check only covers a cached parent. InodeToPath now records, per hard-linked inode, the newest name a local rename or link gave it with the filer stamp, and the closed-inode peer-delete path keeps that name when the delete is older. The record lives in a side map dropped on Forget, so InodeEntry keeps its size class. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: sign rename stream responses with the filer signature A rename response carries the stamp of the filer that applied it, so the mount can tell which clock the stamp belongs to, as it does for create acks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: order a peer delete against a local rename or link only on one filer clock The stamp of a local rename or link comes from the filer that applied it, and a delete logged by another filer runs on a different clock. Keep the name only when both carry the same nonzero filer signature; otherwise drop it as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: drop the local-name stamp fence for uncached parents Ordering a delayed peer delete against a local rename by filer stamp needs a record per alias, a shared filer clock, and the open-handle path to follow; that is a larger change than this fix. Back to the cached-entry check, which covers a cached parent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: skymazer <54621405+skymazer@users.noreply.github.com>
This commit is contained in:
4 files changed
+133
-18
No files matched your search
@@ -629,6 +629,32 @@ func (i *InodeToPath) RemovePath(path util.FullPath, onStillReferenced func(inod
|
||||
delete(i.wasCachedDirs, path)
|
||||
}
|
||||
|
||||
// RemoveAlias drops a name only while its inode keeps another one, returning
|
||||
// the inode's primary path afterwards. A sole name stays for a delayed rename
|
||||
// to move.
|
||||
func (i *InodeToPath) RemoveAlias(path util.FullPath) (util.FullPath, bool) {
|
||||
i.Lock()
|
||||
defer i.Unlock()
|
||||
inode, found := i.path2inode[path]
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
ie := i.inode2path[inode]
|
||||
if ie == nil {
|
||||
return "", false
|
||||
}
|
||||
survives := false
|
||||
for _, p := range ie.appendPaths(nil) {
|
||||
survives = survives || p != path
|
||||
}
|
||||
if !survives {
|
||||
return "", false
|
||||
}
|
||||
delete(i.path2inode, path)
|
||||
ie.removeOnePath(path)
|
||||
return ie.path, true
|
||||
}
|
||||
|
||||
func (i *InodeToPath) removePathFromInode2Path(inode uint64, path util.FullPath) {
|
||||
ie, found := i.inode2path[inode]
|
||||
if !found {
|
||||
|
||||
@@ -230,3 +230,18 @@ func TestLookupRecordsHardLinkNames(t *testing.T) {
|
||||
t.Fatal("the final forget left /b mapped to a released inode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveAliasKeepsSoleName(t *testing.T) {
|
||||
table := NewInodeToPath(util.FullPath("/"), 0)
|
||||
inode := table.Lookup("/a", 1, false, true, 42, true)
|
||||
if _, ok := table.RemoveAlias("/a"); ok || !table.HasPath("/a") {
|
||||
t.Fatal("removed the only name")
|
||||
}
|
||||
table.AddPath(inode, "/b")
|
||||
if survivor, ok := table.RemoveAlias("/a"); !ok || survivor != "/b" || table.HasPath("/a") {
|
||||
t.Fatalf("RemoveAlias(/a) = %q, %v, want /b", survivor, ok)
|
||||
}
|
||||
if _, ok := table.RemoveAlias("/b"); ok || !table.HasPath("/b") {
|
||||
t.Fatal("removed the surviving name")
|
||||
}
|
||||
}
|
||||
+28
-18
@@ -966,6 +966,12 @@ func (wfs *WFS) onEntryInvalidation(invalidation meta_cache.EntryInvalidation) {
|
||||
handled, replacedInode := wfs.invalidateOpenFileHandle(invalidation)
|
||||
if !handled && invalidation.RenamedTo != "" {
|
||||
_, replacedInode = wfs.inodeToPath.MovePath(invalidation.Path, invalidation.RenamedTo)
|
||||
} else if !handled && invalidation.Deleted && len(invalidation.LiveHardLinkId) > 0 {
|
||||
// Otherwise the next open by nodeid resolves to the deleted name. A
|
||||
// newer stored entry means something has taken the name since.
|
||||
if _, _, newer := wfs.cachedEntrySince(invalidation.Path, invalidation.TsNs); !newer {
|
||||
wfs.inodeToPath.RemoveAlias(invalidation.Path)
|
||||
}
|
||||
}
|
||||
// A rename over an existing file destroys it, so its handle must not flush
|
||||
// the old content back over what took the name. Marked from here, holding
|
||||
@@ -1063,12 +1069,9 @@ func (wfs *WFS) invalidateOpenFileHandle(invalidation meta_cache.EntryInvalidati
|
||||
// hit there is a stale leftover — use the event entry instead.
|
||||
var candidate *filer_pb.Entry
|
||||
candidateTsNs := eventTsNs
|
||||
dir, _ := filePath.DirAndName()
|
||||
if wfs.metaCache.IsDirectoryCached(util.FullPath(dir)) {
|
||||
if storeEntry, storeVersionTsNs, findErr := wfs.metaCache.FindEntry(context.Background(), filePath); findErr == nil && storeEntry != nil && storeVersionTsNs >= eventTsNs {
|
||||
candidate = storeEntry.ToProtoEntry()
|
||||
candidateTsNs = storeVersionTsNs
|
||||
}
|
||||
if storeEntry, storeVersionTsNs, found := wfs.cachedEntrySince(filePath, eventTsNs); found {
|
||||
candidate = storeEntry.ToProtoEntry()
|
||||
candidateTsNs = storeVersionTsNs
|
||||
}
|
||||
if candidate == nil && eventEntry != nil {
|
||||
candidate = proto.Clone(eventEntry).(*filer_pb.Entry)
|
||||
@@ -1089,7 +1092,7 @@ func (wfs *WFS) invalidateOpenFileHandle(invalidation meta_cache.EntryInvalidati
|
||||
if invalidation.Deleted {
|
||||
// The next flush reads the survivor's stored entry before
|
||||
// publishing, so the version stays where it was.
|
||||
if len(invalidation.LiveHardLinkId) > 0 && wfs.followSurvivingLink(fh, inode, filePath) {
|
||||
if len(invalidation.LiveHardLinkId) > 0 && wfs.followSurvivingLink(fh, filePath) {
|
||||
fh.linkRefresh = invalidation.LiveHardLinkId
|
||||
return
|
||||
}
|
||||
@@ -1137,22 +1140,29 @@ func (wfs *WFS) invalidateOpenFileHandle(invalidation meta_cache.EntryInvalidati
|
||||
return
|
||||
}
|
||||
|
||||
// cachedEntrySince returns the meta cache's entry at path when its directory is
|
||||
// cached and the entry is no older than tsNs.
|
||||
func (wfs *WFS) cachedEntrySince(path util.FullPath, tsNs int64) (*filer.Entry, int64, bool) {
|
||||
dir, _ := path.DirAndName()
|
||||
if !wfs.metaCache.IsDirectoryCached(util.FullPath(dir)) {
|
||||
return nil, 0, false
|
||||
}
|
||||
entry, versionTsNs, err := wfs.metaCache.FindEntry(context.Background(), path)
|
||||
if err != nil || entry == nil || versionTsNs < tsNs {
|
||||
return nil, 0, false
|
||||
}
|
||||
return entry, versionTsNs, true
|
||||
}
|
||||
|
||||
// followSurvivingLink drops a hard link name another client deleted and
|
||||
// keeps the handle on a name of the inode this mount still knows, reporting
|
||||
// false when it knows none.
|
||||
func (wfs *WFS) followSurvivingLink(fh *FileHandle, inode uint64, removed util.FullPath) bool {
|
||||
survives := false
|
||||
for _, p := range wfs.inodeToPath.GetAllPaths(inode) {
|
||||
survives = survives || p != removed
|
||||
}
|
||||
if !survives {
|
||||
return false
|
||||
}
|
||||
wfs.inodeToPath.RemovePath(removed, nil)
|
||||
if survivor, status := wfs.inodeToPath.GetPath(inode); status == fuse.OK {
|
||||
func (wfs *WFS) followSurvivingLink(fh *FileHandle, removed util.FullPath) bool {
|
||||
survivor, ok := wfs.inodeToPath.RemoveAlias(removed)
|
||||
if ok {
|
||||
fh.RememberPath(survivor)
|
||||
}
|
||||
return true
|
||||
return ok
|
||||
}
|
||||
|
||||
func (wfs *WFS) LookupFn() wdclient.LookupFileIdFunctionType {
|
||||
|
||||
@@ -4,9 +4,11 @@ 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"
|
||||
)
|
||||
@@ -106,6 +108,68 @@ func TestPeerUnlinkedAliasWithUnknownSurvivorStops(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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")
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user