Skip to content

Commit

Permalink
erigon seg rm-state --domain=receipt support (#12016)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Sep 19, 2024
1 parent 7bcad69 commit 3dde8be
Showing 1 changed file with 71 additions and 43 deletions.
114 changes: 71 additions & 43 deletions turbo/app/snapshots_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,16 @@ var snapshotCommand = cli.Command{
Flags: joinFlags([]cli.Flag{&utils.DataDirFlag}),
},
{
Name: "rm-state-snapshots",
Action: doRmStateSnapshots,
Flags: joinFlags([]cli.Flag{&utils.DataDirFlag, &cli.StringFlag{Name: "step", Required: false}, &cli.BoolFlag{Name: "latest", Required: false}}),
Name: "rm-state-snapshots",
Aliases: []string{"rm-state-segments", "rm-state"},
Action: doRmStateSnapshots,
Flags: joinFlags([]cli.Flag{
&utils.DataDirFlag,
&cli.StringFlag{Name: "step"},
&cli.BoolFlag{Name: "latest"},
&cli.StringFlag{Name: "domain"},
},
),
},
{
Name: "diff",
Expand Down Expand Up @@ -302,13 +309,6 @@ func doRmStateSnapshots(cliCtx *cli.Context) error {
dirs := datadir.New(cliCtx.String(utils.DataDirFlag.Name))

removeLatest := cliCtx.Bool("latest")
steprm := cliCtx.String("step")
if steprm == "" && !removeLatest {
return errors.New("step to remove is required (eg 0-2) OR flag --latest provided")
}
if steprm != "" {
removeLatest = false // --step has higher priority
}

_maxFrom := uint64(0)
files := make([]snaptype.FileInfo, 0)
Expand Down Expand Up @@ -346,52 +346,80 @@ func doRmStateSnapshots(cliCtx *cli.Context) error {
}
}

var minS, maxS uint64
if removeLatest {
AllowPruneSteps:
fmt.Printf("remove latest snapshot files with stepFrom=%d?\n1) Remove\n2) Exit\n (pick number): ", _maxFrom)
var ans uint8
_, err := fmt.Scanf("%d\n", &ans)
if err != nil {
return err
if cliCtx.IsSet("step") {
steprm := cliCtx.String("step")
if steprm == "" && !removeLatest {
return errors.New("step to remove is required (eg 0-2) OR flag --latest provided")
}
switch ans {
case 1:
minS, maxS = _maxFrom, math.MaxUint64
break
case 2:
return nil
default:
fmt.Printf("invalid input: %d; Just an answer number expected.\n", ans)
goto AllowPruneSteps
if steprm != "" {
removeLatest = false // --step has higher priority
}
} else if steprm != "" {
parseStep := func(step string) (uint64, uint64, error) {
var from, to uint64
if _, err := fmt.Sscanf(step, "%d-%d", &from, &to); err != nil {
return 0, 0, fmt.Errorf("step expected in format from-to, got %s", step)

var minS, maxS uint64
if removeLatest {
AllowPruneSteps:
fmt.Printf("remove latest snapshot files with stepFrom=%d?\n1) Remove\n2) Exit\n (pick number): ", _maxFrom)
var ans uint8
_, err := fmt.Scanf("%d\n", &ans)
if err != nil {
return err
}
switch ans {
case 1:
minS, maxS = _maxFrom, math.MaxUint64
break
case 2:
return nil
default:
fmt.Printf("invalid input: %d; Just an answer number expected.\n", ans)
goto AllowPruneSteps
}
} else if steprm != "" {
parseStep := func(step string) (uint64, uint64, error) {
var from, to uint64
if _, err := fmt.Sscanf(step, "%d-%d", &from, &to); err != nil {
return 0, 0, fmt.Errorf("step expected in format from-to, got %s", step)
}
return from, to, nil
}
var err error
minS, maxS, err = parseStep(steprm)
if err != nil {
return err
}
} else {
panic("unexpected arguments")
}

var removed int
for _, res := range files {
if res.From >= minS && res.To <= maxS {
if err := os.Remove(res.Path); err != nil {
return fmt.Errorf("failed to remove %s: %w", res.Path, err)
}
removed++
}
return from, to, nil
}
var err error
minS, maxS, err = parseStep(steprm)
fmt.Printf("removed %d state segments files\n", removed)
}
if cliCtx.IsSet("domain") {
domainToRemove, err := kv.String2Domain(cliCtx.String("domain"))
if err != nil {
return err
}
} else {
panic("unexpected arguments")
}

var removed int
for _, res := range files {
if res.From >= minS && res.To <= maxS {
var removed int
for _, res := range files {
if !strings.Contains(res.Name(), domainToRemove.String()) {
continue
}
if err := os.Remove(res.Path); err != nil {
return fmt.Errorf("failed to remove %s: %w", res.Path, err)
}
removed++
}
fmt.Printf("removed %d state segments files\n", removed)
}
fmt.Printf("removed %d state snapshot files\n", removed)

return nil
}

Expand Down

0 comments on commit 3dde8be

Please sign in to comment.