Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
57 changes: 57 additions & 0 deletions cmd/integration/commands/state_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package commands

import (
"context"
"os"

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

withDataDir2(rebuildCmd)
withHistoryDomain(rebuildCmd)

historyCmd.AddCommand(printCmd)
historyCmd.AddCommand(rebuildCmd)

rootCmd.AddCommand(historyCmd)
}

Expand Down Expand Up @@ -98,3 +104,54 @@ var printCmd = &cobra.Command{
}
},
}

var rebuildCmd = &cobra.Command{
Use: "rebuild",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no description of cli method - in erigon snapshots --help will be impossible to understand what rebuild sub-command does - because it's generic name.

Run: func(cmd *cobra.Command, args []string) {
logger := debug.SetupCobra(cmd, "integration")

dirs, l, err := datadir.New(datadirCli).MustFlock()
if err != nil {
logger.Error("Opening Datadir", "error", err)
return
}
defer l.Unlock()

domainKV, err := kv.String2Domain(historyDomain)
if err != nil {
logger.Error("Failed to resolve domain", "error", err)
return
}

history, err := state.NewHistory(
statecfg.Schema.GetDomainCfg(domainKV).Hist,
config3.DefaultStepSize,
config3.DefaultStepsInFrozenFile,
dirs,
logger,
)
if err != nil {
logger.Error("Failed to init history", "error", err)
return
}
history.Scan(toStep * config3.DefaultStepSize)

roTx := history.BeginFilesRo()
defer roTx.Close()

for i := uint64(0); i < roTx.FirstStepNotInFiles().ToTxNum(config3.DefaultStepSize); {
fromTxNum := i
i += config3.DefaultStepSize * config3.DefaultStepsInFrozenFile

if i > roTx.FirstStepNotInFiles().ToTxNum(config3.DefaultStepSize) {
i = roTx.FirstStepNotInFiles().ToTxNum(config3.DefaultStepSize)
}

err = roTx.Rebuild(context.TODO(), fromTxNum, i)
if err != nil {
logger.Error("Failed to rebuild history", "error", err)
return
}
}
},
}
23 changes: 23 additions & 0 deletions db/state/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,29 @@ func (ht *HistoryRoTx) HistoryDump(fromTxNum, toTxNum int, dumpTo io.Writer) err
return nil
}

func (ht *HistoryRoTx) Rebuild(ctx context.Context, fromTxNum, toTxNum uint64) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebuild method doesn't have comment - so i'm not sure what this method intended to do. I see it calling merge - if it's just a merge then why it's not called Merge?

if len(ht.iit.files) == 0 {
return nil
}

mergeRange := NewHistoryRanges(
*NewMergeRange("", true, fromTxNum, toTxNum),
*NewMergeRange("", true, fromTxNum, toTxNum),
)

efFiles, vFiles, err := ht.staticFilesInRange(mergeRange)
if err != nil {
return err
}

_, _, err = ht.mergeFiles(ctx, efFiles, vFiles, mergeRange, background.NewProgressSet())
if err != nil {
return err
}

return nil
}

func (ht *HistoryRoTx) idxRangeOnDB(key []byte, startTxNum, endTxNum int, asc order.By, limit int, roTx kv.Tx) (stream.U64, error) {
if ht.h.HistoryLargeValues {
from := make([]byte, len(key)+8)
Expand Down
Loading