2020-11-30 14:30:49 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
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/blobovnicza"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
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", ModeDegraded),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
|
|
|
|
err = s.SetMode(ModeDegraded)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not switch to mode %s", ModeDegraded)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
// Open opens all Shard's components.
|
|
|
|
func (s *Shard) Open() error {
|
2020-12-01 07:35:25 +00:00
|
|
|
components := []interface{ Open() error }{
|
2022-06-27 08:06:01 +00:00
|
|
|
s.blobStor, s.pilorama,
|
2020-12-01 07:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.hasWriteCache() {
|
|
|
|
components = append(components, s.writeCache)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, component := range components {
|
2020-11-30 14:30:49 +00:00
|
|
|
if err := component.Open(); err != nil {
|
2022-06-27 08:06:01 +00:00
|
|
|
if component == s.metaBase {
|
|
|
|
err = s.handleMetabaseFailure("open", err)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-06-27 08:06:01 +00:00
|
|
|
|
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() != ModeDegraded {
|
|
|
|
var initMetabase initializer
|
|
|
|
|
|
|
|
if s.needRefillMetabase() {
|
|
|
|
initMetabase = (*metabaseSynchronizer)(s)
|
|
|
|
} else {
|
|
|
|
initMetabase = s.metaBase
|
|
|
|
}
|
|
|
|
|
|
|
|
components = []initializer{
|
|
|
|
s.blobStor, initMetabase, s.pilorama,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
components = []initializer{s.blobStor, s.pilorama}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for _, component := range components {
|
2022-06-27 08:06:01 +00:00
|
|
|
if err := component.Init(); err != nil {
|
|
|
|
if component == s.metaBase {
|
|
|
|
err = s.handleMetabaseFailure("init", err)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 12:04:37 +00:00
|
|
|
s.gc = &gc{
|
|
|
|
gcCfg: s.gcCfg,
|
|
|
|
remover: s.removeGarbage,
|
|
|
|
stopChannel: make(chan struct{}),
|
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()
|
|
|
|
|
|
|
|
return blobstor.IterateBinaryObjects(s.blobStor, func(addr oid.Address, data []byte, blzID *blobovnicza.ID) error {
|
|
|
|
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
|
|
|
|
|
2021-09-24 15:32:59 +00:00
|
|
|
inhumePrm.WithTombstoneAddress(tombAddr)
|
2021-09-13 13:38:58 +00:00
|
|
|
inhumePrm.WithAddresses(tombMembers...)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-14 13:37:09 +00:00
|
|
|
err := meta.Put(s.metaBase, obj, blzID)
|
2022-03-17 08:03:58 +00:00
|
|
|
if err != nil && !meta.IsErrRemoved(err) {
|
2021-09-14 13:37:09 +00:00
|
|
|
return 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
|
|
|
|
|
|
|
if s.hasWriteCache() {
|
|
|
|
components = append(components, s.writeCache)
|
|
|
|
}
|
|
|
|
|
2022-06-27 08:06:01 +00:00
|
|
|
components = append(components, s.pilorama, s.blobStor)
|
|
|
|
|
|
|
|
if s.GetMode() != ModeDegraded {
|
|
|
|
components = append(components, 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
|
|
|
|
}
|