Skip to content

Commit f2eb3b1

Browse files
holimankaralabe
authored andcommitted
core: lessen mem-spike during 1.8->1.9 conversion (#19610)
* core/blockchain: lessen mem-spike during 1.8->1.9 conversion * core/blockchain.go: make levedb->freezer conversion gradually * core/blockchain: write the batch
1 parent 7fd82a0 commit f2eb3b1

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

core/blockchain.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,12 @@ func (bc *BlockChain) truncateAncient(head uint64) error {
927927
return nil
928928
}
929929

930+
// numberHash is just a container for a number and a hash, to represent a block
931+
type numberHash struct {
932+
number uint64
933+
hash common.Hash
934+
}
935+
930936
// InsertReceiptChain attempts to complete an already existing header chain with
931937
// transaction and receipt data.
932938
func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts, ancientLimit uint64) (int, error) {
@@ -998,7 +1004,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
9981004
}
9991005
}
10001006
}()
1001-
var deleted types.Blocks
1007+
var deleted []*numberHash
10021008
for i, block := range blockChain {
10031009
// Short circuit insertion if shutting down or processing failed
10041010
if atomic.LoadInt32(&bc.procInterrupt) == 1 {
@@ -1033,12 +1039,40 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
10331039

10341040
// Always keep genesis block in active database.
10351041
if b.NumberU64() != 0 {
1036-
deleted = append(deleted, b)
1042+
deleted = append(deleted, &numberHash{b.NumberU64(), b.Hash()})
10371043
}
10381044
if time.Since(logged) > 8*time.Second {
10391045
log.Info("Migrating ancient blocks", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
10401046
logged = time.Now()
10411047
}
1048+
// Don't collect too much in-memory, write it out every 100K blocks
1049+
if len(deleted) > 100000 {
1050+
1051+
// Sync the ancient store explicitly to ensure all data has been flushed to disk.
1052+
if err := bc.db.Sync(); err != nil {
1053+
return 0, err
1054+
}
1055+
// Wipe out canonical block data.
1056+
for _, nh := range deleted {
1057+
rawdb.DeleteBlockWithoutNumber(batch, nh.hash, nh.number)
1058+
rawdb.DeleteCanonicalHash(batch, nh.number)
1059+
}
1060+
if err := batch.Write(); err != nil {
1061+
return 0, err
1062+
}
1063+
batch.Reset()
1064+
// Wipe out side chain too.
1065+
for _, nh := range deleted {
1066+
for _, hash := range rawdb.ReadAllHashes(bc.db, nh.number) {
1067+
rawdb.DeleteBlock(batch, hash, nh.number)
1068+
}
1069+
}
1070+
if err := batch.Write(); err != nil {
1071+
return 0, err
1072+
}
1073+
batch.Reset()
1074+
deleted = deleted[0:]
1075+
}
10421076
}
10431077
if count > 0 {
10441078
log.Info("Migrated ancient blocks", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
@@ -1066,7 +1100,11 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
10661100
previous = nil // disable rollback explicitly
10671101

10681102
// Wipe out canonical block data.
1069-
for _, block := range append(deleted, blockChain...) {
1103+
for _, nh := range deleted {
1104+
rawdb.DeleteBlockWithoutNumber(batch, nh.hash, nh.number)
1105+
rawdb.DeleteCanonicalHash(batch, nh.number)
1106+
}
1107+
for _, block := range blockChain {
10701108
// Always keep genesis block in active database.
10711109
if block.NumberU64() != 0 {
10721110
rawdb.DeleteBlockWithoutNumber(batch, block.Hash(), block.NumberU64())
@@ -1079,7 +1117,12 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
10791117
batch.Reset()
10801118

10811119
// Wipe out side chain too.
1082-
for _, block := range append(deleted, blockChain...) {
1120+
for _, nh := range deleted {
1121+
for _, hash := range rawdb.ReadAllHashes(bc.db, nh.number) {
1122+
rawdb.DeleteBlock(batch, hash, nh.number)
1123+
}
1124+
}
1125+
for _, block := range blockChain {
10831126
// Always keep genesis block in active database.
10841127
if block.NumberU64() != 0 {
10851128
for _, hash := range rawdb.ReadAllHashes(bc.db, block.NumberU64()) {

0 commit comments

Comments
 (0)