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.

170 lines
5.3 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
5 years ago
5 years ago
5 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/chunk_cache"
  11. "github.com/chrislusf/seaweedfs/weed/wdclient"
  12. )
  13. type ChunkReadAt struct {
  14. masterClient *wdclient.MasterClient
  15. chunkViews []*ChunkView
  16. lookupFileId func(fileId string) (targetUrl string, err error)
  17. readerLock sync.Mutex
  18. fetcherLock sync.Mutex
  19. fileSize int64
  20. lastChunkFileId string
  21. lastChunkData []byte
  22. chunkCache chunk_cache.ChunkCache
  23. }
  24. // var _ = io.ReaderAt(&ChunkReadAt{})
  25. type LookupFileIdFunctionType func(fileId string) (targetUrl string, err error)
  26. func LookupFn(filerClient filer_pb.FilerClient) LookupFileIdFunctionType {
  27. vidCache := make(map[string]*filer_pb.Locations)
  28. return func(fileId string) (targetUrl 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. volumeServerAddress := filerClient.AdjustedUrl(locations.Locations[rand.Intn(len(locations.Locations))].Url)
  50. targetUrl = fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  51. return
  52. }
  53. }
  54. func NewChunkReaderAtFromClient(filerClient filer_pb.FilerClient, chunkViews []*ChunkView, chunkCache chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt {
  55. return &ChunkReadAt{
  56. chunkViews: chunkViews,
  57. lookupFileId: LookupFn(filerClient),
  58. chunkCache: chunkCache,
  59. fileSize: fileSize,
  60. }
  61. }
  62. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  63. c.readerLock.Lock()
  64. defer c.readerLock.Unlock()
  65. 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))
  66. return c.doReadAt(p[n:], offset+int64(n))
  67. }
  68. func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
  69. var buffer []byte
  70. startOffset, remaining := offset, int64(len(p))
  71. for i, chunk := range c.chunkViews {
  72. if remaining <= 0 {
  73. break
  74. }
  75. if startOffset < chunk.LogicOffset {
  76. gap := int(chunk.LogicOffset - startOffset)
  77. glog.V(4).Infof("zero [%d,%d)", startOffset, startOffset+int64(gap))
  78. n += int(min(int64(gap), remaining))
  79. startOffset, remaining = chunk.LogicOffset, remaining-int64(gap)
  80. if remaining <= 0 {
  81. break
  82. }
  83. }
  84. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  85. chunkStart, chunkStop := max(chunk.LogicOffset, startOffset), min(chunk.LogicOffset+int64(chunk.Size), startOffset+remaining)
  86. if chunkStart >= chunkStop {
  87. continue
  88. }
  89. 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))
  90. buffer, err = c.readFromWholeChunkData(chunk)
  91. if err != nil {
  92. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  93. return
  94. }
  95. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  96. copied := copy(p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], buffer[bufferOffset:bufferOffset+chunkStop-chunkStart])
  97. n += copied
  98. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  99. }
  100. glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  101. if err == nil && remaining > 0 && c.fileSize > startOffset {
  102. delta := int(min(remaining, c.fileSize-startOffset))
  103. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+int64(delta), c.fileSize)
  104. n += delta
  105. }
  106. if err == nil && offset+int64(len(p)) > c.fileSize {
  107. err = io.EOF
  108. }
  109. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  110. return
  111. }
  112. func (c *ChunkReadAt) readFromWholeChunkData(chunkView *ChunkView) (chunkData []byte, err error) {
  113. c.fetcherLock.Lock()
  114. defer c.fetcherLock.Unlock()
  115. if c.lastChunkFileId == chunkView.FileId {
  116. return c.lastChunkData, nil
  117. }
  118. 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)
  119. chunkData = c.chunkCache.GetChunk(chunkView.FileId, chunkView.ChunkSize)
  120. if chunkData != nil {
  121. glog.V(4).Infof("cache hit %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset-chunkView.Offset, chunkView.LogicOffset-chunkView.Offset+int64(len(chunkData)))
  122. } else {
  123. chunkData, err = c.doFetchFullChunkData(chunkView)
  124. if err != nil {
  125. return
  126. }
  127. c.chunkCache.SetChunk(chunkView.FileId, chunkData)
  128. c.lastChunkData = chunkData
  129. c.lastChunkFileId = chunkView.FileId
  130. }
  131. return
  132. }
  133. func (c *ChunkReadAt) doFetchFullChunkData(chunkView *ChunkView) ([]byte, error) {
  134. data, err := fetchChunk(c.lookupFileId, chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped)
  135. return data, err
  136. }