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

etcdctl: add timeout to snapshot save command #10301

Merged
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
9 changes: 8 additions & 1 deletion etcdctl/ctlv3/command/snapshot_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,15 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
sp := snapshot.NewV3(lg)
cfg := mustClientCfgFromCmd(cmd)

// if user does not specify "--command-timeout" flag, there will be no timeout for snapshot save command
ctx, cancel := context.WithCancel(context.Background())
if isCommandTimeoutFlagSet(cmd) {
ctx, cancel = commandCtx(cmd)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like you could condense this a bit.

ctx, cancel := context.WithCancel(context.Background())
if ifCommandTimeoutFlagSet(cmd) {
	ctx, cancel = commandCtx(cmd)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

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 isCommandTimeoutFlagSet(cmd *cobra.Command) bool {
commandTimeoutFlag := cmd.Flags().Lookup("command-timeout")
if commandTimeoutFlag == nil {
panic("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