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.

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