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.

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