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.

186 lines
4.9 KiB

7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "math"
  6. "os"
  7. "sync"
  8. "time"
  9. "github.com/seaweedfs/fuse"
  10. "github.com/seaweedfs/fuse/fs"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. "github.com/karlseguin/ccache"
  15. "google.golang.org/grpc"
  16. )
  17. type Option struct {
  18. FilerGrpcAddress string
  19. FilerMountRootPath string
  20. Collection string
  21. Replication string
  22. TtlSec int32
  23. ChunkSizeLimit int64
  24. DataCenter string
  25. DirListingLimit int
  26. EntryCacheTtl time.Duration
  27. MountUid uint32
  28. MountGid uint32
  29. MountMode os.FileMode
  30. }
  31. var _ = fs.FS(&WFS{})
  32. var _ = fs.FSStatfser(&WFS{})
  33. type WFS struct {
  34. option *Option
  35. listDirectoryEntriesCache *ccache.Cache
  36. // contains all open handles
  37. handles []*FileHandle
  38. pathToHandleIndex map[string]int
  39. pathToHandleLock sync.Mutex
  40. bufPool sync.Pool
  41. stats statsCache
  42. }
  43. type statsCache struct {
  44. filer_pb.StatisticsResponse
  45. lastChecked int64 // unix time in seconds
  46. }
  47. func NewSeaweedFileSystem(option *Option) *WFS {
  48. return &WFS{
  49. option: option,
  50. listDirectoryEntriesCache: ccache.New(ccache.Configure().MaxSize(int64(option.DirListingLimit) + 200).ItemsToPrune(100)),
  51. pathToHandleIndex: make(map[string]int),
  52. bufPool: sync.Pool{
  53. New: func() interface{} {
  54. return make([]byte, option.ChunkSizeLimit)
  55. },
  56. },
  57. }
  58. }
  59. func (wfs *WFS) Root() (fs.Node, error) {
  60. return &Dir{Path: wfs.option.FilerMountRootPath, wfs: wfs}, nil
  61. }
  62. func (wfs *WFS) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  63. return util.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  64. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  65. return fn(client)
  66. }, wfs.option.FilerGrpcAddress)
  67. }
  68. func (wfs *WFS) AcquireHandle(file *File, uid, gid uint32) (fileHandle *FileHandle) {
  69. wfs.pathToHandleLock.Lock()
  70. defer wfs.pathToHandleLock.Unlock()
  71. fullpath := file.fullpath()
  72. index, found := wfs.pathToHandleIndex[fullpath]
  73. if found && wfs.handles[index] != nil {
  74. glog.V(2).Infoln(fullpath, "found fileHandle id", index)
  75. return wfs.handles[index]
  76. }
  77. fileHandle = newFileHandle(file, uid, gid)
  78. for i, h := range wfs.handles {
  79. if h == nil {
  80. wfs.handles[i] = fileHandle
  81. fileHandle.handle = uint64(i)
  82. wfs.pathToHandleIndex[fullpath] = i
  83. glog.V(4).Infoln(fullpath, "reuse fileHandle id", fileHandle.handle)
  84. return
  85. }
  86. }
  87. wfs.handles = append(wfs.handles, fileHandle)
  88. fileHandle.handle = uint64(len(wfs.handles) - 1)
  89. glog.V(2).Infoln(fullpath, "new fileHandle id", fileHandle.handle)
  90. wfs.pathToHandleIndex[fullpath] = int(fileHandle.handle)
  91. return
  92. }
  93. func (wfs *WFS) ReleaseHandle(fullpath string, handleId fuse.HandleID) {
  94. wfs.pathToHandleLock.Lock()
  95. defer wfs.pathToHandleLock.Unlock()
  96. glog.V(4).Infof("%s releasing handle id %d current handles length %d", fullpath, handleId, len(wfs.handles))
  97. delete(wfs.pathToHandleIndex, fullpath)
  98. if int(handleId) < len(wfs.handles) {
  99. wfs.handles[int(handleId)] = nil
  100. }
  101. return
  102. }
  103. // Statfs is called to obtain file system metadata. Implements fuse.FSStatfser
  104. func (wfs *WFS) Statfs(ctx context.Context, req *fuse.StatfsRequest, resp *fuse.StatfsResponse) error {
  105. glog.V(4).Infof("reading fs stats: %+v", req)
  106. if wfs.stats.lastChecked < time.Now().Unix()-20 {
  107. err := wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  108. request := &filer_pb.StatisticsRequest{
  109. Collection: wfs.option.Collection,
  110. Replication: wfs.option.Replication,
  111. Ttl: fmt.Sprintf("%ds", wfs.option.TtlSec),
  112. }
  113. glog.V(4).Infof("reading filer stats: %+v", request)
  114. resp, err := client.Statistics(ctx, request)
  115. if err != nil {
  116. glog.V(0).Infof("reading filer stats %v: %v", request, err)
  117. return err
  118. }
  119. glog.V(4).Infof("read filer stats: %+v", resp)
  120. wfs.stats.TotalSize = resp.TotalSize
  121. wfs.stats.UsedSize = resp.UsedSize
  122. wfs.stats.FileCount = resp.FileCount
  123. wfs.stats.lastChecked = time.Now().Unix()
  124. return nil
  125. })
  126. if err != nil {
  127. glog.V(0).Infof("filer Statistics: %v", err)
  128. return err
  129. }
  130. }
  131. totalDiskSize := wfs.stats.TotalSize
  132. usedDiskSize := wfs.stats.UsedSize
  133. actualFileCount := wfs.stats.FileCount
  134. // Compute the total number of available blocks
  135. resp.Blocks = totalDiskSize / blockSize
  136. // Compute the number of used blocks
  137. numblocks := uint64(usedDiskSize / blockSize)
  138. // Report the number of free and available blocks for the block size
  139. resp.Bfree = resp.Blocks - numblocks
  140. resp.Bavail = resp.Blocks - numblocks
  141. resp.Bsize = uint32(blockSize)
  142. // Report the total number of possible files in the file system (and those free)
  143. resp.Files = math.MaxInt64
  144. resp.Ffree = math.MaxInt64 - actualFileCount
  145. // Report the maximum length of a name and the minimum fragment size
  146. resp.Namelen = 1024
  147. resp.Frsize = uint32(blockSize)
  148. return nil
  149. }