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.

235 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. !chunkView.IsFullChunk)
  99. if err != nil {
  100. 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)
  101. err = fmt.Errorf("failed to read http://%s/%s: %v",
  102. locations.Locations[0].Url, chunkView.FileId, err)
  103. return
  104. }
  105. glog.V(4).Infof("read fh read %d bytes: %+v", n, chunkView)
  106. totalRead += n
  107. }(chunkView)
  108. }
  109. wg.Wait()
  110. resp.Data = buff[:totalRead]
  111. return err
  112. }
  113. // Write to the file handle
  114. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  115. // write the request to volume servers
  116. 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)))
  117. chunks, err := fh.dirtyPages.AddPage(ctx, req.Offset, req.Data)
  118. if err != nil {
  119. 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)
  120. 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)
  121. }
  122. resp.Size = len(req.Data)
  123. if req.Offset == 0 {
  124. fh.contentType = http.DetectContentType(req.Data)
  125. fh.dirtyMetadata = true
  126. }
  127. for _, chunk := range chunks {
  128. fh.f.entry.Chunks = append(fh.f.entry.Chunks, chunk)
  129. 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))
  130. fh.dirtyMetadata = true
  131. }
  132. return nil
  133. }
  134. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  135. glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
  136. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  137. fh.f.isOpen = false
  138. return nil
  139. }
  140. // Flush - experimenting with uploading at flush, this slows operations down till it has been
  141. // completely flushed
  142. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  143. // fflush works at fh level
  144. // send the data to the OS
  145. glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
  146. chunk, err := fh.dirtyPages.FlushToStorage(ctx)
  147. if err != nil {
  148. glog.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  149. return fmt.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  150. }
  151. if chunk != nil {
  152. fh.f.entry.Chunks = append(fh.f.entry.Chunks, chunk)
  153. }
  154. if !fh.dirtyMetadata {
  155. return nil
  156. }
  157. return fh.f.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  158. if fh.f.entry.Attributes != nil {
  159. fh.f.entry.Attributes.Mime = fh.contentType
  160. fh.f.entry.Attributes.Uid = req.Uid
  161. fh.f.entry.Attributes.Gid = req.Gid
  162. }
  163. request := &filer_pb.CreateEntryRequest{
  164. Directory: fh.f.dir.Path,
  165. Entry: fh.f.entry,
  166. }
  167. glog.V(1).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.entry.Chunks))
  168. for i, chunk := range fh.f.entry.Chunks {
  169. 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))
  170. }
  171. if _, err := client.CreateEntry(ctx, request); err != nil {
  172. return fmt.Errorf("update fh: %v", err)
  173. }
  174. return nil
  175. })
  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. }