Skip to content

Commit

Permalink
Add option to defrag a data directory directly, for cases where etcd …
Browse files Browse the repository at this point in the history
…is not running.
  • Loading branch information
jpbetz committed Aug 4, 2017
1 parent faa4a62 commit 57a8f32
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion etcdctl/ctlv3/command/defrag_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,37 @@ package command
import (
"fmt"
"os"
"path/filepath"

"github.com/coreos/etcd/mvcc/backend"
"github.com/spf13/cobra"
)

var (
defragDataDir string
)

// NewDefragCommand returns the cobra command for "Defrag".
func NewDefragCommand() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "defrag",
Short: "Defragments the storage of the etcd members with given endpoints",
Run: defragCommandFunc,
}
cmd.Flags().StringVar(&defragDataDir, "data-dir", "", "Path to the data directory")
return cmd
}

func defragCommandFunc(cmd *cobra.Command, args []string) {
if len(defragDataDir) > 0 {
err := defragFile(defragDataDir)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to defragment etcd data[%s] (%v)\n", defragDataDir, err)
os.Exit(ExitError)
}
return
}

failures := 0
c := mustClientFromCmd(cmd)
for _, ep := range c.Endpoints() {
Expand All @@ -49,3 +66,11 @@ func defragCommandFunc(cmd *cobra.Command, args []string) {
os.Exit(ExitError)
}
}

func defragDataDir(dataDir string) error {
dbFile := filepath.Join(dataDir, "member", "snap", "db")
cfg := backend.DefaultBackendConfig()
cfg.Path = dbFile
b := backend.New(cfg)
return b.Defrag()
}

0 comments on commit 57a8f32

Please sign in to comment.