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.

156 lines
4.2 KiB

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