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.

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