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.

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