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.

231 lines
6.6 KiB

4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
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
  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. "github.com/golang/groupcache/singleflight"
  14. )
  15. type ChunkReadAt struct {
  16. masterClient *wdclient.MasterClient
  17. chunkViews []*ChunkView
  18. lookupFileId LookupFileIdFunctionType
  19. readerLock sync.Mutex
  20. fileSize int64
  21. fetchGroup singleflight.Group
  22. chunkCache chunk_cache.ChunkCache
  23. lastChunkFileId string
  24. lastChunkData []byte
  25. }
  26. var _ = io.ReaderAt(&ChunkReadAt{})
  27. var _ = io.Closer(&ChunkReadAt{})
  28. type LookupFileIdFunctionType func(fileId string) (targetUrls []string, err error)
  29. func LookupFn(filerClient filer_pb.FilerClient) LookupFileIdFunctionType {
  30. vidCache := make(map[string]*filer_pb.Locations)
  31. var vicCacheLock sync.RWMutex
  32. return func(fileId string) (targetUrls []string, err error) {
  33. vid := VolumeId(fileId)
  34. vicCacheLock.RLock()
  35. locations, found := vidCache[vid]
  36. vicCacheLock.RUnlock()
  37. if !found {
  38. util.Retry("lookup volume "+vid, func() error {
  39. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  40. resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  41. VolumeIds: []string{vid},
  42. })
  43. if err != nil {
  44. return err
  45. }
  46. locations = resp.LocationsMap[vid]
  47. if locations == nil || len(locations.Locations) == 0 {
  48. glog.V(0).Infof("failed to locate %s", fileId)
  49. return fmt.Errorf("failed to locate %s", fileId)
  50. }
  51. vicCacheLock.Lock()
  52. vidCache[vid] = locations
  53. vicCacheLock.Unlock()
  54. return nil
  55. })
  56. return err
  57. })
  58. }
  59. if err != nil {
  60. return nil, err
  61. }
  62. for _, loc := range locations.Locations {
  63. volumeServerAddress := filerClient.AdjustedUrl(loc)
  64. targetUrl := fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  65. targetUrls = append(targetUrls, targetUrl)
  66. }
  67. for i := len(targetUrls) - 1; i > 0; i-- {
  68. j := rand.Intn(i + 1)
  69. targetUrls[i], targetUrls[j] = targetUrls[j], targetUrls[i]
  70. }
  71. return
  72. }
  73. }
  74. func NewChunkReaderAtFromClient(filerClient filer_pb.FilerClient, chunkViews []*ChunkView, chunkCache chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt {
  75. return &ChunkReadAt{
  76. chunkViews: chunkViews,
  77. lookupFileId: LookupFn(filerClient),
  78. chunkCache: chunkCache,
  79. fileSize: fileSize,
  80. }
  81. }
  82. func (c *ChunkReadAt) Close() error {
  83. c.lastChunkData = nil
  84. c.lastChunkFileId = ""
  85. return nil
  86. }
  87. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  88. c.readerLock.Lock()
  89. defer c.readerLock.Unlock()
  90. 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))
  91. return c.doReadAt(p[n:], offset+int64(n))
  92. }
  93. func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
  94. startOffset, remaining := offset, int64(len(p))
  95. var nextChunk *ChunkView
  96. for i, chunk := range c.chunkViews {
  97. if remaining <= 0 {
  98. break
  99. }
  100. if i+1 < len(c.chunkViews) {
  101. nextChunk = c.chunkViews[i+1]
  102. } else {
  103. nextChunk = nil
  104. }
  105. if startOffset < chunk.LogicOffset {
  106. gap := int(chunk.LogicOffset - startOffset)
  107. glog.V(4).Infof("zero [%d,%d)", startOffset, startOffset+int64(gap))
  108. n += int(min(int64(gap), remaining))
  109. startOffset, remaining = chunk.LogicOffset, remaining-int64(gap)
  110. if remaining <= 0 {
  111. break
  112. }
  113. }
  114. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  115. chunkStart, chunkStop := max(chunk.LogicOffset, startOffset), min(chunk.LogicOffset+int64(chunk.Size), startOffset+remaining)
  116. if chunkStart >= chunkStop {
  117. continue
  118. }
  119. 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))
  120. var buffer []byte
  121. buffer, err = c.readFromWholeChunkData(chunk, nextChunk)
  122. if err != nil {
  123. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  124. return
  125. }
  126. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  127. copied := copy(p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], buffer[bufferOffset:bufferOffset+chunkStop-chunkStart])
  128. n += copied
  129. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  130. }
  131. glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  132. if err == nil && remaining > 0 && c.fileSize > startOffset {
  133. delta := int(min(remaining, c.fileSize-startOffset))
  134. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+int64(delta), c.fileSize)
  135. n += delta
  136. }
  137. if err == nil && offset+int64(len(p)) >= c.fileSize {
  138. err = io.EOF
  139. }
  140. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  141. return
  142. }
  143. func (c *ChunkReadAt) readFromWholeChunkData(chunkView *ChunkView, nextChunkViews ...*ChunkView) (chunkData []byte, err error) {
  144. if c.lastChunkFileId == chunkView.FileId {
  145. return c.lastChunkData, nil
  146. }
  147. v, doErr := c.readOneWholeChunk(chunkView)
  148. if doErr != nil {
  149. return nil, doErr
  150. }
  151. chunkData = v.([]byte)
  152. c.lastChunkData = chunkData
  153. c.lastChunkFileId = chunkView.FileId
  154. for _, nextChunkView := range nextChunkViews {
  155. if c.chunkCache != nil && nextChunkView != nil {
  156. go c.readOneWholeChunk(nextChunkView)
  157. }
  158. }
  159. return
  160. }
  161. func (c *ChunkReadAt) readOneWholeChunk(chunkView *ChunkView) (interface{}, error) {
  162. var err error
  163. return c.fetchGroup.Do(chunkView.FileId, func() (interface{}, error) {
  164. glog.V(4).Infof("readFromWholeChunkData %s offset %d [%d,%d) size at least %d", chunkView.FileId, chunkView.Offset, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size), chunkView.ChunkSize)
  165. data := c.chunkCache.GetChunk(chunkView.FileId, chunkView.ChunkSize)
  166. if data != nil {
  167. glog.V(4).Infof("cache hit %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset-chunkView.Offset, chunkView.LogicOffset-chunkView.Offset+int64(len(data)))
  168. } else {
  169. var err error
  170. data, err = c.doFetchFullChunkData(chunkView)
  171. if err != nil {
  172. return data, err
  173. }
  174. c.chunkCache.SetChunk(chunkView.FileId, data)
  175. }
  176. return data, err
  177. })
  178. }
  179. func (c *ChunkReadAt) doFetchFullChunkData(chunkView *ChunkView) ([]byte, error) {
  180. glog.V(4).Infof("+ doFetchFullChunkData %s", chunkView.FileId)
  181. data, err := fetchChunk(c.lookupFileId, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped)
  182. glog.V(4).Infof("- doFetchFullChunkData %s", chunkView.FileId)
  183. return data, err
  184. }