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.

211 lines
6.2 KiB

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