Skip to content

Commit 76700ac

Browse files
authored
core/state/pruner: move the compaction out of the pruning procedure (#22579)
The main idea behind it is: the range compaction is very expensive which can take a few hours to finish. During this long procedure, a lot of exceptions can occur, e.g. - Geth is killed manually - Geth is killed because of machine crash - etc In order to minimize the effect of the exceptions, the compaction is moved out of the pruning. So that even the compaction is not finished, the pruning is regarded as done.
1 parent 27056f6 commit 76700ac

1 file changed

Lines changed: 22 additions & 46 deletions

File tree

‎core/state/pruner/pruner.go‎

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func NewPruner(db ethdb.Database, datadir, trieCachePath string, bloomSize uint6
113113
}, nil
114114
}
115115

116-
func prune(maindb ethdb.Database, stateBloom *stateBloom, middleStateRoots map[common.Hash]struct{}, start time.Time) error {
116+
func prune(snaptree *snapshot.Tree, root common.Hash, maindb ethdb.Database, stateBloom *stateBloom, bloomPath string, middleStateRoots map[common.Hash]struct{}, start time.Time) error {
117117
// Delete all stale trie nodes in the disk. With the help of state bloom
118118
// the trie nodes(and codes) belong to the active state will be filtered
119119
// out. A very small part of stale tries will also be filtered because of
@@ -186,6 +186,25 @@ func prune(maindb ethdb.Database, stateBloom *stateBloom, middleStateRoots map[c
186186
iter.Release()
187187
log.Info("Pruned state data", "nodes", count, "size", size, "elapsed", common.PrettyDuration(time.Since(pstart)))
188188

189+
// Pruning is done, now drop the "useless" layers from the snapshot.
190+
// Firstly, flushing the target layer into the disk. After that all
191+
// diff layers below the target will all be merged into the disk.
192+
if err := snaptree.Cap(root, 0); err != nil {
193+
return err
194+
}
195+
// Secondly, flushing the snapshot journal into the disk. All diff
196+
// layers upon are dropped silently. Eventually the entire snapshot
197+
// tree is converted into a single disk layer with the pruning target
198+
// as the root.
199+
if _, err := snaptree.Journal(root); err != nil {
200+
return err
201+
}
202+
// Delete the state bloom, it marks the entire pruning procedure is
203+
// finished. If any crashes or manual exit happens before this,
204+
// `RecoverPruning` will pick it up in the next restarts to redo all
205+
// the things.
206+
os.RemoveAll(bloomPath)
207+
189208
// Start compactions, will remove the deleted data from the disk immediately.
190209
// Note for small pruning, the compaction is skipped.
191210
if count >= rangeCompactionThreshold {
@@ -314,29 +333,7 @@ func (p *Pruner) Prune(root common.Hash) error {
314333
return err
315334
}
316335
log.Info("State bloom filter committed", "name", filterName)
317-
318-
if err := prune(p.db, p.stateBloom, middleRoots, start); err != nil {
319-
return err
320-
}
321-
// Pruning is done, now drop the "useless" layers from the snapshot.
322-
// Firstly, flushing the target layer into the disk. After that all
323-
// diff layers below the target will all be merged into the disk.
324-
if err := p.snaptree.Cap(root, 0); err != nil {
325-
return err
326-
}
327-
// Secondly, flushing the snapshot journal into the disk. All diff
328-
// layers upon the target layer are dropped silently. Eventually the
329-
// entire snapshot tree is converted into a single disk layer with
330-
// the pruning target as the root.
331-
if _, err := p.snaptree.Journal(root); err != nil {
332-
return err
333-
}
334-
// Delete the state bloom, it marks the entire pruning procedure is
335-
// finished. If any crashes or manual exit happens before this,
336-
// `RecoverPruning` will pick it up in the next restarts to redo all
337-
// the things.
338-
os.RemoveAll(filterName)
339-
return nil
336+
return prune(p.snaptree, root, p.db, p.stateBloom, filterName, middleRoots, start)
340337
}
341338

342339
// RecoverPruning will resume the pruning procedure during the system restart.
@@ -400,28 +397,7 @@ func RecoverPruning(datadir string, db ethdb.Database, trieCachePath string) err
400397
log.Error("Pruning target state is not existent")
401398
return errors.New("non-existent target state")
402399
}
403-
if err := prune(db, stateBloom, middleRoots, time.Now()); err != nil {
404-
return err
405-
}
406-
// Pruning is done, now drop the "useless" layers from the snapshot.
407-
// Firstly, flushing the target layer into the disk. After that all
408-
// diff layers below the target will all be merged into the disk.
409-
if err := snaptree.Cap(stateBloomRoot, 0); err != nil {
410-
return err
411-
}
412-
// Secondly, flushing the snapshot journal into the disk. All diff
413-
// layers upon are dropped silently. Eventually the entire snapshot
414-
// tree is converted into a single disk layer with the pruning target
415-
// as the root.
416-
if _, err := snaptree.Journal(stateBloomRoot); err != nil {
417-
return err
418-
}
419-
// Delete the state bloom, it marks the entire pruning procedure is
420-
// finished. If any crashes or manual exit happens before this,
421-
// `RecoverPruning` will pick it up in the next restarts to redo all
422-
// the things.
423-
os.RemoveAll(stateBloomPath)
424-
return nil
400+
return prune(snaptree, stateBloomRoot, db, stateBloom, stateBloomPath, middleRoots, time.Now())
425401
}
426402

427403
// extractGenesis loads the genesis state and commits all the state entries

0 commit comments

Comments
 (0)