stream.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }