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.

178 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
4 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 nextChunks []*ChunkView
  91. for i, chunk := range c.chunkViews {
  92. if remaining <= 0 {
  93. break
  94. }
  95. if i+1 < len(c.chunkViews) {
  96. nextChunks = c.chunkViews[i+1:]
  97. }
  98. if startOffset < chunk.LogicOffset {
  99. gap := int(chunk.LogicOffset - startOffset)
  100. glog.V(4).Infof("zero [%d,%d)", startOffset, chunk.LogicOffset)
  101. n += int(min(int64(gap), remaining))
  102. startOffset, remaining = chunk.LogicOffset, remaining-int64(gap)
  103. if remaining <= 0 {
  104. break
  105. }
  106. }
  107. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  108. chunkStart, chunkStop := max(chunk.LogicOffset, startOffset), min(chunk.LogicOffset+int64(chunk.Size), startOffset+remaining)
  109. if chunkStart >= chunkStop {
  110. continue
  111. }
  112. // 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))
  113. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  114. copied, err := c.readChunkSliceAt(p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], chunk, nextChunks, uint64(bufferOffset))
  115. if err != nil {
  116. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  117. return copied, err
  118. }
  119. n += copied
  120. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  121. }
  122. // glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  123. if err == nil && remaining > 0 && c.fileSize > startOffset {
  124. delta := int(min(remaining, c.fileSize-startOffset))
  125. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+int64(delta), c.fileSize)
  126. n += delta
  127. }
  128. if err == nil && offset+int64(len(p)) >= c.fileSize {
  129. err = io.EOF
  130. }
  131. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  132. return
  133. }
  134. func (c *ChunkReadAt) readChunkSliceAt(buffer []byte, chunkView *ChunkView, nextChunkViews []*ChunkView, offset uint64) (n int, err error) {
  135. if c.readerPattern.IsRandomMode() {
  136. return c.readerCache.ReadChunkAt(buffer, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset), int(chunkView.ChunkSize), chunkView.LogicOffset == 0)
  137. }
  138. n, err = c.readerCache.ReadChunkAt(buffer, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped, int64(offset), int(chunkView.ChunkSize), chunkView.LogicOffset == 0)
  139. for i, nextChunk := range nextChunkViews {
  140. if i < 2 {
  141. c.readerCache.MaybeCache(nextChunk.FileId, nextChunk.CipherKey, nextChunk.IsGzipped, int(nextChunk.ChunkSize))
  142. } else {
  143. break
  144. }
  145. }
  146. return
  147. }