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.

241 lines
6.7 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
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. 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. 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)
  110. 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)
  111. }
  112. resp.Size = len(req.Data)
  113. if req.Offset == 0 {
  114. fh.contentType = http.DetectContentType(req.Data)
  115. fh.dirtyMetadata = true
  116. }
  117. for _, chunk := range chunks {
  118. fh.f.Chunks = append(fh.f.Chunks, chunk)
  119. 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))
  120. fh.dirtyMetadata = true
  121. }
  122. return nil
  123. }
  124. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  125. glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
  126. fh.f.wfs.ReleaseHandle(fuse.HandleID(fh.handle))
  127. fh.f.isOpen = false
  128. return nil
  129. }
  130. // Flush - experimenting with uploading at flush, this slows operations down till it has been
  131. // completely flushed
  132. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  133. // fflush works at fh level
  134. // send the data to the OS
  135. glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
  136. chunk, err := fh.dirtyPages.FlushToStorage(ctx)
  137. if err != nil {
  138. glog.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. 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)
  140. }
  141. if chunk != nil {
  142. fh.f.Chunks = append(fh.f.Chunks, chunk)
  143. fh.dirtyMetadata = true
  144. }
  145. if !fh.dirtyMetadata {
  146. return nil
  147. }
  148. if len(fh.f.Chunks) == 0 {
  149. glog.V(2).Infof("fh %s/%s flush skipping empty: %v", fh.f.dir.Path, fh.f.Name, req)
  150. return nil
  151. }
  152. err = fh.f.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  153. if fh.f.attributes != nil {
  154. fh.f.attributes.Mime = fh.contentType
  155. fh.f.attributes.Uid = req.Uid
  156. fh.f.attributes.Gid = req.Gid
  157. }
  158. request := &filer_pb.UpdateEntryRequest{
  159. Directory: fh.f.dir.Path,
  160. Entry: &filer_pb.Entry{
  161. Name: fh.f.Name,
  162. Attributes: fh.f.attributes,
  163. Chunks: fh.f.Chunks,
  164. },
  165. }
  166. glog.V(1).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.Chunks))
  167. for i, chunk := range fh.f.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.UpdateEntry(ctx, request); err != nil {
  171. return fmt.Errorf("update fh: %v", err)
  172. }
  173. return nil
  174. })
  175. if err == nil {
  176. fh.dirtyMetadata = false
  177. }
  178. return err
  179. }
  180. func volumeId(fileId string) string {
  181. lastCommaIndex := strings.LastIndex(fileId, ",")
  182. if lastCommaIndex > 0 {
  183. return fileId[:lastCommaIndex]
  184. }
  185. return fileId
  186. }