Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etcdctlv3: Add option to defrag a data directory directly #8367

Merged
merged 1 commit into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 18 additions & 4 deletions etcdctl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -734,11 +734,16 @@ If NOSPACE alarm is present:
# alarm:NOSPACE
```

### DEFRAG
### DEFRAG [options]

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.

#### Options

- data-dir -- Optional. If present, defragments a data directory not in use by etcd.

#### Output

Expand All @@ -752,6 +757,15 @@ 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
# Defragment while etcd is not running
./etcdctl defrag --data-dir default.etcd
# success (exit status 0)
# Error: cannot open database at default.etcd/member/snap/db
```

#### 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()
}