[#1867] services/control: Interpret empty list of IDs as all shards

In neofs-cli the flag is still required, but `all` can be used to
process all shards.

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
Evgenii Stratonikov 2022-10-10 21:14:12 +03:00 committed by fyrchik
parent b632260995
commit c0199dee93
7 changed files with 30 additions and 11 deletions

View file

@ -48,7 +48,8 @@ func initControlEvacuateShardCmd() {
flags := evacuateShardCmd.Flags() flags := evacuateShardCmd.Flags()
flags.String(controlRPC, controlRPCDefault, controlRPCUsage) flags.String(controlRPC, controlRPCDefault, controlRPCUsage)
flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding") flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding")
flags.Bool(shardAllFlag, false, "Process all shards")
flags.Bool(dumpIgnoreErrorsFlag, false, "Skip invalid/unreadable objects") flags.Bool(dumpIgnoreErrorsFlag, false, "Skip invalid/unreadable objects")
_ = evacuateShardCmd.MarkFlagRequired(shardIDFlag) evacuateShardCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag)
} }

View file

@ -45,6 +45,7 @@ func initControlFlushCacheCmd() {
ff := flushCacheCmd.Flags() ff := flushCacheCmd.Flags()
ff.String(controlRPC, controlRPCDefault, controlRPCUsage) ff.String(controlRPC, controlRPCDefault, controlRPCUsage)
ff.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding") ff.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding")
ff.Bool(shardAllFlag, false, "Process all shards")
_ = flushCacheCmd.MarkFlagRequired(shardIDFlag) flushCacheCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag)
} }

View file

@ -1,7 +1,6 @@
package control package control
import ( import (
"errors"
"fmt" "fmt"
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
@ -16,6 +15,7 @@ import (
const ( const (
shardModeFlag = "mode" shardModeFlag = "mode"
shardIDFlag = "id" shardIDFlag = "id"
shardAllFlag = "all"
shardClearErrorsFlag = "clear-errors" shardClearErrorsFlag = "clear-errors"
shardModeReadOnly = "read-only" shardModeReadOnly = "read-only"
@ -38,6 +38,7 @@ func initControlSetShardModeCmd() {
flags.String(controlRPC, controlRPCDefault, controlRPCUsage) flags.String(controlRPC, controlRPCDefault, controlRPCUsage)
flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding") flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding")
flags.Bool(shardAllFlag, false, "Process all shards")
flags.String(shardModeFlag, "", flags.String(shardModeFlag, "",
fmt.Sprintf("New shard mode keyword ('%s', '%s', '%s')", fmt.Sprintf("New shard mode keyword ('%s', '%s', '%s')",
shardModeReadWrite, shardModeReadWrite,
@ -46,6 +47,8 @@ func initControlSetShardModeCmd() {
), ),
) )
flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0") flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0")
setShardModeCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag)
} }
func setShardMode(cmd *cobra.Command, _ []string) { func setShardMode(cmd *cobra.Command, _ []string) {
@ -102,9 +105,14 @@ func getShardID(cmd *cobra.Command) []byte {
} }
func getShardIDList(cmd *cobra.Command) [][]byte { func getShardIDList(cmd *cobra.Command) [][]byte {
all, _ := cmd.Flags().GetBool(shardAllFlag)
if all {
return nil
}
sidList, _ := cmd.Flags().GetStringSlice(shardIDFlag) sidList, _ := cmd.Flags().GetStringSlice(shardIDFlag)
if len(sidList) == 0 { if len(sidList) == 0 {
common.ExitOnErr(cmd, "", errors.New("no shard IDs were provided")) common.ExitOnErr(cmd, "", fmt.Errorf("either --%s or --%s flag must be provided", shardIDFlag, shardAllFlag))
} }
// We can sort the ID list and perform this check without additional allocations, // We can sort the ID list and perform this check without additional allocations,

View file

@ -26,7 +26,7 @@ func (s *Server) EvacuateShard(_ context.Context, req *control.EvacuateShardRequ
} }
var prm engine.EvacuateShardPrm var prm engine.EvacuateShardPrm
prm.WithShardIDList(getShardIDList(req.GetBody().GetShard_ID())) prm.WithShardIDList(s.getShardIDList(req.GetBody().GetShard_ID()))
prm.WithIgnoreErrors(req.GetBody().GetIgnoreErrors()) prm.WithIgnoreErrors(req.GetBody().GetIgnoreErrors())
prm.WithFaultHandler(s.replicate) prm.WithFaultHandler(s.replicate)

View file

@ -15,7 +15,7 @@ func (s *Server) FlushCache(_ context.Context, req *control.FlushCacheRequest) (
return nil, status.Error(codes.PermissionDenied, err.Error()) return nil, status.Error(codes.PermissionDenied, err.Error())
} }
for _, shardID := range getShardIDList(req.GetBody().GetShard_ID()) { for _, shardID := range s.getShardIDList(req.GetBody().GetShard_ID()) {
var prm engine.FlushWriteCachePrm var prm engine.FlushWriteCachePrm
prm.SetShardID(shardID) prm.SetShardID(shardID)

View file

@ -2,10 +2,19 @@ package control
import "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" import "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
func getShardIDList(raw [][]byte) []*shard.ID { func (s *Server) getShardIDList(raw [][]byte) []*shard.ID {
if len(raw) != 0 {
res := make([]*shard.ID, 0, len(raw)) res := make([]*shard.ID, 0, len(raw))
for i := range raw { for i := range raw {
res = append(res, shard.NewIDFromBytes(raw[i])) res = append(res, shard.NewIDFromBytes(raw[i]))
} }
return res return res
} }
info := s.s.DumpInfo()
res := make([]*shard.ID, 0, len(info.Shards))
for i := range info.Shards {
res = append(res, info.Shards[i].ID)
}
return res
}

View file

@ -36,7 +36,7 @@ func (s *Server) SetShardMode(_ context.Context, req *control.SetShardModeReques
return nil, status.Error(codes.Internal, fmt.Sprintf("unknown shard mode: %s", requestedMode)) return nil, status.Error(codes.Internal, fmt.Sprintf("unknown shard mode: %s", requestedMode))
} }
for _, shardID := range getShardIDList(req.Body.GetShard_ID()) { for _, shardID := range s.getShardIDList(req.Body.GetShard_ID()) {
err = s.s.SetShardMode(shardID, m, false) err = s.s.SetShardMode(shardID, m, false)
if err != nil { if err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())