Skip to content

Commit

Permalink
cli: Increase default execution timeout in async-await commands
Browse files Browse the repository at this point in the history
Some CLI commands like `container create` are async-await. Previously,
default execution timeout was 15s for such commands. Since more than one
RPC is needed for theses commands (e.g. creation request and polling),
there is a need to increase default timeout in waiting mode.

Add `GetCommandContextWithAwait` and use it in `create`, `delete` and
`set-eacl` commands of `container` section. Make command to time out
after 1m if `--await` flag is set while `--timeout` one is omitted.

Closes nspcc-dev#2124.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Jun 13, 2023
1 parent 2610ede commit c3c286f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
13 changes: 13 additions & 0 deletions cmd/neofs-cli/internal/commonflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ func Bind(cmd *cobra.Command) {
_ = viper.BindPFlag(Timeout, ff.Lookup(Timeout))
}

// GetCommandContextWithAwait works like GetCommandContext but uses specified
// await timeout if 'timeout' flag is omitted and given boolean await flag is
// set.
func GetCommandContextWithAwait(cmd *cobra.Command, awaitFlag string, awaitTimeout time.Duration) (context.Context, context.CancelFunc) {
if !viper.IsSet(Timeout) {
if await, _ := cmd.Flags().GetBool(awaitFlag); await {
return getCommandContext(cmd, awaitTimeout)
}
}

return GetCommandContext(cmd)
}

// GetCommandContext returns cmd context with timeout specified in 'timeout' flag
// if the flag is set.
func GetCommandContext(cmd *cobra.Command) (context.Context, context.CancelFunc) {
Expand Down
12 changes: 8 additions & 4 deletions cmd/neofs-cli/modules/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var createContainerCmd = &cobra.Command{
Long: `Create new container and register it in the NeoFS.
It will be stored in sidechain when inner ring will accepts it.`,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := commonflags.GetCommandContext(cmd)
ctx, cancel := getAwaitContext(cmd)
defer cancel()

placementPolicy, err := parseContainerPolicy(cmd, containerPolicy)
Expand Down Expand Up @@ -134,17 +134,21 @@ It will be stored in sidechain when inner ring will accepts it.`,
getPrm.SetClient(cli)
getPrm.SetContainer(id)

for i := 0; i < awaitTimeout; i++ {
for {
time.Sleep(1 * time.Second)

select {
case <-ctx.Done():
common.ExitOnErr(cmd, "", errCreateTimeout)
default:
}

_, err := internalclient.GetContainer(ctx, getPrm)
if err == nil {
cmd.Println("container has been persisted on sidechain")
return
}
}

common.ExitOnErr(cmd, "", errCreateTimeout)
}
},
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/neofs-cli/modules/container/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var deleteContainerCmd = &cobra.Command{
Long: `Delete existing container.
Only owner of the container has a permission to remove container.`,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := commonflags.GetCommandContext(cmd)
ctx, cancel := getAwaitContext(cmd)
defer cancel()

id := parseContainerID(cmd)
Expand Down Expand Up @@ -107,17 +107,21 @@ Only owner of the container has a permission to remove container.`,
getPrm.SetClient(cli)
getPrm.SetContainer(id)

for i := 0; i < awaitTimeout; i++ {
for {
time.Sleep(1 * time.Second)

select {
case <-ctx.Done():
common.ExitOnErr(cmd, "", errDeleteTimeout)
default:
}

_, err := internalclient.GetContainer(ctx, getPrm)
if err != nil {
cmd.Println("container has been removed:", containerID)
return
}
}

common.ExitOnErr(cmd, "", errDeleteTimeout)
}
},
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/neofs-cli/modules/container/set_eacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var setExtendedACLCmd = &cobra.Command{
Long: `Set new extended ACL table for container.
Container ID in EACL table will be substituted with ID from the CLI.`,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := commonflags.GetCommandContext(cmd)
ctx, cancel := getAwaitContext(cmd)
defer cancel()

id := parseContainerID(cmd)
Expand Down Expand Up @@ -71,9 +71,15 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
getEACLPrm.SetClient(cli)
getEACLPrm.SetContainer(id)

for i := 0; i < awaitTimeout; i++ {
for {
time.Sleep(1 * time.Second)

select {
case <-ctx.Done():
common.ExitOnErr(cmd, "", errSetEACLTimeout)
default:
}

res, err := internalclient.EACL(ctx, getEACLPrm)
if err == nil {
// compare binary values because EACL could have been set already
Expand All @@ -90,7 +96,6 @@ Container ID in EACL table will be substituted with ID from the CLI.`,
}
}

common.ExitOnErr(cmd, "", errSetEACLTimeout)
}
},
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/neofs-cli/modules/container/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package container

import (
"context"
"errors"
"time"

"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
Expand All @@ -12,8 +14,6 @@ import (

const (
attributeDelimiter = "="

awaitTimeout = 120 // in seconds
)

var (
Expand Down Expand Up @@ -55,3 +55,7 @@ func getSession(cmd *cobra.Command) *session.Container {

return &res
}

func getAwaitContext(cmd *cobra.Command) (context.Context, context.CancelFunc) {
return commonflags.GetCommandContextWithAwait(cmd, "await", time.Minute)
}

0 comments on commit c3c286f

Please sign in to comment.