Skip to content

Commit faf06c6

Browse files
JkLondonJkLondonAskAlexSharov
authored
[main] building indexes on old files if have some (cp from 3.1) (erigontech#17018)
cp from erigontech#16984 --------- Co-authored-by: JkLondon <me@ilyamikheev.com> Co-authored-by: alex <AskAlexSharov@gmail.com>
1 parent 0614ee8 commit faf06c6

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

cmd/capcli/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"errors"
2424
"fmt"
25+
"github.com/erigontech/erigon/db/snaptype"
2526
"io"
2627
"math"
2728
"net/http"
@@ -64,7 +65,6 @@ import (
6465
"github.com/erigontech/erigon/db/kv"
6566
"github.com/erigontech/erigon/db/snapshotsync"
6667
"github.com/erigontech/erigon/db/snapshotsync/freezeblocks"
67-
"github.com/erigontech/erigon/db/snaptype"
6868
"github.com/erigontech/erigon/eth/ethconfig"
6969
"github.com/erigontech/erigon/turbo/debug"
7070
)

db/snapshotsync/snapshots.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"github.com/erigontech/erigon/db/version"
2324
"math"
2425
"os"
2526
"path/filepath"
@@ -464,7 +465,19 @@ func (s *DirtySegment) openIdx(dir string) (err error) {
464465
if s.indexes[i] != nil {
465466
continue
466467
}
467-
index, err := recsplit.OpenIndex(filepath.Join(dir, fileName))
468+
fPathMask, err := version.ReplaceVersionWithMask(filepath.Join(dir, fileName))
469+
if err != nil {
470+
return fmt.Errorf("[open index] can't replace with mask in file %s: %w", fileName, err)
471+
}
472+
fPath, _, ok, err := version.FindFilesWithVersionsByPattern(fPathMask)
473+
if err != nil {
474+
return fmt.Errorf("%w, fileName: %s", err, fileName)
475+
}
476+
if !ok {
477+
_, fName := filepath.Split(fPath)
478+
return fmt.Errorf("[open index] find files by pattern err %w fname %s", os.ErrNotExist, fName)
479+
}
480+
index, err := recsplit.OpenIndex(fPath)
468481

469482
if err != nil {
470483
return fmt.Errorf("%w, fileName: %s", err, fileName)

db/snaptype/files.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,24 @@ func FileName(version Version, from, to uint64, fileType string) string {
3939
return fmt.Sprintf("%s-%06d-%06d-%s", version.String(), from/1_000, to/1_000, fileType)
4040
}
4141

42+
func FileMask(from, to uint64, fileType string) string {
43+
return fmt.Sprintf("*-%06d-%06d-%s", from/1_000, to/1_000, fileType)
44+
}
45+
4246
func SegmentFileName(version Version, from, to uint64, t Enum) string {
4347
return FileName(version, from, to, t.String()) + ".seg"
4448
}
4549
func IdxFileName(version Version, from, to uint64, fType string) string {
4650
return FileName(version, from, to, fType) + ".idx"
4751
}
4852

53+
func SegmentFileMask(from, to uint64, t Enum) string {
54+
return FileMask(from, to, t.String()) + ".seg"
55+
}
56+
func IdxFileMask(from, to uint64, fType string) string {
57+
return FileMask(from, to, fType) + ".idx"
58+
}
59+
4960
func FilterExt(in []FileInfo, expectExt string) (out []FileInfo) {
5061
for _, f := range in {
5162
if f.Ext != expectExt { // filter out only compressed files

db/snaptype/type.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ type Type interface {
184184
Name() string
185185
FileName(version Version, from uint64, to uint64) string
186186
FileInfo(dir string, from uint64, to uint64) FileInfo
187+
FileInfoByMask(dir string, from uint64, to uint64) FileInfo
187188
IdxFileName(version Version, from uint64, to uint64, index ...Index) string
188189
IdxFileNames(version Version, from uint64, to uint64) []string
189190
Indexes() []Index
@@ -254,11 +255,29 @@ func (s snapType) FileName(version Version, from uint64, to uint64) string {
254255
return SegmentFileName(version, from, to, s.enum)
255256
}
256257

258+
func (s snapType) FileMask(from uint64, to uint64) string {
259+
return SegmentFileMask(from, to, s.enum)
260+
}
261+
257262
func (s snapType) FileInfo(dir string, from uint64, to uint64) FileInfo {
258263
f, _, _ := ParseFileName(dir, s.FileName(s.versions.Current, from, to))
259264
return f
260265
}
261266

267+
func (s snapType) FileInfoByMask(dir string, from uint64, to uint64) FileInfo {
268+
fName, _, ok, err := version.FindFilesWithVersionsByPattern(filepath.Join(dir, s.FileName(s.versions.Current, from, to)))
269+
if err != nil {
270+
log.Debug("[snaptype] file mask error", "err", err, "fName", s.FileName(s.versions.Current, from, to))
271+
return FileInfo{}
272+
}
273+
if !ok {
274+
return FileInfo{}
275+
}
276+
277+
f, _, _ := ParseFileName("", fName)
278+
return f
279+
}
280+
262281
func (s snapType) ExtractRange(ctx context.Context, info FileInfo, rangeExtractor RangeExtractor, indexBuilder IndexBuilder, firstKeyGetter FirstKeyGetter, db kv.RoDB, chainConfig *chain.Config, tmpDir string, workers int, lvl log.Lvl, logger log.Logger, hashResolver BlockHashResolver) (uint64, error) {
263282
if rangeExtractor == nil {
264283
rangeExtractor = s.rangeExtractor
@@ -429,6 +448,22 @@ func BuildIndex(ctx context.Context, info FileInfo, cfg recsplit.RecSplitArgs, l
429448
}
430449
}()
431450

451+
fPathMask, err := version.ReplaceVersionWithMask(info.Path)
452+
if err != nil {
453+
return fmt.Errorf("[build index] can't replace with mask in file %s: %w", info.Name(), err)
454+
}
455+
fPath, fileVer, ok, err := version.FindFilesWithVersionsByPattern(fPathMask)
456+
if err != nil {
457+
_, fName := filepath.Split(fPath)
458+
return fmt.Errorf("build index err %w fname %s", err, fName)
459+
}
460+
if !ok {
461+
_, fName := filepath.Split(fPath)
462+
return fmt.Errorf("build index err %w fname %s", os.ErrNotExist, fName)
463+
}
464+
info.Version = fileVer
465+
info.Path = fPath
466+
432467
d, err := seg.NewDecompressor(info.Path)
433468
if err != nil {
434469
return fmt.Errorf("can't open %s for indexing: %w", info.Name(), err)
@@ -441,7 +476,7 @@ func BuildIndex(ctx context.Context, info FileInfo, cfg recsplit.RecSplitArgs, l
441476
p.Total.Store(uint64(d.Count()))
442477
}
443478
cfg.KeyCount = d.Count()
444-
cfg.IndexFile = filepath.Join(info.Dir(), info.Type.IdxFileName(info.Version, info.From, info.To))
479+
cfg.IndexFile = filepath.Join(info.Dir(), info.Type.IdxFileName(fileVer, info.From, info.To))
445480
rs, err := recsplit.NewRecSplit(cfg, logger)
446481
if err != nil {
447482
return err

db/version/file_version.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,12 @@ func (v *Version) UnmarshalYAML(node *yaml.Node) error {
217217
*v = ver
218218
return nil
219219
}
220+
221+
func VersionTooLowPanic(filename string, version Versions) {
222+
panic(fmt.Sprintf(
223+
"Version is too low, try to run snapshot reset: `erigon --datadir $DATADIR --chain $CHAIN snapshots reset`. file=%s, min_supported=%s, current=%s",
224+
filename,
225+
version.MinSupported,
226+
version.Current,
227+
))
228+
}

0 commit comments

Comments
 (0)