erigon-pulse/trie/trie_observers.go
Alex Sharov daa359c363
Mgr schedule iterator (#566)
* db based version of PrefixByCumulativeWitnessSize

* db based version of PrefixByCumulativeWitnessSize

* retain all in Trie by default

* fix WitnessLen logic in calcTrie roots

* Rename IntermediateTrieWitnessLenBucket to IntermediateWitnessLenBucket

* handle corner cases in WL

* Use correct incarnation for IH bucket

* use name WitnessSize

* save progress towards db-only witness estimation

* results from trie and from db are still different

* less recursion

* correct incarnation in CumulativeSearch

* reuse results from previous Tick, separate concepts of parent and startKey

* experiment: if not including trie structure to WitnessSize will reduce cumulative error

* tool to generate all IH and tool to calculate assessment of cumulative error

* tool to generate all IH

* Calculate totalWitnessSize based on DB data - then schedule will not overrun state during MGR cycle

* better stats

* Calculate totalWitnessSize based on DB data - then schedule will not overrun state during MGR cycle

* Calculate totalWitnessSize based on DB data - then schedule will not overrun state during MGR cycle

* calculate ticks size distribution

* estimate cumulative error

* fix linter

* resetIH from scratch if needed

* cleanup

* fix test

* fix test
2020-05-28 12:33:05 +01:00

113 lines
3.4 KiB
Go

package trie
import "github.com/ledgerwatch/turbo-geth/common"
type Observer interface {
BranchNodeCreated(hex []byte)
BranchNodeDeleted(hex []byte)
BranchNodeTouched(hex []byte)
CodeNodeCreated(hex []byte, size uint)
CodeNodeDeleted(hex []byte)
CodeNodeTouched(hex []byte)
CodeNodeSizeChanged(hex []byte, newSize uint)
WillUnloadBranchNode(key []byte, nodeHash common.Hash, incarnation uint64, witnessSize uint64)
WillUnloadNode(key []byte, nodeHash common.Hash)
BranchNodeLoaded(prefixAsNibbles []byte, incarnation uint64)
}
var _ Observer = (*NoopObserver)(nil) // make sure that NoopTrieObserver is compliant
// NoopTrieObserver might be used to emulate optional methods in observers
type NoopObserver struct{}
func (*NoopObserver) BranchNodeCreated(_ []byte) {}
func (*NoopObserver) BranchNodeDeleted(_ []byte) {}
func (*NoopObserver) BranchNodeTouched(_ []byte) {}
func (*NoopObserver) CodeNodeCreated(_ []byte, _ uint) {}
func (*NoopObserver) CodeNodeDeleted(_ []byte) {}
func (*NoopObserver) CodeNodeTouched(_ []byte) {}
func (*NoopObserver) CodeNodeSizeChanged(_ []byte, _ uint) {}
func (*NoopObserver) WillUnloadBranchNode(_ []byte, _ common.Hash, _ uint64, _ uint64) {}
func (*NoopObserver) WillUnloadNode(_ []byte, _ common.Hash) {}
func (*NoopObserver) BranchNodeLoaded(_ []byte, _ uint64) {}
// TrieObserverMux multiplies the callback methods and sends them to
// all it's children.
type ObserverMux struct {
children []Observer
}
func NewTrieObserverMux() *ObserverMux {
return &ObserverMux{make([]Observer, 0)}
}
func (mux *ObserverMux) AddChild(child Observer) {
if child == nil {
return
}
mux.children = append(mux.children, child)
}
func (mux *ObserverMux) BranchNodeCreated(hex []byte) {
for _, child := range mux.children {
child.BranchNodeCreated(hex)
}
}
func (mux *ObserverMux) BranchNodeDeleted(hex []byte) {
for _, child := range mux.children {
child.BranchNodeDeleted(hex)
}
}
func (mux *ObserverMux) BranchNodeTouched(hex []byte) {
for _, child := range mux.children {
child.BranchNodeTouched(hex)
}
}
func (mux *ObserverMux) CodeNodeCreated(hex []byte, size uint) {
for _, child := range mux.children {
child.CodeNodeCreated(hex, size)
}
}
func (mux *ObserverMux) CodeNodeDeleted(hex []byte) {
for _, child := range mux.children {
child.CodeNodeDeleted(hex)
}
}
func (mux *ObserverMux) CodeNodeTouched(hex []byte) {
for _, child := range mux.children {
child.CodeNodeTouched(hex)
}
}
func (mux *ObserverMux) CodeNodeSizeChanged(hex []byte, newSize uint) {
for _, child := range mux.children {
child.CodeNodeSizeChanged(hex, newSize)
}
}
func (mux *ObserverMux) WillUnloadNode(key []byte, nodeHash common.Hash) {
for _, child := range mux.children {
child.WillUnloadNode(key, nodeHash)
}
}
func (mux *ObserverMux) WillUnloadBranchNode(key []byte, nodeHash common.Hash, incarnation uint64, witnessSize uint64) {
for _, child := range mux.children {
child.WillUnloadBranchNode(key, nodeHash, incarnation, witnessSize)
}
}
func (mux *ObserverMux) BranchNodeLoaded(prefixAsNibbles []byte, incarnation uint64) {
for _, child := range mux.children {
child.BranchNodeLoaded(prefixAsNibbles, incarnation)
}
}