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.

210 lines
5.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package filer
  2. import (
  3. "bytes"
  4. "io"
  5. "math"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/chrislusf/seaweedfs/weed/wdclient"
  11. )
  12. func StreamContent(masterClient *wdclient.MasterClient, w io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int64) error {
  13. // fmt.Printf("start to stream content for chunks: %+v\n", chunks)
  14. chunkViews := ViewFromChunks(masterClient.LookupFileId, chunks, offset, size)
  15. fileId2Url := make(map[string][]string)
  16. for _, chunkView := range chunkViews {
  17. urlStrings, err := masterClient.LookupFileId(chunkView.FileId)
  18. if err != nil {
  19. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  20. return err
  21. }
  22. fileId2Url[chunkView.FileId] = urlStrings
  23. }
  24. for _, chunkView := range chunkViews {
  25. urlStrings := fileId2Url[chunkView.FileId]
  26. data, err := retriedFetchChunkData(urlStrings, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size))
  27. if err == nil {
  28. return err
  29. }
  30. w.Write(data)
  31. }
  32. return nil
  33. }
  34. // ---------------- ReadAllReader ----------------------------------
  35. func ReadAll(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) ([]byte, error) {
  36. buffer := bytes.Buffer{}
  37. lookupFileIdFn := func(fileId string) (targetUrls []string, err error) {
  38. return masterClient.LookupFileId(fileId)
  39. }
  40. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  41. for _, chunkView := range chunkViews {
  42. urlStrings, err := lookupFileIdFn(chunkView.FileId)
  43. if err != nil {
  44. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  45. return nil, err
  46. }
  47. data, err := retriedFetchChunkData(urlStrings, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size))
  48. if err != nil {
  49. return nil, err
  50. }
  51. buffer.Write(data)
  52. }
  53. return buffer.Bytes(), nil
  54. }
  55. // ---------------- ChunkStreamReader ----------------------------------
  56. type ChunkStreamReader struct {
  57. chunkViews []*ChunkView
  58. logicOffset int64
  59. buffer []byte
  60. bufferOffset int64
  61. bufferPos int
  62. chunkIndex int
  63. lookupFileId LookupFileIdFunctionType
  64. }
  65. var _ = io.ReadSeeker(&ChunkStreamReader{})
  66. func NewChunkStreamReaderFromFiler(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  67. lookupFileIdFn := func(fileId string) (targetUrl []string, err error) {
  68. return masterClient.LookupFileId(fileId)
  69. }
  70. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  71. return &ChunkStreamReader{
  72. chunkViews: chunkViews,
  73. lookupFileId: lookupFileIdFn,
  74. }
  75. }
  76. func NewChunkStreamReader(filerClient filer_pb.FilerClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  77. lookupFileIdFn := LookupFn(filerClient)
  78. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  79. return &ChunkStreamReader{
  80. chunkViews: chunkViews,
  81. lookupFileId: lookupFileIdFn,
  82. }
  83. }
  84. func (c *ChunkStreamReader) Read(p []byte) (n int, err error) {
  85. for n < len(p) {
  86. if c.isBufferEmpty() {
  87. if c.chunkIndex >= len(c.chunkViews) {
  88. return n, io.EOF
  89. }
  90. chunkView := c.chunkViews[c.chunkIndex]
  91. c.fetchChunkToBuffer(chunkView)
  92. c.chunkIndex++
  93. }
  94. t := copy(p[n:], c.buffer[c.bufferPos:])
  95. c.bufferPos += t
  96. n += t
  97. }
  98. return
  99. }
  100. func (c *ChunkStreamReader) isBufferEmpty() bool {
  101. return len(c.buffer) <= c.bufferPos
  102. }
  103. func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) {
  104. var totalSize int64
  105. for _, chunk := range c.chunkViews {
  106. totalSize += int64(chunk.Size)
  107. }
  108. var err error
  109. switch whence {
  110. case io.SeekStart:
  111. case io.SeekCurrent:
  112. offset += c.bufferOffset + int64(c.bufferPos)
  113. case io.SeekEnd:
  114. offset = totalSize + offset
  115. }
  116. if offset > totalSize {
  117. err = io.ErrUnexpectedEOF
  118. }
  119. for i, chunk := range c.chunkViews {
  120. if chunk.LogicOffset <= offset && offset < chunk.LogicOffset+int64(chunk.Size) {
  121. if c.isBufferEmpty() || c.bufferOffset != chunk.LogicOffset {
  122. c.fetchChunkToBuffer(chunk)
  123. c.chunkIndex = i + 1
  124. break
  125. }
  126. }
  127. }
  128. c.bufferPos = int(offset - c.bufferOffset)
  129. return offset, err
  130. }
  131. func (c *ChunkStreamReader) fetchChunkToBuffer(chunkView *ChunkView) error {
  132. urlStrings, err := c.lookupFileId(chunkView.FileId)
  133. if err != nil {
  134. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  135. return err
  136. }
  137. var buffer bytes.Buffer
  138. for _, urlString := range urlStrings {
  139. err = util.ReadUrlAsStream(urlString, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size), func(data []byte) {
  140. buffer.Write(data)
  141. })
  142. if err != nil {
  143. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  144. buffer.Reset()
  145. } else {
  146. break
  147. }
  148. }
  149. if err != nil {
  150. return err
  151. }
  152. c.buffer = buffer.Bytes()
  153. c.bufferPos = 0
  154. c.bufferOffset = chunkView.LogicOffset
  155. // glog.V(0).Infof("read %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size))
  156. return nil
  157. }
  158. func (c *ChunkStreamReader) Close() {
  159. // TODO try to release and reuse buffer
  160. }
  161. func VolumeId(fileId string) string {
  162. lastCommaIndex := strings.LastIndex(fileId, ",")
  163. if lastCommaIndex > 0 {
  164. return fileId[:lastCommaIndex]
  165. }
  166. return fileId
  167. }