[#1118] services/control: allow to reset error counter in SetShardMode

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-31 18:15:33 +03:00 committed by Alex Vanin
parent 6ad2624552
commit 7fb15fa1d0
7 changed files with 36 additions and 5 deletions

View file

@ -73,6 +73,7 @@ const (
shardModeFlag = "mode" shardModeFlag = "mode"
shardIDFlag = "id" shardIDFlag = "id"
shardClearErrorsFlag = "clear-errors"
shardModeReadOnly = "read-only" shardModeReadOnly = "read-only"
shardModeReadWrite = "read-write" shardModeReadWrite = "read-write"
@ -127,6 +128,7 @@ func initControlSetShardModeCmd() {
shardModeReadOnly, shardModeReadOnly,
), ),
) )
flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0")
} }
func initControlShardsListCmd() { func initControlShardsListCmd() {
@ -510,6 +512,9 @@ func setShardMode(cmd *cobra.Command, _ []string) {
body.SetMode(mode) body.SetMode(mode)
body.SetShardID(rawID) body.SetShardID(rawID)
reset, _ := cmd.Flags().GetBool(shardClearErrorsFlag)
body.ClearErrorCounter(reset)
err = controlSvc.SignMessage(key, req) err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err)) exitOnErr(cmd, errf("could not sign request: %w", err))

View file

@ -114,6 +114,12 @@ func TestErrorReporting(t *testing.T) {
checkShardState(t, e, id[0], errThreshold+i, shard.ModeReadOnly) checkShardState(t, e, id[0], errThreshold+i, shard.ModeReadOnly)
checkShardState(t, e, id[1], 0, shard.ModeReadWrite) checkShardState(t, e, id[1], 0, shard.ModeReadWrite)
} }
require.NoError(t, e.SetShardMode(id[0], shard.ModeReadWrite, false))
checkShardState(t, e, id[0], errThreshold+1, shard.ModeReadWrite)
require.NoError(t, e.SetShardMode(id[0], shard.ModeReadWrite, true))
checkShardState(t, e, id[0], 0, shard.ModeReadWrite)
}) })
} }

View file

@ -118,12 +118,15 @@ func (e *StorageEngine) iterateOverUnsortedShards(handler func(hashedShard) (sto
// SetShardMode sets mode of the shard with provided identifier. // SetShardMode sets mode of the shard with provided identifier.
// //
// Returns an error if shard mode was not set, or shard was not found in storage engine. // Returns an error if shard mode was not set, or shard was not found in storage engine.
func (e *StorageEngine) SetShardMode(id *shard.ID, m shard.Mode) error { func (e *StorageEngine) SetShardMode(id *shard.ID, m shard.Mode, resetErrorCounter bool) error {
e.mtx.RLock() e.mtx.RLock()
defer e.mtx.RUnlock() defer e.mtx.RUnlock()
for shID, sh := range e.shards { for shID, sh := range e.shards {
if id.String() == shID { if id.String() == shID {
if resetErrorCounter {
sh.errorCount.Store(0)
}
return sh.SetMode(m) return sh.SetMode(m)
} }
} }

View file

@ -33,7 +33,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))
} }
err = s.s.SetShardMode(requestedShard, mode) err = s.s.SetShardMode(requestedShard, mode, false)
if err != nil { if err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }

View file

@ -761,10 +761,16 @@ func (x *SetShardModeRequest_Body) SetMode(v ShardMode) {
x.Mode = v x.Mode = v
} }
// ClearErrorCounter sets flag signifying whether error counter for shard should be cleared.
func (x *SetShardModeRequest_Body) ClearErrorCounter(reset bool) {
x.ResetErrorCounter = reset
}
const ( const (
_ = iota _ = iota
setShardModeReqBodyShardIDFNum setShardModeReqBodyShardIDFNum
setShardModeReqBodyModeFNum setShardModeReqBodyModeFNum
setShardModeReqBodyResetCounterFNum
) )
// StableMarshal reads binary representation of set shard mode request body // StableMarshal reads binary representation of set shard mode request body
@ -797,7 +803,14 @@ func (x *SetShardModeRequest_Body) StableMarshal(buf []byte) ([]byte, error) {
offset += n offset += n
_, err = proto.EnumMarshal(setShardModeReqBodyModeFNum, buf[offset:], int32(x.Mode)) n, err = proto.EnumMarshal(setShardModeReqBodyModeFNum, buf[offset:], int32(x.Mode))
if err != nil {
return nil, err
}
offset += n
_, err = proto.BoolMarshal(setShardModeReqBodyResetCounterFNum, buf[offset:], x.ResetErrorCounter)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -818,6 +831,7 @@ func (x *SetShardModeRequest_Body) StableSize() int {
size += proto.BytesSize(setShardModeReqBodyShardIDFNum, x.Shard_ID) size += proto.BytesSize(setShardModeReqBodyShardIDFNum, x.Shard_ID)
size += proto.EnumSize(setShardModeReqBodyModeFNum, int32(x.Mode)) size += proto.EnumSize(setShardModeReqBodyModeFNum, int32(x.Mode))
size += proto.BoolSize(setShardModeReqBodyResetCounterFNum, x.ResetErrorCounter)
return size return size
} }

Binary file not shown.

View file

@ -186,6 +186,9 @@ message SetShardModeRequest {
// Mode that requested to be set. // Mode that requested to be set.
ShardMode mode = 2; ShardMode mode = 2;
// Flag signifying whether error counter should be set to 0.
bool resetErrorCounter = 3;
} }
// Body of set shard mode request message. // Body of set shard mode request message.