Skip to content

Commit

Permalink
etcdctlv3: Add option to defrag a data directory directly, for cases …
Browse files Browse the repository at this point in the history
…where etcd is not running.
  • Loading branch information
jpbetz committed Aug 7, 2017
1 parent a9b9ef5 commit 96ded1a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Documentation/op-guide/maintenance.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ $ etcdctl defrag
Finished defragmenting etcd member[127.0.0.1:2379]
```

To defragment an etcd data directory directly, while etcd is not running, use the command:

``` sh
$ etcdctl defrag --data-dir <path-to-etcd-data-dir>
```

## Space quota

The space quota in `etcd` ensures the cluster operates in a reliable fashion. Without a space quota, `etcd` may suffer from poor performance if the keyspace grows excessively large, or it may simply run out of storage space, leading to unpredictable cluster behavior. If the keyspace's backend database for any member exceeds the space quota, `etcd` raises a cluster-wide alarm that puts the cluster into a maintenance mode which only accepts key reads and deletes. Only after freeing enough space in the keyspace and defragmenting the backend database, along with clearing the space quota alarm can the cluster resume normal operation.
Expand Down
13 changes: 10 additions & 3 deletions etcdctl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,10 @@ If NOSPACE alarm is present:

### DEFRAG

DEFRAG defragments the backend database file for a set of given endpoints. When an etcd member reclaims storage space
from deleted and compacted keys, the space is kept in a free list and the database file remains the same size. By defragmenting
the database, the etcd member releases this free space back to the file system.
DEFRAG defragments the backend database file for a set of given endpoints while etcd is running, or directly defragments an
etcd data directory while etcd is not running. When an etcd member reclaims storage space from deleted and compacted keys, the
space is kept in a free list and the database file remains the same size. By defragmenting the database, the etcd member
releases this free space back to the file system.

#### Output

Expand All @@ -752,6 +753,12 @@ For each endpoints, prints a message indicating whether the endpoint was success
# Failed to defragment etcd member[badendpoint:2379] (grpc: timed out trying to connect)
```

To defragment a data directory directly, use the `--data-dir` flag:

``` bash
./etcdctl defrag --data-dir default.etcd
```

#### Remarks

DEFRAG returns a zero exit code only if it succeeded defragmenting all given endpoints.
Expand Down
40 changes: 39 additions & 1 deletion etcdctl/ctlv3/command/defrag_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,38 @@ package command
import (
"fmt"
"os"
"path/filepath"
"time"

"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", "", "Optional. If present, defragments a data directory not in use by etcd.")
return cmd
}

func defragCommandFunc(cmd *cobra.Command, args []string) {
if len(defragDataDir) > 0 {
err := defragData(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 +67,23 @@ func defragCommandFunc(cmd *cobra.Command, args []string) {
os.Exit(ExitError)
}
}

func defragData(dataDir string) error {
var be backend.Backend

bch := make(chan struct{})
dbDir := filepath.Join(dataDir, "member", "snap", "db")
go func() {
defer close(bch)
be = backend.NewDefaultBackend(dbDir)

}()
select {
case <-bch:
case <-time.After(time.Second):
fmt.Fprintf(os.Stderr, "waiting for etcd to close and release its lock on %q. "+
"To defrag a running etcd instance, omit --data-dir.\n", dbDir)
<-bch
}
return be.Defrag()
}

0 comments on commit 96ded1a

Please sign in to comment.