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.

238 lines
6.5 KiB

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