Skip to content

Commit

Permalink
fix: optimize nodebufferlist fast recovery flush process
Browse files Browse the repository at this point in the history
  • Loading branch information
VM committed Jan 20, 2025
1 parent 4b791a7 commit f3f1b32
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
3 changes: 2 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ var defaultCacheConfig = &CacheConfig{
func DefaultCacheConfigWithScheme(scheme string) *CacheConfig {
config := *defaultCacheConfig
config.StateScheme = scheme
config.UseBase = true
return &config
}

Expand Down Expand Up @@ -2084,7 +2085,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
if bc.snaps != nil && !minerMode {
snapDiffItems, snapBufItems = bc.snaps.Size()
}

var trieDiffNodes, trieBufNodes, trieImmutableBufNodes common.StorageSize
if !minerMode {
trieDiffNodes, trieBufNodes, trieImmutableBufNodes, _ = bc.triedb.Size()
Expand Down
27 changes: 19 additions & 8 deletions triedb/pathdb/nodebufferlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (

// DefaultReserveMultiDifflayerNumber defines the default reserve number of multiDifflayer in nodebufferlist.
DefaultReserveMultiDifflayerNumber = 3

// The max batch size of pebble cannot exceed 4GB, so set maxNodeBufferListSize to 3GB.
maxNodeBufferListSize = 3221225472
)

type KeepRecord struct {
Expand Down Expand Up @@ -225,11 +228,24 @@ func (nf *nodebufferlist) recoverNodeBufferList(freezer *rawdb.ResettableFreezer
nf.size += current.size
nf.layers += current.layers
}
nf.diffToBase(true)

log.Info("Before diffToBase", "base_size", nf.base.size, "tail_state_id", nf.tail.id, "head_state_id", nf.head.id,
"nbl_layers", nf.layers, "base_layers", nf.base.layers, "nf_count", nf.count, "node buffer size", nf.size)

if nf.size >= maxNodeBufferListSize && nf.layers == DefaultReserveMultiDifflayerNumber {
// avoid diff size exceeding max pebble batch size limit, force flush buffer to base
log.Info("node buffer size exceeds 3GB", "node buffer size", nf.size)
nf.diffToBase(true)
} else {
nf.diffToBase(false)
}

log.Info("After diffToBase", "base_size", nf.base.size, "tail_state_id", nf.tail.id,
"head_state_id", nf.head.id, "nbl_layers", nf.layers, "base_layers", nf.base.layers, "nf_count", nf.count, "node buffer size", nf.size)
nf.backgroundFlush()

log.Info("Succeed to recover node buffer list", "base_size", nf.base.size, "tail_state_id", nf.tail.id,
"head_state_id", nf.head.id, "nbl_layers", nf.layers, "base_layers", nf.base.layers)
"head_state_id", nf.head.id, "nbl_layers", nf.layers, "base_layers", nf.base.layers, "nf_count", nf.count, "node buffer size", nf.size)
return nil
}

Expand Down Expand Up @@ -679,17 +695,13 @@ func (nf *nodebufferlist) traverseReverse(cb func(*multiDifflayer) bool) {
// base node buffer, if up to limit size and flush to disk. It is called
// periodically in the background
func (nf *nodebufferlist) diffToBase(skipCountCheck bool) {
count := 0
commitFunc := func(buffer *multiDifflayer) bool {
if nf.base.size >= nf.base.limit {
log.Debug("base node buffer need write disk immediately")
return false
}
if skipCountCheck && count == 1 { // only force flush one buffer to base
return false
}
if !skipCountCheck {
// when using fast recovery, force flush one buffer to base to avoid exceeding pebble batch size limit
// when using fast recovery, force flush buffer to base to avoid exceeding pebble batch size limit
if nf.count <= nf.rsevMdNum {
log.Debug("node buffer list less, waiting more difflayer to be committed")
return false
Expand Down Expand Up @@ -733,7 +745,6 @@ func (nf *nodebufferlist) diffToBase(skipCountCheck bool) {
baseNodeBufferDifflayerAvgSize.Update(int64(nf.base.size / nf.base.layers))
}
nf.report()
count++

return true
}
Expand Down

0 comments on commit f3f1b32

Please sign in to comment.