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.

239 lines
6.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
  1. package filesys
  2. import (
  3. "bazil.org/fuse"
  4. "bazil.org/fuse/fs"
  5. "context"
  6. "fmt"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "net/http"
  12. "strings"
  13. "sync"
  14. "time"
  15. )
  16. type FileHandle struct {
  17. // cache file has been written to
  18. dirtyPages *ContinuousDirtyPages
  19. contentType string
  20. dirtyMetadata bool
  21. handle uint64
  22. f *File
  23. RequestId fuse.RequestID // unique ID for request
  24. NodeId fuse.NodeID // file or directory the request is about
  25. Uid uint32 // user ID of process making request
  26. Gid uint32 // group ID of process making request
  27. }
  28. func newFileHandle(file *File, uid, gid uint32) *FileHandle {
  29. return &FileHandle{
  30. f: file,
  31. dirtyPages: newDirtyPages(file),
  32. Uid: uid,
  33. Gid: gid,
  34. }
  35. }
  36. func (fh *FileHandle) InitializeToFile(file *File, uid, gid uint32) *FileHandle {
  37. newHandle := &FileHandle{
  38. f: file,
  39. dirtyPages: fh.dirtyPages.InitializeToFile(file),
  40. Uid: uid,
  41. Gid: gid,
  42. }
  43. return newHandle
  44. }
  45. var _ = fs.Handle(&FileHandle{})
  46. // var _ = fs.HandleReadAller(&FileHandle{})
  47. var _ = fs.HandleReader(&FileHandle{})
  48. var _ = fs.HandleFlusher(&FileHandle{})
  49. var _ = fs.HandleWriter(&FileHandle{})
  50. var _ = fs.HandleReleaser(&FileHandle{})
  51. func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
  52. glog.V(4).Infof("%s read fh %d: [%d,%d)", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size))
  53. // this value should come from the filer instead of the old f
  54. if len(fh.f.entry.Chunks) == 0 {
  55. glog.V(1).Infof("empty fh %v/%v", fh.f.dir.Path, fh.f.Name)
  56. return nil
  57. }
  58. buff := make([]byte, req.Size)
  59. chunkViews := filer2.ViewFromChunks(fh.f.entry.Chunks, req.Offset, req.Size)
  60. var vids []string
  61. for _, chunkView := range chunkViews {
  62. vids = append(vids, volumeId(chunkView.FileId))
  63. }
  64. vid2Locations := make(map[string]*filer_pb.Locations)
  65. err := fh.f.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  66. glog.V(4).Infof("read fh lookup volume id locations: %v", vids)
  67. resp, err := client.LookupVolume(ctx, &filer_pb.LookupVolumeRequest{
  68. VolumeIds: vids,
  69. })
  70. if err != nil {
  71. return err
  72. }
  73. vid2Locations = resp.LocationsMap
  74. return nil
  75. })
  76. if err != nil {
  77. glog.V(4).Infof("%v/%v read fh lookup volume ids: %v", fh.f.dir.Path, fh.f.Name, err)
  78. return fmt.Errorf("failed to lookup volume ids %v: %v", vids, err)
  79. }
  80. var totalRead int64
  81. var wg sync.WaitGroup
  82. for _, chunkView := range chunkViews {
  83. wg.Add(1)
  84. go func(chunkView *filer2.ChunkView) {
  85. defer wg.Done()
  86. glog.V(4).Infof("read fh reading chunk: %+v", chunkView)
  87. locations := vid2Locations[volumeId(chunkView.FileId)]
  88. if locations == nil || len(locations.Locations) == 0 {
  89. glog.V(0).Infof("failed to locate %s", chunkView.FileId)
  90. err = fmt.Errorf("failed to locate %s", chunkView.FileId)
  91. return
  92. }
  93. var n int64
  94. n, err = util.ReadUrl(
  95. fmt.Sprintf("http://%s/%s", locations.Locations[0].Url, chunkView.FileId),
  96. chunkView.Offset,
  97. int(chunkView.Size),
  98. buff[chunkView.LogicOffset-req.Offset:chunkView.LogicOffset-req.Offset+int64(chunkView.Size)],
  99. !chunkView.IsFullChunk)
  100. if err != nil {
  101. glog.V(0).Infof("%v/%v read http://%s/%v %v bytes: %v", fh.f.dir.Path, fh.f.Name, locations.Locations[0].Url, chunkView.FileId, n, err)
  102. err = fmt.Errorf("failed to read http://%s/%s: %v",
  103. locations.Locations[0].Url, chunkView.FileId, err)
  104. return
  105. }
  106. glog.V(4).Infof("read fh read %d bytes: %+v", n, chunkView)
  107. totalRead += n
  108. }(chunkView)
  109. }
  110. wg.Wait()
  111. resp.Data = buff[:totalRead]
  112. return err
  113. }
  114. // Write to the file handle
  115. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  116. // write the request to volume servers
  117. glog.V(4).Infof("%+v/%v write fh %d: [%d,%d)", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)))
  118. chunks, err := fh.dirtyPages.AddPage(ctx, req.Offset, req.Data)
  119. if err != nil {
  120. glog.Errorf("%+v/%v write fh %d: [%d,%d): %v", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)), err)
  121. return fmt.Errorf("write %s/%s at [%d,%d): %v", fh.f.dir.Path, fh.f.Name, req.Offset, req.Offset+int64(len(req.Data)), err)
  122. }
  123. resp.Size = len(req.Data)
  124. if req.Offset == 0 {
  125. fh.contentType = http.DetectContentType(req.Data)
  126. fh.dirtyMetadata = true
  127. }
  128. for _, chunk := range chunks {
  129. fh.f.entry.Chunks = append(fh.f.entry.Chunks, chunk)
  130. glog.V(1).Infof("uploaded %s/%s to %s [%d,%d)", fh.f.dir.Path, fh.f.Name, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
  131. fh.dirtyMetadata = true
  132. }
  133. return nil
  134. }
  135. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  136. glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
  137. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  138. fh.f.isOpen = false
  139. return nil
  140. }
  141. // Flush - experimenting with uploading at flush, this slows operations down till it has been
  142. // completely flushed
  143. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  144. // fflush works at fh level
  145. // send the data to the OS
  146. glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
  147. chunk, err := fh.dirtyPages.FlushToStorage(ctx)
  148. if err != nil {
  149. glog.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  150. return fmt.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  151. }
  152. if chunk != nil {
  153. fh.f.entry.Chunks = append(fh.f.entry.Chunks, chunk)
  154. }
  155. if !fh.dirtyMetadata {
  156. return nil
  157. }
  158. return fh.f.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  159. if fh.f.entry.Attributes != nil {
  160. fh.f.entry.Attributes.Mime = fh.contentType
  161. fh.f.entry.Attributes.Uid = req.Uid
  162. fh.f.entry.Attributes.Gid = req.Gid
  163. fh.f.entry.Attributes.Mtime = time.Now().Unix()
  164. fh.f.entry.Attributes.Crtime = time.Now().Unix()
  165. fh.f.entry.Attributes.FileMode = uint32(0770)
  166. }
  167. request := &filer_pb.CreateEntryRequest{
  168. Directory: fh.f.dir.Path,
  169. Entry: fh.f.entry,
  170. }
  171. glog.V(1).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.entry.Chunks))
  172. for i, chunk := range fh.f.entry.Chunks {
  173. glog.V(1).Infof("%s/%s chunks %d: %v [%d,%d)", fh.f.dir.Path, fh.f.Name, i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
  174. }
  175. if _, err := client.CreateEntry(ctx, request); err != nil {
  176. return fmt.Errorf("update fh: %v", err)
  177. }
  178. return nil
  179. })
  180. }
  181. func volumeId(fileId string) string {
  182. lastCommaIndex := strings.LastIndex(fileId, ",")
  183. if lastCommaIndex > 0 {
  184. return fileId[:lastCommaIndex]
  185. }
  186. return fileId
  187. }