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.

176 lines
5.4 KiB

4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
  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. )
  14. type ChunkReadAt struct {
  15. masterClient *wdclient.MasterClient
  16. chunkViews []*ChunkView
  17. readerLock sync.Mutex
  18. fileSize int64
  19. readerCache *ReaderCache
  20. readerPattern *ReaderPattern
  21. }
  22. var _ = io.ReaderAt(&ChunkReadAt{})
  23. var _ = io.Closer(&ChunkReadAt{})
  24. func LookupFn(filerClient filer_pb.FilerClient) wdclient.LookupFileIdFunctionType {
  25. vidCache := make(map[string]*filer_pb.Locations)
  26. var vicCacheLock sync.RWMutex
  27. return func(fileId string) (targetUrls []string, err error) {
  28. vid := VolumeId(fileId)
  29. vicCacheLock.RLock()
  30. locations, found := vidCache[vid]
  31. vicCacheLock.RUnlock()
  32. if !found {
  33. util.Retry("lookup volume "+vid, func() error {
  34. err = filerClient.WithFilerClient(false, 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. vicCacheLock.Lock()
  47. vidCache[vid] = locations
  48. vicCacheLock.Unlock()
  49. return nil
  50. })
  51. return err
  52. })
  53. }
  54. if err != nil {
  55. return nil, err
  56. }
  57. for _, loc := range locations.Locations {
  58. volumeServerAddress := filerClient.AdjustedUrl(loc)
  59. targetUrl := fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  60. targetUrls = append(targetUrls, targetUrl)
  61. }
  62. for i := len(targetUrls) - 1; i > 0; i-- {
  63. j := rand.Intn(i + 1)
  64. targetUrls[i], targetUrls[j] = targetUrls[j], targetUrls[i]
  65. }
  66. return
  67. }
  68. }
  69. func NewChunkReaderAtFromClient(lookupFn wdclient.LookupFileIdFunctionType, chunkViews []*ChunkView, chunkCache chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt {
  70. return &ChunkReadAt{
  71. chunkViews: chunkViews,
  72. fileSize: fileSize,
  73. readerCache: newReaderCache(5, chunkCache, lookupFn),
  74. readerPattern: NewReaderPattern(),
  75. }
  76. }
  77. func (c *ChunkReadAt) Close() error {
  78. c.readerCache.destroy()
  79. return nil
  80. }
  81. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  82. c.readerPattern.MonitorReadAt(offset, len(p))
  83. c.readerLock.Lock()
  84. defer c.readerLock.Unlock()
  85. // 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))
  86. return c.doReadAt(p, offset)
  87. }
  88. func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
  89. startOffset, remaining := offset, int64(len(p))
  90. var nextChunk *ChunkView
  91. for i, chunk := range c.chunkViews {
  92. if remaining <= 0 {
  93. break
  94. }
  95. if i+1 < len(c.chunkViews) {
  96. nextChunk = c.chunkViews[i+1]
  97. } else {
  98. nextChunk = nil
  99. }
  100. if startOffset < chunk.LogicOffset {
  101. gap := int(chunk.LogicOffset - startOffset)
  102. glog.V(4).Infof("zero [%d,%d)", startOffset, chunk.LogicOffset)
  103. n += int(min(int64(gap), remaining))
  104. startOffset, remaining = chunk.LogicOffset, remaining-int64(gap)
  105. if remaining <= 0 {
  106. break
  107. }
  108. }
  109. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  110. chunkStart, chunkStop := max(chunk.LogicOffset, startOffset), min(chunk.LogicOffset+int64(chunk.Size), startOffset+remaining)
  111. if chunkStart >= chunkStop {
  112. continue
  113. }
  114. // 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))
  115. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  116. copied, err := c.readChunkSliceAt(p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], chunk, nextChunk, uint64(bufferOffset))
  117. if err != nil {
  118. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  119. return copied, err
  120. }
  121. n += copied
  122. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  123. }
  124. // glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  125. if err == nil && remaining > 0 && c.fileSize > startOffset {
  126. delta := int(min(remaining, c.fileSize-startOffset))
  127. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+int64(delta), c.fileSize)
  128. n += delta
  129. }
  130. if err == nil && offset+int64(len(p)) >= c.fileSize {
  131. err = io.EOF
  132. }
  133. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  134. return
  135. }
  136. func (c *ChunkReadAt) readChunkSliceAt(buffer []byte, chunkView *ChunkView, nextChunkViews *ChunkView, offset uint64) (n int, err error) {
  137. if c.readerPattern.IsRandomMode() {
  138. return c.readerCache.ReadChunkAt(buffer, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset), int(chunkView.ChunkSize), chunkView.LogicOffset == 0)
  139. }
  140. n, err = c.readerCache.ReadChunkAt(buffer, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset), int(chunkView.ChunkSize), chunkView.LogicOffset == 0)
  141. if nextChunkViews != nil {
  142. c.readerCache.MaybeCache(nextChunkViews.FileId, nextChunkViews.CipherKey, nextChunkViews.IsGzipped, int(nextChunkViews.ChunkSize))
  143. }
  144. return
  145. }