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.

219 lines
5.6 KiB

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