2020-02-05 13:12:09 +01:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-01-08 15:01:49 +02:00
|
|
|
"sync"
|
|
|
|
|
2020-02-05 13:12:09 +01:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-06 23:14:55 +08:00
|
|
|
// triePrefetchMetricsPrefix is the prefix under which to publish the metrics.
|
2021-01-08 15:01:49 +02:00
|
|
|
triePrefetchMetricsPrefix = "trie/prefetch/"
|
2020-02-05 13:12:09 +01:00
|
|
|
)
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// triePrefetcher is an active prefetcher, which receives accounts or storage
|
|
|
|
// items and does trie-loading of them. The goal is to get as much useful content
|
|
|
|
// into the caches as possible.
|
|
|
|
//
|
|
|
|
// Note, the prefetcher's API is not thread safe.
|
|
|
|
type triePrefetcher struct {
|
2022-06-06 23:14:55 +08:00
|
|
|
db Database // Database to fetch trie nodes through
|
|
|
|
root common.Hash // Root hash of the account trie for metrics
|
2023-09-29 10:46:23 -04:00
|
|
|
fetches map[string]Trie // Partially or fully fetched tries. Only populated for inactive copies.
|
2022-06-06 23:14:55 +08:00
|
|
|
fetchers map[string]*subfetcher // Subfetchers for each trie
|
2021-01-08 15:01:49 +02:00
|
|
|
|
|
|
|
deliveryMissMeter metrics.Meter
|
|
|
|
accountLoadMeter metrics.Meter
|
|
|
|
accountDupMeter metrics.Meter
|
|
|
|
accountSkipMeter metrics.Meter
|
|
|
|
accountWasteMeter metrics.Meter
|
|
|
|
storageLoadMeter metrics.Meter
|
|
|
|
storageDupMeter metrics.Meter
|
|
|
|
storageSkipMeter metrics.Meter
|
|
|
|
storageWasteMeter metrics.Meter
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
func newTriePrefetcher(db Database, root common.Hash, namespace string) *triePrefetcher {
|
|
|
|
prefix := triePrefetchMetricsPrefix + namespace
|
|
|
|
p := &triePrefetcher{
|
|
|
|
db: db,
|
|
|
|
root: root,
|
2022-06-06 23:14:55 +08:00
|
|
|
fetchers: make(map[string]*subfetcher), // Active prefetchers use the fetchers map
|
2021-01-08 15:01:49 +02:00
|
|
|
|
|
|
|
deliveryMissMeter: metrics.GetOrRegisterMeter(prefix+"/deliverymiss", nil),
|
|
|
|
accountLoadMeter: metrics.GetOrRegisterMeter(prefix+"/account/load", nil),
|
|
|
|
accountDupMeter: metrics.GetOrRegisterMeter(prefix+"/account/dup", nil),
|
|
|
|
accountSkipMeter: metrics.GetOrRegisterMeter(prefix+"/account/skip", nil),
|
|
|
|
accountWasteMeter: metrics.GetOrRegisterMeter(prefix+"/account/waste", nil),
|
|
|
|
storageLoadMeter: metrics.GetOrRegisterMeter(prefix+"/storage/load", nil),
|
|
|
|
storageDupMeter: metrics.GetOrRegisterMeter(prefix+"/storage/dup", nil),
|
|
|
|
storageSkipMeter: metrics.GetOrRegisterMeter(prefix+"/storage/skip", nil),
|
|
|
|
storageWasteMeter: metrics.GetOrRegisterMeter(prefix+"/storage/waste", nil),
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
return p
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// close iterates over all the subfetchers, aborts any that were left spinning
|
|
|
|
// and reports the stats to the metrics subsystem.
|
|
|
|
func (p *triePrefetcher) close() {
|
|
|
|
for _, fetcher := range p.fetchers {
|
|
|
|
fetcher.abort() // safe to do multiple times
|
2020-02-05 13:12:09 +01:00
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
if metrics.Enabled {
|
|
|
|
if fetcher.root == p.root {
|
|
|
|
p.accountLoadMeter.Mark(int64(len(fetcher.seen)))
|
|
|
|
p.accountDupMeter.Mark(int64(fetcher.dups))
|
|
|
|
p.accountSkipMeter.Mark(int64(len(fetcher.tasks)))
|
2020-02-05 13:12:09 +01:00
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
for _, key := range fetcher.used {
|
|
|
|
delete(fetcher.seen, string(key))
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
p.accountWasteMeter.Mark(int64(len(fetcher.seen)))
|
2020-02-05 13:12:09 +01:00
|
|
|
} else {
|
2021-01-08 15:01:49 +02:00
|
|
|
p.storageLoadMeter.Mark(int64(len(fetcher.seen)))
|
|
|
|
p.storageDupMeter.Mark(int64(fetcher.dups))
|
|
|
|
p.storageSkipMeter.Mark(int64(len(fetcher.tasks)))
|
|
|
|
|
|
|
|
for _, key := range fetcher.used {
|
|
|
|
delete(fetcher.seen, string(key))
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
p.storageWasteMeter.Mark(int64(len(fetcher.seen)))
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
// Clear out all fetchers (will crash on a second call, deliberate)
|
|
|
|
p.fetchers = nil
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// copy creates a deep-but-inactive copy of the trie prefetcher. Any trie data
|
|
|
|
// already loaded will be copied over, but no goroutines will be started. This
|
|
|
|
// is mostly used in the miner which creates a copy of it's actively mutated
|
|
|
|
// state to be sealed while it may further mutate the state.
|
|
|
|
func (p *triePrefetcher) copy() *triePrefetcher {
|
|
|
|
copy := &triePrefetcher{
|
|
|
|
db: p.db,
|
|
|
|
root: p.root,
|
2022-06-06 23:14:55 +08:00
|
|
|
fetches: make(map[string]Trie), // Active prefetchers use the fetches map
|
2021-01-08 15:01:49 +02:00
|
|
|
|
|
|
|
deliveryMissMeter: p.deliveryMissMeter,
|
|
|
|
accountLoadMeter: p.accountLoadMeter,
|
|
|
|
accountDupMeter: p.accountDupMeter,
|
|
|
|
accountSkipMeter: p.accountSkipMeter,
|
|
|
|
accountWasteMeter: p.accountWasteMeter,
|
|
|
|
storageLoadMeter: p.storageLoadMeter,
|
|
|
|
storageDupMeter: p.storageDupMeter,
|
|
|
|
storageSkipMeter: p.storageSkipMeter,
|
|
|
|
storageWasteMeter: p.storageWasteMeter,
|
|
|
|
}
|
|
|
|
// If the prefetcher is already a copy, duplicate the data
|
|
|
|
if p.fetches != nil {
|
|
|
|
for root, fetch := range p.fetches {
|
2022-09-01 15:34:32 +08:00
|
|
|
if fetch == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
copy.fetches[root] = p.db.CopyTrie(fetch)
|
|
|
|
}
|
|
|
|
return copy
|
|
|
|
}
|
|
|
|
// Otherwise we're copying an active fetcher, retrieve the current states
|
2022-06-06 23:14:55 +08:00
|
|
|
for id, fetcher := range p.fetchers {
|
|
|
|
copy.fetches[id] = fetcher.peek()
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
return copy
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// prefetch schedules a batch of trie items to prefetch.
|
2023-03-23 11:52:22 +01:00
|
|
|
func (p *triePrefetcher) prefetch(owner common.Hash, root common.Hash, addr common.Address, keys [][]byte) {
|
2021-01-08 15:01:49 +02:00
|
|
|
// If the prefetcher is an inactive one, bail out
|
|
|
|
if p.fetches != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Active fetcher, schedule the retrievals
|
2022-06-06 23:14:55 +08:00
|
|
|
id := p.trieID(owner, root)
|
|
|
|
fetcher := p.fetchers[id]
|
2021-01-08 15:01:49 +02:00
|
|
|
if fetcher == nil {
|
2023-03-23 11:52:22 +01:00
|
|
|
fetcher = newSubfetcher(p.db, p.root, owner, root, addr)
|
2022-06-06 23:14:55 +08:00
|
|
|
p.fetchers[id] = fetcher
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
fetcher.schedule(keys)
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// trie returns the trie matching the root hash, or nil if the prefetcher doesn't
|
|
|
|
// have it.
|
2022-06-06 23:14:55 +08:00
|
|
|
func (p *triePrefetcher) trie(owner common.Hash, root common.Hash) Trie {
|
2021-01-08 15:01:49 +02:00
|
|
|
// If the prefetcher is inactive, return from existing deep copies
|
2022-06-06 23:14:55 +08:00
|
|
|
id := p.trieID(owner, root)
|
2021-01-08 15:01:49 +02:00
|
|
|
if p.fetches != nil {
|
2022-06-06 23:14:55 +08:00
|
|
|
trie := p.fetches[id]
|
2021-01-08 15:01:49 +02:00
|
|
|
if trie == nil {
|
|
|
|
p.deliveryMissMeter.Mark(1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p.db.CopyTrie(trie)
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
// Otherwise the prefetcher is active, bail if no trie was prefetched for this root
|
2022-06-06 23:14:55 +08:00
|
|
|
fetcher := p.fetchers[id]
|
2021-01-08 15:01:49 +02:00
|
|
|
if fetcher == nil {
|
|
|
|
p.deliveryMissMeter.Mark(1)
|
|
|
|
return nil
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
// Interrupt the prefetcher if it's by any chance still running and return
|
|
|
|
// a copy of any pre-loaded trie.
|
|
|
|
fetcher.abort() // safe to do multiple times
|
|
|
|
|
|
|
|
trie := fetcher.peek()
|
|
|
|
if trie == nil {
|
|
|
|
p.deliveryMissMeter.Mark(1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return trie
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// used marks a batch of state items used to allow creating statistics as to
|
|
|
|
// how useful or wasteful the prefetcher is.
|
2022-06-06 23:14:55 +08:00
|
|
|
func (p *triePrefetcher) used(owner common.Hash, root common.Hash, used [][]byte) {
|
|
|
|
if fetcher := p.fetchers[p.trieID(owner, root)]; fetcher != nil {
|
2021-01-08 15:01:49 +02:00
|
|
|
fetcher.used = used
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 23:14:55 +08:00
|
|
|
// trieID returns an unique trie identifier consists the trie owner and root hash.
|
|
|
|
func (p *triePrefetcher) trieID(owner common.Hash, root common.Hash) string {
|
2023-09-29 10:46:23 -04:00
|
|
|
trieID := make([]byte, common.HashLength*2)
|
|
|
|
copy(trieID, owner.Bytes())
|
|
|
|
copy(trieID[common.HashLength:], root.Bytes())
|
|
|
|
return string(trieID)
|
2022-06-06 23:14:55 +08:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// subfetcher is a trie fetcher goroutine responsible for pulling entries for a
|
|
|
|
// single trie. It is spawned when a new root is encountered and lives until the
|
|
|
|
// main prefetcher is paused and either all requested items are processed or if
|
|
|
|
// the trie being worked on is retrieved from the prefetcher.
|
|
|
|
type subfetcher struct {
|
2023-03-23 11:52:22 +01:00
|
|
|
db Database // Database to load trie nodes through
|
|
|
|
state common.Hash // Root hash of the state to prefetch
|
|
|
|
owner common.Hash // Owner of the trie, usually account hash
|
|
|
|
root common.Hash // Root hash of the trie to prefetch
|
|
|
|
addr common.Address // Address of the account that the trie belongs to
|
|
|
|
trie Trie // Trie being populated with nodes
|
2021-01-08 15:01:49 +02:00
|
|
|
|
|
|
|
tasks [][]byte // Items queued up for retrieval
|
|
|
|
lock sync.Mutex // Lock protecting the task queue
|
|
|
|
|
|
|
|
wake chan struct{} // Wake channel if a new task is scheduled
|
|
|
|
stop chan struct{} // Channel to interrupt processing
|
2022-08-19 01:00:21 -05:00
|
|
|
term chan struct{} // Channel to signal interruption
|
2021-01-08 15:01:49 +02:00
|
|
|
copy chan chan Trie // Channel to request a copy of the current trie
|
|
|
|
|
|
|
|
seen map[string]struct{} // Tracks the entries already loaded
|
|
|
|
dups int // Number of duplicate preload tasks
|
|
|
|
used [][]byte // Tracks the entries used in the end
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSubfetcher creates a goroutine to prefetch state items belonging to a
|
|
|
|
// particular root hash.
|
2023-03-23 11:52:22 +01:00
|
|
|
func newSubfetcher(db Database, state common.Hash, owner common.Hash, root common.Hash, addr common.Address) *subfetcher {
|
2021-01-08 15:01:49 +02:00
|
|
|
sf := &subfetcher{
|
2022-06-06 23:14:55 +08:00
|
|
|
db: db,
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 16:01:02 +08:00
|
|
|
state: state,
|
2022-06-06 23:14:55 +08:00
|
|
|
owner: owner,
|
|
|
|
root: root,
|
2023-03-23 11:52:22 +01:00
|
|
|
addr: addr,
|
2022-06-06 23:14:55 +08:00
|
|
|
wake: make(chan struct{}, 1),
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
term: make(chan struct{}),
|
|
|
|
copy: make(chan chan Trie),
|
|
|
|
seen: make(map[string]struct{}),
|
2021-01-08 15:01:49 +02:00
|
|
|
}
|
|
|
|
go sf.loop()
|
|
|
|
return sf
|
|
|
|
}
|
|
|
|
|
|
|
|
// schedule adds a batch of trie keys to the queue to prefetch.
|
|
|
|
func (sf *subfetcher) schedule(keys [][]byte) {
|
|
|
|
// Append the tasks to the current queue
|
|
|
|
sf.lock.Lock()
|
|
|
|
sf.tasks = append(sf.tasks, keys...)
|
|
|
|
sf.lock.Unlock()
|
|
|
|
|
|
|
|
// Notify the prefetcher, it's fine if it's already terminated
|
2020-02-05 13:12:09 +01:00
|
|
|
select {
|
2021-01-08 15:01:49 +02:00
|
|
|
case sf.wake <- struct{}{}:
|
2020-02-05 13:12:09 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// peek tries to retrieve a deep copy of the fetcher's trie in whatever form it
|
|
|
|
// is currently.
|
|
|
|
func (sf *subfetcher) peek() Trie {
|
|
|
|
ch := make(chan Trie)
|
|
|
|
select {
|
|
|
|
case sf.copy <- ch:
|
|
|
|
// Subfetcher still alive, return copy from it
|
|
|
|
return <-ch
|
|
|
|
|
|
|
|
case <-sf.term:
|
|
|
|
// Subfetcher already terminated, return a copy directly
|
|
|
|
if sf.trie == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return sf.db.CopyTrie(sf.trie)
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// abort interrupts the subfetcher immediately. It is safe to call abort multiple
|
|
|
|
// times but it is not thread safe.
|
|
|
|
func (sf *subfetcher) abort() {
|
2020-02-05 13:12:09 +01:00
|
|
|
select {
|
2021-01-08 15:01:49 +02:00
|
|
|
case <-sf.stop:
|
2020-02-05 13:12:09 +01:00
|
|
|
default:
|
2021-01-08 15:01:49 +02:00
|
|
|
close(sf.stop)
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
2021-01-08 15:01:49 +02:00
|
|
|
<-sf.term
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 15:01:49 +02:00
|
|
|
// loop waits for new tasks to be scheduled and keeps loading them until it runs
|
|
|
|
// out of tasks or its underlying trie is retrieved for committing.
|
|
|
|
func (sf *subfetcher) loop() {
|
|
|
|
// No matter how the loop stops, signal anyone waiting that it's terminated
|
|
|
|
defer close(sf.term)
|
|
|
|
|
|
|
|
// Start by opening the trie and stop processing if it fails
|
2022-06-06 23:14:55 +08:00
|
|
|
if sf.owner == (common.Hash{}) {
|
|
|
|
trie, err := sf.db.OpenTrie(sf.root)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Trie prefetcher failed opening trie", "root", sf.root, "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sf.trie = trie
|
|
|
|
} else {
|
2023-11-14 13:09:40 +01:00
|
|
|
// The trie argument can be nil as verkle doesn't support prefetching
|
|
|
|
// yet. TODO FIX IT(rjl493456442), otherwise code will panic here.
|
|
|
|
trie, err := sf.db.OpenStorageTrie(sf.state, sf.addr, sf.root, nil)
|
2022-06-06 23:14:55 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Trie prefetcher failed opening trie", "root", sf.root, "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sf.trie = trie
|
2021-01-08 15:01:49 +02:00
|
|
|
}
|
|
|
|
// Trie opened successfully, keep prefetching items
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sf.wake:
|
|
|
|
// Subfetcher was woken up, retrieve any tasks to avoid spinning the lock
|
|
|
|
sf.lock.Lock()
|
|
|
|
tasks := sf.tasks
|
|
|
|
sf.tasks = nil
|
|
|
|
sf.lock.Unlock()
|
|
|
|
|
|
|
|
// Prefetch any tasks until the loop is interrupted
|
|
|
|
for i, task := range tasks {
|
|
|
|
select {
|
|
|
|
case <-sf.stop:
|
|
|
|
// If termination is requested, add any leftover back and return
|
|
|
|
sf.lock.Lock()
|
|
|
|
sf.tasks = append(sf.tasks, tasks[i:]...)
|
|
|
|
sf.lock.Unlock()
|
|
|
|
return
|
|
|
|
|
|
|
|
case ch := <-sf.copy:
|
|
|
|
// Somebody wants a copy of the current trie, grant them
|
|
|
|
ch <- sf.db.CopyTrie(sf.trie)
|
|
|
|
|
|
|
|
default:
|
|
|
|
// No termination request yet, prefetch the next entry
|
2021-07-12 21:34:20 +02:00
|
|
|
if _, ok := sf.seen[string(task)]; ok {
|
2021-01-08 15:01:49 +02:00
|
|
|
sf.dups++
|
|
|
|
} else {
|
2023-03-23 11:52:22 +01:00
|
|
|
if len(task) == common.AddressLength {
|
2023-03-27 10:48:46 +02:00
|
|
|
sf.trie.GetAccount(common.BytesToAddress(task))
|
2023-03-23 11:52:22 +01:00
|
|
|
} else {
|
2023-03-27 10:48:46 +02:00
|
|
|
sf.trie.GetStorage(sf.addr, task)
|
2023-03-23 11:52:22 +01:00
|
|
|
}
|
2021-07-12 21:34:20 +02:00
|
|
|
sf.seen[string(task)] = struct{}{}
|
2021-01-08 15:01:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case ch := <-sf.copy:
|
|
|
|
// Somebody wants a copy of the current trie, grant them
|
|
|
|
ch <- sf.db.CopyTrie(sf.trie)
|
|
|
|
|
|
|
|
case <-sf.stop:
|
|
|
|
// Termination is requested, abort and leave remaining tasks
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-02-05 13:12:09 +01:00
|
|
|
}
|