Skip to content

Commit

Permalink
Merge pull request #1439 from 0chain/hotfix/download-buffer
Browse files Browse the repository at this point in the history
fix release chunk
  • Loading branch information
dabasov authored Mar 27, 2024
2 parents ff968b9 + 44cac9d commit 0cfe13d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion zboxcore/sdk/blockdownloadworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func AddBlockDownloadReq(ctx context.Context, req *BlockDownloadRequest, rb zbox
if rb != nil {
reqCtx, cncl := context.WithTimeout(ctx, (time.Second * 10))
defer cncl()
req.respBuf = rb.RequestChunk(reqCtx, int(req.blockNum/req.numBlocks))
req.respBuf = rb.RequestChunk(reqCtx, int(req.blockNum))
if len(req.respBuf) == 0 {
req.respBuf = make([]byte, int(req.numBlocks)*effectiveBlockSize)
}
Expand Down
6 changes: 4 additions & 2 deletions zboxcore/sdk/download_progress_storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type DownloadProgressStorer interface {
// Load load download progress by id
Load(id string) *DownloadProgress
Load(id string, numBlocks int) *DownloadProgress
// Update download progress
Update(writtenBlock int)
// Remove remove download progress by id
Expand Down Expand Up @@ -72,7 +72,7 @@ func (ds *FsDownloadProgressStorer) Start(ctx context.Context) {
}()
}

func (ds *FsDownloadProgressStorer) Load(progressID string) *DownloadProgress {
func (ds *FsDownloadProgressStorer) Load(progressID string, numBlocks int) *DownloadProgress {
dp := &DownloadProgress{}
buf, err := sys.Files.ReadFile(progressID)
if err != nil {
Expand All @@ -82,6 +82,8 @@ func (ds *FsDownloadProgressStorer) Load(progressID string) *DownloadProgress {
return nil
}
ds.dp = dp
dp.numBlocks = numBlocks
ds.next = dp.LastWrittenBlock
return ds.dp
}

Expand Down
14 changes: 7 additions & 7 deletions zboxcore/sdk/downloadworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (req *DownloadRequest) downloadBlock(
err.Error(), req.blobbers[result.idx].Baseurl)
logger.Logger.Error(err)
if req.bufferMap != nil {
req.bufferMap[result.idx].ReleaseChunk(int(req.startBlock / req.numBlocks))
req.bufferMap[result.idx].ReleaseChunk(int(req.startBlock))
}
} else if timeRequest {
req.downloadQueue[result.idx].timeTaken = result.timeTaken
Expand Down Expand Up @@ -603,7 +603,7 @@ func (req *DownloadRequest) processDownload() {
hashWg.Wait()
}
for _, rb := range req.bufferMap {
rb.ReleaseChunk(i)
rb.ReleaseChunk(int(startBlock + int64(i)*numBlocks))
}
downloaded = downloaded + totalWritten
remainingSize -= int64(totalWritten)
Expand Down Expand Up @@ -651,7 +651,7 @@ func (req *DownloadRequest) processDownload() {
hashWg.Wait()
}
for _, rb := range req.bufferMap {
rb.ReleaseChunk(i)
rb.ReleaseChunk(int(startBlock + int64(i)*numBlocks))
}

downloaded = downloaded + totalWritten
Expand Down Expand Up @@ -729,7 +729,7 @@ func (req *DownloadRequest) processDownload() {
return errors.Wrap(err, fmt.Sprintf("WriteAt failed for block %d. ", startBlock+int64(j)*numBlocks))
}
for _, rb := range req.bufferMap {
rb.ReleaseChunk(j)
rb.ReleaseChunk(int(startBlock + int64(j)*numBlocks))
}
if req.downloadStorer != nil {
go req.downloadStorer.Update(int(startBlock + int64(j)*numBlocks + blocksToDownload))
Expand Down Expand Up @@ -1035,17 +1035,17 @@ func (req *DownloadRequest) calculateShardsParams(
progressID := req.progressID()
var dp *DownloadProgress
if info.Size() > 0 {
dp = req.downloadStorer.Load(progressID)
dp = req.downloadStorer.Load(progressID, int(req.numBlocks))
}
if dp != nil {
req.startBlock = int64(dp.LastWrittenBlock)
} else {
dp = &DownloadProgress{
ID: progressID,
ID: progressID,
numBlocks: int(req.numBlocks),
}
req.downloadStorer.Save(dp)
}
dp.numBlocks = int(req.numBlocks)
}
}

Expand Down

0 comments on commit 0cfe13d

Please sign in to comment.