Skip to content

Commit

Permalink
etcdctl: add timeout to snapshot save command
Browse files Browse the repository at this point in the history
snapshot save command by default has no timeout, but respects user
specified command timeout.
  • Loading branch information
jingyih committed Dec 5, 2018
1 parent 7f450bf commit bfcb5d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
14 changes: 13 additions & 1 deletion etcdctl/ctlv3/command/snapshot_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,20 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
sp := snapshot.NewV3(lg)
cfg := mustClientCfgFromCmd(cmd)

var (
ctx context.Context
cancel context.CancelFunc
)
if ifCommandTimeoutFlagSet(cmd) {
ctx, cancel = commandCtx(cmd)
} else {
// if user does not specify "--command-timeout" flag, there will be no timeout for snapshot save command
ctx, cancel = context.WithCancel(context.Background())
}
defer cancel()

path := args[0]
if err := sp.Save(context.TODO(), *cfg, path); err != nil {
if err := sp.Save(ctx, *cfg, path); err != nil {
ExitWithError(ExitInterrupted, err)
}
fmt.Printf("Snapshot saved at %s\n", path)
Expand Down
8 changes: 8 additions & 0 deletions etcdctl/ctlv3/command/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ func commandCtx(cmd *cobra.Command) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), timeOut)
}

func ifCommandTimeoutFlagSet(cmd *cobra.Command) bool {
commandTimeoutFlag := cmd.Flags().Lookup("command-timeout")
if commandTimeoutFlag == nil {
ExitWithError(ExitError, fmt.Errorf("expect command-timeout flag to exist"))
}
return commandTimeoutFlag.Changed
}

// get the process_resident_memory_bytes from <server:2379>/metrics
func endpointMemoryMetrics(host string) float64 {
residentMemoryKey := "process_resident_memory_bytes"
Expand Down

0 comments on commit bfcb5d3

Please sign in to comment.