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.

188 lines
4.8 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
  1. package filer2
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "math"
  8. "strings"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/chrislusf/seaweedfs/weed/wdclient"
  13. )
  14. func StreamContent(masterClient *wdclient.MasterClient, w io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int64) error {
  15. chunkViews := ViewFromChunks(chunks, offset, size)
  16. fileId2Url := make(map[string]string)
  17. for _, chunkView := range chunkViews {
  18. urlString, err := masterClient.LookupFileId(chunkView.FileId)
  19. if err != nil {
  20. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  21. return err
  22. }
  23. fileId2Url[chunkView.FileId] = urlString
  24. }
  25. for _, chunkView := range chunkViews {
  26. urlString := fileId2Url[chunkView.FileId]
  27. err := util.ReadUrlAsStream(urlString, chunkView.CipherKey, chunkView.isGzipped, chunkView.IsFullChunk, chunkView.Offset, int(chunkView.Size), func(data []byte) {
  28. w.Write(data)
  29. })
  30. if err != nil {
  31. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  32. return err
  33. }
  34. }
  35. return nil
  36. }
  37. type ChunkStreamReader struct {
  38. masterClient *wdclient.MasterClient
  39. chunkViews []*ChunkView
  40. logicOffset int64
  41. buffer []byte
  42. bufferOffset int64
  43. bufferPos int
  44. chunkIndex int
  45. lookupFileId func(fileId string) (targetUrl string, err error)
  46. }
  47. var _ = io.ReadSeeker(&ChunkStreamReader{})
  48. func NewChunkStreamReaderFromFiler(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  49. chunkViews := ViewFromChunks(chunks, 0, math.MaxInt64)
  50. return &ChunkStreamReader{
  51. chunkViews: chunkViews,
  52. lookupFileId: func(fileId string) (targetUrl string, err error) {
  53. return masterClient.LookupFileId(fileId)
  54. },
  55. }
  56. }
  57. func NewChunkStreamReaderFromClient(filerClient filer_pb.FilerClient, chunkViews []*ChunkView) *ChunkStreamReader {
  58. return &ChunkStreamReader{
  59. chunkViews: chunkViews,
  60. lookupFileId: func(fileId string) (targetUrl string, err error) {
  61. err = filerClient.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  62. vid := VolumeId(fileId)
  63. resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
  64. VolumeIds: []string{vid},
  65. })
  66. if err != nil {
  67. return err
  68. }
  69. locations := resp.LocationsMap[vid]
  70. if locations == nil || len(locations.Locations) == 0 {
  71. glog.V(0).Infof("failed to locate %s", fileId)
  72. return fmt.Errorf("failed to locate %s", fileId)
  73. }
  74. volumeServerAddress := filerClient.AdjustedUrl(locations.Locations[0].Url)
  75. targetUrl = fmt.Sprintf("http://%s/%s", volumeServerAddress, fileId)
  76. return nil
  77. })
  78. return
  79. },
  80. }
  81. }
  82. func (c *ChunkStreamReader) Read(p []byte) (n int, err error) {
  83. if c.isBufferEmpty() {
  84. if c.chunkIndex >= len(c.chunkViews) {
  85. return 0, io.EOF
  86. }
  87. chunkView := c.chunkViews[c.chunkIndex]
  88. println("fetch1")
  89. c.fetchChunkToBuffer(chunkView)
  90. c.chunkIndex++
  91. }
  92. n = copy(p, c.buffer[c.bufferPos:])
  93. c.bufferPos += n
  94. return
  95. }
  96. func (c *ChunkStreamReader) isBufferEmpty() bool {
  97. return len(c.buffer) <= c.bufferPos
  98. }
  99. func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) {
  100. var totalSize int64
  101. for _, chunk := range c.chunkViews {
  102. totalSize += int64(chunk.Size)
  103. }
  104. var err error
  105. switch whence {
  106. case io.SeekStart:
  107. case io.SeekCurrent:
  108. offset += c.bufferOffset + int64(c.bufferPos)
  109. case io.SeekEnd:
  110. offset = totalSize + offset
  111. }
  112. if offset > totalSize {
  113. err = io.ErrUnexpectedEOF
  114. }
  115. for i, chunk := range c.chunkViews {
  116. if chunk.LogicOffset <= offset && offset < chunk.LogicOffset+int64(chunk.Size) {
  117. if c.isBufferEmpty() || c.bufferOffset != chunk.LogicOffset {
  118. c.fetchChunkToBuffer(chunk)
  119. c.chunkIndex = i + 1
  120. break
  121. }
  122. }
  123. }
  124. c.bufferPos = int(offset - c.bufferOffset)
  125. return offset, err
  126. }
  127. func (c *ChunkStreamReader) fetchChunkToBuffer(chunkView *ChunkView) error {
  128. urlString, err := c.lookupFileId(chunkView.FileId)
  129. if err != nil {
  130. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  131. return err
  132. }
  133. var buffer bytes.Buffer
  134. err = util.ReadUrlAsStream(urlString, chunkView.CipherKey, chunkView.isGzipped, chunkView.IsFullChunk, chunkView.Offset, int(chunkView.Size), func(data []byte) {
  135. buffer.Write(data)
  136. })
  137. if err != nil {
  138. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  139. return err
  140. }
  141. c.buffer = buffer.Bytes()
  142. c.bufferPos = 0
  143. c.bufferOffset = chunkView.LogicOffset
  144. // glog.V(0).Infof("read %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size))
  145. return nil
  146. }
  147. func VolumeId(fileId string) string {
  148. lastCommaIndex := strings.LastIndex(fileId, ",")
  149. if lastCommaIndex > 0 {
  150. return fileId[:lastCommaIndex]
  151. }
  152. return fileId
  153. }