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.

245 lines
6.6 KiB

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