reader_at.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math/rand"
  7. "sync"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  12. )
  13. type ChunkReadAt struct {
  14. masterClient *wdclient.MasterClient
  15. chunkViews *IntervalList[*ChunkView]
  16. fileSize int64
  17. readerCache *ReaderCache
  18. readerPattern *ReaderPattern
  19. lastChunkFid string
  20. ctx context.Context // Context used for cancellation during chunk read operations
  21. }
  22. var _ = io.ReaderAt(&ChunkReadAt{})
  23. var _ = io.Closer(&ChunkReadAt{})
  24. func LookupFn(filerClient filer_pb.FilerClient) wdclient.LookupFileIdFunctionType {
  25. vidCache := make(map[string]*filer_pb.Locations)
  26. var vicCacheLock sync.RWMutex
  27. return func(ctx context.Context, fileId string) (targetUrls []string, err error) {
  28. vid := VolumeId(fileId)
  29. vicCacheLock.RLock()
  30. locations, found := vidCache[vid]
  31. vicCacheLock.RUnlock()
  32. if !found {
  33. util.Retry("lookup volume "+vid, func() error {
  34. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  35. resp, err := client.LookupVolume(ctx, &filer_pb.LookupVolumeRequest{
  36. VolumeIds: []string{vid},
  37. })
  38. if err != nil {
  39. return err
  40. }
  41. locations = resp.LocationsMap[vid]
  42. if locations == nil || len(locations.Locations) == 0 {
  43. glog.V(0).InfofCtx(ctx, "failed to locate %s", fileId)
  44. return fmt.Errorf("failed to locate %s", fileId)
  45. }
  46. vicCacheLock.Lock()
  47. vidCache[vid] = locations
  48. vicCacheLock.Unlock()
  49. return nil
  50. })
  51. return err
  52. })
  53. }
  54. if err != nil {
  55. return nil, err
  56. }
  57. fcDataCenter := filerClient.GetDataCenter()
  58. var sameDcTargetUrls, otherTargetUrls []string
  59. for _, loc := range locations.Locations {
  60. volumeServerAddress := filerClient.AdjustedUrl(loc)
  61. targetUrl := fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  62. if fcDataCenter == "" || fcDataCenter != loc.DataCenter {
  63. otherTargetUrls = append(otherTargetUrls, targetUrl)
  64. } else {
  65. sameDcTargetUrls = append(sameDcTargetUrls, targetUrl)
  66. }
  67. }
  68. rand.Shuffle(len(sameDcTargetUrls), func(i, j int) {
  69. sameDcTargetUrls[i], sameDcTargetUrls[j] = sameDcTargetUrls[j], sameDcTargetUrls[i]
  70. })
  71. rand.Shuffle(len(otherTargetUrls), func(i, j int) {
  72. otherTargetUrls[i], otherTargetUrls[j] = otherTargetUrls[j], otherTargetUrls[i]
  73. })
  74. // Prefer same data center
  75. targetUrls = append(sameDcTargetUrls, otherTargetUrls...)
  76. return
  77. }
  78. }
  79. func NewChunkReaderAtFromClient(ctx context.Context, readerCache *ReaderCache, chunkViews *IntervalList[*ChunkView], fileSize int64) *ChunkReadAt {
  80. return &ChunkReadAt{
  81. chunkViews: chunkViews,
  82. fileSize: fileSize,
  83. readerCache: readerCache,
  84. readerPattern: NewReaderPattern(),
  85. ctx: ctx,
  86. }
  87. }
  88. func (c *ChunkReadAt) Size() int64 {
  89. return c.fileSize
  90. }
  91. func (c *ChunkReadAt) Close() error {
  92. c.readerCache.destroy()
  93. return nil
  94. }
  95. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  96. c.readerPattern.MonitorReadAt(offset, len(p))
  97. c.chunkViews.Lock.RLock()
  98. defer c.chunkViews.Lock.RUnlock()
  99. // glog.V(4).Infof("ReadAt [%d,%d) of total file size %d bytes %d chunk views", offset, offset+int64(len(p)), c.fileSize, len(c.chunkViews))
  100. n, _, err = c.doReadAt(c.ctx, p, offset)
  101. return
  102. }
  103. func (c *ChunkReadAt) ReadAtWithTime(ctx context.Context, p []byte, offset int64) (n int, ts int64, err error) {
  104. c.readerPattern.MonitorReadAt(offset, len(p))
  105. c.chunkViews.Lock.RLock()
  106. defer c.chunkViews.Lock.RUnlock()
  107. // glog.V(4).Infof("ReadAt [%d,%d) of total file size %d bytes %d chunk views", offset, offset+int64(len(p)), c.fileSize, len(c.chunkViews))
  108. return c.doReadAt(ctx, p, offset)
  109. }
  110. func (c *ChunkReadAt) doReadAt(ctx context.Context, p []byte, offset int64) (n int, ts int64, err error) {
  111. startOffset, remaining := offset, int64(len(p))
  112. var nextChunks *Interval[*ChunkView]
  113. for x := c.chunkViews.Front(); x != nil; x = x.Next {
  114. chunk := x.Value
  115. if remaining <= 0 {
  116. break
  117. }
  118. if x.Next != nil {
  119. nextChunks = x.Next
  120. }
  121. if startOffset < chunk.ViewOffset {
  122. gap := chunk.ViewOffset - startOffset
  123. glog.V(4).Infof("zero [%d,%d)", startOffset, chunk.ViewOffset)
  124. n += zero(p, startOffset-offset, gap)
  125. startOffset, remaining = chunk.ViewOffset, remaining-gap
  126. if remaining <= 0 {
  127. break
  128. }
  129. }
  130. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.ViewOffset, chunk.ViewOffset+int64(chunk.ViewSize))
  131. chunkStart, chunkStop := max(chunk.ViewOffset, startOffset), min(chunk.ViewOffset+int64(chunk.ViewSize), startOffset+remaining)
  132. if chunkStart >= chunkStop {
  133. continue
  134. }
  135. // glog.V(4).Infof("read [%d,%d), %d/%d chunk %s [%d,%d)", chunkStart, chunkStop, i, len(c.chunkViews), chunk.FileId, chunk.ViewOffset-chunk.Offset, chunk.ViewOffset-chunk.Offset+int64(chunk.ViewSize))
  136. bufferOffset := chunkStart - chunk.ViewOffset + chunk.OffsetInChunk
  137. ts = chunk.ModifiedTsNs
  138. copied, err := c.readChunkSliceAt(ctx, p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], chunk, nextChunks, uint64(bufferOffset))
  139. if err != nil {
  140. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  141. return copied, ts, err
  142. }
  143. n += copied
  144. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  145. }
  146. // glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  147. // zero the remaining bytes if a gap exists at the end of the last chunk (or a fully sparse file)
  148. if err == nil && remaining > 0 {
  149. var delta int64
  150. if c.fileSize >= startOffset {
  151. delta = min(remaining, c.fileSize-startOffset)
  152. startOffset -= offset
  153. }
  154. if delta > 0 {
  155. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+delta, c.fileSize)
  156. n += zero(p, startOffset, delta)
  157. }
  158. }
  159. if err == nil && offset+int64(len(p)) >= c.fileSize {
  160. err = io.EOF
  161. }
  162. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  163. return
  164. }
  165. func (c *ChunkReadAt) readChunkSliceAt(ctx context.Context, buffer []byte, chunkView *ChunkView, nextChunkViews *Interval[*ChunkView], offset uint64) (n int, err error) {
  166. if c.readerPattern.IsRandomMode() {
  167. n, err := c.readerCache.chunkCache.ReadChunkAt(buffer, chunkView.FileId, offset)
  168. if n > 0 {
  169. return n, err
  170. }
  171. return fetchChunkRange(ctx, buffer, c.readerCache.lookupFileIdFn, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset))
  172. }
  173. shouldCache := (uint64(chunkView.ViewOffset) + chunkView.ChunkSize) <= c.readerCache.chunkCache.GetMaxFilePartSizeInCache()
  174. n, err = c.readerCache.ReadChunkAt(buffer, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset), int(chunkView.ChunkSize), shouldCache)
  175. if c.lastChunkFid != chunkView.FileId {
  176. if chunkView.OffsetInChunk == 0 { // start of a new chunk
  177. if c.lastChunkFid != "" {
  178. c.readerCache.UnCache(c.lastChunkFid)
  179. }
  180. if nextChunkViews != nil {
  181. c.readerCache.MaybeCache(nextChunkViews) // just read the next chunk if at the very beginning
  182. }
  183. }
  184. }
  185. c.lastChunkFid = chunkView.FileId
  186. return
  187. }
  188. func zero(buffer []byte, start, length int64) int {
  189. if length <= 0 {
  190. return 0
  191. }
  192. end := min(start+length, int64(len(buffer)))
  193. start = max(start, 0)
  194. // zero the bytes
  195. for o := start; o < end; o++ {
  196. buffer[o] = 0
  197. }
  198. return int(end - start)
  199. }