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.

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