Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

downloader: prohibit_new_downloads.lock check missed download .torrent from WebSeed #9295

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions erigon-lib/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
Expand All @@ -41,7 +39,6 @@ import (
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/common/dir"
"github.com/ledgerwatch/erigon-lib/diagnostics"
"github.com/ledgerwatch/erigon-lib/downloader/downloadercfg"
"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
Expand Down Expand Up @@ -147,27 +144,6 @@ func New(ctx context.Context, cfg *downloadercfg.Cfg, dirs datadir.Dirs, logger
return d, nil
}

const ProhibitNewDownloadsFileName = "prohibit_new_downloads.lock"

// Erigon "download once" - means restart/upgrade/downgrade will not download files (and will be fast)
// After "download once" - Erigon will produce and seed new files
// Downloader will able: seed new files (already existing on FS), download uncomplete parts of existing files (if Verify found some bad parts)
func (d *Downloader) prohibitNewDownloads() error {
fPath := filepath.Join(d.SnapDir(), ProhibitNewDownloadsFileName)
f, err := os.Create(fPath)
if err != nil {
return err
}
defer f.Close()
if err := f.Sync(); err != nil {
return err
}
return nil
}
func (d *Downloader) newDownloadsAreProhibited() bool {
return dir.FileExist(filepath.Join(d.SnapDir(), ProhibitNewDownloadsFileName))
}

func (d *Downloader) MainLoopInBackground(silent bool) {
d.wg.Add(1)
go func() {
Expand Down Expand Up @@ -605,7 +581,7 @@ func (d *Downloader) AddMagnetLink(ctx context.Context, infoHash metainfo.Hash,
if d.alreadyHaveThisName(name) {
return nil
}
if d.newDownloadsAreProhibited() {
if d.torrentFiles.newDownloadsAreProhibited() {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion erigon-lib/downloader/downloader_grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type GrpcServer struct {
}

func (s *GrpcServer) ProhibitNewDownloads(context.Context, *proto_downloader.ProhibitNewDownloadsRequest) (*emptypb.Empty, error) {
if err := s.d.prohibitNewDownloads(); err != nil {
if err := s.d.torrentFiles.prohibitNewDownloads(); err != nil {
return nil, err
}
return nil, nil
Expand Down
49 changes: 42 additions & 7 deletions erigon-lib/downloader/torrent_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"

"github.com/anacrolix/torrent"
Expand All @@ -28,8 +29,10 @@ func (tf *TorrentFiles) Exists(name string) bool {
}

func (tf *TorrentFiles) exists(name string) bool {
fPath := filepath.Join(tf.dir, name)
return dir2.FileExist(fPath + ".torrent")
if !strings.HasSuffix(name, ".torrent") {
name += ".torrent"
}
return dir2.FileExist(filepath.Join(tf.dir, name))
}
func (tf *TorrentFiles) Delete(name string) error {
tf.lock.Lock()
Expand All @@ -38,8 +41,10 @@ func (tf *TorrentFiles) Delete(name string) error {
}

func (tf *TorrentFiles) delete(name string) error {
fPath := filepath.Join(tf.dir, name)
return os.Remove(fPath + ".torrent")
if !strings.HasSuffix(name, ".torrent") {
name += ".torrent"
}
return os.Remove(filepath.Join(tf.dir, name))
}

func (tf *TorrentFiles) Create(torrentFilePath string, res []byte) error {
Expand Down Expand Up @@ -91,11 +96,10 @@ func (tf *TorrentFiles) createTorrentFromMetaInfo(fPath string, mi *metainfo.Met
return nil
}

func (tf *TorrentFiles) LoadByName(fName string) (*torrent.TorrentSpec, error) {
func (tf *TorrentFiles) LoadByName(name string) (*torrent.TorrentSpec, error) {
tf.lock.Lock()
defer tf.lock.Unlock()
fPath := filepath.Join(tf.dir, fName+".torrent")
return tf.load(fPath)
return tf.load(filepath.Join(tf.dir, name))
}

func (tf *TorrentFiles) LoadByPath(fPath string) (*torrent.TorrentSpec, error) {
Expand All @@ -105,10 +109,41 @@ func (tf *TorrentFiles) LoadByPath(fPath string) (*torrent.TorrentSpec, error) {
}

func (tf *TorrentFiles) load(fPath string) (*torrent.TorrentSpec, error) {
if !strings.HasSuffix(fPath, ".torrent") {
fPath += ".torrent"
}
mi, err := metainfo.LoadFromFile(fPath)
if err != nil {
return nil, fmt.Errorf("LoadFromFile: %w, file=%s", err, fPath)
}
mi.AnnounceList = Trackers
return torrent.TorrentSpecFromMetaInfoErr(mi)
}

const ProhibitNewDownloadsFileName = "prohibit_new_downloads.lock"

// Erigon "download once" - means restart/upgrade/downgrade will not download files (and will be fast)
// After "download once" - Erigon will produce and seed new files
// Downloader will able: seed new files (already existing on FS), download uncomplete parts of existing files (if Verify found some bad parts)
func (tf *TorrentFiles) prohibitNewDownloads() error {
tf.lock.Lock()
defer tf.lock.Unlock()
return CreateProhibitNewDownloadsFile(tf.dir)
}
func (tf *TorrentFiles) newDownloadsAreProhibited() bool {
tf.lock.Lock()
defer tf.lock.Unlock()
return dir2.FileExist(filepath.Join(tf.dir, ProhibitNewDownloadsFileName))
}
func CreateProhibitNewDownloadsFile(dir string) error {
fPath := filepath.Join(dir, ProhibitNewDownloadsFileName)
f, err := os.Create(fPath)
if err != nil {
return err
}
defer f.Close()
if err := f.Sync(); err != nil {
return err
}
return nil
}
3 changes: 3 additions & 0 deletions erigon-lib/downloader/webseed.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ func (d *WebSeeds) downloadTorrentFilesFromProviders(ctx context.Context, rootDi
if len(d.TorrentUrls()) == 0 {
return
}
if d.torrentFiles.newDownloadsAreProhibited() {
return
}
var addedNew int
e, ctx := errgroup.WithContext(ctx)
e.SetLimit(1024)
Expand Down
6 changes: 5 additions & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,15 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
config.Sync.UseSnapshots = useSnapshots
config.Snapshot.Enabled = ethconfig.UseSnapshotsByChainName(config.Genesis.Config.ChainName) && useSnapshots
}

return nil
}); err != nil {
return nil, err
}
if !config.Sync.UseSnapshots {
if err := downloader.CreateProhibitNewDownloadsFile(dirs.Snap); err != nil {
return nil, err
}
}

ctx, ctxCancel := context.WithCancel(context.Background())

Expand Down
Loading