filehandle_read.go 3.4 KB

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