Files
seaweedfs/weed/mount/weedfs_remote_rename_test.go
T
Chris LuandDevin 0ea818a44f mount: drain OFD locks when a file description is released (#12117)
* mount: drain OFD locks when a file description is released

The kernel scopes OFD fcntl locks to the open file description but
reports no lock_owner for them at RELEASE — it only sends one for flock
holders — so an OFD write lock taken on a file handle outlived the
holder and blocked every later open of that file (e.g. etcd failing to
reopen its WAL after a restart). The mount now records the owner of each
granted non-flock lock under its file handle and releases those owners
on RELEASE, which also covers any POSIX locks left over when FLUSH could
not run.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* mount: give every open its own fh value over the shared handle

AcquireFileHandle reuses one FileHandle per inode, so every open reported
the same fh to the kernel. A RELEASE for one description could then not
be told apart from a sibling still holding the inode open — draining
lock owners recorded under the shared fh dropped locks belonging to
descriptions that stay open.

Each open now registers its own fh value in fh2inode, all resolving to
the shared FileHandle, and RELEASE removes exactly the id that is
closing. This also stops a duplicated RELEASE on one id from
double-decrementing the open count.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* mount: forget flushed lock owners so a later release cannot re-drop locks

POSIX lock owners are process-scoped: Flush releases every lock the
process holds on the inode, but the owner stayed recorded under each
open's fh for the Release-time drain. If the process took a fresh lock
through another open before an old description finished closing, the
stale record made that RELEASE drop the new lock too.

Flush now drops the released owner from every recorded open of the
inode, so the Release drain only ever fires for owners whose locks were
never flushed.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* mount: keep close-time lock owner records when the release fails

A routed POSIX owner release that errors keeps the flush hint for a
retry, but Flush also dropped the owner from every open's release
records unconditionally — so the final Release skipped the retry and
the owner filer's lock was stranded until lease reaping. Only forget
the owner once its locks were actually released.

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-26 22:02:13 -07:00

123 lines
4.8 KiB
Go

package mount
import (
"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"
)
// The filer sends one event per moved entry, so children follow their parent.
func TestRemoteRenameMovesInodePaths(t *testing.T) {
dir := util.FullPath("/images")
wfs := newPagingWFS(t, dir, []string{"f.jpg"}, 0)
now := time.Now().Unix()
subInode := wfs.inodeToPath.Lookup(dir.Child("sub"), now, true, false, 0, true)
childInode := wfs.inodeToPath.Lookup(dir.Child("sub").Child("f.jpg"), now, false, false, 0, true)
wfs.onEntryInvalidation(meta_cache.EntryInvalidation{
Path: dir.Child("sub"),
RenamedTo: dir.Child("moved"),
WasDirectory: true,
})
wfs.onEntryInvalidation(meta_cache.EntryInvalidation{
Path: dir.Child("sub").Child("f.jpg"),
RenamedTo: dir.Child("moved").Child("f.jpg"),
})
if got, _ := wfs.inodeToPath.GetPath(subInode); got != dir.Child("moved") {
t.Errorf("renamed directory resolves to %s, want %s", got, dir.Child("moved"))
}
if got, _ := wfs.inodeToPath.GetPath(childInode); got != dir.Child("moved").Child("f.jpg") {
t.Errorf("child resolves to %s, want %s", got, dir.Child("moved").Child("f.jpg"))
}
if wfs.inodeToPath.HasPath(dir.Child("sub")) {
t.Error("pre-rename directory path still in the table")
}
}
// The handle path and the table move are two halves of one rename. Doing the
// move twice would unlink what the first one just put at the target, leaving
// the renamed inode with no path at all.
func TestRemoteRenameMovesOnceWithAnOpenHandle(t *testing.T) {
wfs := newInvalidateTestWFS(t)
oldPath, newPath := util.FullPath("/dir/file"), util.FullPath("/dir/renamed")
inode := wfs.inodeToPath.Lookup(oldPath, time.Now().Unix(), false, false, 0, true)
wfs.fhMap.AcquireFileHandle(wfs, inode, &filer_pb.Entry{
Name: "file",
Attributes: &filer_pb.FuseAttributes{FileSize: 88},
}, 0, 0)
wfs.onEntryInvalidation(meta_cache.EntryInvalidation{Path: oldPath, RenamedTo: newPath, TsNs: 1000})
if got, status := wfs.inodeToPath.GetPath(inode); status != fuse.OK || got != newPath {
t.Errorf("inode resolves to %q (%v), want %s", got, status, newPath)
}
if got, found := wfs.inodeToPath.GetInode(newPath); !found || got != inode {
t.Errorf("%s resolves to %d (found %v), want %d", newPath, got, found, inode)
}
}
// The subscription can redeliver a rename once it falls out of the dedup ring.
// Replaying one must not unlink what the first delivery put at the target, nor
// mark the moved file's handle deleted.
func TestRemoteRenameReplayLeavesTheTargetAlone(t *testing.T) {
wfs := newInvalidateTestWFS(t)
oldPath, newPath := util.FullPath("/dir/file"), util.FullPath("/dir/renamed")
inode := wfs.inodeToPath.Lookup(oldPath, time.Now().Unix(), false, false, 0, true)
fh, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, &filer_pb.Entry{
Name: "file",
Attributes: &filer_pb.FuseAttributes{FileSize: 88},
}, 0, 0)
rename := meta_cache.EntryInvalidation{Path: oldPath, RenamedTo: newPath, TsNs: 1000}
wfs.onEntryInvalidation(rename)
wfs.onEntryInvalidation(rename)
if got, status := wfs.inodeToPath.GetPath(inode); status != fuse.OK || got != newPath {
t.Errorf("inode resolves to %q (%v), want %s", got, status, newPath)
}
if got, found := wfs.inodeToPath.GetInode(newPath); !found || got != inode {
t.Errorf("%s resolves to %d (found %v), want %d", newPath, got, found, inode)
}
if fh.isDeleted {
t.Error("the moved file's handle was marked deleted by the replay")
}
}
// A rename event older than the handle's version fence is still a rename. The
// fence is about which version of the content the handle holds, and skipping
// the move would leave both the inode and the handle on a name the filer has
// vacated.
func TestRenameBelowTheVersionFenceStillMoves(t *testing.T) {
wfs := newInvalidateTestWFS(t)
oldPath, newPath := util.FullPath("/dir/file"), util.FullPath("/dir/renamed")
inode := wfs.inodeToPath.Lookup(oldPath, time.Now().Unix(), false, false, 0, true)
fh, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, &filer_pb.Entry{
Name: "file",
Attributes: &filer_pb.FuseAttributes{FileSize: 88},
}, 0, 0)
// The handle already reflects a later position in the same clock domain, so
// the fence would drop this event.
fh.advanceEntryVersion(5000, wfs.signature)
wfs.onEntryInvalidation(meta_cache.EntryInvalidation{
Path: oldPath, RenamedTo: newPath, TsNs: 1000,
Signatures: []int32{wfs.signature},
})
if got, status := wfs.inodeToPath.GetPath(inode); status != fuse.OK || got != newPath {
t.Errorf("inode resolves to %q (%v), want %s", got, status, newPath)
}
if dir, name := fh.savedDir, fh.savedName; util.FullPath(dir).Child(name) != newPath {
t.Errorf("handle remembers %s/%s, want %s", dir, name, newPath)
}
}