2020-11-30 14:30:49 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2023-03-23 14:59:14 +00:00
|
|
|
"context"
|
2022-07-27 18:38:28 +00:00
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2021-09-13 13:38:58 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/pkg/tracing"
|
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/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-06-21 07:52:03 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-30 14:30:49 +00:00
|
|
|
)
|
|
|
|
|
2022-06-27 08:06:01 +00:00
|
|
|
func (s *Shard) handleMetabaseFailure(stage string, err error) error {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Error(logs.ShardMetabaseFailureSwitchingMode,
|
2022-06-27 08:06:01 +00:00
|
|
|
zap.String("stage", stage),
|
|
|
|
zap.Stringer("mode", mode.ReadOnly),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
err = s.SetMode(mode.ReadOnly)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Error(logs.ShardCantMoveShardToReadonlySwitchMode,
|
2022-06-27 08:06:01 +00:00
|
|
|
zap.String("stage", stage),
|
|
|
|
zap.Stringer("mode", mode.DegradedReadOnly),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
err = s.SetMode(mode.DegradedReadOnly)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not switch to mode %s", mode.DegradedReadOnly)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
// Open opens all Shard's components.
|
|
|
|
func (s *Shard) Open() error {
|
2022-06-28 13:42:50 +00:00
|
|
|
components := []interface{ Open(bool) error }{
|
2022-07-18 10:16:23 +00:00
|
|
|
s.blobStor, s.metaBase,
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:35:25 +00:00
|
|
|
if s.hasWriteCache() {
|
|
|
|
components = append(components, s.writeCache)
|
|
|
|
}
|
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
if s.pilorama != nil {
|
|
|
|
components = append(components, s.pilorama)
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:25:26 +00:00
|
|
|
for i, component := range components {
|
2022-06-28 13:42:50 +00:00
|
|
|
if err := component.Open(false); err != nil {
|
2022-06-27 08:06:01 +00:00
|
|
|
if component == s.metaBase {
|
2022-08-22 12:25:26 +00:00
|
|
|
// We must first open all other components to avoid
|
|
|
|
// opening non-existent DB in read-only mode.
|
|
|
|
for j := i + 1; j < len(components); j++ {
|
|
|
|
if err := components[j].Open(false); err != nil {
|
|
|
|
// Other components must be opened, fail.
|
|
|
|
return fmt.Errorf("could not open %T: %w", components[j], err)
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 08:06:01 +00:00
|
|
|
err = s.handleMetabaseFailure("open", err)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not open %T: %w", component, err)
|
2020-11-30 14:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-27 08:06:01 +00:00
|
|
|
type metabaseSynchronizer Shard
|
|
|
|
|
|
|
|
func (x *metabaseSynchronizer) Init() error {
|
2023-04-12 14:01:29 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(context.TODO(), "metabaseSynchronizer.Init")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
return (*Shard)(x).refillMetabase(ctx)
|
2022-06-27 08:06:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
// Init initializes all Shard's components.
|
2023-03-23 14:59:14 +00:00
|
|
|
func (s *Shard) Init(ctx context.Context) error {
|
2022-06-27 08:06:01 +00:00
|
|
|
type initializer interface {
|
|
|
|
Init() error
|
2021-09-13 13:56:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 08:06:01 +00:00
|
|
|
var components []initializer
|
|
|
|
|
|
|
|
if !s.GetMode().NoMetabase() {
|
|
|
|
var initMetabase initializer
|
|
|
|
|
|
|
|
if s.needRefillMetabase() {
|
|
|
|
initMetabase = (*metabaseSynchronizer)(s)
|
|
|
|
} else {
|
|
|
|
initMetabase = s.metaBase
|
|
|
|
}
|
|
|
|
|
|
|
|
components = []initializer{
|
|
|
|
s.blobStor, initMetabase,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
components = []initializer{s.blobStor}
|
2022-07-18 10:16:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 07:35:25 +00:00
|
|
|
if s.hasWriteCache() {
|
2022-06-27 08:06:01 +00:00
|
|
|
components = append(components, s.writeCache)
|
2020-12-01 07:35:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
if s.pilorama != nil {
|
2022-06-27 08:06:01 +00:00
|
|
|
components = append(components, s.pilorama)
|
2022-06-28 13:42:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 07:35:25 +00:00
|
|
|
for _, component := range components {
|
2022-06-27 08:06:01 +00:00
|
|
|
if err := component.Init(); err != nil {
|
|
|
|
if component == s.metaBase {
|
2022-09-21 20:57:46 +00:00
|
|
|
if errors.Is(err, meta.ErrOutdatedVersion) {
|
2022-10-21 19:03:24 +00:00
|
|
|
return fmt.Errorf("metabase initialization: %w", err)
|
2022-09-21 20:57:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 08:06:01 +00:00
|
|
|
err = s.handleMetabaseFailure("init", err)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not initialize %T: %w", component, err)
|
2020-11-30 14:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
s.updateMetrics()
|
2022-08-19 17:24:05 +00:00
|
|
|
|
2021-08-04 12:04:37 +00:00
|
|
|
s.gc = &gc{
|
2022-10-16 12:07:38 +00:00
|
|
|
gcCfg: &s.gcCfg,
|
2021-08-04 12:04:37 +00:00
|
|
|
remover: s.removeGarbage,
|
|
|
|
stopChannel: make(chan struct{}),
|
2022-09-26 06:30:41 +00:00
|
|
|
eventChan: make(chan Event),
|
2021-02-16 17:34:45 +00:00
|
|
|
mEventHandler: map[eventType]*eventHandlers{
|
|
|
|
eventNewEpoch: {
|
|
|
|
cancelFunc: func() {},
|
|
|
|
handlers: []eventHandler{
|
2023-03-28 08:17:15 +00:00
|
|
|
s.collectExpiredLocks,
|
|
|
|
s.collectExpiredObjects,
|
2021-02-17 12:27:40 +00:00
|
|
|
s.collectExpiredTombstones,
|
2021-02-16 17:34:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-16 11:38:34 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 14:59:14 +00:00
|
|
|
s.gc.init(ctx)
|
2021-02-16 11:38:34 +00:00
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) refillMetabase(ctx context.Context) error {
|
2021-09-13 13:38:58 +00:00
|
|
|
err := s.metaBase.Reset()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not reset metabase: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-21 07:52:03 +00:00
|
|
|
obj := objectSDK.New()
|
|
|
|
|
2023-05-24 11:09:11 +00:00
|
|
|
err = blobstor.IterateBinaryObjects(ctx, s.blobStor, func(addr oid.Address, data []byte, descriptor []byte) error {
|
2022-06-21 07:52:03 +00:00
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Warn(logs.ShardCouldNotUnmarshalObject,
|
2022-06-21 07:52:03 +00:00
|
|
|
zap.Stringer("address", addr),
|
|
|
|
zap.String("err", err.Error()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-21 14:06:46 +00:00
|
|
|
var err error
|
2022-06-20 12:28:20 +00:00
|
|
|
switch obj.Type() {
|
|
|
|
case objectSDK.TypeTombstone:
|
2023-04-12 14:01:29 +00:00
|
|
|
err = s.refillTombstoneObject(ctx, obj)
|
2022-06-20 12:28:20 +00:00
|
|
|
case objectSDK.TypeLock:
|
2023-04-12 14:01:29 +00:00
|
|
|
err = s.refillLockObject(ctx, obj)
|
2023-03-21 14:06:46 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-13 13:38:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
var mPrm meta.PutPrm
|
2022-07-12 14:59:37 +00:00
|
|
|
mPrm.SetObject(obj)
|
2022-07-06 14:09:50 +00:00
|
|
|
mPrm.SetStorageID(descriptor)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err = s.metaBase.Put(ctx, mPrm)
|
2022-10-31 07:08:30 +00:00
|
|
|
if err != nil && !meta.IsErrRemoved(err) && !errors.Is(err, meta.ErrObjectIsExpired) {
|
2021-09-14 13:37:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-09-13 13:38:58 +00:00
|
|
|
})
|
2022-09-08 17:32:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not put objects to the meta: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.metaBase.SyncCounters()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not sync object counters: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-09-13 13:38:58 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) refillLockObject(ctx context.Context, obj *objectSDK.Object) error {
|
2023-03-21 14:06:46 +00:00
|
|
|
var lock objectSDK.Lock
|
|
|
|
if err := lock.Unmarshal(obj.Payload()); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal lock content: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
locked := make([]oid.ID, lock.NumberOfMembers())
|
|
|
|
lock.ReadMembers(locked)
|
|
|
|
|
|
|
|
cnr, _ := obj.ContainerID()
|
|
|
|
id, _ := obj.ID()
|
2023-04-12 14:01:29 +00:00
|
|
|
err := s.metaBase.Lock(ctx, cnr, id, locked)
|
2023-03-21 14:06:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not lock objects: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) refillTombstoneObject(ctx context.Context, obj *objectSDK.Object) error {
|
2023-03-21 14:06:46 +00:00
|
|
|
tombstone := objectSDK.NewTombstone()
|
|
|
|
|
|
|
|
if err := tombstone.Unmarshal(obj.Payload()); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal tombstone content: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tombAddr := object.AddressOf(obj)
|
|
|
|
memberIDs := tombstone.Members()
|
|
|
|
tombMembers := make([]oid.Address, 0, len(memberIDs))
|
|
|
|
|
|
|
|
for i := range memberIDs {
|
|
|
|
a := tombAddr
|
|
|
|
a.SetObject(memberIDs[i])
|
|
|
|
|
|
|
|
tombMembers = append(tombMembers, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
var inhumePrm meta.InhumePrm
|
|
|
|
|
|
|
|
inhumePrm.SetTombstoneAddress(tombAddr)
|
|
|
|
inhumePrm.SetAddresses(tombMembers...)
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := s.metaBase.Inhume(ctx, inhumePrm)
|
2023-03-21 14:06:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not inhume objects: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
// Close releases all Shard's components.
|
|
|
|
func (s *Shard) Close() error {
|
2022-01-18 14:26:30 +00:00
|
|
|
components := []interface{ Close() error }{}
|
2020-12-01 07:35:25 +00:00
|
|
|
|
2022-07-18 10:16:23 +00:00
|
|
|
if s.pilorama != nil {
|
|
|
|
components = append(components, s.pilorama)
|
|
|
|
}
|
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
if s.hasWriteCache() {
|
|
|
|
components = append(components, s.writeCache)
|
|
|
|
}
|
|
|
|
|
2022-07-18 10:16:23 +00:00
|
|
|
components = append(components, s.blobStor, s.metaBase)
|
2022-01-18 14:26:30 +00:00
|
|
|
|
2023-02-06 10:35:28 +00:00
|
|
|
var lastErr error
|
2020-12-01 07:35:25 +00:00
|
|
|
for _, component := range components {
|
2020-11-30 14:30:49 +00:00
|
|
|
if err := component.Close(); err != nil {
|
2023-02-06 10:35:28 +00:00
|
|
|
lastErr = err
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Error(logs.ShardCouldNotCloseShardComponent, zap.Error(err))
|
2020-11-30 14:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:12:35 +00:00
|
|
|
// If Init/Open was unsuccessful gc can be nil.
|
|
|
|
if s.gc != nil {
|
|
|
|
s.gc.stop()
|
|
|
|
}
|
2021-08-04 12:04:37 +00:00
|
|
|
|
2023-02-06 10:35:28 +00:00
|
|
|
return lastErr
|
2020-11-30 14:30:49 +00:00
|
|
|
}
|
2022-10-16 11:39:47 +00:00
|
|
|
|
|
|
|
// Reload reloads configuration portions that are necessary.
|
|
|
|
// If a config option is invalid, it logs an error and returns nil.
|
|
|
|
// If there was a problem with applying new configuration, an error is returned.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) Reload(ctx context.Context, opts ...Option) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.Reload")
|
|
|
|
defer span.End()
|
|
|
|
|
2022-10-16 11:39:47 +00:00
|
|
|
// Do not use defaultCfg here missing options need not be reloaded.
|
|
|
|
var c cfg
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&c)
|
|
|
|
}
|
|
|
|
|
2023-05-23 12:02:38 +00:00
|
|
|
unlock := s.lockExclusive()
|
|
|
|
defer unlock()
|
2022-10-16 11:39:47 +00:00
|
|
|
|
|
|
|
ok, err := s.metaBase.Reload(c.metaOpts...)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, meta.ErrDegradedMode) {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Error(logs.ShardCantOpenMetabaseMoveToADegradedMode, zap.Error(err))
|
2022-10-16 11:39:47 +00:00
|
|
|
_ = s.setMode(mode.DegradedReadOnly)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok {
|
2022-10-16 13:11:29 +00:00
|
|
|
var err error
|
2022-10-16 11:39:47 +00:00
|
|
|
if c.refillMetabase {
|
2022-10-16 13:11:29 +00:00
|
|
|
// Here we refill metabase only if a new instance was opened. This is a feature,
|
|
|
|
// we don't want to hang for some time just because we forgot to change
|
|
|
|
// config after the node was updated.
|
2023-04-12 14:01:29 +00:00
|
|
|
err = s.refillMetabase(ctx)
|
2022-10-16 11:39:47 +00:00
|
|
|
} else {
|
2022-10-16 13:11:29 +00:00
|
|
|
err = s.metaBase.Init()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Error(logs.ShardCantInitializeMetabaseMoveToADegradedreadonlyMode, zap.Error(err))
|
2022-10-16 13:11:29 +00:00
|
|
|
_ = s.setMode(mode.DegradedReadOnly)
|
|
|
|
return err
|
2022-10-16 11:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Info(logs.ShardTryingToRestoreReadwriteMode)
|
2022-10-16 13:11:29 +00:00
|
|
|
return s.setMode(mode.ReadWrite)
|
2022-10-16 11:39:47 +00:00
|
|
|
}
|
2023-05-23 12:02:38 +00:00
|
|
|
|
|
|
|
func (s *Shard) lockExclusive() func() {
|
|
|
|
s.setModeRequested.Store(true)
|
|
|
|
val := s.gcCancel.Load()
|
|
|
|
if val != nil {
|
|
|
|
cancelGC := val.(context.CancelFunc)
|
|
|
|
cancelGC()
|
|
|
|
}
|
|
|
|
s.m.Lock()
|
|
|
|
s.setModeRequested.Store(false)
|
|
|
|
return s.m.Unlock
|
|
|
|
}
|