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/coreos/etcd/mvcc/backend"
24	"github.com/spf13/cobra"
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.Flags().StringVar(&defragDataDir, "data-dir", "", "Optional. If present, defragments a data directory not in use by etcd.")
39	return cmd
40}
41
42func defragCommandFunc(cmd *cobra.Command, args []string) {
43	if len(defragDataDir) > 0 {
44		err := defragData(defragDataDir)
45		if err != nil {
46			fmt.Fprintf(os.Stderr, "Failed to defragment etcd data[%s] (%v)\n", defragDataDir, err)
47			os.Exit(ExitError)
48		}
49		return
50	}
51
52	failures := 0
53	c := mustClientFromCmd(cmd)
54	for _, ep := range c.Endpoints() {
55		ctx, cancel := commandCtx(cmd)
56		_, err := c.Defragment(ctx, ep)
57		cancel()
58		if err != nil {
59			fmt.Fprintf(os.Stderr, "Failed to defragment etcd member[%s] (%v)\n", ep, err)
60			failures++
61		} else {
62			fmt.Printf("Finished defragmenting etcd member[%s]\n", ep)
63		}
64	}
65
66	if failures != 0 {
67		os.Exit(ExitError)
68	}
69}
70
71func defragData(dataDir string) error {
72	var be backend.Backend
73
74	bch := make(chan struct{})
75	dbDir := filepath.Join(dataDir, "member", "snap", "db")
76	go func() {
77		defer close(bch)
78		be = backend.NewDefaultBackend(dbDir)
79
80	}()
81	select {
82	case <-bch:
83	case <-time.After(time.Second):
84		fmt.Fprintf(os.Stderr, "waiting for etcd to close and release its lock on %q. "+
85			"To defrag a running etcd instance, omit --data-dir.\n", dbDir)
86		<-bch
87	}
88	return be.Defrag()
89}
90