core/rawdb: use AncientRange when initializing leveldb from freezer (#23612)

* core/rawdb: utilize AncientRange when initiating from freezer

* core/rawdb: remove debug sanity check
This commit is contained in:
Martin Holst Swende 2021-11-23 12:37:26 +01:00 committed by GitHub
parent 50e07a1e16
commit 347c37b362
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,24 +44,29 @@ func InitDatabaseFromFreezer(db ethdb.Database) {
logged = start.Add(-7 * time.Second) // Unindex during import is fast, don't double log logged = start.Add(-7 * time.Second) // Unindex during import is fast, don't double log
hash common.Hash hash common.Hash
) )
for i := uint64(0); i < frozen; i++ { for i := uint64(0); i < frozen; {
// Since the freezer has all data in sequential order on a file, // We read 100K hashes at a time, for a total of 3.2M
// it would be 'neat' to read more data in one go, and let the count := uint64(100_000)
// freezerdb return N items (e.g up to 1000 items per go) if i+count > frozen {
// That would require an API change in Ancients though count = frozen - i
if h, err := db.Ancient(freezerHashTable, i); err != nil { }
data, err := db.AncientRange(freezerHashTable, i, count, 32*count)
if err != nil {
log.Crit("Failed to init database from freezer", "err", err) log.Crit("Failed to init database from freezer", "err", err)
} else { }
for j, h := range data {
number := i + uint64(j)
hash = common.BytesToHash(h) hash = common.BytesToHash(h)
} WriteHeaderNumber(batch, hash, number)
WriteHeaderNumber(batch, hash, i) // If enough data was accumulated in memory or we're at the last block, dump to disk
// If enough data was accumulated in memory or we're at the last block, dump to disk if batch.ValueSize() > ethdb.IdealBatchSize {
if batch.ValueSize() > ethdb.IdealBatchSize { if err := batch.Write(); err != nil {
if err := batch.Write(); err != nil { log.Crit("Failed to write data to db", "err", err)
log.Crit("Failed to write data to db", "err", err) }
batch.Reset()
} }
batch.Reset()
} }
i += uint64(len(data))
// If we've spent too much time already, notify the user of what we're doing // If we've spent too much time already, notify the user of what we're doing
if time.Since(logged) > 8*time.Second { if time.Since(logged) > 8*time.Second {
log.Info("Initializing database from freezer", "total", frozen, "number", i, "hash", hash, "elapsed", common.PrettyDuration(time.Since(start))) log.Info("Initializing database from freezer", "total", frozen, "number", i, "hash", hash, "elapsed", common.PrettyDuration(time.Since(start)))