@ -17,6 +17,7 @@
package state
package state
import (
import (
"errors"
"sync"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
@ -27,6 +28,10 @@ import (
var (
var (
// triePrefetchMetricsPrefix is the prefix under which to publish the metrics.
// triePrefetchMetricsPrefix is the prefix under which to publish the metrics.
triePrefetchMetricsPrefix = "trie/prefetch/"
triePrefetchMetricsPrefix = "trie/prefetch/"
// errTerminated is returned if a fetcher is attempted to be operated after it
// has already terminated.
errTerminated = errors . New ( "fetcher is already terminated" )
)
)
// triePrefetcher is an active prefetcher, which receives accounts or storage
// triePrefetcher is an active prefetcher, which receives accounts or storage
@ -37,160 +42,126 @@ var (
type triePrefetcher struct {
type triePrefetcher struct {
db Database // Database to fetch trie nodes through
db Database // Database to fetch trie nodes through
root common . Hash // Root hash of the account trie for metrics
root common . Hash // Root hash of the account trie for metrics
fetches map [ string ] Trie // Partially or fully fetched tries. Only populated for inactive copies.
fetchers map [ string ] * subfetcher // Subfetchers for each trie
fetchers map [ string ] * subfetcher // Subfetchers for each trie
term chan struct { } // Channel to signal interruption
deliveryMissMeter metrics . Meter
deliveryMissMeter metrics . Meter
accountLoadMeter metrics . Meter
accountLoadMeter metrics . Meter
accountDupMeter metrics . Meter
accountDupMeter metrics . Meter
accountSkipMeter metrics . Meter
accountWasteMeter metrics . Meter
accountWasteMeter metrics . Meter
storageLoadMeter metrics . Meter
storageLoadMeter metrics . Meter
storageDupMeter metrics . Meter
storageDupMeter metrics . Meter
storageSkipMeter metrics . Meter
storageWasteMeter metrics . Meter
storageWasteMeter metrics . Meter
}
}
func newTriePrefetcher ( db Database , root common . Hash , namespace string ) * triePrefetcher {
func newTriePrefetcher ( db Database , root common . Hash , namespace string ) * triePrefetcher {
prefix := triePrefetchMetricsPrefix + namespace
prefix := triePrefetchMetricsPrefix + namespace
p := & triePrefetcher {
return & triePrefetcher {
db : db ,
db : db ,
root : root ,
root : root ,
fetchers : make ( map [ string ] * subfetcher ) , // Active prefetchers use the fetchers map
fetchers : make ( map [ string ] * subfetcher ) , // Active prefetchers use the fetchers map
term : make ( chan struct { } ) ,
deliveryMissMeter : metrics . GetOrRegisterMeter ( prefix + "/deliverymiss" , nil ) ,
deliveryMissMeter : metrics . GetOrRegisterMeter ( prefix + "/deliverymiss" , nil ) ,
accountLoadMeter : metrics . GetOrRegisterMeter ( prefix + "/account/load" , nil ) ,
accountLoadMeter : metrics . GetOrRegisterMeter ( prefix + "/account/load" , nil ) ,
accountDupMeter : metrics . GetOrRegisterMeter ( prefix + "/account/dup" , nil ) ,
accountDupMeter : metrics . GetOrRegisterMeter ( prefix + "/account/dup" , nil ) ,
accountSkipMeter : metrics . GetOrRegisterMeter ( prefix + "/account/skip" , nil ) ,
accountWasteMeter : metrics . GetOrRegisterMeter ( prefix + "/account/waste" , nil ) ,
accountWasteMeter : metrics . GetOrRegisterMeter ( prefix + "/account/waste" , nil ) ,
storageLoadMeter : metrics . GetOrRegisterMeter ( prefix + "/storage/load" , nil ) ,
storageLoadMeter : metrics . GetOrRegisterMeter ( prefix + "/storage/load" , nil ) ,
storageDupMeter : metrics . GetOrRegisterMeter ( prefix + "/storage/dup" , nil ) ,
storageDupMeter : metrics . GetOrRegisterMeter ( prefix + "/storage/dup" , nil ) ,
storageSkipMeter : metrics . GetOrRegisterMeter ( prefix + "/storage/skip" , nil ) ,
storageWasteMeter : metrics . GetOrRegisterMeter ( prefix + "/storage/waste" , nil ) ,
storageWasteMeter : metrics . GetOrRegisterMeter ( prefix + "/storage/waste" , nil ) ,
}
}
return p
}
}
// close iterates over all the subfetchers, aborts any that were left spinning
// terminate iterates over all the subfetchers and issues a terminateion request
// and reports the stats to the metrics subsystem.
// to all of them. Depending on the async parameter, the method will either block
func ( p * triePrefetcher ) close ( ) {
// until all subfetchers spin down, or return immediately.
func ( p * triePrefetcher ) terminate ( async bool ) {
// Short circuit if the fetcher is already closed
select {
case <- p . term :
return
default :
}
// Termiante all sub-fetchers, sync or async, depending on the request
for _ , fetcher := range p . fetchers {
for _ , fetcher := range p . fetchers {
fetcher . abort ( ) // safe to do multiple times
fetcher . terminate ( async )
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 ) ) )
for _ , key := range fetcher . used {
delete ( fetcher . seen , string ( key ) )
}
p . accountWasteMeter . Mark ( int64 ( len ( fetcher . seen ) ) )
} else {
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 ) )
}
p . storageWasteMeter . Mark ( int64 ( len ( fetcher . seen ) ) )
}
}
}
}
// Clear out all fetchers (will crash on a second call, deliberate)
close ( p . term )
p . fetchers = nil
}
}
// copy creates a deep-but-inactive copy of the trie prefetcher. Any trie data
// report aggregates the pre-fetching and usage metrics and reports them.
// already loaded will be copied over, but no goroutines will be started. This
func ( p * triePrefetcher ) report ( ) {
// is mostly used in the miner which creates a copy of it's actively mutated
if ! metrics . Enabled {
// state to be sealed while it may further mutate the state.
return
func ( p * triePrefetcher ) copy ( ) * triePrefetcher {
copy := & triePrefetcher {
db : p . db ,
root : p . root ,
fetches : make ( map [ string ] Trie ) , // Active prefetchers use the fetches map
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
for _ , fetcher := range p . fetchers {
if p . fetches != nil {
fetcher . wait ( ) // ensure the fetcher's idle before poking in its internals
for root , fetch := range p . fetches {
if fetch == nil {
if fetcher . root == p . root {
continue
p . accountLoadMeter . Mark ( int64 ( len ( fetcher . seen ) ) )
p . accountDupMeter . Mark ( int64 ( fetcher . dups ) )
for _ , key := range fetcher . used {
delete ( fetcher . seen , string ( key ) )
}
}
copy . fetches [ root ] = p . db . CopyTrie ( fetch )
p . accountWasteMeter . Mark ( int64 ( len ( fetcher . seen ) ) )
} else {
p . storageLoadMeter . Mark ( int64 ( len ( fetcher . seen ) ) )
p . storageDupMeter . Mark ( int64 ( fetcher . dups ) )
for _ , key := range fetcher . used {
delete ( fetcher . seen , string ( key ) )
}
p . storageWasteMeter . Mark ( int64 ( len ( fetcher . seen ) ) )
}
}
return copy
}
// Otherwise we're copying an active fetcher, retrieve the current states
for id , fetcher := range p . fetchers {
copy . fetches [ id ] = fetcher . peek ( )
}
}
return copy
}
}
// prefetch schedules a batch of trie items to prefetch.
// prefetch schedules a batch of trie items to prefetch. After the prefetcher is
func ( p * triePrefetcher ) prefetch ( owner common . Hash , root common . Hash , addr common . Address , keys [ ] [ ] byte ) {
// closed, all the following tasks scheduled will not be executed and an error
// If the prefetcher is an inactive one, bail out
// will be returned.
if p . fetches != nil {
//
return
// prefetch is called from two locations:
//
// 1. Finalize of the state-objects storage roots. This happens at the end
// of every transaction, meaning that if several transactions touches
// upon the same contract, the parameters invoking this method may be
// repeated.
// 2. Finalize of the main account trie. This happens only once per block.
func ( p * triePrefetcher ) prefetch ( owner common . Hash , root common . Hash , addr common . Address , keys [ ] [ ] byte ) error {
// Ensure the subfetcher is still alive
select {
case <- p . term :
return errTerminated
default :
}
}
// Active fetcher, schedule the retrievals
id := p . trieID ( owner , root )
id := p . trieID ( owner , root )
fetcher := p . fetchers [ id ]
fetcher := p . fetchers [ id ]
if fetcher == nil {
if fetcher == nil {
fetcher = newSubfetcher ( p . db , p . root , owner , root , addr )
fetcher = newSubfetcher ( p . db , p . root , owner , root , addr )
p . fetchers [ id ] = fetcher
p . fetchers [ id ] = fetcher
}
}
fetcher . schedule ( keys )
return fetcher . schedule ( keys )
}
}
// trie returns the trie matching the root hash, or nil if the prefetcher doesn't
// trie returns the trie matching the root hash, blocking until the fetcher of
// have it.
// the given trie terminates. If no fetcher exists for the request, nil will be
func ( p * triePrefetcher ) trie ( owner common . Hash , root common . Hash ) Trie {
// returned.
// If the prefetcher is inactive, return from existing deep copies
func ( p * triePrefetcher ) trie ( owner common . Hash , root common . Hash ) ( Trie , error ) {
id := p . trieID ( owner , root )
// Bail if no trie was prefetched for this root
if p . fetches != nil {
fetcher := p . fetchers [ p . trieID ( owner , root ) ]
trie := p . fetches [ id ]
if trie == nil {
p . deliveryMissMeter . Mark ( 1 )
return nil
}
return p . db . CopyTrie ( trie )
}
// Otherwise the prefetcher is active, bail if no trie was prefetched for this root
fetcher := p . fetchers [ id ]
if fetcher == nil {
if fetcher == nil {
log . Error ( "Prefetcher missed to load trie" , "owner" , owner , "root" , root )
p . deliveryMissMeter . Mark ( 1 )
p . deliveryMissMeter . Mark ( 1 )
return nil
return nil , nil
}
}
// Interrupt the prefetcher if it's by any chance still running and return
// Subfetcher exists, retrieve its trie
// a copy of any pre-loaded trie.
return fetcher . peek ( ) , nil
fetcher . abort ( ) // safe to do multiple times
trie := fetcher . peek ( )
if trie == nil {
p . deliveryMissMeter . Mark ( 1 )
return nil
}
return trie
}
}
// used marks a batch of state items used to allow creating statistics as to
// used marks a batch of state items used to allow creating statistics as to
// how useful or wasteful the pre fetcher is.
// how useful or wasteful the fetcher is.
func ( p * triePrefetcher ) used ( owner common . Hash , root common . Hash , used [ ] [ ] byte ) {
func ( p * triePrefetcher ) used ( owner common . Hash , root common . Hash , used [ ] [ ] byte ) {
if fetcher := p . fetchers [ p . trieID ( owner , root ) ] ; fetcher != nil {
if fetcher := p . fetchers [ p . trieID ( owner , root ) ] ; fetcher != nil {
fetcher . wait ( ) // ensure the fetcher's idle before poking in its internals
fetcher . used = used
fetcher . used = used
}
}
}
}
@ -218,10 +189,9 @@ type subfetcher struct {
tasks [ ] [ ] byte // Items queued up for retrieval
tasks [ ] [ ] byte // Items queued up for retrieval
lock sync . Mutex // Lock protecting the task queue
lock sync . Mutex // Lock protecting the task queue
wake chan struct { } // Wake channel if a new task is scheduled
wake chan struct { } // Wake channel if a new task is scheduled
stop chan struct { } // Channel to interrupt processing
stop chan struct { } // Channel to interrupt processing
term chan struct { } // Channel to signal interruption
term chan struct { } // Channel to signal interruption
copy chan chan Trie // Channel to request a copy of the current trie
seen map [ string ] struct { } // Tracks the entries already loaded
seen map [ string ] struct { } // Tracks the entries already loaded
dups int // Number of duplicate preload tasks
dups int // Number of duplicate preload tasks
@ -240,7 +210,6 @@ func newSubfetcher(db Database, state common.Hash, owner common.Hash, root commo
wake : make ( chan struct { } , 1 ) ,
wake : make ( chan struct { } , 1 ) ,
stop : make ( chan struct { } ) ,
stop : make ( chan struct { } ) ,
term : make ( chan struct { } ) ,
term : make ( chan struct { } ) ,
copy : make ( chan chan Trie ) ,
seen : make ( map [ string ] struct { } ) ,
seen : make ( map [ string ] struct { } ) ,
}
}
go sf . loop ( )
go sf . loop ( )
@ -248,50 +217,61 @@ func newSubfetcher(db Database, state common.Hash, owner common.Hash, root commo
}
}
// schedule adds a batch of trie keys to the queue to prefetch.
// schedule adds a batch of trie keys to the queue to prefetch.
func ( sf * subfetcher ) schedule ( keys [ ] [ ] byte ) {
func ( sf * subfetcher ) schedule ( keys [ ] [ ] byte ) error {
// Ensure the subfetcher is still alive
select {
case <- sf . term :
return errTerminated
default :
}
// Append the tasks to the current queue
// Append the tasks to the current queue
sf . lock . Lock ( )
sf . lock . Lock ( )
sf . tasks = append ( sf . tasks , keys ... )
sf . tasks = append ( sf . tasks , keys ... )
sf . lock . Unlock ( )
sf . lock . Unlock ( )
// Notify the prefetcher, it's fine if it's already terminated
// Notify the background thread to execute scheduled tasks
select {
select {
case sf . wake <- struct { } { } :
case sf . wake <- struct { } { } :
// Wake signal sent
default :
default :
// Wake signal not sent as a previous is already queued
}
}
return nil
}
}
// peek tries to retrieve a deep copy of the fetcher's trie in whatever form it
// wait blocks until the subfetcher terminates. This method is used to block on
// is currently.
// an async termination before accessing internal fields from the fetcher.
func ( sf * subfetcher ) peek ( ) Trie {
func ( sf * subfetcher ) wait ( ) {
ch := make ( chan Trie )
<- sf . term
select {
}
case sf . copy <- ch :
// Subfetcher still alive, return copy from it
return <- ch
case <- sf . term :
// peek retrieves the fetcher's trie, populated with any pre-fetched data. The
// Subfetcher already terminated, return a copy directly
// returned trie will be a shallow copy, so modifying it will break subsequent
if sf . trie == nil {
// peeks for the original data. The method will block until all the scheduled
return nil
// data has been loaded and the fethcer terminated.
}
func ( sf * subfetcher ) peek ( ) Trie {
return sf . db . CopyTrie ( sf . trie )
// Block until the fertcher terminates, then retrieve the trie
}
sf . wait ( )
return sf . trie
}
}
// abort interrupts the subfetcher immediately. It is safe to call abort multiple
// terminate requests the subfetcher to stop accepting new tasks and spin down
// times but it is not thread safe.
// as soon as everything is loaded. Depending on the async parameter, the method
func ( sf * subfetcher ) abort ( ) {
// will either block until all disk loads finish or return immediately.
func ( sf * subfetcher ) terminate ( async bool ) {
select {
select {
case <- sf . stop :
case <- sf . stop :
default :
default :
close ( sf . stop )
close ( sf . stop )
}
}
if async {
return
}
<- sf . term
<- sf . term
}
}
// loop waits for new tasks to be scheduled and keeps loading them until it runs
// loop loads newly-scheduled trie tasks as they are received and loads them, stopping
// out of tasks or its underlying trie is retrieved for committing .
// when requested .
func ( sf * subfetcher ) loop ( ) {
func ( sf * subfetcher ) loop ( ) {
// No matter how the loop stops, signal anyone waiting that it's terminated
// No matter how the loop stops, signal anyone waiting that it's terminated
defer close ( sf . term )
defer close ( sf . term )
@ -305,8 +285,6 @@ func (sf *subfetcher) loop() {
}
}
sf . trie = trie
sf . trie = trie
} else {
} else {
// 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 )
trie , err := sf . db . OpenStorageTrie ( sf . state , sf . addr , sf . root , nil )
if err != nil {
if err != nil {
log . Warn ( "Trie prefetcher failed opening trie" , "root" , sf . root , "err" , err )
log . Warn ( "Trie prefetcher failed opening trie" , "root" , sf . root , "err" , err )
@ -318,48 +296,38 @@ func (sf *subfetcher) loop() {
for {
for {
select {
select {
case <- sf . wake :
case <- sf . wake :
// Subfetcher was woken up, retrieve any tasks to avoid spinning the lock
// Execute all remaining tasks in single run
sf . lock . Lock ( )
sf . lock . Lock ( )
tasks := sf . tasks
tasks := sf . tasks
sf . tasks = nil
sf . tasks = nil
sf . lock . Unlock ( )
sf . lock . Unlock ( )
// Prefetch any tasks until the loop is interrupted
for _ , task := range tasks {
for i , task := range tasks {
if _ , ok := sf . seen [ string ( task ) ] ; ok {
select {
sf . dups ++
case <- sf . stop :
continue
// If termination is requested, add any leftover back and return
}
sf . lock . Lock ( )
if len ( task ) == common . AddressLength {
sf . tasks = append ( sf . tasks , tasks [ i : ] ... )
sf . trie . GetAccount ( common . BytesToAddress ( task ) )
sf . lock . Unlock ( )
} else {
return
sf . trie . GetStorage ( sf . addr , task )
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
if _ , ok := sf . seen [ string ( task ) ] ; ok {
sf . dups ++
} else {
if len ( task ) == common . AddressLength {
sf . trie . GetAccount ( common . BytesToAddress ( task ) )
} else {
sf . trie . GetStorage ( sf . addr , task )
}
sf . seen [ string ( task ) ] = struct { } { }
}
}
}
sf . seen [ string ( task ) ] = struct { } { }
}
}
case ch := <- sf . copy :
// Somebody wants a copy of the current trie, grant them
ch <- sf . db . CopyTrie ( sf . trie )
case <- sf . stop :
case <- sf . stop :
// Termination is requested, abort and leave remaining tasks
// Termination is requested, abort if no more tasks are pending. If
return
// there are some, exhaust them first.
sf . lock . Lock ( )
done := sf . tasks == nil
sf . lock . Unlock ( )
if done {
return
}
// Some tasks are pending, loop and pick them up (that wake branch
// will be selected eventually, whilst stop remains closed to this
// branch will also run afterwards).
}
}
}
}
}
}