Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cleanup and repair commands #4587

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
136 changes: 136 additions & 0 deletions cmd/bee/cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
optionNameValidationPin = "validate-pin"
optionNameCollectionPin = "pin"
optionNameOutputLocation = "output"
optionNameRepair = "repair"
)

func (c *command) initDBCmd() {
Expand All @@ -46,6 +47,8 @@ func (c *command) initDBCmd() {
dbCompactCmd(cmd)
dbValidateCmd(cmd)
dbValidatePinsCmd(cmd)
dbFixRefCntCmd(cmd)
dbFixSharkyCmd(cmd)

c.root.AddCommand(cmd)
}
Expand Down Expand Up @@ -172,6 +175,139 @@ func dbCompactCmd(cmd *cobra.Command) {
cmd.AddCommand(c)
}

func dbFixRefCntCmd(cmd *cobra.Command) {
c := &cobra.Command{
Use: "fixrefcnt",
Short: "Recalculates chunk reference counters.",
RunE: func(cmd *cobra.Command, args []string) (err error) {
v, err := cmd.Flags().GetString(optionNameVerbosity)
if err != nil {
return fmt.Errorf("get verbosity: %w", err)
}
v = strings.ToLower(v)
logger, err := newLogger(cmd, v)
if err != nil {
return fmt.Errorf("new logger: %w", err)
}

dataDir, err := cmd.Flags().GetString(optionNameDataDir)
if err != nil {
return fmt.Errorf("get data-dir: %w", err)
}
if dataDir == "" {
return errors.New("no data-dir provided")
}

repair, err := cmd.Flags().GetBool(optionNameRepair)
if err != nil {
return fmt.Errorf("get repair: %w", err)
}

logger.Warning("fixrefcnt recalculates the chunk reference counter for all known chunks.")
if !repair {
logger.Warning("By default, it will only report discrepancies unless --" + optionNameRepair + " is specified.")
}
logger.Warning("Also, fixrefcnt will not reduce the RefCnt below 1. This may change in the future.")
logger.Warning(" Discrepancies logged at Warning level.")
logger.Warning(" Progress logged at Info level.")
logger.Warning(" ???? logged at Debug level.")

if repair {
logger.Warning("starting to repair the DB with data-dir", "path", dataDir)
logger.Warning("this is VERY experimental and may completely corrupt your localstore!")
logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...")
time.Sleep(10 * time.Second)
logger.Warning("proceeding with database repair...")
}

localstorePath := path.Join(dataDir, "localstore")

err = storer.FixRefCnt(context.Background(), localstorePath, &storer.Options{
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
}, repair)
if err != nil {
return fmt.Errorf("localstore: %w", err)
}

return nil
},
}
c.Flags().String(optionNameDataDir, "", "data directory")
c.Flags().String(optionNameVerbosity, "info", "verbosity level")
c.Flags().Bool(optionNameRepair, false, "actually (attempt to) FIX any discovered discrepancies")
cmd.AddCommand(c)
}

func dbFixSharkyCmd(cmd *cobra.Command) {
c := &cobra.Command{
Use: "fixsharky",
Short: "Removes invalid references to the sharky store.",
RunE: func(cmd *cobra.Command, args []string) (err error) {
v, err := cmd.Flags().GetString(optionNameVerbosity)
if err != nil {
return fmt.Errorf("get verbosity: %w", err)
}
v = strings.ToLower(v)
logger, err := newLogger(cmd, v)
if err != nil {
return fmt.Errorf("new logger: %w", err)
}

dataDir, err := cmd.Flags().GetString(optionNameDataDir)
if err != nil {
return fmt.Errorf("get data-dir: %w", err)
}
if dataDir == "" {
return errors.New("no data-dir provided")
}

repair, err := cmd.Flags().GetBool(optionNameRepair)
if err != nil {
return fmt.Errorf("get repair: %w", err)
}

logger.Warning("fixsharky ensures that all references to sharky return a chunk that hashes to the expected reference.")
if !repair {
logger.Warning("By default, it will only report issues (like validate) unless --" + optionNameRepair + " is specified.")
}
logger.Warning("Note: This WILL leave (hopefully less impactful) inconsistencies in your localstore.")
logger.Warning(" Discrepancies logged at Warning level.")
logger.Warning(" Progress logged at Info level.")
logger.Warning(" SOC chunks logged at Debug level.")

if repair {
logger.Warning("starting to repair the DB with data-dir", "path", dataDir)
logger.Warning("this is VERY experimental and may completely corrupt your localstore!")
logger.Warning("you have another 10 seconds to change your mind and kill this process with CTRL-C...")
time.Sleep(10 * time.Second)
logger.Warning("proceeding with database repair...")
}

localstorePath := path.Join(dataDir, "localstore")

err = storer.FixSharky(context.Background(), localstorePath, &storer.Options{
Logger: logger,
RadiusSetter: noopRadiusSetter{},
Batchstore: new(postage.NoOpBatchStore),
ReserveCapacity: node.ReserveCapacity,
}, repair)
if err != nil {
return fmt.Errorf("localstore: %w", err)
}

return nil
},
}
c.Flags().String(optionNameDataDir, "", "data directory")
c.Flags().String(optionNameVerbosity, "info", "verbosity level")
c.Flags().Bool(optionNameRepair, false, "actually (attempt to) FIX any discovered inconsistencies")
c.Flags().Bool(optionNameValidation, false, "run chunk validation checks again after any repairs")
cmd.AddCommand(c)
}

func dbValidatePinsCmd(cmd *cobra.Command) {
c := &cobra.Command{
Use: "validate-pin",
Expand Down
Loading
Loading