From 3b68d57c287fdc8b21b1ec333796615c42510930 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Thu, 14 Dec 2023 07:53:54 +0100 Subject: [PATCH] collect statistics even for download completed or not started files (#8956) Co-authored-by: alex.sharov --- erigon-lib/downloader/downloader.go | 86 ++++++++++++++++------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/erigon-lib/downloader/downloader.go b/erigon-lib/downloader/downloader.go index 8d6a1025a..330545592 100644 --- a/erigon-lib/downloader/downloader.go +++ b/erigon-lib/downloader/downloader.go @@ -351,6 +351,7 @@ func (d *Downloader) ReCalcStats(interval time.Duration) { var zeroProgress []string var noMetadata []string + for _, t := range torrents { select { case <-t.GotInfo(): @@ -363,54 +364,21 @@ func (d *Downloader) ReCalcStats(interval time.Duration) { } stats.BytesCompleted += uint64(t.BytesCompleted()) stats.BytesTotal += uint64(t.Length()) - if t.Complete.Bool() { - break //of switch - } progress := float32(float64(100) * (float64(t.BytesCompleted()) / float64(t.Length()))) if progress == 0 { zeroProgress = append(zeroProgress, t.Name()) - break //of switch } d.logger.Log(d.verbosity, "[snapshots] progress", "file", t.Name(), "progress", fmt.Sprintf("%.2f%%", progress), "peers", len(peersOfThisFile), "webseeds", len(weebseedPeersOfThisFile)) isDiagEnabled := diagnostics.TypeOf(diagnostics.SegmentDownloadStatistics{}).Enabled() if d.verbosity >= log.LvlInfo || isDiagEnabled { - + webseedRates, websRates := getWebseedsRatesForlogs(weebseedPeersOfThisFile) + rates, peersRates := getPeersRatesForlogs(peersOfThisFile) // more detailed statistic: download rate of each peer (for each file) - websRates := uint64(0) - webseedRates := make([]interface{}, 0, len(weebseedPeersOfThisFile)*2) - for _, peer := range weebseedPeersOfThisFile { - urlS := strings.Trim(strings.TrimPrefix(peer.String(), "webseed peer for "), "\"") - if urlObj, err := url.Parse(urlS); err == nil { - if shortUrl, err := url.JoinPath(urlObj.Host, urlObj.Path); err == nil { - dr := uint64(peer.DownloadRate()) - webseedRates = append(webseedRates, shortUrl, fmt.Sprintf("%s/s", common.ByteCount(dr))) - websRates += dr - } - - d.logger.Log(d.verbosity, "[snapshots] progress", "name", t.Name(), "progress", fmt.Sprintf("%.2f%%", progress), "webseeds", len(t.Metainfo().UrlList), "peers", len(peersOfThisFile)) - } - } - - lenght := uint64(len(weebseedPeersOfThisFile)) - if lenght > 0 { - websRates = websRates / lenght - } - - d.logger.Info(fmt.Sprintf("[snapshots] webseed peers file=%s", t.Name()), webseedRates...) - rates := make([]interface{}, 0, len(peersOfThisFile)*2) - peersRates := uint64(0) - for _, peer := range peersOfThisFile { - dr := uint64(peer.DownloadRate()) - rates = append(rates, peer.PeerClientName.Load(), fmt.Sprintf("%s/s", common.ByteCount(dr))) - peersRates += dr - } - d.logger.Info(fmt.Sprintf("[snapshots] bittorrent peers file=%s", t.Name()), rates...) - - lenght = uint64(len(peersOfThisFile)) - if lenght > 0 { - peersRates = peersRates / uint64(len(peersOfThisFile)) + if !t.Complete.Bool() && progress != 0 { + d.logger.Info(fmt.Sprintf("[snapshots] webseed peers file=%s", t.Name()), webseedRates...) + d.logger.Info(fmt.Sprintf("[snapshots] bittorrent peers file=%s", t.Name()), rates...) } if isDiagEnabled { @@ -465,6 +433,48 @@ func (d *Downloader) ReCalcStats(interval time.Duration) { d.stats = stats } +func getWebseedsRatesForlogs(weebseedPeersOfThisFile []*torrent.Peer) ([]interface{}, uint64) { + totalRate := uint64(0) + averageRate := uint64(0) + webseedRates := make([]interface{}, 0, len(weebseedPeersOfThisFile)*2) + for _, peer := range weebseedPeersOfThisFile { + urlS := strings.Trim(strings.TrimPrefix(peer.String(), "webseed peer for "), "\"") + if urlObj, err := url.Parse(urlS); err == nil { + if shortUrl, err := url.JoinPath(urlObj.Host, urlObj.Path); err == nil { + rate := uint64(peer.DownloadRate()) + totalRate += rate + webseedRates = append(webseedRates, shortUrl, fmt.Sprintf("%s/s", common.ByteCount(rate))) + } + } + } + + lenght := uint64(len(weebseedPeersOfThisFile)) + if lenght > 0 { + averageRate = totalRate / lenght + } + + return webseedRates, averageRate +} + +func getPeersRatesForlogs(peersOfThisFile []*torrent.PeerConn) ([]interface{}, uint64) { + totalRate := uint64(0) + averageRate := uint64(0) + rates := make([]interface{}, 0, len(peersOfThisFile)*2) + + for _, peer := range peersOfThisFile { + dr := uint64(peer.DownloadRate()) + rates = append(rates, peer.PeerClientName.Load(), fmt.Sprintf("%s/s", common.ByteCount(dr))) + totalRate += dr + } + + lenght := uint64(len(peersOfThisFile)) + if lenght > 0 { + averageRate = totalRate / uint64(len(peersOfThisFile)) + } + + return rates, averageRate +} + func VerifyFile(ctx context.Context, t *torrent.Torrent, completePieces *atomic.Uint64) error { select { case <-ctx.Done():