From daabdfe35752cbe3723a5b673c1eb2bcc62ecdbc Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 6 Jun 2018 22:48:51 -0700 Subject: [PATCH] remove nodemap, fix directory listing cache --- weed/filesys/dir.go | 48 ++++++++++++++------------------------------ weed/filesys/file.go | 2 +- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/weed/filesys/dir.go b/weed/filesys/dir.go index c463fde9c..580947d6e 100644 --- a/weed/filesys/dir.go +++ b/weed/filesys/dir.go @@ -5,8 +5,6 @@ import ( "fmt" "os" "path" - "sync" - "bazil.org/fuse" "bazil.org/fuse/fs" "github.com/chrislusf/seaweedfs/weed/glog" @@ -16,10 +14,8 @@ import ( ) type Dir struct { - Path string - NodeMap map[string]fs.Node - NodeMapLock sync.Mutex - wfs *WFS + Path string + wfs *WFS } var _ = fs.Node(&Dir{}) @@ -38,6 +34,18 @@ func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error { return nil } + item := dir.wfs.listDirectoryEntriesCache.Get(dir.Path) + if item != nil && !item.Expired() { + entry := item.Value().(*filer_pb.Entry) + + attr.Mtime = time.Unix(entry.Attributes.Mtime, 0) + attr.Ctime = time.Unix(entry.Attributes.Crtime, 0) + attr.Gid = entry.Attributes.Gid + attr.Uid = entry.Attributes.Uid + + return nil + } + parent, name := filepath.Split(dir.Path) var attributes *filer_pb.FuseAttributes @@ -74,7 +82,7 @@ func (dir *Dir) Attr(context context.Context, attr *fuse.Attr) error { } attr.Mtime = time.Unix(attributes.Mtime, 0) - attr.Ctime = time.Unix(attributes.Mtime, 0) + attr.Ctime = time.Unix(attributes.Crtime, 0) attr.Gid = attributes.Gid attr.Uid = attributes.Uid @@ -121,7 +129,6 @@ func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest, if err == nil { file := dir.newFile(req.Name, nil) - dir.NodeMap[req.Name] = file file.isOpen = true return file, dir.wfs.AcquireHandle(file, req.Uid, req.Gid), nil } @@ -130,8 +137,6 @@ func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest, } func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) { - dir.NodeMapLock.Lock() - defer dir.NodeMapLock.Unlock() err := dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error { @@ -161,7 +166,6 @@ func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, err if err == nil { node := &Dir{Path: path.Join(dir.Path, req.Name), wfs: dir.wfs} - dir.NodeMap[req.Name] = node return node, nil } @@ -170,17 +174,6 @@ func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, err func (dir *Dir) Lookup(ctx context.Context, name string) (node fs.Node, err error) { - dir.NodeMapLock.Lock() - defer dir.NodeMapLock.Unlock() - - if dir.NodeMap == nil { - dir.NodeMap = make(map[string]fs.Node) - } - - if node, ok := dir.NodeMap[name]; ok { - return node, nil - } - var entry *filer_pb.Entry err = dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error { @@ -206,7 +199,6 @@ func (dir *Dir) Lookup(ctx context.Context, name string) (node fs.Node, err erro } else { node = dir.newFile(name, entry.Chunks) } - dir.NodeMap[name] = node return node, nil } @@ -246,9 +238,6 @@ func (dir *Dir) ReadDirAll(ctx context.Context) (ret []fuse.Dirent, err error) { func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { - dir.NodeMapLock.Lock() - defer dir.NodeMapLock.Unlock() - return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error { request := &filer_pb.DeleteEntryRequest{ @@ -264,8 +253,6 @@ func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { return err } - delete(dir.NodeMap, req.Name) - return nil }) @@ -273,9 +260,6 @@ func (dir *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error { - dir.NodeMapLock.Lock() - defer dir.NodeMapLock.Unlock() - newDir := newDirectory.(*Dir) var entry *filer_pb.Entry @@ -338,8 +322,6 @@ func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirector return err } - delete(dir.NodeMap, req.OldName) - } return nil diff --git a/weed/filesys/file.go b/weed/filesys/file.go index 6900190e0..ec98d4d25 100644 --- a/weed/filesys/file.go +++ b/weed/filesys/file.go @@ -34,7 +34,7 @@ func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error { if file.attributes == nil || !file.isOpen { item := file.wfs.listDirectoryEntriesCache.Get(file.fullpath()) - if item != nil { + if item != nil && !item.Expired(){ entry := item.Value().(*filer_pb.Entry) file.Chunks = entry.Chunks file.attributes = entry.Attributes