Skip to content

Commit

Permalink
Fixed snapshot read from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
eastorski committed Dec 17, 2024
1 parent b0578e6 commit c406c8a
Showing 1 changed file with 11 additions and 29 deletions.
40 changes: 11 additions & 29 deletions turbo/snapshotsync/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/erigontech/erigon-lib/common/background"
"github.com/erigontech/erigon-lib/common/datadir"
"github.com/erigontech/erigon-lib/common/dbg"
dir2 "github.com/erigontech/erigon-lib/common/dir"
"github.com/erigontech/erigon-lib/diagnostics"
"github.com/erigontech/erigon-lib/downloader/snaptype"
"github.com/erigontech/erigon-lib/log/v3"
Expand All @@ -56,6 +55,10 @@ type SortedRange interface {

// NoOverlaps - keep largest ranges and avoid overlap
func NoOverlaps[T SortedRange](in []T) (res []T) {
if len(in) == 1 {
return in
}

for i := 0; i < len(in); i++ {
r := in[i]
iFrom, iTo := r.GetRange()
Expand Down Expand Up @@ -83,6 +86,10 @@ func NoGaps[T SortedRange](in []T) (out []T, missingRanges []Range) {
if len(in) == 0 {
return nil, nil
}
if len(in) == 1 {
return in, nil
}

prevTo, _ := in[0].GetRange()
for _, f := range in {
from, to := f.GetRange()
Expand Down Expand Up @@ -980,10 +987,6 @@ func (s *RoSnapshots) InitSegments(fileNames []string) error {
}

func TypedSegments(dir string, minBlock uint64, types []snaptype.Type, allowGaps bool) (res []snaptype.FileInfo, missingSnapshots []Range, err error) {
segmentsTypeCheck := func(dir string, in []snaptype.FileInfo) (res []snaptype.FileInfo) {
return typeOfSegmentsMustExist(dir, in, types)
}

list, err := snaptype.Segments(dir)

if err != nil {
Expand All @@ -1002,10 +1005,11 @@ func TypedSegments(dir string, minBlock uint64, types []snaptype.Type, allowGaps
}

if allowGaps {
l = NoOverlaps(segmentsTypeCheck(dir, l))
l = NoOverlaps(l)
} else {
l, m = NoGaps(NoOverlaps(segmentsTypeCheck(dir, l)))
l, m = NoGaps(NoOverlaps(l))
}

if len(m) > 0 {
lst := m[len(m)-1]
log.Debug("[snapshots] see gap", "type", segType, "from", lst.from)
Expand Down Expand Up @@ -1619,28 +1623,6 @@ func sendDiagnostics(startIndexingTime time.Time, indexPercent map[string]int, a
})
}

func typeOfSegmentsMustExist(dir string, in []snaptype.FileInfo, types []snaptype.Type) (res []snaptype.FileInfo) {
MainLoop:
for _, f := range in {
if f.From == f.To {
continue
}
for _, t := range types {
p := filepath.Join(dir, snaptype.SegmentFileName(f.Version, f.From, f.To, t.Enum()))
exists, err := dir2.FileExist(p)
if err != nil {
log.Debug("[snapshots] FileExist error", "err", err, "path", p)
continue MainLoop
}
if !exists {
continue MainLoop
}
res = append(res, f)
}
}
return res
}

func removeOldFiles(toDel []string, snapDir string) {
for _, f := range toDel {
_ = os.Remove(f)
Expand Down

0 comments on commit c406c8a

Please sign in to comment.