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.

248 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. if !chunkView.IsFullChunk() {
  59. continue
  60. }
  61. gErr.Go(func() error {
  62. _, err := retriedFetchChunkData(urlStrings, chunkView.CipherKey, chunkView.IsGzipped, false, 0, 2)
  63. return err
  64. })
  65. }
  66. }
  67. // ---------------- ReadAllReader ----------------------------------
  68. func ReadAll(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) ([]byte, error) {
  69. buffer := bytes.Buffer{}
  70. lookupFileIdFn := func(fileId string) (targetUrls []string, err error) {
  71. return masterClient.LookupFileId(fileId)
  72. }
  73. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  74. for _, chunkView := range chunkViews {
  75. urlStrings, err := lookupFileIdFn(chunkView.FileId)
  76. if err != nil {
  77. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  78. return nil, err
  79. }
  80. data, err := retriedFetchChunkData(urlStrings, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size))
  81. if err != nil {
  82. return nil, err
  83. }
  84. buffer.Write(data)
  85. }
  86. return buffer.Bytes(), nil
  87. }
  88. // ---------------- ChunkStreamReader ----------------------------------
  89. type ChunkStreamReader struct {
  90. chunkViews []*ChunkView
  91. logicOffset int64
  92. buffer []byte
  93. bufferOffset int64
  94. bufferPos int
  95. chunkIndex int
  96. lookupFileId wdclient.LookupFileIdFunctionType
  97. }
  98. var _ = io.ReadSeeker(&ChunkStreamReader{})
  99. func NewChunkStreamReaderFromFiler(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  100. lookupFileIdFn := func(fileId string) (targetUrl []string, err error) {
  101. return masterClient.LookupFileId(fileId)
  102. }
  103. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  104. return &ChunkStreamReader{
  105. chunkViews: chunkViews,
  106. lookupFileId: lookupFileIdFn,
  107. }
  108. }
  109. func NewChunkStreamReader(filerClient filer_pb.FilerClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  110. lookupFileIdFn := LookupFn(filerClient)
  111. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  112. return &ChunkStreamReader{
  113. chunkViews: chunkViews,
  114. lookupFileId: lookupFileIdFn,
  115. }
  116. }
  117. func (c *ChunkStreamReader) Read(p []byte) (n int, err error) {
  118. for n < len(p) {
  119. if c.isBufferEmpty() {
  120. if c.chunkIndex >= len(c.chunkViews) {
  121. return n, io.EOF
  122. }
  123. chunkView := c.chunkViews[c.chunkIndex]
  124. c.fetchChunkToBuffer(chunkView)
  125. c.chunkIndex++
  126. }
  127. t := copy(p[n:], c.buffer[c.bufferPos:])
  128. c.bufferPos += t
  129. n += t
  130. }
  131. return
  132. }
  133. func (c *ChunkStreamReader) isBufferEmpty() bool {
  134. return len(c.buffer) <= c.bufferPos
  135. }
  136. func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) {
  137. var totalSize int64
  138. for _, chunk := range c.chunkViews {
  139. totalSize += int64(chunk.Size)
  140. }
  141. var err error
  142. switch whence {
  143. case io.SeekStart:
  144. case io.SeekCurrent:
  145. offset += c.bufferOffset + int64(c.bufferPos)
  146. case io.SeekEnd:
  147. offset = totalSize + offset
  148. }
  149. if offset > totalSize {
  150. err = io.ErrUnexpectedEOF
  151. }
  152. for i, chunk := range c.chunkViews {
  153. if chunk.LogicOffset <= offset && offset < chunk.LogicOffset+int64(chunk.Size) {
  154. if c.isBufferEmpty() || c.bufferOffset != chunk.LogicOffset {
  155. c.fetchChunkToBuffer(chunk)
  156. c.chunkIndex = i + 1
  157. break
  158. }
  159. }
  160. }
  161. c.bufferPos = int(offset - c.bufferOffset)
  162. return offset, err
  163. }
  164. func (c *ChunkStreamReader) fetchChunkToBuffer(chunkView *ChunkView) error {
  165. urlStrings, err := c.lookupFileId(chunkView.FileId)
  166. if err != nil {
  167. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  168. return err
  169. }
  170. var buffer bytes.Buffer
  171. var shouldRetry bool
  172. for _, urlString := range urlStrings {
  173. shouldRetry, err = util.ReadUrlAsStream(urlString, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size), func(data []byte) {
  174. buffer.Write(data)
  175. })
  176. if !shouldRetry {
  177. break
  178. }
  179. if err != nil {
  180. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  181. buffer.Reset()
  182. } else {
  183. break
  184. }
  185. }
  186. if err != nil {
  187. return err
  188. }
  189. c.buffer = buffer.Bytes()
  190. c.bufferPos = 0
  191. c.bufferOffset = chunkView.LogicOffset
  192. // glog.V(0).Infof("read %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size))
  193. return nil
  194. }
  195. func (c *ChunkStreamReader) Close() {
  196. // TODO try to release and reuse buffer
  197. }
  198. func VolumeId(fileId string) string {
  199. lastCommaIndex := strings.LastIndex(fileId, ",")
  200. if lastCommaIndex > 0 {
  201. return fileId[:lastCommaIndex]
  202. }
  203. return fileId
  204. }