2020-09-01 15:21:19 +08:00
|
|
|
package filer
|
2019-06-05 16:30:24 +08:00
|
|
|
|
|
|
|
import (
|
2020-03-21 11:31:11 +08:00
|
|
|
"bytes"
|
2020-10-14 04:53:34 +08:00
|
|
|
"fmt"
|
2019-06-05 16:30:24 +08:00
|
|
|
"io"
|
2020-03-21 11:31:11 +08:00
|
|
|
"math"
|
2020-03-22 16:00:36 +08:00
|
|
|
"strings"
|
2021-08-26 08:28:50 +08:00
|
|
|
"sync"
|
2021-05-24 15:14:50 +08:00
|
|
|
"time"
|
2019-06-05 16:30:24 +08:00
|
|
|
|
2024-01-30 02:35:52 +08:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
2019-06-05 16:30:24 +08:00
|
|
|
)
|
|
|
|
|
2022-07-15 19:05:35 +08:00
|
|
|
var getLookupFileIdBackoffSchedule = []time.Duration{
|
|
|
|
150 * time.Millisecond,
|
|
|
|
600 * time.Millisecond,
|
|
|
|
1800 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
2021-08-16 10:46:45 +08:00
|
|
|
func HasData(entry *filer_pb.Entry) bool {
|
|
|
|
|
|
|
|
if len(entry.Content) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-11-15 22:33:36 +08:00
|
|
|
return len(entry.GetChunks()) > 0
|
2021-08-16 10:46:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsSameData(a, b *filer_pb.Entry) bool {
|
|
|
|
|
|
|
|
if len(a.Content) > 0 || len(b.Content) > 0 {
|
|
|
|
return bytes.Equal(a.Content, b.Content)
|
|
|
|
}
|
|
|
|
|
|
|
|
return isSameChunks(a.Chunks, b.Chunks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSameChunks(a, b []*filer_pb.FileChunk) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
2023-09-26 00:35:16 +08:00
|
|
|
slices.SortFunc(a, func(i, j *filer_pb.FileChunk) int {
|
|
|
|
return strings.Compare(i.ETag, j.ETag)
|
2021-08-16 11:23:41 +08:00
|
|
|
})
|
2023-09-26 00:35:16 +08:00
|
|
|
slices.SortFunc(b, func(i, j *filer_pb.FileChunk) int {
|
|
|
|
return strings.Compare(i.ETag, j.ETag)
|
2021-08-16 11:23:41 +08:00
|
|
|
})
|
2021-08-16 10:46:45 +08:00
|
|
|
for i := 0; i < len(a); i++ {
|
2021-08-16 11:23:41 +08:00
|
|
|
if a[i].ETag != b[i].ETag {
|
2021-08-16 10:46:45 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileReader(filerClient filer_pb.FilerClient, entry *filer_pb.Entry) io.Reader {
|
|
|
|
if len(entry.Content) > 0 {
|
|
|
|
return bytes.NewReader(entry.Content)
|
|
|
|
}
|
2022-11-15 22:33:36 +08:00
|
|
|
return NewChunkStreamReader(filerClient, entry.GetChunks())
|
2021-08-16 10:46:45 +08:00
|
|
|
}
|
|
|
|
|
2024-01-30 02:35:52 +08:00
|
|
|
type DoStreamContent func(writer io.Writer) error
|
2022-08-05 16:16:42 +08:00
|
|
|
|
2024-04-08 22:27:00 +08:00
|
|
|
func PrepareStreamContent(masterClient wdclient.HasLookupFileIdFunction, jwtFunc VolumeServerJwtFunction, chunks []*filer_pb.FileChunk, offset int64, size int64) (DoStreamContent, error) {
|
|
|
|
return PrepareStreamContentWithThrottler(masterClient, jwtFunc, chunks, offset, size, 0)
|
2024-01-30 02:35:52 +08:00
|
|
|
}
|
2019-06-05 16:30:24 +08:00
|
|
|
|
2024-04-08 22:27:00 +08:00
|
|
|
type VolumeServerJwtFunction func(fileId string) string
|
|
|
|
|
|
|
|
func noJwtFunc(string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrepareStreamContentWithThrottler(masterClient wdclient.HasLookupFileIdFunction, jwtFunc VolumeServerJwtFunction, chunks []*filer_pb.FileChunk, offset int64, size int64, downloadMaxBytesPs int64) (DoStreamContent, error) {
|
2024-01-30 02:35:52 +08:00
|
|
|
glog.V(4).Infof("prepare to stream content for chunks: %d", len(chunks))
|
2021-01-06 20:21:34 +08:00
|
|
|
chunkViews := ViewFromChunks(masterClient.GetLookupFileIdFunction(), chunks, offset, size)
|
2019-06-05 16:30:24 +08:00
|
|
|
|
2020-10-08 13:49:04 +08:00
|
|
|
fileId2Url := make(map[string][]string)
|
2019-06-05 16:30:24 +08:00
|
|
|
|
2023-01-03 15:20:45 +08:00
|
|
|
for x := chunkViews.Front(); x != nil; x = x.Next {
|
|
|
|
chunkView := x.Value
|
2022-07-15 19:05:35 +08:00
|
|
|
var urlStrings []string
|
|
|
|
var err error
|
|
|
|
for _, backoff := range getLookupFileIdBackoffSchedule {
|
|
|
|
urlStrings, err = masterClient.GetLookupFileIdFunction()(chunkView.FileId)
|
2022-07-26 14:56:45 +08:00
|
|
|
if err == nil && len(urlStrings) > 0 {
|
2022-07-15 19:05:35 +08:00
|
|
|
break
|
|
|
|
}
|
2022-08-16 01:03:52 +08:00
|
|
|
glog.V(4).Infof("waiting for chunk: %s", chunkView.FileId)
|
2022-07-26 14:56:45 +08:00
|
|
|
time.Sleep(backoff)
|
2022-07-15 19:05:35 +08:00
|
|
|
}
|
2019-06-05 16:30:24 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
|
2024-01-30 02:35:52 +08:00
|
|
|
return nil, err
|
2021-03-12 02:34:36 +08:00
|
|
|
} else if len(urlStrings) == 0 {
|
2022-07-15 18:29:15 +08:00
|
|
|
errUrlNotFound := fmt.Errorf("operation LookupFileId %s failed, err: urls not found", chunkView.FileId)
|
|
|
|
glog.Error(errUrlNotFound)
|
2024-01-30 02:35:52 +08:00
|
|
|
return nil, errUrlNotFound
|
2019-06-05 16:30:24 +08:00
|
|
|
}
|
2020-10-08 13:49:04 +08:00
|
|
|
fileId2Url[chunkView.FileId] = urlStrings
|
2019-06-05 16:30:24 +08:00
|
|
|
}
|
|
|
|
|
2024-01-30 02:35:52 +08:00
|
|
|
return func(writer io.Writer) error {
|
|
|
|
downloadThrottler := util.NewWriteThrottler(downloadMaxBytesPs)
|
|
|
|
remaining := size
|
|
|
|
for x := chunkViews.Front(); x != nil; x = x.Next {
|
|
|
|
chunkView := x.Value
|
|
|
|
if offset < chunkView.ViewOffset {
|
|
|
|
gap := chunkView.ViewOffset - offset
|
|
|
|
remaining -= gap
|
|
|
|
glog.V(4).Infof("zero [%d,%d)", offset, chunkView.ViewOffset)
|
|
|
|
err := writeZero(writer, gap)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("write zero [%d,%d)", offset, chunkView.ViewOffset)
|
|
|
|
}
|
|
|
|
offset = chunkView.ViewOffset
|
|
|
|
}
|
|
|
|
urlStrings := fileId2Url[chunkView.FileId]
|
|
|
|
start := time.Now()
|
2024-04-08 22:27:00 +08:00
|
|
|
jwt := jwtFunc(chunkView.FileId)
|
|
|
|
err := retriedStreamFetchChunkData(writer, urlStrings, jwt, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.OffsetInChunk, int(chunkView.ViewSize))
|
2024-01-30 02:35:52 +08:00
|
|
|
offset += int64(chunkView.ViewSize)
|
|
|
|
remaining -= int64(chunkView.ViewSize)
|
|
|
|
stats.FilerRequestHistogram.WithLabelValues("chunkDownload").Observe(time.Since(start).Seconds())
|
2022-01-13 22:20:33 +08:00
|
|
|
if err != nil {
|
2024-01-30 02:35:52 +08:00
|
|
|
stats.FilerHandlerCounter.WithLabelValues("chunkDownloadError").Inc()
|
|
|
|
return fmt.Errorf("read chunk: %v", err)
|
2022-01-13 22:20:33 +08:00
|
|
|
}
|
2024-01-30 02:35:52 +08:00
|
|
|
stats.FilerHandlerCounter.WithLabelValues("chunkDownload").Inc()
|
|
|
|
downloadThrottler.MaybeSlowdown(int64(chunkView.ViewSize))
|
2022-01-13 22:20:33 +08:00
|
|
|
}
|
2024-01-30 02:35:52 +08:00
|
|
|
if remaining > 0 {
|
|
|
|
glog.V(4).Infof("zero [%d,%d)", offset, offset+remaining)
|
|
|
|
err := writeZero(writer, remaining)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("write zero [%d,%d)", offset, offset+remaining)
|
|
|
|
}
|
2022-03-14 18:19:16 +08:00
|
|
|
}
|
2019-06-05 16:30:24 +08:00
|
|
|
|
2024-01-30 02:35:52 +08:00
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}
|
2019-06-05 16:30:24 +08:00
|
|
|
|
2024-01-30 02:35:52 +08:00
|
|
|
func StreamContent(masterClient wdclient.HasLookupFileIdFunction, writer io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int64) error {
|
2024-04-08 22:27:00 +08:00
|
|
|
streamFn, err := PrepareStreamContent(masterClient, noJwtFunc, chunks, offset, size)
|
2024-01-30 02:35:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return streamFn(writer)
|
2019-06-05 16:30:24 +08:00
|
|
|
}
|
2020-03-21 11:31:11 +08:00
|
|
|
|
2020-04-28 15:05:47 +08:00
|
|
|
// ---------------- ReadAllReader ----------------------------------
|
|
|
|
|
2022-01-13 22:20:33 +08:00
|
|
|
func writeZero(w io.Writer, size int64) (err error) {
|
|
|
|
zeroPadding := make([]byte, 1024)
|
|
|
|
var written int
|
|
|
|
for size > 0 {
|
|
|
|
if size > 1024 {
|
|
|
|
written, err = w.Write(zeroPadding)
|
|
|
|
} else {
|
|
|
|
written, err = w.Write(zeroPadding[:size])
|
|
|
|
}
|
|
|
|
size -= int64(written)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-26 18:16:47 +08:00
|
|
|
func ReadAll(buffer []byte, masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) error {
|
2020-04-28 15:05:47 +08:00
|
|
|
|
2020-10-08 13:49:04 +08:00
|
|
|
lookupFileIdFn := func(fileId string) (targetUrls []string, err error) {
|
2020-04-28 15:05:47 +08:00
|
|
|
return masterClient.LookupFileId(fileId)
|
|
|
|
}
|
|
|
|
|
2022-02-26 18:16:47 +08:00
|
|
|
chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, int64(len(buffer)))
|
|
|
|
|
|
|
|
idx := 0
|
2020-07-20 08:59:43 +08:00
|
|
|
|
2023-01-03 15:20:45 +08:00
|
|
|
for x := chunkViews.Front(); x != nil; x = x.Next {
|
|
|
|
chunkView := x.Value
|
2020-10-08 13:49:04 +08:00
|
|
|
urlStrings, err := lookupFileIdFn(chunkView.FileId)
|
2020-04-28 15:05:47 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
|
2022-02-26 18:16:47 +08:00
|
|
|
return err
|
2020-04-28 15:05:47 +08:00
|
|
|
}
|
2020-10-09 14:19:42 +08:00
|
|
|
|
2024-01-14 09:51:53 +08:00
|
|
|
n, err := util.RetriedFetchChunkData(buffer[idx:idx+int(chunkView.ViewSize)], urlStrings, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.OffsetInChunk)
|
2020-10-09 14:19:42 +08:00
|
|
|
if err != nil {
|
2022-02-26 18:16:47 +08:00
|
|
|
return err
|
2020-04-28 15:05:47 +08:00
|
|
|
}
|
2022-02-26 18:16:47 +08:00
|
|
|
idx += n
|
2020-04-28 15:05:47 +08:00
|
|
|
}
|
2022-02-26 18:16:47 +08:00
|
|
|
return nil
|
2020-04-28 15:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------- ChunkStreamReader ----------------------------------
|
2020-03-21 11:31:11 +08:00
|
|
|
type ChunkStreamReader struct {
|
2023-01-06 03:19:21 +08:00
|
|
|
head *Interval[*ChunkView]
|
2023-01-03 15:20:45 +08:00
|
|
|
chunkView *Interval[*ChunkView]
|
2021-09-01 17:45:42 +08:00
|
|
|
totalSize int64
|
|
|
|
logicOffset int64
|
|
|
|
buffer []byte
|
|
|
|
bufferOffset int64
|
|
|
|
bufferLock sync.Mutex
|
|
|
|
chunk string
|
|
|
|
lookupFileId wdclient.LookupFileIdFunctionType
|
2020-03-21 11:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ = io.ReadSeeker(&ChunkStreamReader{})
|
2021-08-08 06:41:07 +08:00
|
|
|
var _ = io.ReaderAt(&ChunkStreamReader{})
|
2020-03-21 11:31:11 +08:00
|
|
|
|
2021-08-08 06:35:27 +08:00
|
|
|
func doNewChunkStreamReader(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
|
2020-07-20 08:59:43 +08:00
|
|
|
|
|
|
|
chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
|
2020-03-21 11:31:11 +08:00
|
|
|
|
2021-08-08 05:46:23 +08:00
|
|
|
var totalSize int64
|
2023-01-03 15:20:45 +08:00
|
|
|
for x := chunkViews.Front(); x != nil; x = x.Next {
|
|
|
|
chunk := x.Value
|
|
|
|
totalSize += int64(chunk.ViewSize)
|
2021-08-08 05:46:23 +08:00
|
|
|
}
|
|
|
|
|
2020-03-21 11:31:11 +08:00
|
|
|
return &ChunkStreamReader{
|
2023-01-06 03:19:21 +08:00
|
|
|
head: chunkViews.Front(),
|
2023-01-03 15:20:45 +08:00
|
|
|
chunkView: chunkViews.Front(),
|
2020-07-20 08:59:43 +08:00
|
|
|
lookupFileId: lookupFileIdFn,
|
2021-08-08 05:46:23 +08:00
|
|
|
totalSize: totalSize,
|
2020-03-22 16:00:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 06:35:27 +08:00
|
|
|
func NewChunkStreamReaderFromFiler(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
|
2020-04-30 08:40:08 +08:00
|
|
|
|
2021-08-08 06:35:27 +08:00
|
|
|
lookupFileIdFn := func(fileId string) (targetUrl []string, err error) {
|
|
|
|
return masterClient.LookupFileId(fileId)
|
|
|
|
}
|
2020-07-20 08:59:43 +08:00
|
|
|
|
2021-08-08 06:35:27 +08:00
|
|
|
return doNewChunkStreamReader(lookupFileIdFn, chunks)
|
|
|
|
}
|
2020-04-30 08:40:08 +08:00
|
|
|
|
2021-08-08 06:35:27 +08:00
|
|
|
func NewChunkStreamReader(filerClient filer_pb.FilerClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
|
2021-08-08 05:46:23 +08:00
|
|
|
|
2021-08-08 06:35:27 +08:00
|
|
|
lookupFileIdFn := LookupFn(filerClient)
|
|
|
|
|
|
|
|
return doNewChunkStreamReader(lookupFileIdFn, chunks)
|
2020-04-30 08:40:08 +08:00
|
|
|
}
|
|
|
|
|
2021-08-08 06:41:07 +08:00
|
|
|
func (c *ChunkStreamReader) ReadAt(p []byte, off int64) (n int, err error) {
|
2021-08-26 08:28:50 +08:00
|
|
|
c.bufferLock.Lock()
|
|
|
|
defer c.bufferLock.Unlock()
|
2021-08-16 02:55:58 +08:00
|
|
|
if err = c.prepareBufferFor(off); err != nil {
|
2021-08-08 06:41:07 +08:00
|
|
|
return
|
|
|
|
}
|
2021-08-16 02:55:58 +08:00
|
|
|
c.logicOffset = off
|
2021-08-26 08:28:50 +08:00
|
|
|
return c.doRead(p)
|
2021-08-08 06:41:07 +08:00
|
|
|
}
|
|
|
|
|
2020-03-21 11:31:11 +08:00
|
|
|
func (c *ChunkStreamReader) Read(p []byte) (n int, err error) {
|
2021-08-26 08:28:50 +08:00
|
|
|
c.bufferLock.Lock()
|
|
|
|
defer c.bufferLock.Unlock()
|
|
|
|
return c.doRead(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChunkStreamReader) doRead(p []byte) (n int, err error) {
|
|
|
|
// 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)))
|
2020-04-29 17:42:58 +08:00
|
|
|
for n < len(p) {
|
2021-08-26 08:28:50 +08:00
|
|
|
// println("read", c.logicOffset)
|
|
|
|
if err = c.prepareBufferFor(c.logicOffset); err != nil {
|
|
|
|
return
|
2020-03-21 11:31:11 +08:00
|
|
|
}
|
2021-08-26 08:28:50 +08:00
|
|
|
t := copy(p[n:], c.buffer[c.logicOffset-c.bufferOffset:])
|
2020-04-29 17:42:58 +08:00
|
|
|
n += t
|
2021-08-08 16:21:42 +08:00
|
|
|
c.logicOffset += int64(t)
|
2020-03-21 11:31:11 +08:00
|
|
|
}
|
2020-03-22 13:16:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChunkStreamReader) isBufferEmpty() bool {
|
2021-09-01 17:45:42 +08:00
|
|
|
return len(c.buffer) <= int(c.logicOffset-c.bufferOffset)
|
2020-03-21 11:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) {
|
2021-08-26 08:28:50 +08:00
|
|
|
c.bufferLock.Lock()
|
|
|
|
defer c.bufferLock.Unlock()
|
2020-03-22 13:16:00 +08:00
|
|
|
|
|
|
|
var err error
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
case io.SeekCurrent:
|
2021-08-08 16:21:42 +08:00
|
|
|
offset += c.logicOffset
|
2020-03-22 13:16:00 +08:00
|
|
|
case io.SeekEnd:
|
2021-08-08 05:46:23 +08:00
|
|
|
offset = c.totalSize + offset
|
2020-03-22 13:16:00 +08:00
|
|
|
}
|
2021-08-08 05:46:23 +08:00
|
|
|
if offset > c.totalSize {
|
2020-03-22 13:16:00 +08:00
|
|
|
err = io.ErrUnexpectedEOF
|
2021-08-08 16:21:42 +08:00
|
|
|
} else {
|
|
|
|
c.logicOffset = offset
|
2020-03-22 13:16:00 +08:00
|
|
|
}
|
|
|
|
|
2021-08-08 16:21:42 +08:00
|
|
|
return offset, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-26 08:28:50 +08:00
|
|
|
func insideChunk(offset int64, chunk *ChunkView) bool {
|
2023-01-03 15:20:45 +08:00
|
|
|
return chunk.ViewOffset <= offset && offset < chunk.ViewOffset+int64(chunk.ViewSize)
|
2021-08-26 08:28:50 +08:00
|
|
|
}
|
|
|
|
|
2021-08-08 16:21:42 +08:00
|
|
|
func (c *ChunkStreamReader) prepareBufferFor(offset int64) (err error) {
|
2021-08-08 06:35:27 +08:00
|
|
|
// stay in the same chunk
|
2021-08-26 08:28:50 +08:00
|
|
|
if c.bufferOffset <= offset && offset < c.bufferOffset+int64(len(c.buffer)) {
|
|
|
|
return nil
|
2021-08-08 06:35:27 +08:00
|
|
|
}
|
2023-02-17 17:31:44 +08:00
|
|
|
// glog.V(2).Infof("c.chunkView: %v buffer:[%d,%d) offset:%d totalSize:%d", c.chunkView, c.bufferOffset, c.bufferOffset+int64(len(c.buffer)), offset, c.totalSize)
|
2023-02-17 17:30:55 +08:00
|
|
|
|
|
|
|
// find a possible chunk view
|
|
|
|
p := c.chunkView
|
|
|
|
for p != nil {
|
|
|
|
chunk := p.Value
|
2023-02-17 17:31:44 +08:00
|
|
|
// glog.V(2).Infof("prepareBufferFor check chunk:[%d,%d)", chunk.ViewOffset, chunk.ViewOffset+int64(chunk.ViewSize))
|
2023-02-17 17:30:55 +08:00
|
|
|
if insideChunk(offset, chunk) {
|
|
|
|
if c.isBufferEmpty() || c.bufferOffset != chunk.ViewOffset {
|
|
|
|
c.chunkView = p
|
|
|
|
return c.fetchChunkToBuffer(chunk)
|
2023-01-06 03:19:21 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-17 17:30:55 +08:00
|
|
|
if offset < c.bufferOffset {
|
|
|
|
p = p.Prev
|
|
|
|
} else {
|
|
|
|
p = p.Next
|
|
|
|
}
|
2020-03-22 13:16:00 +08:00
|
|
|
}
|
2023-01-06 03:19:21 +08:00
|
|
|
|
|
|
|
return io.EOF
|
2020-03-21 11:31:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChunkStreamReader) fetchChunkToBuffer(chunkView *ChunkView) error {
|
2020-10-08 13:49:04 +08:00
|
|
|
urlStrings, err := c.lookupFileId(chunkView.FileId)
|
2020-03-21 11:31:11 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
|
|
|
|
return err
|
|
|
|
}
|
2020-03-22 13:16:00 +08:00
|
|
|
var buffer bytes.Buffer
|
2020-10-13 15:29:46 +08:00
|
|
|
var shouldRetry bool
|
2020-10-08 13:49:04 +08:00
|
|
|
for _, urlString := range urlStrings {
|
2023-01-03 15:20:45 +08:00
|
|
|
shouldRetry, err = util.ReadUrlAsStream(urlString+"?readDeleted=true", chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.OffsetInChunk, int(chunkView.ViewSize), func(data []byte) {
|
2020-10-08 13:49:04 +08:00
|
|
|
buffer.Write(data)
|
|
|
|
})
|
2020-10-13 15:29:46 +08:00
|
|
|
if !shouldRetry {
|
|
|
|
break
|
|
|
|
}
|
2020-10-08 13:49:04 +08:00
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
|
|
|
|
buffer.Reset()
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-03-21 11:31:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-22 13:16:00 +08:00
|
|
|
c.buffer = buffer.Bytes()
|
2023-01-03 15:20:45 +08:00
|
|
|
c.bufferOffset = chunkView.ViewOffset
|
2021-08-26 08:28:50 +08:00
|
|
|
c.chunk = chunkView.FileId
|
2020-03-22 13:16:00 +08:00
|
|
|
|
2023-01-03 15:20:45 +08:00
|
|
|
// glog.V(0).Infof("fetched %s [%d,%d)", chunkView.FileId, chunkView.ViewOffset, chunkView.ViewOffset+int64(chunkView.ViewSize))
|
2020-03-22 16:00:36 +08:00
|
|
|
|
2020-03-21 11:31:11 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-22 16:00:36 +08:00
|
|
|
|
2020-04-28 15:05:47 +08:00
|
|
|
func (c *ChunkStreamReader) Close() {
|
|
|
|
// TODO try to release and reuse buffer
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
func VolumeId(fileId string) string {
|
|
|
|
lastCommaIndex := strings.LastIndex(fileId, ",")
|
|
|
|
if lastCommaIndex > 0 {
|
|
|
|
return fileId[:lastCommaIndex]
|
2020-03-22 16:00:36 +08:00
|
|
|
}
|
2020-03-23 15:01:34 +08:00
|
|
|
return fileId
|
2020-03-22 16:00:36 +08:00
|
|
|
}
|