Skip to content

Commit

Permalink
prune: time-limit PruneTable func (#12511)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Oct 28, 2024
1 parent da83d73 commit 3cd48aa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
19 changes: 12 additions & 7 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,9 @@ func IsPosBlock(db kv.Getter, blockHash common.Hash) (trans bool, err error) {
}

// PruneTable has `limit` parameter to avoid too large data deletes per one sync cycle - better delete by small portions to reduce db.FreeList size
func PruneTable(tx kv.RwTx, table string, pruneTo uint64, ctx context.Context, limit int) error {
func PruneTable(tx kv.RwTx, table string, pruneTo uint64, ctx context.Context, limit int, timeout time.Duration) error {
t := time.Now()
c, err := tx.RwCursor(table)

if err != nil {
return fmt.Errorf("failed to create cursor for pruning %w", err)
}
Expand All @@ -1226,14 +1226,19 @@ func PruneTable(tx kv.RwTx, table string, pruneTo uint64, ctx context.Context, l
if blockNum >= pruneTo {
break
}
select {
case <-ctx.Done():
return common.ErrStopped
default:
}
if err = c.DeleteCurrent(); err != nil {
return fmt.Errorf("failed to remove for block %d: %w", blockNum, err)
}
if i%100 == 0 {
select {
case <-ctx.Done():
return common.ErrStopped
default:
}
if time.Since(t) > timeout {
break
}
}
}
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions erigon-lib/state/domain_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,19 +483,19 @@ func (sd *SharedDomains) replaceShortenedKeysInBranch(prefix []byte, branch comm
acc := sd.aggTx.d[kv.AccountsDomain]
storageItem := sto.lookupVisibleFileByItsRange(fStartTxNum, fEndTxNum)
if storageItem == nil {
sd.logger.Warn(fmt.Sprintf("visible storage file of steps %d-%d not found\n", fStartTxNum/sd.aggTx.a.aggregationStep, fEndTxNum/sd.aggTx.a.aggregationStep))
sd.logger.Warn(fmt.Sprintf("visible storage file of steps %d-%d not found", fStartTxNum/sd.aggTx.a.aggregationStep, fEndTxNum/sd.aggTx.a.aggregationStep))
storageItem = sto.lookupDirtyFileByItsRange(fStartTxNum, fEndTxNum)
if storageItem == nil {
sd.logger.Crit(fmt.Sprintf("dirty storage file of steps %d-%d not found\n", fStartTxNum/sd.aggTx.a.aggregationStep, fEndTxNum/sd.aggTx.a.aggregationStep))
sd.logger.Crit(fmt.Sprintf("dirty storage file of steps %d-%d not found", fStartTxNum/sd.aggTx.a.aggregationStep, fEndTxNum/sd.aggTx.a.aggregationStep))
return nil, errors.New("storage file not found")
}
}
accountItem := acc.lookupVisibleFileByItsRange(fStartTxNum, fEndTxNum)
if accountItem == nil {
sd.logger.Warn(fmt.Sprintf("visible account file of steps %d-%d not found\n", fStartTxNum/sd.aggTx.a.aggregationStep, fEndTxNum/sd.aggTx.a.aggregationStep))
sd.logger.Warn(fmt.Sprintf("visible account file of steps %d-%d not found", fStartTxNum/sd.aggTx.a.aggregationStep, fEndTxNum/sd.aggTx.a.aggregationStep))
accountItem = acc.lookupDirtyFileByItsRange(fStartTxNum, fEndTxNum)
if accountItem == nil {
sd.logger.Crit(fmt.Sprintf("dirty account file of steps %d-%d not found\n", fStartTxNum/sd.aggTx.a.aggregationStep, fEndTxNum/sd.aggTx.a.aggregationStep))
sd.logger.Crit(fmt.Sprintf("dirty account file of steps %d-%d not found", fStartTxNum/sd.aggTx.a.aggregationStep, fEndTxNum/sd.aggTx.a.aggregationStep))
return nil, errors.New("account file not found")
}
}
Expand Down
7 changes: 5 additions & 2 deletions eth/stagedsync/stage_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"math"
"time"

"github.com/c2h5oh/datasize"
Expand Down Expand Up @@ -393,10 +394,12 @@ func PruneExecutionStage(s *PruneState, tx kv.RwTx, cfg ExecuteBlockCfg, ctx con
// (chunkLen is 8Kb) * (1_000 chunks) = 8mb
// Some blocks on bor-mainnet have 400 chunks of diff = 3mb
var pruneDiffsLimitOnChainTip = 1_000
pruneTimeout := 250 * time.Millisecond
if s.CurrentSyncCycle.IsInitialCycle {
pruneDiffsLimitOnChainTip *= 10
pruneDiffsLimitOnChainTip = math.MaxInt
pruneTimeout = time.Hour
}
if err := rawdb.PruneTable(tx, kv.ChangeSets3, s.ForwardProgress-config3.MaxReorgDepthV3, ctx, pruneDiffsLimitOnChainTip); err != nil {
if err := rawdb.PruneTable(tx, kv.ChangeSets3, s.ForwardProgress-config3.MaxReorgDepthV3, ctx, pruneDiffsLimitOnChainTip, pruneTimeout); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions turbo/jsonrpc/eth_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,10 @@ func doPrune(t *testing.T, db kv.RwDB, pruneTo uint64) {
err = rawdb.PruneTableDupSort(tx, kv.StorageChangeSet, "", pruneTo, logEvery, ctx)
assert.NoError(t, err)

err = rawdb.PruneTable(tx, kv.Receipts, pruneTo, ctx, math.MaxInt32)
err = rawdb.PruneTable(tx, kv.Receipts, pruneTo, ctx, math.MaxInt32, time.Hour)
assert.NoError(t, err)

err = rawdb.PruneTable(tx, kv.Log, pruneTo, ctx, math.MaxInt32)
err = rawdb.PruneTable(tx, kv.Log, pruneTo, ctx, math.MaxInt32, time.Hour)
assert.NoError(t, err)

err = rawdb.PruneTableDupSort(tx, kv.CallTraceSet, "", pruneTo, logEvery, ctx)
Expand Down

0 comments on commit 3cd48aa

Please sign in to comment.