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.

179 lines
5.5 KiB

4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math/rand"
  7. "sync"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/chrislusf/seaweedfs/weed/util/chunk_cache"
  12. "github.com/chrislusf/seaweedfs/weed/wdclient"
  13. )
  14. type ChunkReadAt struct {
  15. masterClient *wdclient.MasterClient
  16. chunkViews []*ChunkView
  17. readerLock sync.Mutex
  18. fileSize int64
  19. readerCache *ReaderCache
  20. readerPattern *ReaderPattern
  21. lastChunkFid string
  22. }
  23. var _ = io.ReaderAt(&ChunkReadAt{})
  24. var _ = io.Closer(&ChunkReadAt{})
  25. func LookupFn(filerClient filer_pb.FilerClient) wdclient.LookupFileIdFunctionType {
  26. vidCache := make(map[string]*filer_pb.Locations)
  27. var vicCacheLock sync.RWMutex
  28. return func(fileId string) (targetUrls []string, err error) {
  29. vid := VolumeId(fileId)
  30. vicCacheLock.RLock()
  31. locations, found := vidCache[vid]
  32. vicCacheLock.RUnlock()
  33. if !found {
  34. util.Retry("lookup volume "+vid, func() error {
  35. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  36. resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  37. VolumeIds: []string{vid},
  38. })
  39. if err != nil {
  40. return err
  41. }
  42. locations = resp.LocationsMap[vid]
  43. if locations == nil || len(locations.Locations) == 0 {
  44. glog.V(0).Infof("failed to locate %s", fileId)
  45. return fmt.Errorf("failed to locate %s", fileId)
  46. }
  47. vicCacheLock.Lock()
  48. vidCache[vid] = locations
  49. vicCacheLock.Unlock()
  50. return nil
  51. })
  52. return err
  53. })
  54. }
  55. if err != nil {
  56. return nil, err
  57. }
  58. for _, loc := range locations.Locations {
  59. volumeServerAddress := filerClient.AdjustedUrl(loc)
  60. targetUrl := fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  61. targetUrls = append(targetUrls, targetUrl)
  62. }
  63. for i := len(targetUrls) - 1; i > 0; i-- {
  64. j := rand.Intn(i + 1)
  65. targetUrls[i], targetUrls[j] = targetUrls[j], targetUrls[i]
  66. }
  67. return
  68. }
  69. }
  70. func NewChunkReaderAtFromClient(lookupFn wdclient.LookupFileIdFunctionType, chunkViews []*ChunkView, chunkCache chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt {
  71. return &ChunkReadAt{
  72. chunkViews: chunkViews,
  73. fileSize: fileSize,
  74. readerCache: newReaderCache(32, chunkCache, lookupFn),
  75. readerPattern: NewReaderPattern(),
  76. }
  77. }
  78. func (c *ChunkReadAt) Close() error {
  79. c.readerCache.destroy()
  80. return nil
  81. }
  82. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  83. c.readerPattern.MonitorReadAt(offset, len(p))
  84. c.readerLock.Lock()
  85. defer c.readerLock.Unlock()
  86. // 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))
  87. return c.doReadAt(p, offset)
  88. }
  89. func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
  90. startOffset, remaining := offset, int64(len(p))
  91. var nextChunks []*ChunkView
  92. for i, chunk := range c.chunkViews {
  93. if remaining <= 0 {
  94. break
  95. }
  96. if i+1 < len(c.chunkViews) {
  97. nextChunks = c.chunkViews[i+1:]
  98. }
  99. if startOffset < chunk.LogicOffset {
  100. gap := int(chunk.LogicOffset - startOffset)
  101. glog.V(4).Infof("zero [%d,%d)", startOffset, chunk.LogicOffset)
  102. n += int(min(int64(gap), remaining))
  103. startOffset, remaining = chunk.LogicOffset, remaining-int64(gap)
  104. if remaining <= 0 {
  105. break
  106. }
  107. }
  108. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  109. chunkStart, chunkStop := max(chunk.LogicOffset, startOffset), min(chunk.LogicOffset+int64(chunk.Size), startOffset+remaining)
  110. if chunkStart >= chunkStop {
  111. continue
  112. }
  113. // glog.V(4).Infof("read [%d,%d), %d/%d chunk %s [%d,%d)", chunkStart, chunkStop, i, len(c.chunkViews), chunk.FileId, chunk.LogicOffset-chunk.Offset, chunk.LogicOffset-chunk.Offset+int64(chunk.Size))
  114. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  115. copied, err := c.readChunkSliceAt(p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], chunk, nextChunks, uint64(bufferOffset))
  116. if err != nil {
  117. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  118. return copied, err
  119. }
  120. n += copied
  121. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  122. }
  123. // glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  124. if err == nil && remaining > 0 && c.fileSize > startOffset {
  125. delta := int(min(remaining, c.fileSize-startOffset))
  126. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+int64(delta), c.fileSize)
  127. n += delta
  128. }
  129. if err == nil && offset+int64(len(p)) >= c.fileSize {
  130. err = io.EOF
  131. }
  132. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  133. return
  134. }
  135. func (c *ChunkReadAt) readChunkSliceAt(buffer []byte, chunkView *ChunkView, nextChunkViews []*ChunkView, offset uint64) (n int, err error) {
  136. if c.readerPattern.IsRandomMode() {
  137. return c.readerCache.ReadChunkAt(buffer, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset), int(chunkView.ChunkSize), chunkView.LogicOffset == 0)
  138. }
  139. n, err = c.readerCache.ReadChunkAt(buffer, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset), int(chunkView.ChunkSize), chunkView.LogicOffset == 0)
  140. if c.lastChunkFid != "" && c.lastChunkFid != chunkView.FileId {
  141. if chunkView.Offset == 0 { // start of a new chunk
  142. c.readerCache.UnCache(c.lastChunkFid)
  143. c.readerCache.MaybeCache(nextChunkViews)
  144. }
  145. }
  146. c.lastChunkFid = chunkView.FileId
  147. return
  148. }