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.

147 lines
4.0 KiB

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