2022-05-23 16:26:27 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-10-14 08:36:54 +00:00
|
|
|
"strings"
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-05-24 08:22:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
2022-05-23 16:26:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
shardModeFlag = "mode"
|
|
|
|
shardIDFlag = "id"
|
2022-10-10 18:14:12 +00:00
|
|
|
shardAllFlag = "all"
|
2022-05-23 16:26:27 +00:00
|
|
|
shardClearErrorsFlag = "clear-errors"
|
|
|
|
)
|
|
|
|
|
2022-10-14 08:36:54 +00:00
|
|
|
// maps string command input to control.ShardMode. To support new mode, it's
|
|
|
|
// enough to add the map entry.
|
|
|
|
var mShardModes = map[string]control.ShardMode{
|
|
|
|
"read-only": control.ShardMode_READ_ONLY,
|
|
|
|
"read-write": control.ShardMode_READ_WRITE,
|
|
|
|
"degraded-read-write": control.ShardMode_DEGRADED,
|
|
|
|
"degraded-read-only": control.ShardMode_DEGRADED_READ_ONLY,
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:26:27 +00:00
|
|
|
var setShardModeCmd = &cobra.Command{
|
|
|
|
Use: "set-mode",
|
|
|
|
Short: "Set work mode of the shard",
|
|
|
|
Long: "Set work mode of the shard",
|
|
|
|
Run: setShardMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initControlSetShardModeCmd() {
|
2022-10-17 15:37:15 +00:00
|
|
|
initControlFlags(setShardModeCmd)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
flags := setShardModeCmd.Flags()
|
2022-10-10 17:58:28 +00:00
|
|
|
flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding")
|
2022-10-10 18:14:12 +00:00
|
|
|
flags.Bool(shardAllFlag, false, "Process all shards")
|
2022-10-14 08:36:54 +00:00
|
|
|
|
|
|
|
modes := make([]string, 0, len(mShardModes))
|
|
|
|
for strMode := range mShardModes {
|
|
|
|
modes = append(modes, "'"+strMode+"'")
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:26:27 +00:00
|
|
|
flags.String(shardModeFlag, "",
|
2022-10-14 08:36:54 +00:00
|
|
|
fmt.Sprintf("New shard mode keyword (%s)", strings.Join(modes, ",")),
|
2022-05-23 16:26:27 +00:00
|
|
|
)
|
|
|
|
flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0")
|
2022-10-10 18:14:12 +00:00
|
|
|
|
|
|
|
setShardModeCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag)
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setShardMode(cmd *cobra.Command, _ []string) {
|
2022-05-24 08:22:23 +00:00
|
|
|
pk := key.Get(cmd)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2022-10-14 08:36:54 +00:00
|
|
|
strMode, _ := cmd.Flags().GetString(shardModeFlag)
|
|
|
|
|
|
|
|
mode, ok := mShardModes[strMode]
|
|
|
|
if !ok {
|
|
|
|
common.ExitOnErr(cmd, "", fmt.Errorf("unsupported mode %s", strMode))
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := new(control.SetShardModeRequest)
|
|
|
|
|
|
|
|
body := new(control.SetShardModeRequest_Body)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
body.SetMode(mode)
|
2022-10-10 17:58:28 +00:00
|
|
|
body.SetShardIDList(getShardIDList(cmd))
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
reset, _ := cmd.Flags().GetBool(shardClearErrorsFlag)
|
|
|
|
body.ClearErrorCounter(reset)
|
|
|
|
|
|
|
|
signRequest(cmd, pk, req)
|
|
|
|
|
|
|
|
cli := getClient(cmd, pk)
|
|
|
|
|
|
|
|
var resp *control.SetShardModeResponse
|
|
|
|
var err error
|
|
|
|
err = cli.ExecRaw(func(client *rawclient.Client) error {
|
|
|
|
resp, err = control.SetShardMode(client, req)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
|
|
|
|
|
|
|
verifyResponse(cmd, resp.GetSignature(), resp.GetBody())
|
|
|
|
|
|
|
|
cmd.Println("Shard mode update request successfully sent.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getShardID(cmd *cobra.Command) []byte {
|
|
|
|
sid, _ := cmd.Flags().GetString(shardIDFlag)
|
|
|
|
raw, err := base58.Decode(sid)
|
|
|
|
common.ExitOnErr(cmd, "incorrect shard ID encoding: %w", err)
|
|
|
|
return raw
|
|
|
|
}
|
2022-10-10 17:58:28 +00:00
|
|
|
|
|
|
|
func getShardIDList(cmd *cobra.Command) [][]byte {
|
2022-10-10 18:14:12 +00:00
|
|
|
all, _ := cmd.Flags().GetBool(shardAllFlag)
|
|
|
|
if all {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:58:28 +00:00
|
|
|
sidList, _ := cmd.Flags().GetStringSlice(shardIDFlag)
|
|
|
|
if len(sidList) == 0 {
|
2022-10-10 18:14:12 +00:00
|
|
|
common.ExitOnErr(cmd, "", fmt.Errorf("either --%s or --%s flag must be provided", shardIDFlag, shardAllFlag))
|
2022-10-10 17:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We can sort the ID list and perform this check without additional allocations,
|
|
|
|
// but preserving the user order is a nice thing to have.
|
|
|
|
// Also, this is a CLI, we don't care too much about this.
|
|
|
|
seen := make(map[string]struct{})
|
|
|
|
for i := range sidList {
|
|
|
|
if _, ok := seen[sidList[i]]; ok {
|
|
|
|
common.ExitOnErr(cmd, "", fmt.Errorf("duplicated shard IDs: %s", sidList[i]))
|
|
|
|
}
|
|
|
|
seen[sidList[i]] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([][]byte, 0, len(sidList))
|
|
|
|
for i := range sidList {
|
|
|
|
raw, err := base58.Decode(sidList[i])
|
|
|
|
common.ExitOnErr(cmd, "incorrect shard ID encoding: %w", err)
|
|
|
|
|
|
|
|
res = append(res, raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|