filehandle_read.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.IsInRemoteOnly() {
  26. glog.V(4).Infof("download remote entry %s", fileFullPath)
  27. err := fh.downloadRemoteEntry(entry)
  28. if err != nil {
  29. glog.V(1).Infof("download remote entry %s: %v", fileFullPath, err)
  30. return 0, 0, err
  31. }
  32. }
  33. fileSize := int64(entry.Attributes.FileSize)
  34. if fileSize == 0 {
  35. fileSize = int64(filer.FileSize(entry.GetEntry()))
  36. }
  37. if fileSize == 0 {
  38. glog.V(1).Infof("empty fh %v", fileFullPath)
  39. return 0, 0, io.EOF
  40. } else if offset >= fileSize {
  41. glog.V(1).Infof("invalid read, fileSize %d, offset %d for %s", fileSize, offset, fileFullPath)
  42. return 0, 0, io.EOF
  43. }
  44. if offset < int64(len(entry.Content)) {
  45. totalRead := copy(buff, entry.Content[offset:])
  46. glog.V(4).Infof("file handle read cached %s [%d,%d] %d", fileFullPath, offset, offset+int64(totalRead), totalRead)
  47. return int64(totalRead), 0, nil
  48. }
  49. totalRead, ts, err := fh.entryChunkGroup.ReadDataAt(fileSize, buff, offset)
  50. if err != nil && err != io.EOF {
  51. glog.Errorf("file handle read %s: %v", fileFullPath, err)
  52. }
  53. // glog.V(4).Infof("file handle read %s [%d,%d] %d : %v", fileFullPath, offset, offset+int64(totalRead), totalRead, err)
  54. return int64(totalRead), ts, err
  55. }
  56. func (fh *FileHandle) downloadRemoteEntry(entry *LockedEntry) error {
  57. fileFullPath := fh.FullPath()
  58. dir, _ := fileFullPath.DirAndName()
  59. err := fh.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  60. request := &filer_pb.CacheRemoteObjectToLocalClusterRequest{
  61. Directory: string(dir),
  62. Name: entry.Name,
  63. }
  64. glog.V(4).Infof("download entry: %v", request)
  65. resp, err := client.CacheRemoteObjectToLocalCluster(context.Background(), request)
  66. if err != nil {
  67. return fmt.Errorf("CacheRemoteObjectToLocalCluster file %s: %v", fileFullPath, err)
  68. }
  69. entry.SetEntry(resp.Entry)
  70. fh.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, resp.Entry))
  71. return nil
  72. })
  73. return err
  74. }