2020-11-30 14:30:49 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2021-09-13 13:38:58 +00:00
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2021-09-13 13:38:58 +00:00
|
|
|
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"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"
|
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 }{
|
|
|
|
s.blobStor, s.metaBase,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
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{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobstor.IterateObjects(s.blobStor, func(obj *object.Object, blzID *blobovnicza.ID) error {
|
|
|
|
if obj.Type() == objectSDK.TypeTombstone {
|
|
|
|
tombstone := objectSDK.NewTombstone()
|
|
|
|
|
|
|
|
if err := tombstone.Unmarshal(obj.Payload()); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal tombstone content: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tombAddr := obj.Address()
|
|
|
|
cid := tombAddr.ContainerID()
|
|
|
|
memberIDs := tombstone.Members()
|
|
|
|
tombMembers := make([]*objectSDK.Address, 0, len(memberIDs))
|
|
|
|
|
|
|
|
for _, id := range memberIDs {
|
|
|
|
if id == nil {
|
|
|
|
return errors.New("empty member in tombstone")
|
|
|
|
}
|
|
|
|
|
|
|
|
a := objectSDK.NewAddress()
|
|
|
|
a.SetContainerID(cid)
|
|
|
|
a.SetObjectID(id)
|
|
|
|
|
|
|
|
tombMembers = append(tombMembers, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
var inhumePrm meta.InhumePrm
|
|
|
|
|
|
|
|
inhumePrm.WithAddresses(tombAddr)
|
|
|
|
inhumePrm.WithAddresses(tombMembers...)
|
|
|
|
|
|
|
|
_, err = s.metaBase.Inhume(&inhumePrm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not inhume objects: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta.Put(s.metaBase, obj, blzID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:30:49 +00:00
|
|
|
// Close releases all Shard's components.
|
|
|
|
func (s *Shard) Close() error {
|
2020-12-01 07:35:25 +00:00
|
|
|
components := []interface{ Close() error }{
|
|
|
|
s.blobStor, s.metaBase,
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.hasWriteCache() {
|
|
|
|
components = append(components, s.writeCache)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|