2022-05-23 16:26:27 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2023-09-18 07:05:17 +00:00
|
|
|
"bytes"
|
2022-05-23 16:26:27 +00:00
|
|
|
"fmt"
|
2024-02-14 07:50:27 +00:00
|
|
|
"slices"
|
2022-10-14 08:36:54 +00:00
|
|
|
"strings"
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
rawclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
|
2022-05-23 16:26:27 +00:00
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"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
|
2022-10-15 07:57:06 +00:00
|
|
|
// enough to add the map entry. Modes are automatically printed in command help
|
|
|
|
// messages.
|
|
|
|
var mShardModes = map[string]struct {
|
|
|
|
val control.ShardMode
|
|
|
|
|
|
|
|
// flag to support shard mode implicitly without help message. The flag is set
|
|
|
|
// for values which are not expected to be set by users but still supported
|
|
|
|
// for developers.
|
|
|
|
unsafe bool
|
|
|
|
}{
|
|
|
|
"read-only": {val: control.ShardMode_READ_ONLY},
|
|
|
|
"read-write": {val: control.ShardMode_READ_WRITE},
|
|
|
|
"degraded-read-write": {val: control.ShardMode_DEGRADED, unsafe: true},
|
|
|
|
"degraded-read-only": {val: control.ShardMode_DEGRADED_READ_ONLY},
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterates over string representations of safe supported shard modes. Safe means
|
|
|
|
// modes which are expected to be used by any user. All other supported modes
|
|
|
|
// are for developers only.
|
|
|
|
func iterateSafeShardModes(f func(string)) {
|
|
|
|
for strMode, mode := range mShardModes {
|
|
|
|
if !mode.unsafe {
|
|
|
|
f(strMode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// looks up for supported control.ShardMode represented by the given string.
|
|
|
|
// Returns false if no corresponding mode exists.
|
|
|
|
func lookUpShardModeFromString(str string) (control.ShardMode, bool) {
|
|
|
|
mode, ok := mShardModes[str]
|
|
|
|
if !ok {
|
|
|
|
return control.ShardMode_SHARD_MODE_UNDEFINED, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode.val, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// looks up for string representation of supported shard mode. Returns false
|
|
|
|
// if mode is not supported.
|
|
|
|
func lookUpShardModeString(m control.ShardMode) (string, bool) {
|
|
|
|
for strMode, mode := range mShardModes {
|
|
|
|
if mode.val == m {
|
|
|
|
return strMode, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
2022-10-14 08:36:54 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-10-15 07:57:06 +00:00
|
|
|
modes := make([]string, 0)
|
|
|
|
|
|
|
|
iterateSafeShardModes(func(strMode string) {
|
2022-10-14 08:36:54 +00:00
|
|
|
modes = append(modes, "'"+strMode+"'")
|
2022-10-15 07:57:06 +00:00
|
|
|
})
|
2022-10-14 08:36:54 +00:00
|
|
|
|
2022-05-23 16:26:27 +00:00
|
|
|
flags.String(shardModeFlag, "",
|
2022-10-15 07:57:06 +00:00
|
|
|
fmt.Sprintf("New shard mode (%s)", strings.Join(modes, ", ")),
|
2022-05-23 16:26:27 +00:00
|
|
|
)
|
2023-02-01 14:25:59 +00:00
|
|
|
_ = setShardModeCmd.MarkFlagRequired(shardModeFlag)
|
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)
|
|
|
|
|
2022-10-15 07:57:06 +00:00
|
|
|
mode, ok := lookUpShardModeFromString(strMode)
|
2022-10-14 08:36:54 +00:00
|
|
|
if !ok {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.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
|
|
|
|
})
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
verifyResponse(cmd, resp.GetSignature(), resp.GetBody())
|
|
|
|
|
|
|
|
cmd.Println("Shard mode update request successfully sent.")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-30 15:18:58 +00:00
|
|
|
return getShardIDListFromIDFlag(cmd, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getShardIDListFromIDFlag(cmd *cobra.Command, withAllFlag bool) [][]byte {
|
2022-10-10 17:58:28 +00:00
|
|
|
sidList, _ := cmd.Flags().GetStringSlice(shardIDFlag)
|
|
|
|
if len(sidList) == 0 {
|
2024-01-30 15:18:58 +00:00
|
|
|
if withAllFlag {
|
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("either --%s or --%s flag must be provided", shardIDFlag, shardAllFlag))
|
|
|
|
} else {
|
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("--%s flag value must be provided", shardIDFlag))
|
|
|
|
}
|
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 {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("duplicated shard IDs: %s", sidList[i]))
|
2022-10-10 17:58:28 +00:00
|
|
|
}
|
|
|
|
seen[sidList[i]] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([][]byte, 0, len(sidList))
|
|
|
|
for i := range sidList {
|
|
|
|
raw, err := base58.Decode(sidList[i])
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "incorrect shard ID encoding: %w", err)
|
2022-10-10 17:58:28 +00:00
|
|
|
|
|
|
|
res = append(res, raw)
|
|
|
|
}
|
|
|
|
|
2024-02-14 07:50:27 +00:00
|
|
|
slices.SortFunc(res, bytes.Compare)
|
2022-10-10 17:58:28 +00:00
|
|
|
return res
|
|
|
|
}
|