reader_at.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 wdclient.LookupFileIdFunctionType
  19. readerLock sync.Mutex
  20. fileSize int64
  21. fetchGroup singleflight.Group
  22. chunkCache chunk_cache.ChunkCache
  23. lastChunkFileId string
  24. lastChunkData []byte
  25. readerPattern *ReaderPattern
  26. }
  27. var _ = io.ReaderAt(&ChunkReadAt{})
  28. var _ = io.Closer(&ChunkReadAt{})
  29. func LookupFn(filerClient filer_pb.FilerClient) wdclient.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(false, 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(lookupFn wdclient.LookupFileIdFunctionType, chunkViews []*ChunkView, chunkCache chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt {
  75. return &ChunkReadAt{
  76. chunkViews: chunkViews,
  77. lookupFileId: lookupFn,
  78. chunkCache: chunkCache,
  79. fileSize: fileSize,
  80. readerPattern: NewReaderPattern(),
  81. }
  82. }
  83. func (c *ChunkReadAt) Close() error {
  84. c.lastChunkData = nil
  85. c.lastChunkFileId = ""
  86. return nil
  87. }
  88. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  89. c.readerPattern.MonitorReadAt(offset, len(p))
  90. c.readerLock.Lock()
  91. defer c.readerLock.Unlock()
  92. // 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))
  93. return c.doReadAt(p, offset)
  94. }
  95. func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
  96. startOffset, remaining := offset, int64(len(p))
  97. var nextChunk *ChunkView
  98. for i, chunk := range c.chunkViews {
  99. if remaining <= 0 {
  100. break
  101. }
  102. if i+1 < len(c.chunkViews) {
  103. nextChunk = c.chunkViews[i+1]
  104. } else {
  105. nextChunk = nil
  106. }
  107. if startOffset < chunk.LogicOffset {
  108. gap := int(chunk.LogicOffset - startOffset)
  109. glog.V(4).Infof("zero [%d,%d)", startOffset, chunk.LogicOffset)
  110. n += int(min(int64(gap), remaining))
  111. startOffset, remaining = chunk.LogicOffset, remaining-int64(gap)
  112. if remaining <= 0 {
  113. break
  114. }
  115. }
  116. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  117. chunkStart, chunkStop := max(chunk.LogicOffset, startOffset), min(chunk.LogicOffset+int64(chunk.Size), startOffset+remaining)
  118. if chunkStart >= chunkStop {
  119. continue
  120. }
  121. 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))
  122. var buffer []byte
  123. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  124. bufferLength := chunkStop - chunkStart
  125. buffer, err = c.readChunkSlice(chunk, nextChunk, uint64(bufferOffset), uint64(bufferLength))
  126. if err != nil {
  127. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  128. return
  129. }
  130. copied := copy(p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], buffer)
  131. n += copied
  132. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  133. }
  134. glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  135. if err == nil && remaining > 0 && c.fileSize > startOffset {
  136. delta := int(min(remaining, c.fileSize-startOffset))
  137. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+int64(delta), c.fileSize)
  138. n += delta
  139. }
  140. if err == nil && offset+int64(len(p)) >= c.fileSize {
  141. err = io.EOF
  142. }
  143. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  144. return
  145. }
  146. func (c *ChunkReadAt) readChunkSlice(chunkView *ChunkView, nextChunkViews *ChunkView, offset, length uint64) ([]byte, error) {
  147. var chunkSlice []byte
  148. if chunkView.LogicOffset == 0 {
  149. chunkSlice = c.chunkCache.GetChunkSlice(chunkView.FileId, offset, length)
  150. }
  151. if len(chunkSlice) > 0 {
  152. return chunkSlice, nil
  153. }
  154. if c.lookupFileId == nil {
  155. return nil, nil
  156. }
  157. if c.readerPattern.IsRandomMode() {
  158. return c.doFetchRangeChunkData(chunkView, offset, length)
  159. }
  160. chunkData, err := c.readFromWholeChunkData(chunkView, nextChunkViews)
  161. if err != nil {
  162. return nil, err
  163. }
  164. wanted := min(int64(length), int64(len(chunkData))-int64(offset))
  165. return chunkData[offset : int64(offset)+wanted], nil
  166. }
  167. func (c *ChunkReadAt) readFromWholeChunkData(chunkView *ChunkView, nextChunkViews ...*ChunkView) (chunkData []byte, err error) {
  168. if c.lastChunkFileId == chunkView.FileId {
  169. return c.lastChunkData, nil
  170. }
  171. v, doErr := c.readOneWholeChunk(chunkView)
  172. if doErr != nil {
  173. return nil, doErr
  174. }
  175. chunkData = v.([]byte)
  176. c.lastChunkData = chunkData
  177. c.lastChunkFileId = chunkView.FileId
  178. for _, nextChunkView := range nextChunkViews {
  179. if c.chunkCache != nil && nextChunkView != nil {
  180. go c.readOneWholeChunk(nextChunkView)
  181. }
  182. }
  183. return
  184. }
  185. func (c *ChunkReadAt) readOneWholeChunk(chunkView *ChunkView) (interface{}, error) {
  186. var err error
  187. return c.fetchGroup.Do(chunkView.FileId, func() (interface{}, error) {
  188. 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)
  189. var data []byte
  190. if chunkView.LogicOffset == 0 {
  191. data = c.chunkCache.GetChunk(chunkView.FileId, chunkView.ChunkSize)
  192. }
  193. if data != nil {
  194. glog.V(4).Infof("cache hit %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset-chunkView.Offset, chunkView.LogicOffset-chunkView.Offset+int64(len(data)))
  195. } else {
  196. var err error
  197. data, err = c.doFetchFullChunkData(chunkView)
  198. if err != nil {
  199. return data, err
  200. }
  201. if chunkView.LogicOffset == 0 {
  202. // only cache the first chunk
  203. c.chunkCache.SetChunk(chunkView.FileId, data)
  204. }
  205. }
  206. return data, err
  207. })
  208. }
  209. func (c *ChunkReadAt) doFetchFullChunkData(chunkView *ChunkView) ([]byte, error) {
  210. glog.V(4).Infof("+ doFetchFullChunkData %s", chunkView.FileId)
  211. data, err := fetchChunk(c.lookupFileId, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped)
  212. glog.V(4).Infof("- doFetchFullChunkData %s", chunkView.FileId)
  213. return data, err
  214. }
  215. func (c *ChunkReadAt) doFetchRangeChunkData(chunkView *ChunkView, offset, length uint64) ([]byte, error) {
  216. glog.V(4).Infof("+ doFetchFullChunkData %s", chunkView.FileId)
  217. data, err := fetchChunkRange(c.lookupFileId, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset), int(length))
  218. glog.V(4).Infof("- doFetchFullChunkData %s", chunkView.FileId)
  219. return data, err
  220. }