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
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:35:25 +00:00
|
|
|
for _, component := range components {
|
2022-06-28 13:42:50 +00:00
|
|
|
if err := component.Open(false); err != nil {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes all Shard's components.
|
|
|
|
func (s *Shard) Init() error {
|
2021-09-13 13:56:07 +00:00
|
|
|
var fMetabase func() error
|
|
|
|
|
|
|
|
if s.needRefillMetabase() {
|
|
|
|
fMetabase = s.refillMetabase
|
|
|
|
} else {
|
|
|
|
fMetabase = s.metaBase.Init
|
|
|
|
}
|
|
|
|
|
|
|
|
components := []func() error{
|
2022-07-18 10:16:23 +00:00
|
|
|
s.blobStor.Init, fMetabase,
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:35:25 +00:00
|
|
|
if s.hasWriteCache() {
|
2021-09-13 13:56:07 +00:00
|
|
|
components = append(components, s.writeCache.Init)
|
2020-12-01 07:35:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
if s.pilorama != nil {
|
|
|
|
components = append(components, s.pilorama.Init)
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:35:25 +00:00
|
|
|
for _, component := range components {
|
2021-09-13 13:56:07 +00:00
|
|
|
if err := component(); err != nil {
|
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
|
|
|
|
|
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)
|
|
|
|
mPrm.SetBlobovniczaID(blzID)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
|
|
|
_, err := s.metaBase.Put(mPrm)
|
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
|
|
|
|
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
|
|
|
|
}
|