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.

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