[#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

@ -71,8 +71,9 @@ const (
netmapStatusOffline = "offline"
netmapStatusMaintenance = "maintenance"
shardModeFlag = "mode"
shardIDFlag = "id"
shardModeFlag = "mode"
shardIDFlag = "id"
shardClearErrorsFlag = "clear-errors"
shardModeReadOnly = "read-only"
shardModeReadWrite = "read-write"
@ -127,6 +128,7 @@ func initControlSetShardModeCmd() {
shardModeReadOnly,
),
)
flags.Bool(shardClearErrorsFlag, false, "Set shard error count to 0")
}
func initControlShardsListCmd() {
@ -510,6 +512,9 @@ func setShardMode(cmd *cobra.Command, _ []string) {
body.SetMode(mode)
body.SetShardID(rawID)
reset, _ := cmd.Flags().GetBool(shardClearErrorsFlag)
body.ClearErrorCounter(reset)
err = controlSvc.SignMessage(key, req)
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[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.
//
// 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()
defer e.mtx.RUnlock()
for shID, sh := range e.shards {
if id.String() == shID {
if resetErrorCounter {
sh.errorCount.Store(0)
}
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))
}
err = s.s.SetShardMode(requestedShard, mode)
err = s.s.SetShardMode(requestedShard, mode, false)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

View file

@ -761,10 +761,16 @@ func (x *SetShardModeRequest_Body) SetMode(v ShardMode) {
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 (
_ = iota
setShardModeReqBodyShardIDFNum
setShardModeReqBodyModeFNum
setShardModeReqBodyResetCounterFNum
)
// 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
_, 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 {
return nil, err
}
@ -818,6 +831,7 @@ func (x *SetShardModeRequest_Body) StableSize() int {
size += proto.BytesSize(setShardModeReqBodyShardIDFNum, x.Shard_ID)
size += proto.EnumSize(setShardModeReqBodyModeFNum, int32(x.Mode))
size += proto.BoolSize(setShardModeReqBodyResetCounterFNum, x.ResetErrorCounter)
return size
}

Binary file not shown.

View file

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