You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.0 KiB

7 years ago
  1. package filesys
  2. import (
  3. "bazil.org/fuse/fs"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/karlseguin/ccache"
  7. "sync"
  8. "bazil.org/fuse"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. type WFS struct {
  13. filerGrpcAddress string
  14. listDirectoryEntriesCache *ccache.Cache
  15. collection string
  16. replication string
  17. ttlSec int32
  18. chunkSizeLimit int64
  19. dataCenter string
  20. // contains all open handles
  21. handles []*FileHandle
  22. pathToHandleIndex map[string]int
  23. pathToHandleLock sync.Mutex
  24. }
  25. func NewSeaweedFileSystem(filerGrpcAddress string, collection string, replication string, ttlSec int32, chunkSizeLimitMB int, dataCenter string) *WFS {
  26. return &WFS{
  27. filerGrpcAddress: filerGrpcAddress,
  28. listDirectoryEntriesCache: ccache.New(ccache.Configure().MaxSize(6000).ItemsToPrune(100)),
  29. collection: collection,
  30. replication: replication,
  31. ttlSec: ttlSec,
  32. chunkSizeLimit: int64(chunkSizeLimitMB) * 1024 * 1024,
  33. dataCenter: dataCenter,
  34. pathToHandleIndex: make(map[string]int),
  35. }
  36. }
  37. func (wfs *WFS) Root() (fs.Node, error) {
  38. return &Dir{Path: "/", wfs: wfs}, nil
  39. }
  40. func (wfs *WFS) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  41. grpcConnection, err := util.GrpcDial(wfs.filerGrpcAddress)
  42. if err != nil {
  43. return fmt.Errorf("fail to dial %s: %v", wfs.filerGrpcAddress, err)
  44. }
  45. defer grpcConnection.Close()
  46. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  47. return fn(client)
  48. }
  49. func (wfs *WFS) AcquireHandle(file *File, uid, gid uint32) (handle *FileHandle) {
  50. wfs.pathToHandleLock.Lock()
  51. defer wfs.pathToHandleLock.Unlock()
  52. fullpath := file.fullpath()
  53. index, found := wfs.pathToHandleIndex[fullpath]
  54. if found && wfs.handles[index] != nil {
  55. glog.V(4).Infoln(fullpath, "found handle id", index)
  56. return wfs.handles[index]
  57. }
  58. // create a new handler
  59. handle = &FileHandle{
  60. f: file,
  61. dirtyPages: newDirtyPages(file),
  62. Uid: uid,
  63. Gid: gid,
  64. }
  65. if found && wfs.handles[index] != nil {
  66. glog.V(4).Infoln(fullpath, "reuse previous handle id", index)
  67. wfs.handles[index] = handle
  68. handle.handle = uint64(index)
  69. return
  70. }
  71. for i, h := range wfs.handles {
  72. if h == nil {
  73. wfs.handles[i] = handle
  74. handle.handle = uint64(i)
  75. wfs.pathToHandleIndex[fullpath] = i
  76. glog.V(4).Infoln(fullpath, "reuse handle id", handle.handle)
  77. return
  78. }
  79. }
  80. wfs.handles = append(wfs.handles, handle)
  81. handle.handle = uint64(len(wfs.handles) - 1)
  82. glog.V(4).Infoln(fullpath, "new handle id", handle.handle)
  83. wfs.pathToHandleIndex[fullpath] = int(handle.handle)
  84. return
  85. }
  86. func (wfs *WFS) ReleaseHandle(handleId fuse.HandleID) {
  87. wfs.pathToHandleLock.Lock()
  88. defer wfs.pathToHandleLock.Unlock()
  89. glog.V(4).Infoln("releasing handle id", handleId, "current handles lengh", len(wfs.handles))
  90. if int(handleId) < len(wfs.handles) {
  91. wfs.handles[int(handleId)] = nil
  92. }
  93. return
  94. }