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.

215 lines
6.3 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. if err != nil {
  51. return nil, err
  52. }
  53. for _, loc := range locations.Locations {
  54. volumeServerAddress := filerClient.AdjustedUrl(loc.Url)
  55. targetUrl := fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  56. targetUrls = append(targetUrls, targetUrl)
  57. }
  58. for i := len(targetUrls) - 1; i > 0; i-- {
  59. j := rand.Intn(i + 1)
  60. targetUrls[i], targetUrls[j] = targetUrls[j], targetUrls[i]
  61. }
  62. return
  63. }
  64. }
  65. func NewChunkReaderAtFromClient(filerClient filer_pb.FilerClient, chunkViews []*ChunkView, chunkCache chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt {
  66. return &ChunkReadAt{
  67. chunkViews: chunkViews,
  68. lookupFileId: LookupFn(filerClient),
  69. chunkCache: chunkCache,
  70. fileSize: fileSize,
  71. }
  72. }
  73. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  74. c.readerLock.Lock()
  75. defer c.readerLock.Unlock()
  76. 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))
  77. return c.doReadAt(p[n:], offset+int64(n))
  78. }
  79. func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
  80. var buffer []byte
  81. startOffset, remaining := offset, int64(len(p))
  82. var nextChunk *ChunkView
  83. for i, chunk := range c.chunkViews {
  84. if remaining <= 0 {
  85. break
  86. }
  87. if i+1 < len(c.chunkViews) {
  88. nextChunk = c.chunkViews[i+1]
  89. } else {
  90. nextChunk = nil
  91. }
  92. if startOffset < chunk.LogicOffset {
  93. gap := int(chunk.LogicOffset - startOffset)
  94. glog.V(4).Infof("zero [%d,%d)", startOffset, startOffset+int64(gap))
  95. n += int(min(int64(gap), remaining))
  96. startOffset, remaining = chunk.LogicOffset, remaining-int64(gap)
  97. if remaining <= 0 {
  98. break
  99. }
  100. }
  101. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  102. chunkStart, chunkStop := max(chunk.LogicOffset, startOffset), min(chunk.LogicOffset+int64(chunk.Size), startOffset+remaining)
  103. if chunkStart >= chunkStop {
  104. continue
  105. }
  106. 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))
  107. buffer, err = c.readFromWholeChunkData(chunk, nextChunk)
  108. if err != nil {
  109. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  110. return
  111. }
  112. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  113. copied := copy(p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], buffer[bufferOffset:bufferOffset+chunkStop-chunkStart])
  114. n += copied
  115. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  116. }
  117. glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  118. if err == nil && remaining > 0 && c.fileSize > startOffset {
  119. delta := int(min(remaining, c.fileSize-startOffset))
  120. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+int64(delta), c.fileSize)
  121. n += delta
  122. }
  123. if err == nil && offset+int64(len(p)) > c.fileSize {
  124. err = io.EOF
  125. }
  126. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  127. return
  128. }
  129. func (c *ChunkReadAt) readFromWholeChunkData(chunkView *ChunkView, nextChunkViews ...*ChunkView) (chunkData []byte, err error) {
  130. if c.lastChunkFileId == chunkView.FileId {
  131. return c.lastChunkData, nil
  132. }
  133. v, doErr := c.readOneWholeChunk(chunkView)
  134. if doErr != nil {
  135. return nil, doErr
  136. }
  137. chunkData = v.([]byte)
  138. c.lastChunkData = chunkData
  139. c.lastChunkFileId = chunkView.FileId
  140. for _, nextChunkView := range nextChunkViews {
  141. if c.chunkCache != nil && nextChunkView != nil {
  142. go c.readOneWholeChunk(nextChunkView)
  143. }
  144. }
  145. return
  146. }
  147. func (c *ChunkReadAt) readOneWholeChunk(chunkView *ChunkView) (interface{}, error) {
  148. var err error
  149. return c.fetchGroup.Do(chunkView.FileId, func() (interface{}, error) {
  150. 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)
  151. data := c.chunkCache.GetChunk(chunkView.FileId, chunkView.ChunkSize)
  152. if data != nil {
  153. glog.V(4).Infof("cache hit %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset-chunkView.Offset, chunkView.LogicOffset-chunkView.Offset+int64(len(data)))
  154. } else {
  155. var err error
  156. data, err = c.doFetchFullChunkData(chunkView)
  157. if err != nil {
  158. return data, err
  159. }
  160. c.chunkCache.SetChunk(chunkView.FileId, data)
  161. }
  162. return data, err
  163. })
  164. }
  165. func (c *ChunkReadAt) doFetchFullChunkData(chunkView *ChunkView) ([]byte, error) {
  166. glog.V(4).Infof("+ doFetchFullChunkData %s", chunkView.FileId)
  167. data, err := fetchChunk(c.lookupFileId, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped)
  168. glog.V(4).Infof("- doFetchFullChunkData %s", chunkView.FileId)
  169. return data, err
  170. }