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.

149 lines
4.9 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
4 years ago
  1. package filer2
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util/chunk_cache"
  10. "github.com/chrislusf/seaweedfs/weed/wdclient"
  11. )
  12. type ChunkReadAt struct {
  13. masterClient *wdclient.MasterClient
  14. chunkViews []*ChunkView
  15. lookupFileId func(fileId string) (targetUrl string, err error)
  16. readerLock sync.Mutex
  17. fileSize int64
  18. chunkCache chunk_cache.ChunkCache
  19. }
  20. // var _ = io.ReaderAt(&ChunkReadAt{})
  21. type LookupFileIdFunctionType func(fileId string) (targetUrl string, err error)
  22. func LookupFn(filerClient filer_pb.FilerClient) LookupFileIdFunctionType {
  23. return func(fileId string) (targetUrl string, err error) {
  24. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  25. vid := VolumeId(fileId)
  26. resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  27. VolumeIds: []string{vid},
  28. })
  29. if err != nil {
  30. return err
  31. }
  32. locations := resp.LocationsMap[vid]
  33. if locations == nil || len(locations.Locations) == 0 {
  34. glog.V(0).Infof("failed to locate %s", fileId)
  35. return fmt.Errorf("failed to locate %s", fileId)
  36. }
  37. volumeServerAddress := filerClient.AdjustedUrl(locations.Locations[0].Url)
  38. targetUrl = fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  39. return nil
  40. })
  41. return
  42. }
  43. }
  44. func NewChunkReaderAtFromClient(filerClient filer_pb.FilerClient, chunkViews []*ChunkView, chunkCache chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt {
  45. return &ChunkReadAt{
  46. chunkViews: chunkViews,
  47. lookupFileId: LookupFn(filerClient),
  48. chunkCache: chunkCache,
  49. fileSize: fileSize,
  50. }
  51. }
  52. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  53. c.readerLock.Lock()
  54. defer c.readerLock.Unlock()
  55. 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))
  56. return c.doReadAt(p[n:], offset+int64(n))
  57. }
  58. func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
  59. var buffer []byte
  60. startOffset, remaining := offset, int64(len(p))
  61. for i, chunk := range c.chunkViews {
  62. if remaining <= 0 {
  63. break
  64. }
  65. if startOffset < chunk.LogicOffset {
  66. gap := int(chunk.LogicOffset - startOffset)
  67. glog.V(4).Infof("zero [%d,%d)", startOffset, startOffset+int64(gap))
  68. n += int(min(int64(gap), remaining))
  69. startOffset, remaining = chunk.LogicOffset, remaining-int64(gap)
  70. if remaining <= 0 {
  71. break
  72. }
  73. }
  74. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  75. chunkStart, chunkStop := max(chunk.LogicOffset, startOffset), min(chunk.LogicOffset+int64(chunk.Size), startOffset+remaining)
  76. if chunkStart >= chunkStop {
  77. continue
  78. }
  79. 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))
  80. buffer, err = c.readFromWholeChunkData(chunk)
  81. if err != nil {
  82. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  83. return
  84. }
  85. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  86. copied := copy(p[startOffset-offset:chunkStop-chunkStart+startOffset-offset], buffer[bufferOffset:bufferOffset+chunkStop-chunkStart])
  87. n += copied
  88. startOffset, remaining = startOffset+int64(copied), remaining-int64(copied)
  89. }
  90. glog.V(4).Infof("doReadAt [%d,%d), n:%v, err:%v", offset, offset+int64(len(p)), n, err)
  91. if err == nil && remaining > 0 && c.fileSize > startOffset {
  92. delta := int(min(remaining, c.fileSize - startOffset))
  93. glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+int64(delta), c.fileSize)
  94. n += delta
  95. }
  96. if err == nil && offset+int64(len(p)) > c.fileSize {
  97. err = io.EOF
  98. }
  99. // fmt.Printf("~~~ filled %d, err: %v\n\n", n, err)
  100. return
  101. }
  102. func (c *ChunkReadAt) readFromWholeChunkData(chunkView *ChunkView) (chunkData []byte, err error) {
  103. 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)
  104. chunkData = c.chunkCache.GetChunk(chunkView.FileId, chunkView.ChunkSize)
  105. if chunkData != nil {
  106. glog.V(5).Infof("cache hit %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset-chunkView.Offset, chunkView.LogicOffset-chunkView.Offset+int64(len(chunkData)))
  107. } else {
  108. glog.V(4).Infof("doFetchFullChunkData %s", chunkView.FileId)
  109. chunkData, err = c.doFetchFullChunkData(chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped)
  110. if err != nil {
  111. return
  112. }
  113. c.chunkCache.SetChunk(chunkView.FileId, chunkData)
  114. }
  115. return
  116. }
  117. func (c *ChunkReadAt) doFetchFullChunkData(fileId string, cipherKey []byte, isGzipped bool) ([]byte, error) {
  118. return fetchChunk(c.lookupFileId, fileId, cipherKey, isGzipped)
  119. }