2020-11-30 14:30:49 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-06-27 08:06:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-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 {
|
|
|
|
s.log.Error("metabase failure, switching mode",
|
|
|
|
zap.String("stage", stage),
|
|
|
|
zap.Stringer("mode", mode.ReadOnly),
|
|
|
|
zap.Error(err))
|
|
|
|
|
|
|
|
err = s.SetMode(mode.ReadOnly)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Error("can't move shard to readonly, switch mode",
|
|
|
|
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 {
|
|
|
|
return (*Shard)(x).refillMetabase()
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
// Init initializes all Shard's components.
|
|
|
|
func (s *Shard) Init() 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) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-08-19 17:24:05 +00:00
|
|
|
s.updateObjectCounter()
|
|
|
|
|
2021-08-04 12:04:37 +00:00
|
|
|
s.gc = &gc{
|
|
|
|
gcCfg: s.gcCfg,
|
|
|
|
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{
|
|
|
|
s.collectExpiredObjects,
|
2021-02-17 12:27:40 +00:00
|
|
|
s.collectExpiredTombstones,
|
2022-03-10 17:58:58 +00:00
|
|
|
s.collectExpiredLocks,
|
2021-02-16 17:34:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-02-16 11:38:34 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 12:04:37 +00:00
|
|
|
s.gc.init()
|
2021-02-16 11:38:34 +00:00
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:38:58 +00:00
|
|
|
func (s *Shard) refillMetabase() error {
|
|
|
|
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()
|
|
|
|
|
2022-09-08 17:32:02 +00:00
|
|
|
err = blobstor.IterateBinaryObjects(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 {
|
|
|
|
s.log.Warn("could not unmarshal object",
|
|
|
|
zap.Stringer("address", addr),
|
|
|
|
zap.String("err", err.Error()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-20 12:28:20 +00:00
|
|
|
//nolint: exhaustive
|
|
|
|
switch obj.Type() {
|
|
|
|
case objectSDK.TypeTombstone:
|
2021-09-13 13:38:58 +00:00
|
|
|
tombstone := objectSDK.NewTombstone()
|
|
|
|
|
|
|
|
if err := tombstone.Unmarshal(obj.Payload()); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal tombstone content: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
tombAddr := object.AddressOf(obj)
|
2021-09-13 13:38:58 +00:00
|
|
|
memberIDs := tombstone.Members()
|
2022-05-31 17:00:41 +00:00
|
|
|
tombMembers := make([]oid.Address, 0, len(memberIDs))
|
2021-09-13 13:38:58 +00:00
|
|
|
|
2022-03-15 11:16:46 +00:00
|
|
|
for i := range memberIDs {
|
2022-05-31 17:00:41 +00:00
|
|
|
a := tombAddr
|
|
|
|
a.SetObject(memberIDs[i])
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
tombMembers = append(tombMembers, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
var inhumePrm meta.InhumePrm
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
inhumePrm.SetTombstoneAddress(tombAddr)
|
|
|
|
inhumePrm.SetAddresses(tombMembers...)
|
2021-09-13 13:38:58 +00:00
|
|
|
|
2022-05-20 16:48:14 +00:00
|
|
|
_, err = s.metaBase.Inhume(inhumePrm)
|
2021-09-13 13:38:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not inhume objects: %w", err)
|
|
|
|
}
|
2022-06-20 12:28:20 +00:00
|
|
|
case objectSDK.TypeLock:
|
|
|
|
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()
|
|
|
|
err = s.metaBase.Lock(cnr, id, locked)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not lock objects: %w", 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
|
|
|
|
|
|
|
_, err := s.metaBase.Put(mPrm)
|
2022-07-27 18:38:28 +00:00
|
|
|
if err != nil && !meta.IsErrRemoved(err) && !errors.Is(err, object.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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not close %s: %w", component, err)
|
2020-11-30 14:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 12:04:37 +00:00
|
|
|
s.gc.stop()
|
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
return nil
|
|
|
|
}
|