Skip to content

Commit

Permalink
Merge pull request #17 from 0chain/debug/mpt-growth
Browse files Browse the repository at this point in the history
Fix MPT state size grows issue
  • Loading branch information
dabasov authored Oct 7, 2023
2 parents 472780d + 2d4735f commit 77baa17
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 6 additions & 6 deletions core/util/merkle_patricia_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,14 +752,14 @@ func (mpt *MerklePatriciaTrie) deleteAfterPathTraversal(node Node) (Node, Key, e
// The value of the branch needs to be updated
nnode := nodeImpl.Clone().(*FullNode)
nnode.SetValue(nil)
if nodeImpl.HasValue() {
mpt.ChangeCollector.DeleteChange(nodeImpl.Value)
}
// if nodeImpl.HasValue() {
// mpt.ChangeCollector.DeleteChange(nodeImpl.Value)
// }
return mpt.insertNode(node, nnode)
case *LeafNode:
if nodeImpl.HasValue() {
mpt.ChangeCollector.DeleteChange(nodeImpl.Value)
}
// if nodeImpl.HasValue() {
// mpt.ChangeCollector.DeleteChange(nodeImpl.Value)
// }
if err := mpt.deleteNode(node); err != nil {
return nil, nil, err
}
Expand Down
22 changes: 20 additions & 2 deletions core/util/mpt_pnodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func newDefaultCFOptions(logDir string) *grocksdb.Options {
opts := grocksdb.NewDefaultOptions()
opts.SetCreateIfMissing(true)
opts.SetCompression(PNodeDBCompression)
opts.SetCreateIfMissingColumnFamilies(true)
opts.OptimizeUniversalStyleCompaction(64 * 1024 * 1024)
if sstType == SSTTypePlainTable {
opts.SetAllowMmapReads(true)
Expand All @@ -61,6 +62,7 @@ func newDefaultCFOptions(logDir string) *grocksdb.Options {
opts.IncreaseParallelism(2) // pruning and saving happen in parallel
opts.SetDbLogDir(logDir)
opts.EnableStatistics()
opts.SetDeleteObsoleteFilesPeriodMicros(uint64(10 * time.Minute.Microseconds()))

return opts
}
Expand All @@ -78,6 +80,7 @@ func newDeadNodesCFOptions() *grocksdb.Options {
opts.SetMaxWriteBufferNumber(4) // default was 2, double to 4
opts.SetWriteBufferSize(128 * 1024 * 1024) // default was 64M, double to 128M
opts.SetMinWriteBufferNumberToMerge(2) // default was 1, double to 2
opts.SetDeleteObsoleteFilesPeriodMicros(uint64(10 * time.Minute.Microseconds()))
return opts
}

Expand All @@ -100,7 +103,7 @@ func NewPNodeDB(stateDir, logDir string) (*PNodeDB, error) {
cfsOpts = []*grocksdb.Options{defaultCFOpts, deadNodesOpts}
)

db, cfhs, err := grocksdb.OpenDbColumnFamilies(newDBOptions(), stateDir, cfs, cfsOpts)
db, cfhs, err := grocksdb.OpenDbColumnFamilies(defaultCFOpts, stateDir, cfs, cfsOpts)
if err != nil {
return nil, err
}
Expand All @@ -119,6 +122,12 @@ func NewPNodeDB(stateDir, logDir string) (*PNodeDB, error) {
}, nil
}

func (pndb *PNodeDB) EstimateSize() (string, string) {
def := pndb.db.GetPropertyCF("rocksdb.estimate-num-keys", pndb.defaultCFH)
dd := pndb.db.GetPropertyCF("rocksdb.estimate-num-keys", pndb.deadNodesCFH)
return def, dd
}

/*GetNode - implement interface */
func (pndb *PNodeDB) GetNode(key Key) (Node, error) {
data, err := pndb.db.Get(pndb.ro, key)
Expand Down Expand Up @@ -225,7 +234,6 @@ func (pndb *PNodeDB) PruneBelowVersion(ctx context.Context, version int64) error
return true // continue
}
ns = append(ns, kk)
count++
}

deadNodesC <- deadNodesRecord{
Expand All @@ -241,6 +249,15 @@ func (pndb *PNodeDB) PruneBelowVersion(ctx context.Context, version int64) error
select {
case dn, ok := <-deadNodesC:
if !ok {
if len(keys) > 0 {
if err := pndb.MultiDeleteNode(keys); err != nil {
return err
}

count += int64(len(keys))
keys = keys[:0]
}

if err := pndb.multiDeleteDeadNodes(pruneRounds); err != nil {
return err
}
Expand All @@ -263,6 +280,7 @@ func (pndb *PNodeDB) PruneBelowVersion(ctx context.Context, version int64) error
return err
}

count += int64(len(keys))
keys = keys[:0]
}
}
Expand Down

0 comments on commit 77baa17

Please sign in to comment.