filehandle_read.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, tsNs int64) (maxStop int64) {
  17. maxStop = fh.dirtyPages.ReadDirtyDataAt(buff, startOffset, tsNs)
  18. return
  19. }
  20. func (fh *FileHandle) readFromChunks(buff []byte, offset int64) (int64, int64, error) {
  21. fh.entryLock.RLock()
  22. defer fh.entryLock.RUnlock()
  23. fileFullPath := fh.FullPath()
  24. entry := fh.GetEntry()
  25. if entry == nil {
  26. return 0, 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, 0, err
  34. }
  35. entry = newEntry
  36. }
  37. fileSize := int64(entry.Attributes.FileSize)
  38. if fileSize == 0 {
  39. fileSize = int64(filer.FileSize(entry))
  40. }
  41. if fileSize == 0 {
  42. glog.V(1).Infof("empty fh %v", fileFullPath)
  43. return 0, 0, io.EOF
  44. }
  45. if offset < int64(len(entry.Content)) {
  46. totalRead := copy(buff, entry.Content[offset:])
  47. glog.V(4).Infof("file handle read cached %s [%d,%d] %d", fileFullPath, offset, offset+int64(totalRead), totalRead)
  48. return int64(totalRead), 0, nil
  49. }
  50. totalRead, ts, err := fh.entryChunkGroup.ReadDataAt(fileSize, buff, offset)
  51. if err != nil && err != io.EOF {
  52. glog.Errorf("file handle read %s: %v", fileFullPath, err)
  53. }
  54. // glog.V(4).Infof("file handle read %s [%d,%d] %d : %v", fileFullPath, offset, offset+int64(totalRead), totalRead, err)
  55. return int64(totalRead), ts, err
  56. }
  57. func (fh *FileHandle) downloadRemoteEntry(entry *filer_pb.Entry) (*filer_pb.Entry, error) {
  58. fileFullPath := fh.FullPath()
  59. dir, _ := fileFullPath.DirAndName()
  60. err := fh.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  61. request := &filer_pb.CacheRemoteObjectToLocalClusterRequest{
  62. Directory: string(dir),
  63. Name: entry.Name,
  64. }
  65. glog.V(4).Infof("download entry: %v", request)
  66. resp, err := client.CacheRemoteObjectToLocalCluster(context.Background(), request)
  67. if err != nil {
  68. return fmt.Errorf("CacheRemoteObjectToLocalCluster file %s: %v", fileFullPath, err)
  69. }
  70. entry = resp.Entry
  71. fh.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, resp.Entry))
  72. return nil
  73. })
  74. return entry, err
  75. }