Skip to content

Commit b001856

Browse files
committed
Implemented rebuild cmd for history snapshots
1 parent f0db073 commit b001856

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

cmd/integration/commands/state_history.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package commands
1818

1919
import (
20+
"context"
2021
"os"
2122

2223
"github.com/erigontech/erigon/db/config3"
@@ -34,7 +35,12 @@ func init() {
3435
withDataDir2(printCmd)
3536
withHistoryDomain(printCmd)
3637

38+
withDataDir2(rebuildCmd)
39+
withHistoryDomain(rebuildCmd)
40+
3741
historyCmd.AddCommand(printCmd)
42+
historyCmd.AddCommand(rebuildCmd)
43+
3844
rootCmd.AddCommand(historyCmd)
3945
}
4046

@@ -98,3 +104,55 @@ var printCmd = &cobra.Command{
98104
}
99105
},
100106
}
107+
108+
var rebuildCmd = &cobra.Command{
109+
Use: "rebuild",
110+
Short: "Regenerate .ef .efi .v .vi domain history snapshots from step 0",
111+
Run: func(cmd *cobra.Command, args []string) {
112+
logger := debug.SetupCobra(cmd, "integration")
113+
114+
dirs, l, err := datadir.New(datadirCli).MustFlock()
115+
if err != nil {
116+
logger.Error("Opening Datadir", "error", err)
117+
return
118+
}
119+
defer l.Unlock()
120+
121+
domainKV, err := kv.String2Domain(historyDomain)
122+
if err != nil {
123+
logger.Error("Failed to resolve domain", "error", err)
124+
return
125+
}
126+
127+
history, err := state.NewHistory(
128+
statecfg.Schema.GetDomainCfg(domainKV).Hist,
129+
config3.DefaultStepSize,
130+
config3.DefaultStepsInFrozenFile,
131+
dirs,
132+
logger,
133+
)
134+
if err != nil {
135+
logger.Error("Failed to init history", "error", err)
136+
return
137+
}
138+
history.Scan(toStep * config3.DefaultStepSize)
139+
140+
roTx := history.BeginFilesRo()
141+
defer roTx.Close()
142+
143+
for i := uint64(0); i < roTx.FirstStepNotInFiles().ToTxNum(config3.DefaultStepSize); {
144+
fromTxNum := i
145+
i += config3.DefaultStepSize * config3.DefaultStepsInFrozenFile
146+
147+
if i > roTx.FirstStepNotInFiles().ToTxNum(config3.DefaultStepSize) {
148+
i = roTx.FirstStepNotInFiles().ToTxNum(config3.DefaultStepSize)
149+
}
150+
151+
err = roTx.CompactRange(context.TODO(), fromTxNum, i)
152+
if err != nil {
153+
logger.Error("Failed to rebuild history", "error", err)
154+
return
155+
}
156+
}
157+
},
158+
}

db/state/history.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,31 @@ func (ht *HistoryRoTx) HistoryDump(fromTxNum, toTxNum int, dumpTo io.Writer) err
14171417
return nil
14181418
}
14191419

1420+
// CompactRange rebuilds the history files within the specified transaction range by performing a forced self-merge.
1421+
// If the range contains existing static files, the method collects all files belonging to that span and merges them.
1422+
func (ht *HistoryRoTx) CompactRange(ctx context.Context, fromTxNum, toTxNum uint64) error {
1423+
if len(ht.iit.files) == 0 {
1424+
return nil
1425+
}
1426+
1427+
mergeRange := NewHistoryRanges(
1428+
*NewMergeRange("", true, fromTxNum, toTxNum),
1429+
*NewMergeRange("", true, fromTxNum, toTxNum),
1430+
)
1431+
1432+
efFiles, vFiles, err := ht.staticFilesInRange(mergeRange)
1433+
if err != nil {
1434+
return err
1435+
}
1436+
1437+
_, _, err = ht.mergeFiles(ctx, efFiles, vFiles, mergeRange, background.NewProgressSet())
1438+
if err != nil {
1439+
return err
1440+
}
1441+
1442+
return nil
1443+
}
1444+
14201445
func (ht *HistoryRoTx) idxRangeOnDB(key []byte, startTxNum, endTxNum int, asc order.By, limit int, roTx kv.Tx) (stream.U64, error) {
14211446
if ht.h.HistoryLargeValues {
14221447
from := make([]byte, len(key)+8)

0 commit comments

Comments
 (0)