diff options
| author | Chris Lu <chrislusf@users.noreply.github.com> | 2022-01-13 09:46:23 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-13 09:46:23 -0800 |
| commit | 4cd446717997d44122656cd34cc56ec4c1db595f (patch) | |
| tree | 484a2eb7f531f961fca7be5e80a64ecdc2d1e47b | |
| parent | fe5b9e39cc5a3526a0fdadf8b518b67b02d5451b (diff) | |
| parent | 45e9c83421c279b901b532f41a5eccbffeb1bd1d (diff) | |
| download | seaweedfs-4cd446717997d44122656cd34cc56ec4c1db595f.tar.xz seaweedfs-4cd446717997d44122656cd34cc56ec4c1db595f.zip | |
Merge pull request #2590 from banjiaojuhao/padding_zero_for_web_read
| -rw-r--r-- | weed/filer/reader_at.go | 2 | ||||
| -rw-r--r-- | weed/filer/stream.go | 36 |
2 files changed, 36 insertions, 2 deletions
diff --git a/weed/filer/reader_at.go b/weed/filer/reader_at.go index 670de382c..b1c15152f 100644 --- a/weed/filer/reader_at.go +++ b/weed/filer/reader_at.go @@ -129,7 +129,7 @@ func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, err error) { } if startOffset < chunk.LogicOffset { gap := int(chunk.LogicOffset - startOffset) - glog.V(4).Infof("zero [%d,%d)", startOffset, startOffset+int64(gap)) + glog.V(4).Infof("zero [%d,%d)", startOffset, chunk.LogicOffset) n += int(min(int64(gap), remaining)) startOffset, remaining = chunk.LogicOffset, remaining-int64(gap) if remaining <= 0 { diff --git a/weed/filer/stream.go b/weed/filer/stream.go index 8baf7aeaa..e5163f2d9 100644 --- a/weed/filer/stream.go +++ b/weed/filer/stream.go @@ -80,11 +80,23 @@ func StreamContent(masterClient wdclient.HasLookupFileIdFunction, writer io.Writ fileId2Url[chunkView.FileId] = urlStrings } + remaining := size for _, chunkView := range chunkViews { - + if offset < chunkView.LogicOffset { + gap := chunkView.LogicOffset - offset + remaining -= gap + glog.V(4).Infof("zero [%d,%d)", offset, chunkView.LogicOffset) + err := writeZero(writer, gap) + if err != nil { + return fmt.Errorf("write zero [%d,%d)", offset, chunkView.LogicOffset) + } + offset = chunkView.LogicOffset + } urlStrings := fileId2Url[chunkView.FileId] start := time.Now() err := retriedStreamFetchChunkData(writer, urlStrings, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size)) + offset += int64(chunkView.Size) + remaining -= int64(chunkView.Size) stats.FilerRequestHistogram.WithLabelValues("chunkDownload").Observe(time.Since(start).Seconds()) if err != nil { stats.FilerRequestCounter.WithLabelValues("chunkDownloadError").Inc() @@ -92,6 +104,11 @@ func StreamContent(masterClient wdclient.HasLookupFileIdFunction, writer io.Writ } stats.FilerRequestCounter.WithLabelValues("chunkDownload").Inc() } + 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) + } return nil @@ -99,6 +116,23 @@ func StreamContent(masterClient wdclient.HasLookupFileIdFunction, writer io.Writ // ---------------- ReadAllReader ---------------------------------- +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 +} + func ReadAll(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) ([]byte, error) { buffer := bytes.Buffer{} |
