Skip to content

Commit

Permalink
Calculate total size of downloaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Sharp authored and Alexey Sharp committed Apr 25, 2022
1 parent c6807b5 commit 64a26df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
25 changes: 19 additions & 6 deletions cmd/downloader/downloader/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package downloader
import (
"context"
"errors"
"fmt"
"path/filepath"

"github.com/anacrolix/torrent"
Expand Down Expand Up @@ -74,13 +75,26 @@ func (s *GrpcServer) Download(ctx context.Context, request *proto_downloader.Dow
infoHashes[i] = gointerfaces.ConvertH160toAddress(it.TorrentHash)
}
}
if err := ResolveAbsentTorrents(ctx, s.t.TorrentClient, infoHashes, s.snapshotDir, s.silent); err != nil {
return nil, err
}
for _, t := range s.t.TorrentClient.Torrents() {
t.AllowDataDownload()
if len(request.Items) == 1 {
t, ok := s.t.TorrentClient.Torrent(infoHashes[0])
if !ok {
return nil, fmt.Errorf("torrent not found: [%x]", infoHashes[0])
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-t.GotInfo():
if !t.Complete.Bool() {
t.DownloadAll()
}
mi := t.Metainfo()
if err := CreateTorrentFileIfNotExists(s.snapshotDir, t.Info(), &mi); err != nil {
return nil, err
}
}
t.AllowDataUpload()
if !t.Complete.Bool() {
t.AllowDataDownload()
t.DownloadAll()
}
}
Expand All @@ -102,7 +116,6 @@ func (s *GrpcServer) Stats(ctx context.Context, request *proto_downloader.StatsR
reply.BytesTotal += uint64(t.Info().TotalLength())
reply.Completed = reply.Completed && t.Complete.Bool()
reply.Connections += uint64(len(t.PeerConns()))

for _, peer := range t.PeerConns() {
peers[peer.PeerID] = struct{}{}
}
Expand Down
22 changes: 22 additions & 0 deletions eth/stagedsync/stage_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,28 @@ func WaitForDownloader(ctx context.Context, tx kv.RwTx, cfg HeadersCfg) error {
var prevBytesCompleted uint64
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
// Initially send all info hashes to calculate total size of all files, to calculate correct progress
req := &proto_downloader.DownloadRequest{Items: make([]*proto_downloader.DownloadItem, len(preverified))}
for i, p := range preverified {
req.Items[i] = &proto_downloader.DownloadItem{
TorrentHash: downloadergrpc.String2Proto(p.Hash),
Path: p.Name,
}
}
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if _, err := cfg.snapshotDownloader.Download(ctx, req); err != nil {
log.Error("[Snapshots] Can't call downloader", "err", err)
time.Sleep(10 * time.Second)
continue
}
break
}
// Now send all info hashes 1 by one
for _, p := range preverified {
req := &proto_downloader.DownloadRequest{Items: make([]*proto_downloader.DownloadItem, 1)}
req.Items[0] = &proto_downloader.DownloadItem{
Expand Down

0 comments on commit 64a26df

Please sign in to comment.