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.6 KiB

5 years ago
4 years ago
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
  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. buffer []byte
  16. bufferFileId string
  17. lookupFileId func(fileId string) (targetUrl string, err error)
  18. readerLock sync.Mutex
  19. fileSize int64
  20. chunkCache *chunk_cache.ChunkCache
  21. }
  22. // var _ = io.ReaderAt(&ChunkReadAt{})
  23. type LookupFileIdFunctionType func(fileId string) (targetUrl string, err error)
  24. func LookupFn(filerClient filer_pb.FilerClient) LookupFileIdFunctionType {
  25. return func(fileId string) (targetUrl string, err error) {
  26. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  27. vid := VolumeId(fileId)
  28. resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  29. VolumeIds: []string{vid},
  30. })
  31. if err != nil {
  32. return err
  33. }
  34. locations := resp.LocationsMap[vid]
  35. if locations == nil || len(locations.Locations) == 0 {
  36. glog.V(0).Infof("failed to locate %s", fileId)
  37. return fmt.Errorf("failed to locate %s", fileId)
  38. }
  39. volumeServerAddress := filerClient.AdjustedUrl(locations.Locations[0].Url)
  40. targetUrl = fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  41. return nil
  42. })
  43. return
  44. }
  45. }
  46. func NewChunkReaderAtFromClient(filerClient filer_pb.FilerClient, chunkViews []*ChunkView, chunkCache *chunk_cache.ChunkCache, fileSize int64) *ChunkReadAt {
  47. return &ChunkReadAt{
  48. chunkViews: chunkViews,
  49. lookupFileId: LookupFn(filerClient),
  50. chunkCache: chunkCache,
  51. fileSize: fileSize,
  52. }
  53. }
  54. func (c *ChunkReadAt) ReadAt(p []byte, offset int64) (n int, err error) {
  55. c.readerLock.Lock()
  56. defer c.readerLock.Unlock()
  57. for n < len(p) && err == nil {
  58. readCount, readErr := c.doReadAt(p[n:], offset+int64(n))
  59. n += readCount
  60. err = readErr
  61. }
  62. return
  63. }
  64. func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) {
  65. var startOffset = offset
  66. for _, chunk := range c.chunkViews {
  67. if startOffset < min(chunk.LogicOffset, int64(len(p))+offset) {
  68. gap := int(min(chunk.LogicOffset, int64(len(p))+offset) - startOffset)
  69. glog.V(4).Infof("zero [%d,%d)", n, n+gap)
  70. n += gap
  71. startOffset = chunk.LogicOffset
  72. }
  73. // fmt.Printf(">>> doReadAt [%d,%d), chunk[%d,%d)\n", offset, offset+int64(len(p)), chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  74. chunkStart, chunkStop := max(chunk.LogicOffset, offset), min(chunk.LogicOffset+int64(chunk.Size), offset+int64(len(p)))
  75. if chunkStart >= chunkStop {
  76. continue
  77. }
  78. glog.V(4).Infof("read [%d,%d), chunk %s [%d,%d)\n", chunkStart, chunkStop, chunk.FileId, chunk.LogicOffset-chunk.Offset, chunk.LogicOffset-chunk.Offset+int64(chunk.Size))
  79. c.buffer, err = c.fetchWholeChunkData(chunk)
  80. if err != nil {
  81. glog.Errorf("fetching chunk %+v: %v\n", chunk, err)
  82. return
  83. }
  84. c.bufferFileId = chunk.FileId
  85. bufferOffset := chunkStart - chunk.LogicOffset + chunk.Offset
  86. copied := copy(p[chunkStart-offset:chunkStop-offset], c.buffer[bufferOffset:bufferOffset+chunkStop-chunkStart])
  87. n += copied
  88. startOffset += int64(copied)
  89. }
  90. // fmt.Printf("> doReadAt [%d,%d), buffer %s:%d, found:%v, err:%v\n", offset, offset+int64(len(p)), c.bufferFileId, int64(len(c.buffer)), found, err)
  91. if startOffset < min(c.fileSize, int64(len(p))+offset) {
  92. gap := int(min(c.fileSize, int64(len(p))+offset) - startOffset)
  93. glog.V(4).Infof("zero2 [%d,%d)", n, n+gap)
  94. n += gap
  95. }
  96. if offset+int64(n) >= 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) fetchWholeChunkData(chunkView *ChunkView) (chunkData []byte, err error) {
  103. glog.V(4).Infof("fetchWholeChunkData %s offset %d [%d,%d)\n", chunkView.FileId, chunkView.Offset, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size))
  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. chunkData, err = c.doFetchFullChunkData(chunkView.FileId, chunkView.CipherKey, chunkView.IsGzipped)
  109. if err != nil {
  110. return
  111. }
  112. c.chunkCache.SetChunk(chunkView.FileId, chunkData)
  113. }
  114. return
  115. }
  116. func (c *ChunkReadAt) doFetchFullChunkData(fileId string, cipherKey []byte, isGzipped bool) ([]byte, error) {
  117. return fetchChunk(c.lookupFileId, fileId, cipherKey, isGzipped)
  118. }