2020-11-19 12:41:10 +00:00
|
|
|
package shard
|
|
|
|
|
2022-01-18 12:47:16 +00:00
|
|
|
import (
|
2024-10-21 07:22:54 +00:00
|
|
|
"context"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-01-23 08:35:58 +00:00
|
|
|
"go.uber.org/zap"
|
2022-01-18 12:47:16 +00:00
|
|
|
)
|
2021-12-27 11:03:15 +00:00
|
|
|
|
|
|
|
// ErrReadOnlyMode is returned when it is impossible to apply operation
|
|
|
|
// that changes shard's memory due to the "read-only" shard's mode.
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrReadOnlyMode = logicerr.New("shard is in read-only mode")
|
2021-12-27 11:03:15 +00:00
|
|
|
|
2022-06-29 11:27:36 +00:00
|
|
|
// ErrDegradedMode is returned when operation requiring metabase is executed in degraded mode.
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrDegradedMode = logicerr.New("shard is in degraded mode")
|
2022-06-29 11:27:36 +00:00
|
|
|
|
2020-11-19 12:56:10 +00:00
|
|
|
// SetMode sets mode of the shard.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that did not allow
|
2021-12-27 11:03:15 +00:00
|
|
|
// setting shard mode.
|
2024-10-21 08:56:38 +00:00
|
|
|
func (s *Shard) SetMode(ctx context.Context, m mode.Mode) error {
|
2023-05-23 12:02:38 +00:00
|
|
|
unlock := s.lockExclusive()
|
|
|
|
defer unlock()
|
2021-12-27 18:07:02 +00:00
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
return s.setMode(ctx, m)
|
2022-10-16 11:39:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
func (s *Shard) setMode(ctx context.Context, m mode.Mode) error {
|
|
|
|
s.log.Info(ctx, logs.ShardSettingShardMode,
|
2023-01-23 08:35:58 +00:00
|
|
|
zap.Stringer("old_mode", s.info.Mode),
|
|
|
|
zap.Stringer("new_mode", m))
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
components := []interface {
|
|
|
|
SetMode(context.Context, mode.Mode) error
|
|
|
|
}{
|
2022-07-05 04:55:46 +00:00
|
|
|
s.metaBase, s.blobStor,
|
|
|
|
}
|
|
|
|
|
2022-01-18 12:47:16 +00:00
|
|
|
if s.hasWriteCache() {
|
2022-07-05 04:55:46 +00:00
|
|
|
components = append(components, s.writeCache)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.pilorama != nil {
|
|
|
|
components = append(components, s.pilorama)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The usual flow of the requests (pilorama is independent):
|
|
|
|
// writecache -> blobstor -> metabase
|
|
|
|
// For mode.ReadOnly and mode.Degraded the order is:
|
|
|
|
// writecache -> blobstor -> metabase
|
|
|
|
// For mode.ReadWrite it is the opposite:
|
|
|
|
// metabase -> blobstor -> writecache
|
|
|
|
if m != mode.ReadWrite {
|
|
|
|
if s.hasWriteCache() {
|
|
|
|
components[0], components[2] = components[2], components[0]
|
|
|
|
} else {
|
|
|
|
components[0], components[1] = components[1], components[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:59:18 +00:00
|
|
|
if !m.Disabled() {
|
|
|
|
for i := range components {
|
2024-10-21 08:56:38 +00:00
|
|
|
if err := components[i].SetMode(ctx, m); err != nil {
|
2023-06-20 08:59:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-05 04:55:46 +00:00
|
|
|
}
|
2022-01-18 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 18:07:02 +00:00
|
|
|
s.info.Mode = m
|
2024-10-03 07:23:59 +00:00
|
|
|
s.metricsWriter.SetMode(s.info.Mode)
|
2020-11-19 13:51:58 +00:00
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
s.log.Info(ctx, logs.ShardShardModeSetSuccessfully,
|
2023-01-23 08:35:58 +00:00
|
|
|
zap.Stringer("mode", s.info.Mode))
|
2020-11-19 13:51:58 +00:00
|
|
|
return nil
|
2020-11-19 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 18:07:02 +00:00
|
|
|
// GetMode returns mode of the shard.
|
2022-06-28 14:05:08 +00:00
|
|
|
func (s *Shard) GetMode() mode.Mode {
|
2021-12-27 18:07:02 +00:00
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
|
|
|
return s.info.Mode
|
2020-11-19 12:56:10 +00:00
|
|
|
}
|