filehandle_read.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "sort"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  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, tsNs int64) (maxStop int64) {
  18. maxStop = fh.dirtyPages.ReadDirtyDataAt(buff, startOffset, tsNs)
  19. return
  20. }
  21. func (fh *FileHandle) readFromChunks(buff []byte, offset int64) (int64, int64, error) {
  22. return fh.readFromChunksWithContext(context.Background(), buff, offset)
  23. }
  24. func (fh *FileHandle) readFromChunksWithContext(ctx context.Context, buff []byte, offset int64) (int64, int64, error) {
  25. fh.entryLock.RLock()
  26. defer fh.entryLock.RUnlock()
  27. fileFullPath := fh.FullPath()
  28. entry := fh.GetEntry()
  29. if entry.IsInRemoteOnly() {
  30. glog.V(4).Infof("download remote entry %s", fileFullPath)
  31. err := fh.downloadRemoteEntry(entry)
  32. if err != nil {
  33. glog.V(1).Infof("download remote entry %s: %v", fileFullPath, err)
  34. return 0, 0, err
  35. }
  36. }
  37. fileSize := int64(entry.Attributes.FileSize)
  38. if fileSize == 0 {
  39. fileSize = int64(filer.FileSize(entry.GetEntry()))
  40. }
  41. if fileSize == 0 {
  42. glog.V(1).Infof("empty fh %v", fileFullPath)
  43. return 0, 0, io.EOF
  44. } else if offset == fileSize {
  45. return 0, 0, io.EOF
  46. } else if offset >= fileSize {
  47. glog.V(1).Infof("invalid read, fileSize %d, offset %d for %s", fileSize, offset, fileFullPath)
  48. return 0, 0, io.EOF
  49. }
  50. if offset < int64(len(entry.Content)) {
  51. totalRead := copy(buff, entry.Content[offset:])
  52. glog.V(4).Infof("file handle read cached %s [%d,%d] %d", fileFullPath, offset, offset+int64(totalRead), totalRead)
  53. return int64(totalRead), 0, nil
  54. }
  55. // Try RDMA acceleration first if available
  56. if fh.wfs.rdmaClient != nil && fh.wfs.option.RdmaEnabled {
  57. totalRead, ts, err := fh.tryRDMARead(ctx, fileSize, buff, offset, entry)
  58. if err == nil {
  59. glog.V(4).Infof("RDMA read successful for %s [%d,%d] %d", fileFullPath, offset, offset+int64(totalRead), totalRead)
  60. return int64(totalRead), ts, nil
  61. }
  62. glog.V(4).Infof("RDMA read failed for %s, falling back to HTTP: %v", fileFullPath, err)
  63. }
  64. // Fall back to normal chunk reading
  65. totalRead, ts, err := fh.entryChunkGroup.ReadDataAt(ctx, fileSize, 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), ts, err
  71. }
  72. // tryRDMARead attempts to read file data using RDMA acceleration
  73. func (fh *FileHandle) tryRDMARead(ctx context.Context, fileSize int64, buff []byte, offset int64, entry *LockedEntry) (int64, int64, error) {
  74. // For now, we'll try to read the chunks directly using RDMA
  75. // This is a simplified approach - in a full implementation, we'd need to
  76. // handle chunk boundaries, multiple chunks, etc.
  77. chunks := entry.GetEntry().Chunks
  78. if len(chunks) == 0 {
  79. return 0, 0, fmt.Errorf("no chunks available for RDMA read")
  80. }
  81. // Find the chunk that contains our offset using binary search
  82. var targetChunk *filer_pb.FileChunk
  83. var chunkOffset int64
  84. // Get cached cumulative offsets for efficient binary search
  85. cumulativeOffsets := fh.getCumulativeOffsets(chunks)
  86. // Use binary search to find the chunk containing the offset
  87. chunkIndex := sort.Search(len(chunks), func(i int) bool {
  88. return offset < cumulativeOffsets[i+1]
  89. })
  90. // Verify the chunk actually contains our offset
  91. if chunkIndex < len(chunks) && offset >= cumulativeOffsets[chunkIndex] {
  92. targetChunk = chunks[chunkIndex]
  93. chunkOffset = offset - cumulativeOffsets[chunkIndex]
  94. }
  95. if targetChunk == nil {
  96. return 0, 0, fmt.Errorf("no chunk found for offset %d", offset)
  97. }
  98. // Calculate how much to read from this chunk
  99. remainingInChunk := int64(targetChunk.Size) - chunkOffset
  100. readSize := min(int64(len(buff)), remainingInChunk)
  101. glog.V(4).Infof("RDMA read attempt: chunk=%s (fileId=%s), chunkOffset=%d, readSize=%d",
  102. targetChunk.FileId, targetChunk.FileId, chunkOffset, readSize)
  103. // Try RDMA read using file ID directly (more efficient)
  104. data, isRDMA, err := fh.wfs.rdmaClient.ReadNeedle(ctx, targetChunk.FileId, uint64(chunkOffset), uint64(readSize))
  105. if err != nil {
  106. return 0, 0, fmt.Errorf("RDMA read failed: %w", err)
  107. }
  108. if !isRDMA {
  109. return 0, 0, fmt.Errorf("RDMA not available for chunk")
  110. }
  111. // Copy data to buffer
  112. copied := copy(buff, data)
  113. return int64(copied), targetChunk.ModifiedTsNs, nil
  114. }
  115. func (fh *FileHandle) downloadRemoteEntry(entry *LockedEntry) error {
  116. fileFullPath := fh.FullPath()
  117. dir, _ := fileFullPath.DirAndName()
  118. err := fh.wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  119. request := &filer_pb.CacheRemoteObjectToLocalClusterRequest{
  120. Directory: string(dir),
  121. Name: entry.Name,
  122. }
  123. glog.V(4).Infof("download entry: %v", request)
  124. resp, err := client.CacheRemoteObjectToLocalCluster(context.Background(), request)
  125. if err != nil {
  126. return fmt.Errorf("CacheRemoteObjectToLocalCluster file %s: %v", fileFullPath, err)
  127. }
  128. fh.SetEntry(resp.Entry)
  129. fh.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, resp.Entry))
  130. return nil
  131. })
  132. return err
  133. }