Files
seaweedfs/weed/mount/weedfs_file_io_test.go
Chris Lu 9896eade51 feat(mount): set FOPEN_KEEP_CACHE on re-open of unchanged files (#9097)
* feat(mount): set FOPEN_KEEP_CACHE when file mtime is unchanged

On re-open of an unmodified file, signal the kernel to preserve its
existing page cache. This eliminates redundant volume server reads for
workloads that repeatedly open-read-close the same files (build systems,
config readers, etc.).

* fix(mount): use guarded type assertion for openMtimeCache load

Use the two-value form of type assertion when loading from sync.Map
to prevent potential panics if a non-int64 value is ever stored.

* fix(mount): skip redundant mtime store and invalidate on truncation

- Avoid redundant sync.Map Store when cached mtime already matches
  the current mtime, reducing contention on the hot open path.
- Invalidate openMtimeCache in SetAttr when file size changes
  (truncation), preventing stale kernel page cache after ftruncate.

* fix(mount): use nanosecond mtime precision and bounded cache for FOPEN_KEEP_CACHE

- Compare both Mtime (seconds) and MtimeNs (nanoseconds) to detect
  sub-second modifications common in automated workloads.
- Replace unbounded sync.Map with a bounded map + mutex (8192 entries,
  random eviction when full), following the existing atimeMap pattern.
- Extract applyKeepCacheFlag and invalidateOpenMtimeCache methods for
  clarity and testability.
- Add tests for nanosecond precision and cache eviction.

* fix(mount): invalidate mtime cache in truncateEntry for O_TRUNC consistency

Add invalidateOpenMtimeCache call to truncateEntry so the Create path
with O_TRUNC follows the same explicit invalidation pattern as SetAttr
and Write.
2026-04-16 11:37:52 -07:00

207 lines
5.0 KiB
Go

package mount
import (
"testing"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
func newTestWFS() *WFS {
return &WFS{
openMtimeCache: make(map[uint64][2]int64, 8192),
}
}
func TestOpenKeepCache_FirstOpen(t *testing.T) {
// First open of a file should NOT set FOPEN_KEEP_CACHE because there
// is no previously cached mtime to compare against.
wfs := newTestWFS()
var out fuse.OpenOut
inode := uint64(42)
entry := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 1000, MtimeNs: 123},
},
}
wfs.applyKeepCacheFlag(inode, entry, &out)
if out.OpenFlags&fuse.FOPEN_KEEP_CACHE != 0 {
t.Error("first open should not set FOPEN_KEEP_CACHE")
}
}
func TestOpenKeepCache_SecondOpenSameMtime(t *testing.T) {
// Second open with an unchanged mtime SHOULD set FOPEN_KEEP_CACHE.
wfs := newTestWFS()
inode := uint64(42)
entry := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 1000, MtimeNs: 123},
},
}
// First open -- populate cache.
var out1 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry, &out1)
// Second open -- mtime unchanged.
var out2 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry, &out2)
if out2.OpenFlags&fuse.FOPEN_KEEP_CACHE == 0 {
t.Error("second open with same mtime should set FOPEN_KEEP_CACHE")
}
}
func TestOpenKeepCache_MtimeChanged(t *testing.T) {
// If the file's mtime changes between opens, FOPEN_KEEP_CACHE must NOT
// be set so the kernel invalidates its page cache.
wfs := newTestWFS()
inode := uint64(42)
entry1 := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 1000, MtimeNs: 0},
},
}
// First open.
var out1 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry1, &out1)
// File is modified externally -- mtime changes.
entry2 := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 2000, MtimeNs: 0},
},
}
var out2 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry2, &out2)
if out2.OpenFlags&fuse.FOPEN_KEEP_CACHE != 0 {
t.Error("open after mtime change should not set FOPEN_KEEP_CACHE")
}
}
func TestOpenKeepCache_NanosecondPrecision(t *testing.T) {
// Two modifications within the same second but different nanoseconds
// must NOT reuse cached page data.
wfs := newTestWFS()
inode := uint64(42)
entry1 := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 1000, MtimeNs: 100},
},
}
var out1 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry1, &out1)
// Same second, different nanosecond.
entry2 := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 1000, MtimeNs: 200},
},
}
var out2 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry2, &out2)
if out2.OpenFlags&fuse.FOPEN_KEEP_CACHE != 0 {
t.Error("open after nanosecond-level mtime change should not set FOPEN_KEEP_CACHE")
}
}
func TestOpenKeepCache_WriteInvalidation(t *testing.T) {
// After a write invalidates the mtime cache, the next open should NOT
// set FOPEN_KEEP_CACHE.
wfs := newTestWFS()
inode := uint64(42)
entry := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 1000, MtimeNs: 0},
},
}
// First open -- populate cache.
var out1 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry, &out1)
// Simulate write invalidation.
wfs.invalidateOpenMtimeCache(inode)
// Next open -- cache was invalidated.
var out2 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry, &out2)
if out2.OpenFlags&fuse.FOPEN_KEEP_CACHE != 0 {
t.Error("open after write invalidation should not set FOPEN_KEEP_CACHE")
}
}
func TestOpenKeepCache_WriteOpenSkipped(t *testing.T) {
// Write-mode opens should never evaluate FOPEN_KEEP_CACHE.
// The caller (WFS.Open) gates on O_ANYWRITE before calling
// applyKeepCacheFlag, so we verify the gate logic here.
wfs := newTestWFS()
inode := uint64(42)
entry := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 1000, MtimeNs: 0},
},
}
// Populate cache.
var out1 fuse.OpenOut
wfs.applyKeepCacheFlag(inode, entry, &out1)
// Simulate write-mode open: the caller would skip applyKeepCacheFlag.
var out2 fuse.OpenOut
flags := uint32(fuse.O_ANYWRITE)
if flags&fuse.O_ANYWRITE == 0 {
wfs.applyKeepCacheFlag(inode, entry, &out2)
}
if out2.OpenFlags&fuse.FOPEN_KEEP_CACHE != 0 {
t.Error("write open should not set FOPEN_KEEP_CACHE")
}
}
func TestOpenKeepCache_BoundedEviction(t *testing.T) {
// Verify the cache doesn't grow beyond openMtimeCacheMaxSize.
wfs := newTestWFS()
entry := &LockedEntry{
Entry: &filer_pb.Entry{
Attributes: &filer_pb.FuseAttributes{Mtime: 1000, MtimeNs: 0},
},
}
for i := uint64(0); i < openMtimeCacheMaxSize+100; i++ {
var out fuse.OpenOut
wfs.applyKeepCacheFlag(i, entry, &out)
}
wfs.openMtimeMu.Lock()
size := len(wfs.openMtimeCache)
wfs.openMtimeMu.Unlock()
if size > openMtimeCacheMaxSize {
t.Errorf("cache size %d exceeds max %d", size, openMtimeCacheMaxSize)
}
}