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.

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