Skip to content

Commit 2f0099c

Browse files
performance: cherry-pick inc-shard-def-size updates (#21033)
Cherry-pick of #20930, #21025, and #21026 onto performance. --------- Co-authored-by: sudeepdino008 <sudeepdino008@gmail.com>
1 parent ed1b6a4 commit 2f0099c

11 files changed

Lines changed: 116 additions & 73 deletions

File tree

cmd/integration/commands/flags.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ var (
4848

4949
dbWriteMap bool
5050

51-
chainTipMode bool
52-
clearCommitment bool
53-
resume bool
54-
noHistory bool
55-
syncCfg = ethconfig.Defaults.Sync
51+
chainTipMode bool
52+
clearCommitment bool
53+
resume bool
54+
noHistory bool
55+
erigondbDomainStepsInFrozenFile string
56+
syncCfg = ethconfig.Defaults.Sync
5657
)
5758

5859
func must(err error) {
@@ -117,7 +118,7 @@ func withYes(cmd *cobra.Command) {
117118
}
118119

119120
func withSqueeze(cmd *cobra.Command) {
120-
cmd.Flags().BoolVar(&squeeze, "squeeze", true, "use offset-pointers from commitment.kv to account.kv")
121+
cmd.Flags().BoolVar(&squeeze, "squeeze", false, "use offset-pointers from commitment.kv to account.kv")
121122
}
122123

123124
func withClearCommitment(cmd *cobra.Command) {
@@ -203,6 +204,12 @@ func withChaosMonkey(cmd *cobra.Command) {
203204
cmd.Flags().BoolVar(&syncCfg.ChaosMonkey, utils.ChaosMonkeyFlag.Name, utils.ChaosMonkeyFlag.Value, utils.ChaosMonkeyFlag.Usage)
204205
}
205206

207+
func withErigondbDomainStepsInFrozenFile(cmd *cobra.Command) {
208+
cmd.Flags().StringVar(&erigondbDomainStepsInFrozenFile,
209+
utils.ErigondbDomainStepsInFrozenFileFlag.Name, "",
210+
utils.ErigondbDomainStepsInFrozenFileFlag.Usage)
211+
}
212+
206213
// withStageBase applies flags common to most stage commands: config, datadir, chain, chaos monkey, heimdall, unwind.
207214
func withStageBase(cmd *cobra.Command) {
208215
withConfig(cmd)

cmd/integration/commands/stages.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"runtime"
2626
"slices"
27+
"strconv"
2728
"strings"
2829
"sync"
2930
"time"
@@ -36,11 +37,13 @@ import (
3637
"golang.org/x/sync/semaphore"
3738

3839
"github.com/erigontech/erigon/cl/clparams"
40+
"github.com/erigontech/erigon/cmd/utils"
3941
"github.com/erigontech/erigon/common"
4042
"github.com/erigontech/erigon/common/dbg"
4143
"github.com/erigontech/erigon/common/dir"
4244
"github.com/erigontech/erigon/common/estimate"
4345
"github.com/erigontech/erigon/common/log/v3"
46+
"github.com/erigontech/erigon/db/config3"
4447
"github.com/erigontech/erigon/db/datadir"
4548
"github.com/erigontech/erigon/db/fromdb"
4649
"github.com/erigontech/erigon/db/integrity"
@@ -322,6 +325,7 @@ func init() {
322325
withPruneTo(cmdStageExec)
323326
withTraceFlags(cmdStageExec)
324327
withChainTipMode(cmdStageExec)
328+
withErigondbDomainStepsInFrozenFile(cmdStageExec)
325329
rootCmd.AddCommand(cmdStageExec)
326330

327331
withStageBase(cmdStageExecReplay)
@@ -1063,7 +1067,28 @@ func allSnapshots(ctx context.Context, db kv.RoDB, logger log.Logger) (*freezebl
10631067
if erigonDBSettings, err = dbstate.ResolveErigonDBSettings(dirs, logger, false); err != nil {
10641068
return
10651069
}
1066-
_aggSingleton = dbstate.New(dirs).Logger(logger).WithErigonDBSettings(erigonDBSettings).MustOpen(ctx, db)
1070+
aggOpts := dbstate.New(dirs).Logger(logger).WithErigonDBSettings(erigonDBSettings)
1071+
if erigondbDomainStepsInFrozenFile != "" {
1072+
var v uint64
1073+
if strings.EqualFold(erigondbDomainStepsInFrozenFile, "inf") {
1074+
v = config3.UnboundedDomainMerge
1075+
} else {
1076+
parsed, perr := strconv.ParseUint(erigondbDomainStepsInFrozenFile, 10, 64)
1077+
if perr != nil || parsed == 0 {
1078+
err = fmt.Errorf("invalid --%s value %q: must be a positive integer or \"Inf\"",
1079+
utils.ErigondbDomainStepsInFrozenFileFlag.Name, erigondbDomainStepsInFrozenFile)
1080+
return
1081+
}
1082+
v = parsed
1083+
}
1084+
stepsStr := "Inf"
1085+
if v != config3.UnboundedDomainMerge {
1086+
stepsStr = fmt.Sprintf("%d", v)
1087+
}
1088+
logger.Info("domain merge cap overridden", "steps_in_frozen_file", stepsStr)
1089+
aggOpts = aggOpts.ErigondbDomainStepsInFrozenFile(v)
1090+
}
1091+
_aggSingleton = aggOpts.MustOpen(ctx, db)
10671092

10681093
_aggSingleton.SetProduceMod(snapCfg.ProduceE3)
10691094

cmd/utils/app/snapshots_cmd.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ var snapshotCommand = cli.Command{
190190
Usage: "create snapshots from the specified block number",
191191
Flags: joinFlags([]cli.Flag{
192192
&utils.DataDirFlag,
193+
&utils.ErigondbDomainStepsInFrozenFileFlag,
193194
}),
194195
},
195196
{
@@ -3199,6 +3200,27 @@ func doRetireCommand(cliCtx *cli.Context, dirs datadir.Dirs) error {
31993200
defer br.MadvNormal().DisableReadAhead()
32003201
defer agg.MadvNormal().DisableReadAhead()
32013202

3203+
if cliCtx.IsSet(utils.ErigondbDomainStepsInFrozenFileFlag.Name) {
3204+
s := cliCtx.String(utils.ErigondbDomainStepsInFrozenFileFlag.Name)
3205+
var v uint64
3206+
if strings.EqualFold(s, "inf") {
3207+
v = config3.UnboundedDomainMerge
3208+
} else {
3209+
parsed, perr := strconv.ParseUint(s, 10, 64)
3210+
if perr != nil || parsed == 0 {
3211+
return fmt.Errorf("invalid --%s value %q: must be a positive integer or \"Inf\"",
3212+
utils.ErigondbDomainStepsInFrozenFileFlag.Name, s)
3213+
}
3214+
v = parsed
3215+
}
3216+
stepsStr := "Inf"
3217+
if v != config3.UnboundedDomainMerge {
3218+
stepsStr = fmt.Sprintf("%d", v)
3219+
}
3220+
logger.Info("domain merge cap overridden", "steps_in_frozen_file", stepsStr)
3221+
agg.SetErigondbDomainStepsInFrozenFile(v)
3222+
}
3223+
32023224
blockSnapBuildSema := semaphore.NewWeighted(int64(runtime.NumCPU()))
32033225
agg.SetSnapshotBuildSema(blockSnapBuildSema)
32043226

db/integrity/commitment_integrity.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ func checkCommitmentRootViaSd(ctx context.Context, tx kv.TemporalTx, f state.Vis
207207
return nil, err
208208
}
209209
sd.GetCommitmentCtx().SetTrace(logger.Enabled(ctx, log.LvlTrace))
210-
sd.GetCommitmentCtx().SetLimitedHistoryStateReader(tx, maxTxNum) // to use tx.Debug().GetLatestFromFiles with maxTxNum
211-
latestTxNum, _, err := sd.SeekCommitment(ctx, tx) // seek commitment again to use the new state reader instead
210+
sd.GetCommitmentCtx().SetStateReader(commitmentdb.NewFilesOnlyStateReader(tx, maxTxNum))
211+
latestTxNum, _, err := sd.SeekCommitment(ctx, tx) // seek commitment again to use the new state reader instead
212212
if err != nil {
213213
return nil, err
214214
}
@@ -240,17 +240,13 @@ func checkCommitmentRootViaSd(ctx context.Context, tx kv.TemporalTx, f state.Vis
240240
}
241241

242242
func checkCommitmentRootViaRecompute(ctx context.Context, tx kv.TemporalTx, sd *execctx.SharedDomains, info commitmentRootInfo, f state.VisibleFile, logger log.Logger) error {
243-
trace := logger.Enabled(ctx, log.LvlTrace)
244-
touchLoggingVisitor := func(k []byte) {
245-
if trace {
246-
logger.Trace("[integrity] CommitmentRoot", "key", common.Address(k), "blockNum", info.blockNum, "file", filepath.Base(f.Fullpath()))
247-
}
248-
}
249-
touches, err := touchHistoricalKeys(sd, tx, kv.AccountsDomain, info.blockMinTxNum, info.txNum+1, 0, nil /* no pre-built index */, touchLoggingVisitor)
243+
accountTouches, storageTouches, err := sd.TouchChangedKeysFromHistory(tx, info.blockMinTxNum, info.txNum+1)
250244
if err != nil {
251245
return err
252246
}
253-
logger.Info("[integrity] CommitmentRoot recomputing", "touches", touches, "file", filepath.Base(f.Fullpath()))
247+
logger.Info("[integrity] CommitmentRoot recomputing",
248+
"accountTouches", accountTouches, "storageTouches", storageTouches,
249+
"file", filepath.Base(f.Fullpath()))
254250
recomputedBytes, err := sd.ComputeCommitment(ctx, tx, false /* saveStateAfter */, info.blockNum, info.txNum, "", nil /* commitProgress */)
255251
if err != nil {
256252
return err
@@ -259,7 +255,10 @@ func checkCommitmentRootViaRecompute(ctx context.Context, tx kv.TemporalTx, sd *
259255
if recomputed != info.rootHash {
260256
return fmt.Errorf("%w: recomputed root does not match verified root: %s != %s", ErrIntegrity, recomputed, info.rootHash)
261257
}
262-
logger.Info("[integrity] CommitmentRoot recomputed matches", "root", recomputed, "touches", touches, "file", filepath.Base(f.Fullpath()))
258+
logger.Info("[integrity] CommitmentRoot recomputed matches",
259+
"root", recomputed,
260+
"accountTouches", accountTouches, "storageTouches", storageTouches,
261+
"file", filepath.Base(f.Fullpath()))
263262
return nil
264263
}
265264

db/state/aggregator.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ func (a *Aggregator) Dirs() datadir.Dirs { return a.dirs }
247247
func (a *Aggregator) StepsInFrozenFile() uint64 { return a.stepsInFrozenFile.Load() }
248248
func (a *Aggregator) Logger() log.Logger { return a.logger }
249249

250+
// SetErigondbDomainStepsInFrozenFile applies a domain-only merge cap override at runtime.
251+
// Intended for one-shot tools (e.g. `erigon seg retire`) that construct the aggregator
252+
// via openAgg and need to override the cap after construction. Must be called before any
253+
// merge work begins.
254+
func (a *Aggregator) SetErigondbDomainStepsInFrozenFile(steps uint64) {
255+
a.erigondbDomainStepsInFrozenFile = steps
256+
}
257+
250258
func (a *Aggregator) ForTestReplaceKeysInValues(domain kv.Domain, v bool) {
251259
a.d[domain].ReplaceKeysInValues = v
252260
}

db/state/domain.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,9 +1354,10 @@ func (dt *DomainRoTx) getLatestFromFiles(k []byte, maxTxNum uint64) (v []byte, f
13541354
}
13551355
}
13561356

1357+
// Walk newest→oldest; skip files starting strictly after maxTxNum so a key's
1358+
// last write in an older .kv is still found (walkback bounded by maxTxNum).
13571359
for i := len(dt.files) - 1; i >= 0; i-- {
1358-
if maxTxNum != math.MaxUint64 && (dt.files[i].startTxNum > maxTxNum || maxTxNum > dt.files[i].endTxNum) { // (maxTxNum > dt.files[i].endTxNum || dt.files[i].startTxNum > maxTxNum) { // skip partially matched files
1359-
//fmt.Printf("getLatestFromFiles: skipping file %d %s, maxTxNum=%d, startTxNum=%d, endTxNum=%d\n", i, dt.files[i].src.decompressor.FileName(), maxTxNum, dt.files[i].startTxNum, dt.files[i].endTxNum)
1360+
if maxTxNum != math.MaxUint64 && dt.files[i].startTxNum > maxTxNum {
13601361
continue
13611362
}
13621363
// fmt.Printf("getLatestFromFiles: lim=%d %d %d %d %d\n", maxTxNum, dt.files[i].startTxNum, dt.files[i].endTxNum, dt.files[i].startTxNum/dt.stepSize, dt.files[i].endTxNum/dt.stepSize)

db/state/domain_committed.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ import (
3535
"github.com/erigontech/erigon/execution/commitment/commitmentdb"
3636
)
3737

38-
const minStepsForReferencing = 2
38+
const minStepsForReferencing = 99999
3939

4040
// ValuesPlainKeyReferencingThresholdReached checks if the range from..to is large enough to use plain key referencing
4141
// Used for commitment branches - to store references to account and storage keys as shortened keys (file offsets)
4242
func ValuesPlainKeyReferencingThresholdReached(stepSize, from, to uint64) bool {
43-
return (to-from)/stepSize >= minStepsForReferencing
43+
return ((to-from)/stepSize)%minStepsForReferencing == 0
44+
}
45+
46+
// Values can use key referencing in this range?
47+
func MayContainValuesPlainKeyReferencing(stepSize, from, to uint64) bool {
48+
return ((to - from) / stepSize) > minStepsForReferencing
4449
}
4550

4651
// replaceShortenedKeysInBranch expands shortened key references (file offsets) in branch data back to full keys

db/state/squeeze.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,7 @@ func RebuildCommitmentFiles(ctx context.Context, rwDb kv.TemporalRwDB, txNumsRea
895895
stepsInShard := uint64(shardTo - shardFrom)
896896
keysPerStep := totalKeys / stepsInShard // how many keys in just one step?
897897

898-
// shardStepsSize := kv.Step(2)
899-
shardStepsSize := kv.Step(min(uint64(math.Pow(2, math.Log2(float64(stepsInShard)))), 16))
898+
shardStepsSize := kv.Step(min(uint64(math.Pow(2, math.Log2(float64(stepsInShard)))), 64))
900899
if uint64(shardStepsSize) != stepsInShard { // processing shard in several smaller steps
901900
shardTo = shardFrom + shardStepsSize // if shard is quite big, we will process it in several steps
902901
}
@@ -975,7 +974,7 @@ func RebuildCommitmentFiles(ctx context.Context, rwDb kv.TemporalRwDB, txNumsRea
975974

976975
domains.SetTxNum(lastTxnumInShard - 1)
977976
currentTxNum := lastTxnumInShard - 1
978-
domains.GetCommitmentCtx().SetLimitedHistoryStateReader(rwTx, lastTxnumInShard) // this helps to read state from correct file during commitment
977+
domains.GetCommitmentCtx().SetStateReader(commitmentdb.NewFilesOnlyStateReader(rwTx, lastTxnumInShard-1))
979978
if concurrent {
980979
domains.EnableParaTrieDB(rwDb)
981980
}
@@ -1044,14 +1043,14 @@ func RebuildCommitmentFiles(ctx context.Context, rwDb kv.TemporalRwDB, txNumsRea
10441043

10451044
acRo.Close()
10461045

1047-
if !squeeze && !statecfg.Schema.CommitmentDomain.ReplaceKeysInValues {
1046+
if !squeeze {
10481047
return latestRoot, nil
10491048
}
10501049
logger.Info("[squeeze] starting")
10511050
a.recalcVisibleFiles()
10521051

10531052
logger.Info(fmt.Sprintf("[squeeze] latest root %x", latestRoot))
1054-
a.ForTestReplaceKeysInValues(kv.CommitmentDomain, true)
1053+
a.ForTestReplaceKeysInValues(kv.CommitmentDomain, squeeze)
10551054

10561055
actx := a.BeginFilesRo()
10571056
defer actx.Close()

db/state/statecfg/state_schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func Configure(Schema SchemaGen, a AggSetters, dirs datadir.Dirs, salt *uint32,
7373
return nil
7474
}
7575

76-
const AggregatorSqueezeCommitmentValues = true
76+
const AggregatorSqueezeCommitmentValues = false
7777
const MaxNonFuriousDirtySpacePerTx = 64 * datasize.MB
7878

7979
var dbgCommBtIndex = dbg.EnvBool("AGG_COMMITMENT_BT", false)

execution/commitment/commitmentdb/commitment_context.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,6 @@ func (sdc *SharedDomainsCommitmentContext) SetCustomHistoryStateReader(stateRead
141141
sdc.SetStateReader(stateReader)
142142
}
143143

144-
// SetLimitedHistoryStateReader sets the state reader to read *limited* (i.e. *without-recent-files*) historical state at specified txNum.
145-
func (sdc *SharedDomainsCommitmentContext) SetLimitedHistoryStateReader(roTx kv.TemporalTx, limitReadAsOfTxNum uint64) {
146-
sdc.SetStateReader(NewLimitedHistoryStateReader(roTx, sdc.sharedDomains, limitReadAsOfTxNum))
147-
}
148-
149144
func (sdc *SharedDomainsCommitmentContext) SetTrace(trace bool) {
150145
sdc.trace = trace
151146
sdc.patriciaTrie.SetTrace(trace)

0 commit comments

Comments
 (0)