1// Copyright 2016 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package command
16
17import (
18	"fmt"
19	"os"
20	"path/filepath"
21	"time"
22
23	"github.com/spf13/cobra"
24	"go.etcd.io/etcd/mvcc/backend"
25)
26
27var (
28	defragDataDir string
29)
30
31// NewDefragCommand returns the cobra command for "Defrag".
32func NewDefragCommand() *cobra.Command {
33	cmd := &cobra.Command{
34		Use:   "defrag",
35		Short: "Defragments the storage of the etcd members with given endpoints",
36		Run:   defragCommandFunc,
37	}
38	cmd.PersistentFlags().BoolVar(&epClusterEndpoints, "cluster", false, "use all endpoints from the cluster member list")
39	cmd.Flags().StringVar(&defragDataDir, "data-dir", "", "Optional. If present, defragments a data directory not in use by etcd.")
40	return cmd
41}
42
43func defragCommandFunc(cmd *cobra.Command, args []string) {
44	if len(defragDataDir) > 0 {
45		err := defragData(defragDataDir)
46		if err != nil {
47			fmt.Fprintf(os.Stderr, "Failed to defragment etcd data[%s] (%v)\n", defragDataDir, err)
48			os.Exit(ExitError)
49		}
50		return
51	}
52
53	failures := 0
54	c := mustClientFromCmd(cmd)
55	for _, ep := range endpointsFromCluster(cmd) {
56		ctx, cancel := commandCtx(cmd)
57		_, err := c.Defragment(ctx, ep)
58		cancel()
59		if err != nil {
60			fmt.Fprintf(os.Stderr, "Failed to defragment etcd member[%s] (%v)\n", ep, err)
61			failures++
62		} else {
63			fmt.Printf("Finished defragmenting etcd member[%s]\n", ep)
64		}
65	}
66
67	if failures != 0 {
68		os.Exit(ExitError)
69	}
70}
71
72func defragData(dataDir string) error {
73	var be backend.Backend
74
75	bch := make(chan struct{})
76	dbDir := filepath.Join(dataDir, "member", "snap", "db")
77	go func() {
78		defer close(bch)
79		be = backend.NewDefaultBackend(dbDir)
80
81	}()
82	select {
83	case <-bch:
84	case <-time.After(time.Second):
85		fmt.Fprintf(os.Stderr, "waiting for etcd to close and release its lock on %q. "+
86			"To defrag a running etcd instance, omit --data-dir.\n", dbDir)
87		<-bch
88	}
89	return be.Defrag()
90}
91