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.

206 lines
6.1 KiB

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