From c0199dee93813d939a5b71b241914918620d8e89 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 10 Oct 2022 21:14:12 +0300 Subject: [PATCH] [#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 --- cmd/neofs-cli/modules/control/evacuate_shard.go | 3 ++- cmd/neofs-cli/modules/control/flush_cache.go | 3 ++- .../modules/control/shards_set_mode.go | 12 ++++++++++-- pkg/services/control/server/evacuate.go | 2 +- pkg/services/control/server/flush_cache.go | 2 +- pkg/services/control/server/helpers.go | 17 +++++++++++++---- pkg/services/control/server/set_shard_mode.go | 2 +- 7 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cmd/neofs-cli/modules/control/evacuate_shard.go b/cmd/neofs-cli/modules/control/evacuate_shard.go index 51cc316977..3c4ccf28ac 100644 --- a/cmd/neofs-cli/modules/control/evacuate_shard.go +++ b/cmd/neofs-cli/modules/control/evacuate_shard.go @@ -48,7 +48,8 @@ func initControlEvacuateShardCmd() { flags := evacuateShardCmd.Flags() flags.String(controlRPC, controlRPCDefault, controlRPCUsage) 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") - _ = evacuateShardCmd.MarkFlagRequired(shardIDFlag) + evacuateShardCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag) } diff --git a/cmd/neofs-cli/modules/control/flush_cache.go b/cmd/neofs-cli/modules/control/flush_cache.go index 6f6ce43886..898b5dc830 100644 --- a/cmd/neofs-cli/modules/control/flush_cache.go +++ b/cmd/neofs-cli/modules/control/flush_cache.go @@ -45,6 +45,7 @@ func initControlFlushCacheCmd() { ff := flushCacheCmd.Flags() ff.String(controlRPC, controlRPCDefault, controlRPCUsage) 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) } diff --git a/cmd/neofs-cli/modules/control/shards_set_mode.go b/cmd/neofs-cli/modules/control/shards_set_mode.go index d4b337c181..f41718430a 100644 --- a/cmd/neofs-cli/modules/control/shards_set_mode.go +++ b/cmd/neofs-cli/modules/control/shards_set_mode.go @@ -1,7 +1,6 @@ package control import ( - "errors" "fmt" "github.com/mr-tron/base58" @@ -16,6 +15,7 @@ import ( const ( shardModeFlag = "mode" shardIDFlag = "id" + shardAllFlag = "all" shardClearErrorsFlag = "clear-errors" shardModeReadOnly = "read-only" @@ -38,6 +38,7 @@ func initControlSetShardModeCmd() { flags.String(controlRPC, controlRPCDefault, controlRPCUsage) flags.StringSlice(shardIDFlag, nil, "List of shard IDs in base58 encoding") + flags.Bool(shardAllFlag, false, "Process all shards") flags.String(shardModeFlag, "", fmt.Sprintf("New shard mode keyword ('%s', '%s', '%s')", shardModeReadWrite, @@ -46,6 +47,8 @@ func initControlSetShardModeCmd() { ), ) flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0") + + setShardModeCmd.MarkFlagsMutuallyExclusive(shardIDFlag, shardAllFlag) } func setShardMode(cmd *cobra.Command, _ []string) { @@ -102,9 +105,14 @@ func getShardID(cmd *cobra.Command) []byte { } func getShardIDList(cmd *cobra.Command) [][]byte { + all, _ := cmd.Flags().GetBool(shardAllFlag) + if all { + return nil + } + sidList, _ := cmd.Flags().GetStringSlice(shardIDFlag) 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, diff --git a/pkg/services/control/server/evacuate.go b/pkg/services/control/server/evacuate.go index 0b8fa8de3f..2c3857ccb5 100644 --- a/pkg/services/control/server/evacuate.go +++ b/pkg/services/control/server/evacuate.go @@ -26,7 +26,7 @@ func (s *Server) EvacuateShard(_ context.Context, req *control.EvacuateShardRequ } 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.WithFaultHandler(s.replicate) diff --git a/pkg/services/control/server/flush_cache.go b/pkg/services/control/server/flush_cache.go index 2d19e5c21d..4f580fbb83 100644 --- a/pkg/services/control/server/flush_cache.go +++ b/pkg/services/control/server/flush_cache.go @@ -15,7 +15,7 @@ func (s *Server) FlushCache(_ context.Context, req *control.FlushCacheRequest) ( 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 prm.SetShardID(shardID) diff --git a/pkg/services/control/server/helpers.go b/pkg/services/control/server/helpers.go index e2a7cd5d79..9f4023566c 100644 --- a/pkg/services/control/server/helpers.go +++ b/pkg/services/control/server/helpers.go @@ -2,10 +2,19 @@ package control import "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" -func getShardIDList(raw [][]byte) []*shard.ID { - res := make([]*shard.ID, 0, len(raw)) - for i := range raw { - res = append(res, shard.NewIDFromBytes(raw[i])) +func (s *Server) getShardIDList(raw [][]byte) []*shard.ID { + if len(raw) != 0 { + res := make([]*shard.ID, 0, len(raw)) + for i := range raw { + res = append(res, shard.NewIDFromBytes(raw[i])) + } + 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 } diff --git a/pkg/services/control/server/set_shard_mode.go b/pkg/services/control/server/set_shard_mode.go index e9d85fe8c6..ed7b26f649 100644 --- a/pkg/services/control/server/set_shard_mode.go +++ b/pkg/services/control/server/set_shard_mode.go @@ -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)) } - for _, shardID := range getShardIDList(req.Body.GetShard_ID()) { + for _, shardID := range s.getShardIDList(req.Body.GetShard_ID()) { err = s.s.SetShardMode(shardID, m, false) if err != nil { return nil, status.Error(codes.Internal, err.Error())