Skip to content

Commit

Permalink
Merge pull request #443 from ahrtr/surgery_abandon_freelist_20230330
Browse files Browse the repository at this point in the history
cmd: add `surgery abandon-freelist` command
  • Loading branch information
ahrtr authored Mar 31, 2023
2 parents 3e560db + dc50a72 commit e6563ee
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
58 changes: 54 additions & 4 deletions cmd/bbolt/command_surgery_cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ var (
)

func newSurgeryCobraCommand() *cobra.Command {
cmd := &cobra.Command{
surgeryCmd := &cobra.Command{
Use: "surgery <subcommand>",
Short: "surgery related commands",
}

cmd.AddCommand(newSurgeryClearPageElementsCommand())
surgeryCmd.AddCommand(newSurgeryClearPageElementsCommand())
surgeryCmd.AddCommand(newSurgeryFreelistCommand())

return cmd
return surgeryCmd
}

func newSurgeryClearPageElementsCommand() *cobra.Command {
Expand All @@ -42,7 +43,6 @@ func newSurgeryClearPageElementsCommand() *cobra.Command {
}
return nil
},

RunE: surgeryClearPageElementFunc,
}

Expand Down Expand Up @@ -78,3 +78,53 @@ func surgeryClearPageElementFunc(cmd *cobra.Command, args []string) error {
fmt.Fprintf(os.Stdout, "All elements in [%d, %d) in page %d were cleared\n", surgeryStartElementIdx, surgeryEndElementIdx, surgeryPageId)
return nil
}

// TODO(ahrtr): add `bbolt surgery freelist rebuild/check ...` commands,
// and move all `surgery freelist` commands into a separate file,
// e.g command_surgery_freelist.go.
func newSurgeryFreelistCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "freelist <subcommand>",
Short: "freelist related surgery commands",
}

cmd.AddCommand(newSurgeryFreelistAbandonCommand())

return cmd
}

func newSurgeryFreelistAbandonCommand() *cobra.Command {
abandonFreelistCmd := &cobra.Command{
Use: "abandon <bbolt-file> [options]",
Short: "Abandon the freelist from both meta pages",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("db file path not provided")
}
if len(args) > 1 {
return errors.New("too many arguments")
}
return nil
},
RunE: surgeryFreelistAbandonFunc,
}

abandonFreelistCmd.Flags().StringVar(&surgeryTargetDBFilePath, "output", "", "path to the target db file")

return abandonFreelistCmd
}

func surgeryFreelistAbandonFunc(cmd *cobra.Command, args []string) error {
srcDBPath := args[0]

if err := copyFile(srcDBPath, surgeryTargetDBFilePath); err != nil {
return fmt.Errorf("[abandon-freelist] copy file failed: %w", err)
}

if err := surgeon.ClearFreelist(surgeryTargetDBFilePath); err != nil {
return fmt.Errorf("abandom-freelist command failed: %w", err)
}

fmt.Fprintf(os.Stdout, "The freelist was abandoned in both meta pages.\nIt may cause some delay on next startup because bbolt needs to scan the whole db to reconstruct the free list.\n")
return nil
}
29 changes: 29 additions & 0 deletions cmd/bbolt/command_surgery_cobra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
bolt "go.etcd.io/bbolt"
main "go.etcd.io/bbolt/cmd/bbolt"
"go.etcd.io/bbolt/internal/btesting"
"go.etcd.io/bbolt/internal/common"
"go.etcd.io/bbolt/internal/guts_cli"
)

Expand Down Expand Up @@ -428,3 +429,31 @@ func testSurgeryClearPageElementsWithOverflow(t *testing.T, startIdx, endIdx int

compareDataAfterClearingElement(t, srcPath, output, pageId, false, startIdx, endIdx)
}

func TestSurgery_Freelist_Abandon(t *testing.T) {
pageSize := 4096
db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: pageSize})
srcPath := db.Path()

defer requireDBNoChange(t, dbData(t, srcPath), srcPath)

rootCmd := main.NewRootCommand()
output := filepath.Join(t.TempDir(), "db")
rootCmd.SetArgs([]string{
"surgery", "freelist", "abandon", srcPath,
"--output", output,
})
err := rootCmd.Execute()
require.NoError(t, err)

meta0 := loadMetaPage(t, output, 0)
assert.Equal(t, common.PgidNoFreelist, meta0.Freelist())
meta1 := loadMetaPage(t, output, 1)
assert.Equal(t, common.PgidNoFreelist, meta1.Freelist())
}

func loadMetaPage(t *testing.T, dbPath string, pageID uint64) *common.Meta {
_, buf, err := guts_cli.ReadPage(dbPath, 0)
require.NoError(t, err)
return common.LoadPageMeta(buf)
}
4 changes: 2 additions & 2 deletions internal/surgeon/surgeon.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ func ClearPageElements(path string, pgId common.Pgid, start, end int, abandonFre

if preOverflow != p.Overflow() || p.IsBranchPage() {
if abandonFreelist {
return false, clearFreelist(path)
return false, ClearFreelist(path)
}
return true, nil
}

return false, nil
}

func clearFreelist(path string) error {
func ClearFreelist(path string) error {
if err := clearFreelistInMetaPage(path, 0); err != nil {
return fmt.Errorf("clearFreelist on meta page 0 failed: %w", err)
}
Expand Down

0 comments on commit e6563ee

Please sign in to comment.