Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions cmd/rpcdaemon/cli/httpcfg/http_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,20 @@ func DefaultDBReadConcurrency() int {
// apply-loop txs, the block-loader tx and the commitment-calculator tx.
const execPermanentReadTxs = 5

// execReadAheadTxs counts the block read-ahead txs held on non-initial applying
// cycles — the steady tip-following mode — so they are counted with the fixed
// holders rather than eating into the reserve.
const execReadAheadTxs = 2

// dbReadTxsReserved is read-tx headroom kept above parallel exec's permanent
// holders. Without it a transient commitment/RPC reader can take the last slot
// a blocked permanent holder needs, deadlocking the pipeline.
const dbReadTxsReserved = 16

// RoTxsLimit sizes the MDBX read-tx semaphore for concurrent worker pools and
// reserved readers.
func RoTxsLimit(dbReadConcurrency, execWorkers, parallelCommitmentReaders, warmupWorkers, blockReadAheadWorkers int) int64 {
func RoTxsLimit(dbReadConcurrency, execWorkers, parallelCommitmentReaders, warmupWorkers, blockReadAheadWorkers, execReadAheadWorkers int) int64 {
limit := DefaultDBReadConcurrency()
if dbReadConcurrency > 0 {
limit = dbReadConcurrency
}
workerReaders := max(execWorkers, 0) + max(parallelCommitmentReaders, 0) + max(warmupWorkers, 0) + max(blockReadAheadWorkers, 0)
return int64(max(limit, workerReaders+execPermanentReadTxs+execReadAheadTxs+dbReadTxsReserved))
workerReaders := max(execWorkers, 0) + max(parallelCommitmentReaders, 0) + max(warmupWorkers, 0) + max(blockReadAheadWorkers, 0) + max(execReadAheadWorkers, 0)
return int64(max(limit, workerReaders+execPermanentReadTxs+dbReadTxsReserved))
}

type HttpCfg struct {
Expand Down
29 changes: 15 additions & 14 deletions cmd/rpcdaemon/cli/httpcfg/http_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,33 @@ import (
func TestReadTxLimitCoversExecReaders(t *testing.T) {
t.Parallel()
// The limit must exceed every long-lived read tx a parallel batch holds
// (see execPermanentReadTxs and execReadAheadTxs) even when GOMAXPROCS is
// (see execPermanentReadTxs and the configured read-ahead workers) even when GOMAXPROCS is
// set below NumCPU and shrinks the derived default.
require.Greater(t, RoTxsLimit(0, runtime.NumCPU(), runtime.NumCPU(), runtime.NumCPU(), runtime.NumCPU()), int64(4*runtime.NumCPU()+execPermanentReadTxs+execReadAheadTxs))
require.Greater(t, RoTxsLimit(0, runtime.NumCPU(), runtime.NumCPU(), runtime.NumCPU(), runtime.NumCPU(), 2), int64(4*runtime.NumCPU()+execPermanentReadTxs+2))
}

func TestRoTxsLimit(t *testing.T) {
t.Parallel()
defaultLimit := int64(DefaultDBReadConcurrency())
floor := func(execWorkers, parallelCommitmentReaders, warmupWorkers, blockReadAheadWorkers int) int64 {
return int64(execWorkers + parallelCommitmentReaders + warmupWorkers + blockReadAheadWorkers + execPermanentReadTxs + execReadAheadTxs + dbReadTxsReserved)
floor := func(execWorkers, parallelCommitmentReaders, warmupWorkers, blockReadAheadWorkers, execReadAheadWorkers int) int64 {
return int64(execWorkers + parallelCommitmentReaders + warmupWorkers + blockReadAheadWorkers + execPermanentReadTxs + execReadAheadWorkers + dbReadTxsReserved)
}
for _, tc := range []struct {
name string
cfg, execWorkers, parallelCommitmentReaders, warmupWorkers, blockReadAheadWorkers int
want int64
name string
cfg, execWorkers, parallelCommitmentReaders, warmupWorkers, blockReadAheadWorkers, execReadAheadWorkers int
want int64
}{
{"default passes through when above floor", 0, 4, 4, 4, 4, defaultLimit},
{"high explicit value passes through", 5000, 8, 8, 8, 8, 5000},
{"low explicit value raised to floor", 8, 64, 6, 6, 6, floor(64, 6, 6, 6)},
{"all worker pools are counted", 8, 8, 6, 6, 6, 49},
{"disabled pools add no readers", 8, 8, 6, 0, 0, 37},
{"default floored below worker count", 0, int(defaultLimit) + 1, 6, 6, 6, floor(int(defaultLimit)+1, 6, 6, 6)},
{"default passes through when above floor", 0, 4, 4, 4, 4, 2, defaultLimit},
{"high explicit value passes through", 5000, 8, 8, 8, 8, 2, 5000},
{"low explicit value raised to floor", 8, 64, 6, 6, 6, 2, floor(64, 6, 6, 6, 2)},
{"all worker pools are counted", 8, 8, 6, 6, 6, 2, 49},
{"disabled pools add no readers", 8, 8, 6, 0, 0, 0, 35},
{"read-ahead reserve follows workers", 8, 8, 0, 0, 0, 7, 36},
{"default floored below worker count", 0, int(defaultLimit) + 1, 6, 6, 6, 2, floor(int(defaultLimit)+1, 6, 6, 6, 2)},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
require.Equal(t, tc.want, RoTxsLimit(tc.cfg, tc.execWorkers, tc.parallelCommitmentReaders, tc.warmupWorkers, tc.blockReadAheadWorkers))
require.Equal(t, tc.want, RoTxsLimit(tc.cfg, tc.execWorkers, tc.parallelCommitmentReaders, tc.warmupWorkers, tc.blockReadAheadWorkers, tc.execReadAheadWorkers))
})
}
}
4 changes: 2 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2125,11 +2125,11 @@ func SetEthConfig(nodeCtx context.Context, ctx *cli.Command, nodeConfig *nodecfg
if statecfg.ExperimentalParallelCommitment {
parallelCommitmentReaders = commitment.ParallelCommitmentReadTxs()
}
if limit := httpcfg.RoTxsLimit(c, cfg.ExecWorkerCount, parallelCommitmentReaders, warmupWorkers, blockReadAheadWorkers); int64(c) < limit {
if limit := httpcfg.RoTxsLimit(c, cfg.ExecWorkerCount, parallelCommitmentReaders, warmupWorkers, blockReadAheadWorkers, dbg.ReadAheadBlocksWorkers); int64(c) < limit {
logger.Warn("db.read.concurrency below the exec read-tx floor; raising to avoid a parallel-exec deadlock",
"configured", c, "using", limit, "execWorkers", cfg.ExecWorkerCount,
"parallelCommitmentReaders", parallelCommitmentReaders, "warmupWorkers", warmupWorkers,
"blockReadAheadWorkers", blockReadAheadWorkers)
"blockReadAheadWorkers", blockReadAheadWorkers, "readAheadExecWorkers", dbg.ReadAheadBlocksWorkers)
}
}
if ctx.IsSet(ExecNoMergeFlag.Name) {
Expand Down
5 changes: 5 additions & 0 deletions common/dbg/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ var (
DisableAdaptivePin = EnvBool("DISABLE_ADAPTIVE_PIN", true)
AssertStateCache = EnvBool("ASSERT_STATE_CACHE", false)
ReadAhead = EnvBool("READ_AHEAD", true)
ReadAheadExec = EnvBool("READ_AHEAD_EXEC", false)
ReadAheadExecCommitment = EnvBool("READ_AHEAD_EXEC_COMMITMENT", false)
ReadAheadInitial = EnvBool("READ_AHEAD_INITIAL", false)
ReadAheadBlocksWorkers = EnvInt("READ_AHEAD_BLOCKS_WORKERS", 2)
ReadAheadDistance = EnvInt("READ_AHEAD_DISTANCE", 500)
ReadAheadWorkers = EnvInt("READ_AHEAD_WORKERS", estimate.AllCPUs())
ReadAheadWait = EnvBool("READ_AHEAD_WAIT", false)
ReadAheadBALCode = EnvBool("READ_AHEAD_BAL_CODE", false)
Expand Down
55 changes: 30 additions & 25 deletions execution/exec/bal_commitment_warmup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import (
"github.com/erigontech/erigon/execution/types"
)

func commitmentStateKey(address, slot []byte) []byte {
return commitment.KeyToHexNibbleHash(slices.Concat(address, slot))
}

func balCommitmentWarmupKeys(bal types.BlockAccessList) [][]byte {
keyCount := 0
for i := range bal {
Expand All @@ -52,14 +56,11 @@ func balCommitmentWarmupKeys(bal types.BlockAccessList) [][]byte {
account := &bal[i]
address := account.Address
if len(account.BalanceChanges)+len(account.NonceChanges)+len(account.CodeChanges) > 0 {
keys = append(keys, commitment.KeyToHexNibbleHash(address[:]))
keys = append(keys, commitmentStateKey(address[:], nil))
}
for _, storage := range account.StorageChanges {
slot := storage.Slot.Value()
plainKey := make([]byte, len(address)+len(slot))
copy(plainKey, address[:])
copy(plainKey[len(address):], slot[:])
keys = append(keys, commitment.KeyToHexNibbleHash(plainKey))
keys = append(keys, commitmentStateKey(address[:], slot[:]))
}
}
slices.SortFunc(keys, bytes.Compare)
Expand Down Expand Up @@ -274,9 +275,9 @@ func withTemporalTx(ctx context.Context, db kv.RoDB, fn func(ttx kv.TemporalTx))

// keysWithChild keeps the keys whose branch at depth has their next nibble.
// A key whose path ends above depth has no branch there, so checks at shallower depths can be skipped.
func keysWithChild(ctx context.Context, db kv.RoDB, ttx kv.TemporalTx, keys [][]byte, depth int) [][]byte {
func keysWithChild(ctx context.Context, db kv.RoDB, ttx kv.TemporalTx, keys [][]byte, depth, par int) [][]byte {
keep := make([]bool, len(keys))
inChunks(ctx, db, ttx, len(keys), max(1, min(prefetchPar, len(keys)/256)), func(tx kv.TemporalTx, from, to int) {
inChunks(ctx, db, ttx, len(keys), max(1, min(par, len(keys)/256)), func(tx kv.TemporalTx, from, to int) {
// Only the child bitmap is needed: a raw branch skips the reads that resolve shortened keys.
raw, rawOK := tx.AggTx().(rawBranchReader)
compact := make([]byte, 0, 128)
Expand Down Expand Up @@ -316,9 +317,8 @@ func keysWithChild(ctx context.Context, db kv.RoDB, ttx kv.TemporalTx, keys [][]
}

// prefetchKeys spreads keys over prefetchPar read txs and returns the first read failure (see firstFailure).
func prefetchKeys(ctx context.Context, db kv.RoDB, ttx kv.TemporalTx, domain kv.Domain, keys [][]byte) error {
par := prefetchPar
if len(keys) < 2*prefetchPar {
func prefetchKeys(ctx context.Context, db kv.RoDB, ttx kv.TemporalTx, domain kv.Domain, keys [][]byte, par int) error {
if len(keys) < 2*par {
par = 1
}
var mu sync.Mutex
Expand Down Expand Up @@ -413,7 +413,7 @@ func prefetchBALCommitment(ctx context.Context, db kv.RoDB, keys [][]byte) {

func prefetchDomain(ctx context.Context, db kv.RoDB, domain kv.Domain, keys [][]byte) error {
var err error
if txErr := withTemporalTx(ctx, db, func(ttx kv.TemporalTx) { err = prefetchKeys(ctx, db, ttx, domain, keys) }); txErr != nil {
if txErr := withTemporalTx(ctx, db, func(ttx kv.TemporalTx) { err = prefetchKeys(ctx, db, ttx, domain, keys, prefetchPar) }); txErr != nil {
return txErr
}
return err
Expand Down Expand Up @@ -449,21 +449,26 @@ func flatPrefixes(keys [][]byte, start int) [][]byte {
func prefetchCommitment(ctx context.Context, db kv.RoDB, hashedKeys [][]byte) error {
var err error
txErr := withTemporalTx(ctx, db, func(ttx kv.TemporalTx) {
p, ok := ttx.AggTx().(latestPrefetcher)
if !ok || trieMostlyResident(ctx, p, hashedKeys) {
return
}
if err = prefetchKeys(ctx, db, ttx, kv.CommitmentDomain, flatPrefixes(hashedKeys, 0)); err != nil {
return
}
// The account trie walk loses a storage key at the extension above its account, so storage tries start again at their root.
if err = walkTriePrefixes(ctx, db, ttx, hashedKeys, 0); err == nil {
err = walkTriePrefixes(ctx, db, ttx, storageKeysOf(hashedKeys), storageTrieDepth)
}
err = prefetchCommitmentWithTx(ctx, db, ttx, hashedKeys, prefetchPar)
})
return cmp.Or(txErr, err)
}

func prefetchCommitmentWithTx(ctx context.Context, db kv.RoDB, ttx kv.TemporalTx, hashedKeys [][]byte, par int) error {
p, ok := ttx.AggTx().(latestPrefetcher)
if !ok || trieMostlyResident(ctx, p, hashedKeys) {
return nil
}
if err := prefetchKeys(ctx, db, ttx, kv.CommitmentDomain, flatPrefixes(hashedKeys, 0), par); err != nil {
return err
}
// The account trie walk loses a storage key at the extension above its account, so storage tries start again at their root.
if err := walkTriePrefixes(ctx, db, ttx, hashedKeys, 0, par); err != nil {
return err
}
return walkTriePrefixes(ctx, db, ttx, storageKeysOf(hashedKeys), storageTrieDepth, par)
}

const storageTrieDepth = 64

func storageKeysOf(hashedKeys [][]byte) [][]byte {
Expand All @@ -477,16 +482,16 @@ func storageKeysOf(hashedKeys [][]byte) [][]byte {
}

// walkTriePrefixes prefetches the trie levels below the flat depth and stops at the first read error.
func walkTriePrefixes(ctx context.Context, db kv.RoDB, ttx kv.TemporalTx, keys [][]byte, start int) error {
func walkTriePrefixes(ctx context.Context, db kv.RoDB, ttx kv.TemporalTx, keys [][]byte, start, par int) error {
active := keys
for depth := start; len(active) > 0 && depth <= commitment.WarmupMaxDepth; depth++ {
if depth > prefetchFlatDepth {
if err := prefetchKeys(ctx, db, ttx, kv.CommitmentDomain, compactPrefixes(active, depth)); err != nil {
if err := prefetchKeys(ctx, db, ttx, kv.CommitmentDomain, compactPrefixes(active, depth), par); err != nil {
return err
}
}
if depth >= prefetchFlatDepth {
active = keysWithChild(ctx, db, ttx, active, depth)
active = keysWithChild(ctx, db, ttx, active, depth, par)
}
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions execution/exec/bal_commitment_warmup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,8 @@ func TestPrefetchKeysKeepsTheFirstReadError(t *testing.T) {
}
parent := prefetcherTx{p: coldPagesPrefetcher{}}
failing := prefetcherDB{p: domainErrPrefetcher{failing: kv.CommitmentDomain}}
require.Error(t, prefetchKeys(context.Background(), failing, parent, kv.CommitmentDomain, keys), "an error from a planner's own tx")
require.NoError(t, prefetchKeys(context.Background(), prefetcherDB{p: coldPagesPrefetcher{}}, parent, kv.CommitmentDomain, keys))
require.Error(t, prefetchKeys(context.Background(), failing, parent, kv.CommitmentDomain, keys, prefetchPar), "an error from a planner's own tx")
require.NoError(t, prefetchKeys(context.Background(), prefetcherDB{p: coldPagesPrefetcher{}}, parent, kv.CommitmentDomain, keys, prefetchPar))
}

// branchTx serves raw commitment branches from a small trie: compact prefix -> child bitmap.
Expand All @@ -545,7 +545,7 @@ func TestKeysWithChildKeepsKeysWhoseBranchHasTheirNextNibble(t *testing.T) {
compact(1): 1 << 2, // branch 1 has only child 2
}}
keys := [][]byte{{1, 2, 0}, {1, 5, 0}, {3, 0, 0}, {4}}
got := keysWithChild(context.Background(), nil, tx, keys, 1)
got := keysWithChild(context.Background(), nil, tx, keys, 1, prefetchPar)
require.Equal(t, [][]byte{{1, 2, 0}}, got, "1-5 has no child at branch 1, 3 has no branch, 4 ends above depth 1")
}

Expand Down
Loading
Loading