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.

228 lines
6.5 KiB

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