From 680068584fa1e27f25229907144fcffd606e88c1 Mon Sep 17 00:00:00 2001 From: will-2012 <117156346+will-2012@users.noreply.github.com> Date: Mon, 17 Jun 2024 14:33:11 +0800 Subject: [PATCH] fix: fix truncate failed Co-authored-by: will@2012 --- trie/triedb/pathdb/disklayer.go | 22 ++++++++++++++++++++-- trie/triedb/pathdb/metrics.go | 2 ++ trie/triedb/pathdb/nodebufferlist.go | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/trie/triedb/pathdb/disklayer.go b/trie/triedb/pathdb/disklayer.go index 585cf10034..395bec3f13 100644 --- a/trie/triedb/pathdb/disklayer.go +++ b/trie/triedb/pathdb/disklayer.go @@ -322,17 +322,35 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) { if !force && rawdb.ReadPersistentStateID(dl.db.diskdb) < oldest { force = true } + ancientNumber, _ := ndl.db.freezer.Ancients() + ancientTail, _ := ndl.db.freezer.Tail() + nblAncientSizeGauge.Update(int64(ancientNumber)) + nblAncientTailGauge.Update(int64(ancientTail)) + if err := ndl.buffer.flush(ndl.db.diskdb, ndl.cleans, ndl.id, force); err != nil { return nil, err } // To remove outdated history objects from the end, we set the 'tail' parameter // to 'oldest-1' due to the offset between the freezer index and the history ID. if overflow { - if nl, ok := dl.buffer.(*nodebufferlist); ok { - oldest = nl.persistID - dl.db.config.StateHistory + 1 + if _, ok := dl.buffer.(*nodebufferlist); ok { + persistentId := rawdb.ReadPersistentStateID(dl.db.diskdb) + if persistentId > dl.db.config.StateHistory { + oldest = persistentId - dl.db.config.StateHistory + 1 + log.Info("force prune ancient under nodebufferlist", "disk_persistent_state_id", persistentId, "truncate_tail", oldest) + } else { + log.Info("no prune ancient under nodebufferlist, less db config state history limit") + return ndl, nil + } } + pruned, err := truncateFromTail(ndl.db.diskdb, ndl.db.freezer, oldest-1) + ancientNumber, _ = ndl.db.freezer.Ancients() + ancientTail, _ = ndl.db.freezer.Tail() + nblAncientSizeGauge.Update(int64(ancientNumber)) + nblAncientTailGauge.Update(int64(ancientTail)) if err != nil { + log.Error("Failed to truncate from tail", "ntail", oldest-1) return nil, err } log.Debug("Pruned state history", "items", pruned, "tailid", oldest) diff --git a/trie/triedb/pathdb/metrics.go b/trie/triedb/pathdb/metrics.go index deffa788fd..8d193a4974 100644 --- a/trie/triedb/pathdb/metrics.go +++ b/trie/triedb/pathdb/metrics.go @@ -69,4 +69,6 @@ var ( nblPrePersistIDGauge = metrics.NewRegisteredGauge("pathdb/nbl/prepersistid", nil) nblBaseLayersGauge = metrics.NewRegisteredGauge("pathdb/nbl/baselayers", nil) nblPreBaseLayersGauge = metrics.NewRegisteredGauge("pathdb/nbl/prebaselayers", nil) + nblAncientSizeGauge = metrics.NewRegisteredGauge("pathdb/nbl/ancient/size", nil) + nblAncientTailGauge = metrics.NewRegisteredGauge("pathdb/nbl/ancient/tail", nil) ) diff --git a/trie/triedb/pathdb/nodebufferlist.go b/trie/triedb/pathdb/nodebufferlist.go index cac36d80bc..7f28eac661 100644 --- a/trie/triedb/pathdb/nodebufferlist.go +++ b/trie/triedb/pathdb/nodebufferlist.go @@ -271,6 +271,8 @@ func (nf *nodebufferlist) flush(db ethdb.KeyValueStore, clean *fastcache.Cache, commitFunc := func(buffer *multiDifflayer) bool { if nf.count <= nf.rsevMdNum { + log.Info("Skip force flush bufferlist due to bufferlist is too less", + "bufferlist_count", nf.count, "reserve_multi_difflayer_number", nf.rsevMdNum) return false } if err := nf.base.commit(buffer.root, buffer.id, buffer.block, buffer.layers, buffer.nodes); err != nil {