Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Oct 17, 2024
1 parent 9e2c1c1 commit f582712
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
29 changes: 15 additions & 14 deletions cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,27 +407,28 @@ func RemoteServices(ctx context.Context, cfg *httpcfg.HttpCfg, logger log.Logger
return nil, nil, nil, nil, nil, nil, nil, ff, nil, nil, errors.New("chain config not found in db. Need start erigon at least once on this db")
}

doOptimisticOpen := false
snapcfg.LoadRemotePreverified()
if preverifiedCfg := downloadercfg.ReadPreverifiedToml(cfg.Dirs, cc.ChainName); preverifiedCfg != nil {
allFilesDownloadComplete, lastUncomplete, err := downloaderrawdb.AllFilesComplete(preverifiedCfg, cfg.Dirs)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
if !allFilesDownloadComplete {
log.Warn("[rpc] download of segments not complete yet (need wait, then RPC will work)", "example_uncomplete_file", lastUncomplete)
allSegmentsDownloadComplete := false
{
snapcfg.LoadRemotePreverified()
if preverifiedCfg := downloadercfg.ReadPreverifiedToml(cfg.Dirs, cc.ChainName); preverifiedCfg != nil {
allSegmentsDownloadComplete, err = downloaderrawdb.AllSegmentsDownloadCompleteFlag(cfg.Dirs)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, err
}
if !allSegmentsDownloadComplete {
log.Warn("[rpc] download of segments not complete yet (need wait, then RPC will work)")
}
} else {
log.Warn("[rpc] download of segments not complete yet")
}
doOptimisticOpen = allFilesDownloadComplete
} else {
log.Warn("[rpc] download of segments not complete yet")
}

// Configure sapshots
allSnapshots = freezeblocks.NewRoSnapshots(cfg.Snap, cfg.Dirs.Snap, 0, logger)
allBorSnapshots = freezeblocks.NewBorRoSnapshots(cfg.Snap, cfg.Dirs.Snap, 0, logger)
// To povide good UX - immediatly can read snapshots after RPCDaemon start, even if Erigon is down
// Erigon does store list of snapshots in db: means RPCDaemon can read this list now, but read by `remoteKvClient.Snapshots` after establish grpc connection
if doOptimisticOpen {
if allSegmentsDownloadComplete {
allSnapshots.OptimisticalyReopenFolder()
allBorSnapshots.OptimisticalyReopenFolder()
}
Expand All @@ -440,7 +441,7 @@ func RemoteServices(ctx context.Context, cfg *httpcfg.HttpCfg, logger log.Logger
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, ff, nil, nil, fmt.Errorf("create aggregator: %w", err)
}
if doOptimisticOpen {
if allSegmentsDownloadComplete {
_ = agg.OpenFolder() //TODO: must use analog of `OptimisticReopenWithDB`
}

Expand Down
25 changes: 7 additions & 18 deletions erigon-lib/downloader/downloaderrawdb/accessors_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"time"

"github.com/erigontech/erigon-lib/chain/snapcfg"
"github.com/erigontech/erigon-lib/common/datadir"
"github.com/erigontech/erigon-lib/kv"
kv2 "github.com/erigontech/erigon-lib/kv/mdbx"
Expand Down Expand Up @@ -78,41 +77,31 @@ func CheckFileComplete(tx kv.Tx, name string, snapDir string) (bool, int64, *tim
return false, 0, nil
}

func allFilesComplete(tx kv.Tx, preverifiedCfg *snapcfg.Cfg, dirs datadir.Dirs) (allFilesDownloadComplete bool, lastUncomplete string) {
for _, p := range preverifiedCfg.Preverified {
complete, _, _ := CheckFileComplete(tx, p.Name, dirs.Snap)
if !complete {
return false, p.Name
}
}
return true, ""
}

func AllFilesComplete(preverifiedCfg *snapcfg.Cfg, dirs datadir.Dirs) (allFilesDownloadComplete bool, lastUncomplete string, err error) {
func AllSegmentsDownloadCompleteFlag(dirs datadir.Dirs) (allFilesDownloadComplete bool, err error) {
limiter := semaphore.NewWeighted(9_000)
downloaderDB, err := kv2.NewMDBX(log.Root()).Label(kv.DownloaderDB).WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
return kv.TablesCfgByLabel(kv.DownloaderDB)
}).RoTxsLimiter(limiter).Path(dirs.Downloader).Accede().Open(context.Background())
if err != nil {
return false, "", err
return false, err
}
defer downloaderDB.Close()

if err := downloaderDB.View(context.Background(), func(tx kv.Tx) error {
allFilesDownloadComplete, lastUncomplete = allFilesComplete(tx, preverifiedCfg, dirs)
return nil
allFilesDownloadComplete, err = ReadAllCompleteFlag(tx)
return err
}); err != nil {
return false, "", err
return false, err
}
return allFilesDownloadComplete, lastUncomplete, nil
return allFilesDownloadComplete, nil
}

var AllCompleteFlagKey = []byte("all_complete")

func WriteAllCompleteFlag(tx kv.RwTx) error {
return tx.Put(kv.BittorrentInfo, AllCompleteFlagKey, []byte{1})
}
func ReadAllCompleteFlag(tx kv.RwTx) (bool, error) {
func ReadAllCompleteFlag(tx kv.Tx) (bool, error) {
v, err := tx.GetOne(kv.BittorrentInfo, AllCompleteFlagKey)
if err != nil {
return false, err
Expand Down

0 comments on commit f582712

Please sign in to comment.