Skip to content

Commit

Permalink
BugFix: snapshot download reporting fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliusan committed Sep 22, 2023
1 parent fb573b4 commit 1351abf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func NewProgressReporter(log *logger.Logger, header string, expected uint64) io.
}

func (pr *progressReporter) Write(p []byte) (int, error) {
pr.total += uint64(len(p))
now := time.Now()
timeDiff := now.Sub(pr.lastReport)
if timeDiff >= logStatusPeriodConst {
Expand Down
20 changes: 12 additions & 8 deletions packages/chain/statemanager/sm_snapshots/snapshot_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (smiT *snapshotManagerImpl) loadSnapshot() SnapshotInfo {
for i := range snapshotPaths {
err := smiT.loadSnapshotFromPath(snapshotInfos[i], snapshotPaths[i])
if err == nil {
smiT.log.Debugf("Snapshot %s successfully loaded from %s", snapshotInfos[i], snapshotPaths[i])
smiT.log.Infof("Snapshot %s successfully loaded from %s", snapshotInfos[i], snapshotPaths[i])
return snapshotInfos[i]
}
smiT.log.Errorf("Failed to load snapshot %s from %s: %v", snapshotInfos[i], snapshotPaths[i], err)
Expand Down Expand Up @@ -272,7 +272,7 @@ func (smiT *snapshotManagerImpl) searchNetworkSnapshots(baseNetworkPaths []strin
smiT.log.Errorf("Search network snapshots: unable to parse url %s: %v", baseNetworkPathWithChainID, err)
return
}
reader, err := smiT.getReadCloser(scheme, basePath, constIndexFileName)
reader, err := smiT.getReadCloser(scheme, "index file", basePath, constIndexFileName)
if err != nil {
smiT.log.Errorf("Search network snapshots: failed to open index file: %v", err)
return
Expand All @@ -283,7 +283,7 @@ func (smiT *snapshotManagerImpl) searchNetworkSnapshots(baseNetworkPaths []strin
for scanner.Scan() {
func() {
snapshotFileName := scanner.Text()
sReader, er := smiT.getReadCloser(scheme, basePath, snapshotFileName)
sReader, er := smiT.getReadCloser(scheme, "snapshot header", basePath, snapshotFileName)
if er != nil {
smiT.log.Errorf("Search network snapshots: failed to open snapshot file: %v", er)
return
Expand Down Expand Up @@ -331,10 +331,14 @@ func (smiT *snapshotManagerImpl) loadSnapshotFromPath(snapshotInfo SnapshotInfo,
loadNetworkFun := func(url string) error {
fileNameLocal := downloadedSnapshotFileName(snapshotInfo.StateIndex(), snapshotInfo.BlockHash())
filePathLocal := filepath.Join(smiT.localPath, fileNameLocal)
err := DownloadToFile(smiT.ctx, url, filePathLocal, constDownloadTimeout, smiT.addProgressReporter)
addProgressReporterFun := func(r io.Reader, f string, s uint64) io.Reader {
return smiT.addProgressReporter(r, fmt.Sprintf("snapshot %s", snapshotInfo), f, s)
}
err := DownloadToFile(smiT.ctx, url, filePathLocal, constDownloadTimeout, addProgressReporterFun)
if err != nil {
return err
}
smiT.log.Debugf("Loading snapshot %s from url %s: snapshot successfully downloaded to %s", snapshotInfo, url, filePathLocal)
return loadLocalFun(filePathLocal)
}

Expand Down Expand Up @@ -369,7 +373,7 @@ func (smiT *snapshotManagerImpl) splitURL(uString string) (scheme string, path s
}
}

func (smiT *snapshotManagerImpl) getReadCloser(scheme string, basePath string, file string) (io.ReadCloser, error) {
func (smiT *snapshotManagerImpl) getReadCloser(scheme string, fileType string, basePath string, file string) (io.ReadCloser, error) {
switch scheme {
case constSchemeHTTP:
fullPath, err := url.JoinPath(basePath, file)
Expand All @@ -380,7 +384,7 @@ func (smiT *snapshotManagerImpl) getReadCloser(scheme string, basePath string, f
if err != nil {
return nil, fmt.Errorf("failed to start downloading file from url %s: %v", fullPath, err)
}
r := smiT.addProgressReporter(downloader, fullPath, downloader.GetLength())
r := smiT.addProgressReporter(downloader, fileType, fullPath, downloader.GetLength())
return NewReaderWithClose(r, downloader.Close), nil
case constSchemeFile:
fullPath := filepath.Join(basePath, file)
Expand All @@ -394,8 +398,8 @@ func (smiT *snapshotManagerImpl) getReadCloser(scheme string, basePath string, f
}
}

func (smiT *snapshotManagerImpl) addProgressReporter(r io.Reader, url string, length uint64) io.Reader {
progressReporter := NewProgressReporter(smiT.log, fmt.Sprintf("downloading file %s", url), length)
func (smiT *snapshotManagerImpl) addProgressReporter(r io.Reader, fileType string, url string, length uint64) io.Reader {
progressReporter := NewProgressReporter(smiT.log, fmt.Sprintf("Downloading %s from url %s", fileType, url), length)
return io.TeeReader(r, progressReporter)
}

Expand Down

0 comments on commit 1351abf

Please sign in to comment.