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.

176 lines
4.4 KiB

  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. )
  14. type FileHandle struct {
  15. // cache file has been written to
  16. dirty bool
  17. cachePath string
  18. handle uint64
  19. wfs *WFS
  20. dirPath string
  21. name string
  22. RequestId fuse.RequestID // unique ID for request
  23. NodeId fuse.NodeID // file or directory the request is about
  24. Uid uint32 // user ID of process making request
  25. Gid uint32 // group ID of process making request
  26. attributes *filer_pb.FuseAttributes
  27. Chunks []*filer_pb.FileChunk
  28. }
  29. var _ = fs.Handle(&FileHandle{})
  30. var _ = fs.HandleReadAller(&FileHandle{})
  31. // var _ = fs.HandleReader(&FileHandle{})
  32. var _ = fs.HandleFlusher(&FileHandle{})
  33. var _ = fs.HandleWriter(&FileHandle{})
  34. var _ = fs.HandleReleaser(&FileHandle{})
  35. func (fh *FileHandle) ReadAll(ctx context.Context) (content []byte, err error) {
  36. glog.V(3).Infof("read all fh %+v/%v", fh.dirPath, fh.name)
  37. if len(fh.Chunks) == 0 {
  38. glog.V(0).Infof("empty fh %v/%v", fh.dirPath, fh.name)
  39. return
  40. }
  41. err = fh.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  42. // FIXME: need to either use Read() or implement differently
  43. chunks, _ := filer2.CompactFileChunks(fh.Chunks)
  44. glog.V(1).Infof("read fh %v/%v %d/%d chunks", fh.dirPath, fh.name, len(chunks), len(fh.Chunks))
  45. request := &filer_pb.GetFileContentRequest{
  46. FileId: chunks[0].FileId,
  47. }
  48. glog.V(1).Infof("read fh content %d chunk %s [%d,%d): %v", len(chunks),
  49. chunks[0].FileId, chunks[0].Offset, chunks[0].Offset+int64(chunks[0].Size), request)
  50. resp, err := client.GetFileContent(ctx, request)
  51. if err != nil {
  52. return err
  53. }
  54. content = resp.Content
  55. return nil
  56. })
  57. return content, err
  58. }
  59. // Write to the file handle
  60. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  61. // write the request to volume servers
  62. // glog.V(3).Infof("write fh %+v", req)
  63. var fileId, host string
  64. if err := fh.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  65. request := &filer_pb.AssignVolumeRequest{
  66. Count: 1,
  67. Replication: "000",
  68. Collection: "",
  69. }
  70. glog.V(1).Infof("assign volume: %v", request)
  71. resp, err := client.AssignVolume(ctx, request)
  72. if err != nil {
  73. return err
  74. }
  75. fileId, host = resp.FileId, resp.Url
  76. return nil
  77. }); err != nil {
  78. return fmt.Errorf("filer assign volume: %v", err)
  79. }
  80. fileUrl := fmt.Sprintf("http://%s/%s", host, fileId)
  81. bufReader := bytes.NewReader(req.Data)
  82. uploadResult, err := operation.Upload(fileUrl, fh.name, bufReader, false, "application/octet-stream", nil, "")
  83. if err != nil {
  84. return fmt.Errorf("upload data: %v", err)
  85. }
  86. if uploadResult.Error != "" {
  87. return fmt.Errorf("upload result: %v", uploadResult.Error)
  88. }
  89. resp.Size = int(uploadResult.Size)
  90. fh.Chunks = append(fh.Chunks, &filer_pb.FileChunk{
  91. FileId: fileId,
  92. Offset: req.Offset,
  93. Size: uint64(uploadResult.Size),
  94. Mtime: time.Now().UnixNano(),
  95. })
  96. glog.V(1).Infof("uploaded %s/%s to: %v, [%d,%d)", fh.dirPath, fh.name, fileUrl, req.Offset, req.Offset+int64(resp.Size))
  97. fh.dirty = true
  98. return nil
  99. }
  100. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  101. glog.V(3).Infof("release fh %+v/%v", fh.dirPath, fh.name)
  102. return nil
  103. }
  104. // Flush - experimenting with uploading at flush, this slows operations down till it has been
  105. // completely flushed
  106. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  107. // fflush works at fh level
  108. // send the data to the OS
  109. glog.V(3).Infof("fh flush %v", req)
  110. if !fh.dirty {
  111. return nil
  112. }
  113. if len(fh.Chunks) == 0 {
  114. glog.V(2).Infof("fh %s/%s flush skipping empty: %v", fh.dirPath, fh.name, req)
  115. return nil
  116. }
  117. err := fh.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  118. request := &filer_pb.UpdateEntryRequest{
  119. Directory: fh.dirPath,
  120. Entry: &filer_pb.Entry{
  121. Name: fh.name,
  122. Attributes: fh.attributes,
  123. Chunks: fh.Chunks,
  124. },
  125. }
  126. glog.V(1).Infof("%s/%s set chunks: %v", fh.dirPath, fh.name, len(fh.Chunks))
  127. if _, err := client.UpdateEntry(ctx, request); err != nil {
  128. return fmt.Errorf("update fh: %v", err)
  129. }
  130. return nil
  131. })
  132. if err == nil {
  133. fh.dirty = false
  134. }
  135. return err
  136. }