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.

116 lines
3.4 KiB

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. "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. )
  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. fh.entryLock.Lock()
  23. defer fh.entryLock.Unlock()
  24. entry := fh.entry
  25. if entry == nil {
  26. return 0, io.EOF
  27. }
  28. if entry.IsInRemoteOnly() {
  29. glog.V(4).Infof("download remote entry %s", fileFullPath)
  30. newEntry, err := fh.downloadRemoteEntry(entry)
  31. if err != nil {
  32. glog.V(1).Infof("download remote entry %s: %v", fileFullPath, err)
  33. return 0, err
  34. }
  35. entry = newEntry
  36. }
  37. fileSize := int64(filer.FileSize(entry))
  38. if fileSize == 0 {
  39. glog.V(1).Infof("empty fh %v", fileFullPath)
  40. return 0, io.EOF
  41. }
  42. if offset+int64(len(buff)) <= int64(len(entry.Content)) {
  43. totalRead := copy(buff, entry.Content[offset:])
  44. glog.V(4).Infof("file handle read cached %s [%d,%d] %d", fileFullPath, offset, offset+int64(totalRead), totalRead)
  45. return int64(totalRead), nil
  46. }
  47. var chunkResolveErr error
  48. if fh.entryViewCache == nil {
  49. fh.entryViewCache, chunkResolveErr = filer.NonOverlappingVisibleIntervals(fh.wfs.LookupFn(), entry.Chunks, 0, fileSize)
  50. if chunkResolveErr != nil {
  51. return 0, fmt.Errorf("fail to resolve chunk manifest: %v", chunkResolveErr)
  52. }
  53. fh.reader = nil
  54. }
  55. reader := fh.reader
  56. if reader == nil {
  57. chunkViews := filer.ViewFromVisibleIntervals(fh.entryViewCache, 0, fileSize)
  58. glog.V(4).Infof("file handle read %s [%d,%d) from %d views", fileFullPath, offset, offset+int64(len(buff)), len(chunkViews))
  59. for _, chunkView := range chunkViews {
  60. glog.V(4).Infof(" read %s [%d,%d) from chunk %+v", fileFullPath, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size), chunkView.FileId)
  61. }
  62. reader = filer.NewChunkReaderAtFromClient(fh.wfs.LookupFn(), chunkViews, fh.wfs.chunkCache, fileSize)
  63. }
  64. fh.reader = reader
  65. totalRead, err := reader.ReadAt(buff, offset)
  66. if err != nil && err != io.EOF {
  67. glog.Errorf("file handle read %s: %v", fileFullPath, err)
  68. }
  69. // glog.V(4).Infof("file handle read %s [%d,%d] %d : %v", fileFullPath, offset, offset+int64(totalRead), totalRead, err)
  70. return int64(totalRead), err
  71. }
  72. func (fh *FileHandle) downloadRemoteEntry(entry *filer_pb.Entry) (*filer_pb.Entry, error) {
  73. fileFullPath := fh.FullPath()
  74. dir, _ := fileFullPath.DirAndName()
  75. err := fh.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  76. request := &filer_pb.CacheRemoteObjectToLocalClusterRequest{
  77. Directory: string(dir),
  78. Name: entry.Name,
  79. }
  80. glog.V(4).Infof("download entry: %v", request)
  81. resp, err := client.CacheRemoteObjectToLocalCluster(context.Background(), request)
  82. if err != nil {
  83. return fmt.Errorf("CacheRemoteObjectToLocalCluster file %s: %v", fileFullPath, err)
  84. }
  85. entry = resp.Entry
  86. fh.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, resp.Entry))
  87. return nil
  88. })
  89. return entry, err
  90. }