From f5827124fb370d5eb61ced7f118c92eb10a0ff97 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Thu, 17 Oct 2024 14:35:57 +0700 Subject: [PATCH] save --- cmd/rpcdaemon/cli/config.go | 29 ++++++++++--------- .../downloaderrawdb/accessors_downloader.go | 25 +++++----------- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/cmd/rpcdaemon/cli/config.go b/cmd/rpcdaemon/cli/config.go index 0eb11f9ab0f..e6f40bdb79d 100644 --- a/cmd/rpcdaemon/cli/config.go +++ b/cmd/rpcdaemon/cli/config.go @@ -407,19 +407,20 @@ 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 @@ -427,7 +428,7 @@ func RemoteServices(ctx context.Context, cfg *httpcfg.HttpCfg, logger log.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() } @@ -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` } diff --git a/erigon-lib/downloader/downloaderrawdb/accessors_downloader.go b/erigon-lib/downloader/downloaderrawdb/accessors_downloader.go index 7b408f715f4..ee29513c96c 100644 --- a/erigon-lib/downloader/downloaderrawdb/accessors_downloader.go +++ b/erigon-lib/downloader/downloaderrawdb/accessors_downloader.go @@ -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" @@ -78,33 +77,23 @@ 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") @@ -112,7 +101,7 @@ 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