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.

112 lines
3.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. )
  10. func (fh *FileHandle) lockForRead(startOffset int64, size int) {
  11. fh.dirtyPages.LockForRead(startOffset, startOffset+int64(size))
  12. }
  13. func (fh *FileHandle) unlockForRead(startOffset int64, size int) {
  14. fh.dirtyPages.UnlockForRead(startOffset, startOffset+int64(size))
  15. }
  16. func (fh *FileHandle) readFromDirtyPages(buff []byte, startOffset int64) (maxStop int64) {
  17. maxStop = fh.dirtyPages.ReadDirtyDataAt(buff, startOffset)
  18. return
  19. }
  20. func (fh *FileHandle) readFromChunks(buff []byte, offset int64) (int64, error) {
  21. fileFullPath := fh.FullPath()
  22. entry := fh.entry
  23. if entry == nil {
  24. return 0, io.EOF
  25. }
  26. if entry.IsInRemoteOnly() {
  27. glog.V(4).Infof("download remote entry %s", fileFullPath)
  28. newEntry, err := fh.downloadRemoteEntry(entry)
  29. if err != nil {
  30. glog.V(1).Infof("download remote entry %s: %v", fileFullPath, err)
  31. return 0, err
  32. }
  33. entry = newEntry
  34. }
  35. fileSize := int64(filer.FileSize(entry))
  36. if fileSize == 0 {
  37. glog.V(1).Infof("empty fh %v", fileFullPath)
  38. return 0, io.EOF
  39. }
  40. if offset+int64(len(buff)) <= int64(len(entry.Content)) {
  41. totalRead := copy(buff, entry.Content[offset:])
  42. glog.V(4).Infof("file handle read cached %s [%d,%d] %d", fileFullPath, offset, offset+int64(totalRead), totalRead)
  43. return int64(totalRead), nil
  44. }
  45. var chunkResolveErr error
  46. if fh.entryViewCache == nil {
  47. fh.entryViewCache, chunkResolveErr = filer.NonOverlappingVisibleIntervals(fh.wfs.LookupFn(), entry.GetChunks(), 0, fileSize)
  48. if chunkResolveErr != nil {
  49. return 0, fmt.Errorf("fail to resolve chunk manifest: %v", chunkResolveErr)
  50. }
  51. fh.CloseReader()
  52. }
  53. if fh.reader == nil {
  54. chunkViews := filer.ViewFromVisibleIntervals(fh.entryViewCache, 0, fileSize)
  55. glog.V(4).Infof("file handle read %s [%d,%d) from %d views", fileFullPath, offset, offset+int64(len(buff)), len(chunkViews))
  56. for _, chunkView := range chunkViews {
  57. glog.V(4).Infof(" read %s [%d,%d) from chunk %+v", fileFullPath, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size), chunkView.FileId)
  58. }
  59. fh.reader = filer.NewChunkReaderAtFromClient(fh.wfs.LookupFn(), chunkViews, fh.wfs.chunkCache, fileSize)
  60. }
  61. totalRead, err := fh.reader.ReadAt(buff, offset)
  62. if err != nil && err != io.EOF {
  63. glog.Errorf("file handle read %s: %v", fileFullPath, err)
  64. }
  65. // glog.V(4).Infof("file handle read %s [%d,%d] %d : %v", fileFullPath, offset, offset+int64(totalRead), totalRead, err)
  66. return int64(totalRead), err
  67. }
  68. func (fh *FileHandle) downloadRemoteEntry(entry *filer_pb.Entry) (*filer_pb.Entry, error) {
  69. fileFullPath := fh.FullPath()
  70. dir, _ := fileFullPath.DirAndName()
  71. err := fh.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  72. request := &filer_pb.CacheRemoteObjectToLocalClusterRequest{
  73. Directory: string(dir),
  74. Name: entry.Name,
  75. }
  76. glog.V(4).Infof("download entry: %v", request)
  77. resp, err := client.CacheRemoteObjectToLocalCluster(context.Background(), request)
  78. if err != nil {
  79. return fmt.Errorf("CacheRemoteObjectToLocalCluster file %s: %v", fileFullPath, err)
  80. }
  81. entry = resp.Entry
  82. fh.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, resp.Entry))
  83. return nil
  84. })
  85. return entry, err
  86. }