Prune traces (#1993)

* Pruning of intermediate table

* Print pruning info

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
This commit is contained in:
ledgerwatch 2021-05-22 17:23:46 +01:00 committed by GitHub
parent 6a19321d82
commit c5054334ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 23 deletions

View File

@ -220,6 +220,23 @@ func ReplacementStages(ctx context.Context,
}
},
},
{
ID: stages.CallTraces,
Build: func(world StageParameters) *Stage {
return &Stage{
ID: stages.CallTraces,
Description: "Generate call traces index",
DisabledDescription: "Work In Progress",
Disabled: !sm.CallTraces,
ExecFunc: func(s *StageState, u Unwinder, tx ethdb.RwTx) error {
return SpawnCallTraces(s, tx, ctx.Done(), callTraces)
},
UnwindFunc: func(u *UnwindState, s *StageState, tx ethdb.RwTx) error {
return UnwindCallTraces(u, s, tx, ctx.Done(), callTraces)
},
}
},
},
{
ID: stages.AccountHistoryIndex,
Build: func(world StageParameters) *Stage {
@ -271,23 +288,6 @@ func ReplacementStages(ctx context.Context,
}
},
},
{
ID: stages.CallTraces,
Build: func(world StageParameters) *Stage {
return &Stage{
ID: stages.CallTraces,
Description: "Generate call traces index",
DisabledDescription: "Work In Progress",
Disabled: !sm.CallTraces,
ExecFunc: func(s *StageState, u Unwinder, tx ethdb.RwTx) error {
return SpawnCallTraces(s, tx, ctx.Done(), callTraces)
},
UnwindFunc: func(u *UnwindState, s *StageState, tx ethdb.RwTx) error {
return UnwindCallTraces(u, s, tx, ctx.Done(), callTraces)
},
}
},
},
{
ID: stages.TxLookup,
Build: func(world StageParameters) *Stage {
@ -346,9 +346,9 @@ func ReplacementUnwindOrder() UnwindOrder {
15,
5, 6, 7, // senders, exec, state snapshot
9, 8, // Unwinding of IHashes needs to happen after unwinding HashState
10, 11, // history
12, // log index
13, // call traces
10, // call traces
11, 12, // history
13, // log index
14, // tx lookup
16, // finish
}

View File

@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"fmt"
"math"
"math/big"
"runtime"
"time"
@ -102,7 +103,7 @@ func promoteCallTraces(logPrefix string, tx ethdb.RwTx, startBlock, endBlock uin
checkFlushEvery := time.NewTicker(flushEvery)
defer checkFlushEvery.Stop()
traceCursor, err := tx.CursorDupSort(dbutils.CallTraceSet)
traceCursor, err := tx.RwCursorDupSort(dbutils.CallTraceSet)
if err != nil {
return fmt.Errorf("%s: failed to create cursor for call traces: %w", logPrefix, err)
}
@ -169,14 +170,36 @@ func promoteCallTraces(logPrefix string, tx ethdb.RwTx, startBlock, endBlock uin
if err != nil {
return fmt.Errorf("%s: failed to move cursor: %w", logPrefix, err)
}
if err = flushBitmaps64(collectorFrom, froms); err != nil {
return err
}
if err = flushBitmaps64(collectorTo, tos); err != nil {
return err
}
// Clean up before loading call traces to reclaim space
var prunedMin uint64 = math.MaxUint64
var prunedMax uint64 = 0
for k, _, err = traceCursor.First(); k != nil && err == nil; k, _, err = traceCursor.Next() {
blockNum := binary.BigEndian.Uint64(k)
if endBlock-blockNum <= params.FullImmutabilityThreshold {
break
}
if err = traceCursor.DeleteCurrent(); err != nil {
return fmt.Errorf("%s: failed to remove trace call set for block %d: %v", logPrefix, blockNum, err)
}
if blockNum < prunedMin {
prunedMin = blockNum
}
if blockNum > prunedMax {
prunedMax = blockNum
}
}
if err != nil {
return fmt.Errorf("%s: failed to move cleanup cursor: %w", logPrefix, err)
}
if prunedMax != 0 {
log.Info(fmt.Sprintf("[%s] Pruned trace call index", logPrefix), "from", prunedMin, "to", prunedMax)
}
if err := finaliseCallTraces(collectorFrom, collectorTo, logPrefix, tx, quit); err != nil {
return fmt.Errorf("[%s] %w", logPrefix, err)
}