Skip to content

Commit 2099940

Browse files
release: downloader: prohibit_new_downloads.lock check missed download (#9300)
Cherry pick PR #9295 --------- Co-authored-by: Alex Sharov <AskAlexSharov@gmail.com>
1 parent 4f6eda7 commit 2099940

File tree

6 files changed

+53
-35
lines changed

6 files changed

+53
-35
lines changed

erigon-lib/downloader/downloader.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"net/url"
24-
"os"
25-
"path/filepath"
2624
"runtime"
2725
"strings"
2826
"sync"
@@ -41,7 +39,6 @@ import (
4139
"github.com/ledgerwatch/erigon-lib/common"
4240
"github.com/ledgerwatch/erigon-lib/common/datadir"
4341
"github.com/ledgerwatch/erigon-lib/common/dbg"
44-
"github.com/ledgerwatch/erigon-lib/common/dir"
4542
"github.com/ledgerwatch/erigon-lib/diagnostics"
4643
"github.com/ledgerwatch/erigon-lib/downloader/downloadercfg"
4744
"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
@@ -147,27 +144,6 @@ func New(ctx context.Context, cfg *downloadercfg.Cfg, dirs datadir.Dirs, logger
147144
return d, nil
148145
}
149146

150-
const ProhibitNewDownloadsFileName = "prohibit_new_downloads.lock"
151-
152-
// Erigon "download once" - means restart/upgrade/downgrade will not download files (and will be fast)
153-
// After "download once" - Erigon will produce and seed new files
154-
// Downloader will able: seed new files (already existing on FS), download uncomplete parts of existing files (if Verify found some bad parts)
155-
func (d *Downloader) prohibitNewDownloads() error {
156-
fPath := filepath.Join(d.SnapDir(), ProhibitNewDownloadsFileName)
157-
f, err := os.Create(fPath)
158-
if err != nil {
159-
return err
160-
}
161-
defer f.Close()
162-
if err := f.Sync(); err != nil {
163-
return err
164-
}
165-
return nil
166-
}
167-
func (d *Downloader) newDownloadsAreProhibited() bool {
168-
return dir.FileExist(filepath.Join(d.SnapDir(), ProhibitNewDownloadsFileName))
169-
}
170-
171147
func (d *Downloader) MainLoopInBackground(silent bool) {
172148
d.wg.Add(1)
173149
go func() {
@@ -605,7 +581,7 @@ func (d *Downloader) AddMagnetLink(ctx context.Context, infoHash metainfo.Hash,
605581
if d.alreadyHaveThisName(name) {
606582
return nil
607583
}
608-
if d.newDownloadsAreProhibited() {
584+
if d.torrentFiles.newDownloadsAreProhibited() {
609585
return nil
610586
}
611587

erigon-lib/downloader/downloader_grpc_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type GrpcServer struct {
4646
}
4747

4848
func (s *GrpcServer) ProhibitNewDownloads(context.Context, *proto_downloader.ProhibitNewDownloadsRequest) (*emptypb.Empty, error) {
49-
if err := s.d.prohibitNewDownloads(); err != nil {
49+
if err := s.d.torrentFiles.prohibitNewDownloads(); err != nil {
5050
return nil, err
5151
}
5252
return nil, nil

erigon-lib/downloader/torrent_files.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"strings"
78
"sync"
89

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

3031
func (tf *TorrentFiles) exists(name string) bool {
31-
fPath := filepath.Join(tf.dir, name)
32-
return dir2.FileExist(fPath + ".torrent")
32+
if !strings.HasSuffix(name, ".torrent") {
33+
name += ".torrent"
34+
}
35+
return dir2.FileExist(filepath.Join(tf.dir, name))
3336
}
3437
func (tf *TorrentFiles) Delete(name string) error {
3538
tf.lock.Lock()
@@ -38,8 +41,10 @@ func (tf *TorrentFiles) Delete(name string) error {
3841
}
3942

4043
func (tf *TorrentFiles) delete(name string) error {
41-
fPath := filepath.Join(tf.dir, name)
42-
return os.Remove(fPath + ".torrent")
44+
if !strings.HasSuffix(name, ".torrent") {
45+
name += ".torrent"
46+
}
47+
return os.Remove(filepath.Join(tf.dir, name))
4348
}
4449

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

94-
func (tf *TorrentFiles) LoadByName(fName string) (*torrent.TorrentSpec, error) {
99+
func (tf *TorrentFiles) LoadByName(name string) (*torrent.TorrentSpec, error) {
95100
tf.lock.Lock()
96101
defer tf.lock.Unlock()
97-
fPath := filepath.Join(tf.dir, fName+".torrent")
98-
return tf.load(fPath)
102+
return tf.load(filepath.Join(tf.dir, name))
99103
}
100104

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

107111
func (tf *TorrentFiles) load(fPath string) (*torrent.TorrentSpec, error) {
112+
if !strings.HasSuffix(fPath, ".torrent") {
113+
fPath += ".torrent"
114+
}
108115
mi, err := metainfo.LoadFromFile(fPath)
109116
if err != nil {
110117
return nil, fmt.Errorf("LoadFromFile: %w, file=%s", err, fPath)
111118
}
112119
mi.AnnounceList = Trackers
113120
return torrent.TorrentSpecFromMetaInfoErr(mi)
114121
}
122+
123+
const ProhibitNewDownloadsFileName = "prohibit_new_downloads.lock"
124+
125+
// Erigon "download once" - means restart/upgrade/downgrade will not download files (and will be fast)
126+
// After "download once" - Erigon will produce and seed new files
127+
// Downloader will able: seed new files (already existing on FS), download uncomplete parts of existing files (if Verify found some bad parts)
128+
func (tf *TorrentFiles) prohibitNewDownloads() error {
129+
tf.lock.Lock()
130+
defer tf.lock.Unlock()
131+
return CreateProhibitNewDownloadsFile(tf.dir)
132+
}
133+
func (tf *TorrentFiles) newDownloadsAreProhibited() bool {
134+
tf.lock.Lock()
135+
defer tf.lock.Unlock()
136+
return dir2.FileExist(filepath.Join(tf.dir, ProhibitNewDownloadsFileName))
137+
}
138+
func CreateProhibitNewDownloadsFile(dir string) error {
139+
fPath := filepath.Join(dir, ProhibitNewDownloadsFileName)
140+
f, err := os.Create(fPath)
141+
if err != nil {
142+
return err
143+
}
144+
defer f.Close()
145+
if err := f.Sync(); err != nil {
146+
return err
147+
}
148+
return nil
149+
}

erigon-lib/downloader/webseed.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ func (d *WebSeeds) downloadTorrentFilesFromProviders(ctx context.Context, rootDi
237237
if len(d.TorrentUrls()) == 0 {
238238
return
239239
}
240+
if d.torrentFiles.newDownloadsAreProhibited() {
241+
return
242+
}
240243
var addedNew int
241244
e, ctx := errgroup.WithContext(ctx)
242245
e.SetLimit(1024)

eth/backend.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,15 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
266266
config.Sync.UseSnapshots = useSnapshots
267267
config.Snapshot.Enabled = ethconfig.UseSnapshotsByChainName(config.Genesis.Config.ChainName) && useSnapshots
268268
}
269-
270269
return nil
271270
}); err != nil {
272271
return nil, err
273272
}
273+
if !config.Sync.UseSnapshots {
274+
if err := downloader.CreateProhibitNewDownloadsFile(dirs.Snap); err != nil {
275+
return nil, err
276+
}
277+
}
274278

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

params/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var (
3333
const (
3434
VersionMajor = 2 // Major version component of the current release
3535
VersionMinor = 57 // Minor version component of the current release
36-
VersionMicro = 0 // Patch version component of the current release
36+
VersionMicro = 1 // Patch version component of the current release
3737
VersionModifier = "" // Modifier component of the current release
3838
VersionKeyCreated = "ErigonVersionCreated"
3939
VersionKeyFinished = "ErigonVersionFinished"

0 commit comments

Comments
 (0)