stream.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package filer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "golang.org/x/exp/slices"
  6. "io"
  7. "math"
  8. "sort"
  9. "strings"
  10. "sync"
  11. "time"
  12. "github.com/seaweedfs/seaweedfs/weed/glog"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/stats"
  15. "github.com/seaweedfs/seaweedfs/weed/util"
  16. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  17. )
  18. var getLookupFileIdBackoffSchedule = []time.Duration{
  19. 150 * time.Millisecond,
  20. 600 * time.Millisecond,
  21. 1800 * time.Millisecond,
  22. }
  23. func HasData(entry *filer_pb.Entry) bool {
  24. if len(entry.Content) > 0 {
  25. return true
  26. }
  27. return len(entry.GetChunks()) > 0
  28. }
  29. func IsSameData(a, b *filer_pb.Entry) bool {
  30. if len(a.Content) > 0 || len(b.Content) > 0 {
  31. return bytes.Equal(a.Content, b.Content)
  32. }
  33. return isSameChunks(a.Chunks, b.Chunks)
  34. }
  35. func isSameChunks(a, b []*filer_pb.FileChunk) bool {
  36. if len(a) != len(b) {
  37. return false
  38. }
  39. slices.SortFunc(a, func(i, j *filer_pb.FileChunk) bool {
  40. return strings.Compare(i.ETag, j.ETag) < 0
  41. })
  42. slices.SortFunc(b, func(i, j *filer_pb.FileChunk) bool {
  43. return strings.Compare(i.ETag, j.ETag) < 0
  44. })
  45. for i := 0; i < len(a); i++ {
  46. if a[i].ETag != b[i].ETag {
  47. return false
  48. }
  49. }
  50. return true
  51. }
  52. func NewFileReader(filerClient filer_pb.FilerClient, entry *filer_pb.Entry) io.Reader {
  53. if len(entry.Content) > 0 {
  54. return bytes.NewReader(entry.Content)
  55. }
  56. return NewChunkStreamReader(filerClient, entry.GetChunks())
  57. }
  58. func StreamContent(masterClient wdclient.HasLookupFileIdFunction, writer io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int64) error {
  59. return StreamContentWithThrottler(masterClient, writer, chunks, offset, size, 0)
  60. }
  61. func StreamContentWithThrottler(masterClient wdclient.HasLookupFileIdFunction, writer io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int64, downloadMaxBytesPs int64) error {
  62. glog.V(4).Infof("start to stream content for chunks: %d", len(chunks))
  63. chunkViews := ViewFromChunks(masterClient.GetLookupFileIdFunction(), chunks, offset, size)
  64. fileId2Url := make(map[string][]string)
  65. for _, chunkView := range chunkViews {
  66. var urlStrings []string
  67. var err error
  68. for _, backoff := range getLookupFileIdBackoffSchedule {
  69. urlStrings, err = masterClient.GetLookupFileIdFunction()(chunkView.FileId)
  70. if err == nil && len(urlStrings) > 0 {
  71. break
  72. }
  73. glog.V(4).Infof("waiting for chunk: %s", chunkView.FileId)
  74. time.Sleep(backoff)
  75. }
  76. if err != nil {
  77. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  78. return err
  79. } else if len(urlStrings) == 0 {
  80. errUrlNotFound := fmt.Errorf("operation LookupFileId %s failed, err: urls not found", chunkView.FileId)
  81. glog.Error(errUrlNotFound)
  82. return errUrlNotFound
  83. }
  84. fileId2Url[chunkView.FileId] = urlStrings
  85. }
  86. downloadThrottler := util.NewWriteThrottler(downloadMaxBytesPs)
  87. remaining := size
  88. for _, chunkView := range chunkViews {
  89. if offset < chunkView.LogicOffset {
  90. gap := chunkView.LogicOffset - offset
  91. remaining -= gap
  92. glog.V(4).Infof("zero [%d,%d)", offset, chunkView.LogicOffset)
  93. err := writeZero(writer, gap)
  94. if err != nil {
  95. return fmt.Errorf("write zero [%d,%d)", offset, chunkView.LogicOffset)
  96. }
  97. offset = chunkView.LogicOffset
  98. }
  99. urlStrings := fileId2Url[chunkView.FileId]
  100. start := time.Now()
  101. err := retriedStreamFetchChunkData(writer, urlStrings, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size))
  102. offset += int64(chunkView.Size)
  103. remaining -= int64(chunkView.Size)
  104. stats.FilerRequestHistogram.WithLabelValues("chunkDownload").Observe(time.Since(start).Seconds())
  105. if err != nil {
  106. stats.FilerRequestCounter.WithLabelValues("chunkDownloadError").Inc()
  107. return fmt.Errorf("read chunk: %v", err)
  108. }
  109. stats.FilerRequestCounter.WithLabelValues("chunkDownload").Inc()
  110. downloadThrottler.MaybeSlowdown(int64(chunkView.Size))
  111. }
  112. if remaining > 0 {
  113. glog.V(4).Infof("zero [%d,%d)", offset, offset+remaining)
  114. err := writeZero(writer, remaining)
  115. if err != nil {
  116. return fmt.Errorf("write zero [%d,%d)", offset, offset+remaining)
  117. }
  118. }
  119. return nil
  120. }
  121. // ---------------- ReadAllReader ----------------------------------
  122. func writeZero(w io.Writer, size int64) (err error) {
  123. zeroPadding := make([]byte, 1024)
  124. var written int
  125. for size > 0 {
  126. if size > 1024 {
  127. written, err = w.Write(zeroPadding)
  128. } else {
  129. written, err = w.Write(zeroPadding[:size])
  130. }
  131. size -= int64(written)
  132. if err != nil {
  133. return
  134. }
  135. }
  136. return
  137. }
  138. func ReadAll(buffer []byte, masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) error {
  139. lookupFileIdFn := func(fileId string) (targetUrls []string, err error) {
  140. return masterClient.LookupFileId(fileId)
  141. }
  142. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, int64(len(buffer)))
  143. idx := 0
  144. for _, chunkView := range chunkViews {
  145. urlStrings, err := lookupFileIdFn(chunkView.FileId)
  146. if err != nil {
  147. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  148. return err
  149. }
  150. n, err := retriedFetchChunkData(buffer[idx:idx+int(chunkView.Size)], urlStrings, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset)
  151. if err != nil {
  152. return err
  153. }
  154. idx += n
  155. }
  156. return nil
  157. }
  158. // ---------------- ChunkStreamReader ----------------------------------
  159. type ChunkStreamReader struct {
  160. chunkViews []*ChunkView
  161. totalSize int64
  162. logicOffset int64
  163. buffer []byte
  164. bufferOffset int64
  165. bufferLock sync.Mutex
  166. chunk string
  167. lookupFileId wdclient.LookupFileIdFunctionType
  168. }
  169. var _ = io.ReadSeeker(&ChunkStreamReader{})
  170. var _ = io.ReaderAt(&ChunkStreamReader{})
  171. func doNewChunkStreamReader(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  172. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  173. slices.SortFunc(chunkViews, func(a, b *ChunkView) bool {
  174. return a.LogicOffset < b.LogicOffset
  175. })
  176. var totalSize int64
  177. for _, chunk := range chunkViews {
  178. totalSize += int64(chunk.Size)
  179. }
  180. return &ChunkStreamReader{
  181. chunkViews: chunkViews,
  182. lookupFileId: lookupFileIdFn,
  183. totalSize: totalSize,
  184. }
  185. }
  186. func NewChunkStreamReaderFromFiler(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  187. lookupFileIdFn := func(fileId string) (targetUrl []string, err error) {
  188. return masterClient.LookupFileId(fileId)
  189. }
  190. return doNewChunkStreamReader(lookupFileIdFn, chunks)
  191. }
  192. func NewChunkStreamReader(filerClient filer_pb.FilerClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  193. lookupFileIdFn := LookupFn(filerClient)
  194. return doNewChunkStreamReader(lookupFileIdFn, chunks)
  195. }
  196. func (c *ChunkStreamReader) ReadAt(p []byte, off int64) (n int, err error) {
  197. c.bufferLock.Lock()
  198. defer c.bufferLock.Unlock()
  199. if err = c.prepareBufferFor(off); err != nil {
  200. return
  201. }
  202. c.logicOffset = off
  203. return c.doRead(p)
  204. }
  205. func (c *ChunkStreamReader) Read(p []byte) (n int, err error) {
  206. c.bufferLock.Lock()
  207. defer c.bufferLock.Unlock()
  208. return c.doRead(p)
  209. }
  210. func (c *ChunkStreamReader) doRead(p []byte) (n int, err error) {
  211. // fmt.Printf("do read [%d,%d) at %s[%d,%d)\n", c.logicOffset, c.logicOffset+int64(len(p)), c.chunk, c.bufferOffset, c.bufferOffset+int64(len(c.buffer)))
  212. for n < len(p) {
  213. // println("read", c.logicOffset)
  214. if err = c.prepareBufferFor(c.logicOffset); err != nil {
  215. return
  216. }
  217. t := copy(p[n:], c.buffer[c.logicOffset-c.bufferOffset:])
  218. n += t
  219. c.logicOffset += int64(t)
  220. }
  221. return
  222. }
  223. func (c *ChunkStreamReader) isBufferEmpty() bool {
  224. return len(c.buffer) <= int(c.logicOffset-c.bufferOffset)
  225. }
  226. func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) {
  227. c.bufferLock.Lock()
  228. defer c.bufferLock.Unlock()
  229. var err error
  230. switch whence {
  231. case io.SeekStart:
  232. case io.SeekCurrent:
  233. offset += c.logicOffset
  234. case io.SeekEnd:
  235. offset = c.totalSize + offset
  236. }
  237. if offset > c.totalSize {
  238. err = io.ErrUnexpectedEOF
  239. } else {
  240. c.logicOffset = offset
  241. }
  242. return offset, err
  243. }
  244. func insideChunk(offset int64, chunk *ChunkView) bool {
  245. return chunk.LogicOffset <= offset && offset < chunk.LogicOffset+int64(chunk.Size)
  246. }
  247. func (c *ChunkStreamReader) prepareBufferFor(offset int64) (err error) {
  248. // stay in the same chunk
  249. if c.bufferOffset <= offset && offset < c.bufferOffset+int64(len(c.buffer)) {
  250. return nil
  251. }
  252. // fmt.Printf("fetch for offset %d\n", offset)
  253. // need to seek to a different chunk
  254. currentChunkIndex := sort.Search(len(c.chunkViews), func(i int) bool {
  255. return offset < c.chunkViews[i].LogicOffset
  256. })
  257. if currentChunkIndex == len(c.chunkViews) {
  258. // not found
  259. if insideChunk(offset, c.chunkViews[0]) {
  260. // fmt.Printf("select0 chunk %d %s\n", currentChunkIndex, c.chunkViews[currentChunkIndex].FileId)
  261. currentChunkIndex = 0
  262. } else if insideChunk(offset, c.chunkViews[len(c.chunkViews)-1]) {
  263. currentChunkIndex = len(c.chunkViews) - 1
  264. // fmt.Printf("select last chunk %d %s\n", currentChunkIndex, c.chunkViews[currentChunkIndex].FileId)
  265. } else {
  266. return io.EOF
  267. }
  268. } else if currentChunkIndex > 0 {
  269. if insideChunk(offset, c.chunkViews[currentChunkIndex]) {
  270. // good hit
  271. } else if insideChunk(offset, c.chunkViews[currentChunkIndex-1]) {
  272. currentChunkIndex -= 1
  273. // fmt.Printf("select -1 chunk %d %s\n", currentChunkIndex, c.chunkViews[currentChunkIndex].FileId)
  274. } else {
  275. // glog.Fatalf("unexpected1 offset %d", offset)
  276. return fmt.Errorf("unexpected1 offset %d", offset)
  277. }
  278. } else {
  279. // glog.Fatalf("unexpected2 offset %d", offset)
  280. return fmt.Errorf("unexpected2 offset %d", offset)
  281. }
  282. // positioning within the new chunk
  283. chunk := c.chunkViews[currentChunkIndex]
  284. if insideChunk(offset, chunk) {
  285. if c.isBufferEmpty() || c.bufferOffset != chunk.LogicOffset {
  286. if err = c.fetchChunkToBuffer(chunk); err != nil {
  287. return
  288. }
  289. }
  290. } else {
  291. // glog.Fatalf("unexpected3 offset %d in %s [%d,%d)", offset, chunk.FileId, chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  292. return fmt.Errorf("unexpected3 offset %d in %s [%d,%d)", offset, chunk.FileId, chunk.LogicOffset, chunk.LogicOffset+int64(chunk.Size))
  293. }
  294. return
  295. }
  296. func (c *ChunkStreamReader) fetchChunkToBuffer(chunkView *ChunkView) error {
  297. urlStrings, err := c.lookupFileId(chunkView.FileId)
  298. if err != nil {
  299. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  300. return err
  301. }
  302. var buffer bytes.Buffer
  303. var shouldRetry bool
  304. for _, urlString := range urlStrings {
  305. shouldRetry, err = util.ReadUrlAsStream(urlString+"?readDeleted=true", chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size), func(data []byte) {
  306. buffer.Write(data)
  307. })
  308. if !shouldRetry {
  309. break
  310. }
  311. if err != nil {
  312. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  313. buffer.Reset()
  314. } else {
  315. break
  316. }
  317. }
  318. if err != nil {
  319. return err
  320. }
  321. c.buffer = buffer.Bytes()
  322. c.bufferOffset = chunkView.LogicOffset
  323. c.chunk = chunkView.FileId
  324. // glog.V(0).Infof("fetched %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size))
  325. return nil
  326. }
  327. func (c *ChunkStreamReader) Close() {
  328. // TODO try to release and reuse buffer
  329. }
  330. func VolumeId(fileId string) string {
  331. lastCommaIndex := strings.LastIndex(fileId, ",")
  332. if lastCommaIndex > 0 {
  333. return fileId[:lastCommaIndex]
  334. }
  335. return fileId
  336. }