mount metacache add ttl (#6360)

* fix:mount deadlock

* fix

* feat: metaCache ttl

* Update weed/command/mount.go

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>

* fix InodeEntry

---------

Co-authored-by: zemul <zhouzemiao@ihuman.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
zemul 2024-12-17 12:19:32 +08:00 committed by GitHub
parent b2f26804a0
commit e77e50886e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 16 deletions

View File

@ -17,6 +17,7 @@ type MountOptions struct {
ttlSec *int ttlSec *int
chunkSizeLimitMB *int chunkSizeLimitMB *int
concurrentWriters *int concurrentWriters *int
cacheMetaTtlSec *int
cacheDirForRead *string cacheDirForRead *string
cacheDirForWrite *string cacheDirForWrite *string
cacheSizeMBForRead *int64 cacheSizeMBForRead *int64
@ -58,6 +59,7 @@ func init() {
mountOptions.cacheDirForRead = cmdMount.Flag.String("cacheDir", os.TempDir(), "local cache directory for file chunks and meta data") mountOptions.cacheDirForRead = cmdMount.Flag.String("cacheDir", os.TempDir(), "local cache directory for file chunks and meta data")
mountOptions.cacheSizeMBForRead = cmdMount.Flag.Int64("cacheCapacityMB", 128, "file chunk read cache capacity in MB") mountOptions.cacheSizeMBForRead = cmdMount.Flag.Int64("cacheCapacityMB", 128, "file chunk read cache capacity in MB")
mountOptions.cacheDirForWrite = cmdMount.Flag.String("cacheDirWrite", "", "buffer writes mostly for large files") mountOptions.cacheDirForWrite = cmdMount.Flag.String("cacheDirWrite", "", "buffer writes mostly for large files")
mountOptions.cacheMetaTtlSec = cmdMount.Flag.Int("cacheMetaTtlSec", 60, "metadata cache validity seconds")
mountOptions.dataCenter = cmdMount.Flag.String("dataCenter", "", "prefer to write to the data center") mountOptions.dataCenter = cmdMount.Flag.String("dataCenter", "", "prefer to write to the data center")
mountOptions.allowOthers = cmdMount.Flag.Bool("allowOthers", true, "allows other users to access the file system") mountOptions.allowOthers = cmdMount.Flag.Bool("allowOthers", true, "allows other users to access the file system")
mountOptions.umaskString = cmdMount.Flag.String("umask", "022", "octal umask, e.g., 022, 0111") mountOptions.umaskString = cmdMount.Flag.String("umask", "022", "octal umask, e.g., 022, 0111")

View File

@ -236,6 +236,7 @@ func RunMount(option *MountOptions, umask os.FileMode) bool {
CacheDirForRead: *option.cacheDirForRead, CacheDirForRead: *option.cacheDirForRead,
CacheSizeMBForRead: *option.cacheSizeMBForRead, CacheSizeMBForRead: *option.cacheSizeMBForRead,
CacheDirForWrite: cacheDirForWrite, CacheDirForWrite: cacheDirForWrite,
CacheMetaTTlSec: *option.cacheMetaTtlSec,
DataCenter: *option.dataCenter, DataCenter: *option.dataCenter,
Quota: int64(*option.collectionQuota) * 1024 * 1024, Quota: int64(*option.collectionQuota) * 1024 * 1024,
MountUid: uid, MountUid: uid,

View File

@ -10,15 +10,17 @@ import (
type InodeToPath struct { type InodeToPath struct {
sync.RWMutex sync.RWMutex
nextInodeId uint64 nextInodeId uint64
inode2path map[uint64]*InodeEntry cacheMetaTtlSec time.Duration
path2inode map[util.FullPath]uint64 inode2path map[uint64]*InodeEntry
path2inode map[util.FullPath]uint64
} }
type InodeEntry struct { type InodeEntry struct {
paths []util.FullPath paths []util.FullPath
nlookup uint64 nlookup uint64
isDirectory bool isDirectory bool
isChildrenCached bool isChildrenCached bool
cachedExpiresTime time.Time
} }
func (ie *InodeEntry) removeOnePath(p util.FullPath) bool { func (ie *InodeEntry) removeOnePath(p util.FullPath) bool {
@ -43,13 +45,15 @@ func (ie *InodeEntry) removeOnePath(p util.FullPath) bool {
return true return true
} }
func NewInodeToPath(root util.FullPath) *InodeToPath { func NewInodeToPath(root util.FullPath, ttlSec int) *InodeToPath {
t := &InodeToPath{ t := &InodeToPath{
inode2path: make(map[uint64]*InodeEntry), inode2path: make(map[uint64]*InodeEntry),
path2inode: make(map[util.FullPath]uint64), path2inode: make(map[util.FullPath]uint64),
cacheMetaTtlSec: time.Second * time.Duration(ttlSec),
} }
t.inode2path[1] = &InodeEntry{[]util.FullPath{root}, 1, true, false} t.inode2path[1] = &InodeEntry{[]util.FullPath{root}, 1, true, false, time.Time{}}
t.path2inode[root] = 1 t.path2inode[root] = 1
return t return t
} }
@ -92,9 +96,9 @@ func (i *InodeToPath) Lookup(path util.FullPath, unixTime int64, isDirectory boo
} }
} else { } else {
if !isLookup { if !isLookup {
i.inode2path[inode] = &InodeEntry{[]util.FullPath{path}, 0, isDirectory, false} i.inode2path[inode] = &InodeEntry{[]util.FullPath{path}, 0, isDirectory, false, time.Time{}}
} else { } else {
i.inode2path[inode] = &InodeEntry{[]util.FullPath{path}, 1, isDirectory, false} i.inode2path[inode] = &InodeEntry{[]util.FullPath{path}, 1, isDirectory, false, time.Time{}}
} }
} }
@ -157,6 +161,9 @@ func (i *InodeToPath) MarkChildrenCached(fullpath util.FullPath) {
} }
path, found := i.inode2path[inode] path, found := i.inode2path[inode]
path.isChildrenCached = true path.isChildrenCached = true
if i.cacheMetaTtlSec > 0 {
path.cachedExpiresTime = time.Now().Add(i.cacheMetaTtlSec)
}
} }
func (i *InodeToPath) IsChildrenCached(fullpath util.FullPath) bool { func (i *InodeToPath) IsChildrenCached(fullpath util.FullPath) bool {
@ -167,8 +174,11 @@ func (i *InodeToPath) IsChildrenCached(fullpath util.FullPath) bool {
return false return false
} }
path, found := i.inode2path[inode] path, found := i.inode2path[inode]
if found { if !found {
return path.isChildrenCached return false
}
if path.isChildrenCached {
return path.cachedExpiresTime.IsZero() || time.Now().Before(path.cachedExpiresTime)
} }
return false return false
} }

View File

@ -42,6 +42,7 @@ type Option struct {
CacheDirForRead string CacheDirForRead string
CacheSizeMBForRead int64 CacheSizeMBForRead int64
CacheDirForWrite string CacheDirForWrite string
CacheMetaTTlSec int
DataCenter string DataCenter string
Umask os.FileMode Umask os.FileMode
Quota int64 Quota int64
@ -89,7 +90,7 @@ func NewSeaweedFileSystem(option *Option) *WFS {
RawFileSystem: fuse.NewDefaultRawFileSystem(), RawFileSystem: fuse.NewDefaultRawFileSystem(),
option: option, option: option,
signature: util.RandomInt32(), signature: util.RandomInt32(),
inodeToPath: NewInodeToPath(util.FullPath(option.FilerMountRootPath)), inodeToPath: NewInodeToPath(util.FullPath(option.FilerMountRootPath), option.CacheMetaTTlSec),
fhMap: NewFileHandleToInode(), fhMap: NewFileHandleToInode(),
dhMap: NewDirectoryHandleToInode(), dhMap: NewDirectoryHandleToInode(),
fhLockTable: util.NewLockTable[FileHandleId](), fhLockTable: util.NewLockTable[FileHandleId](),