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.

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