2020-12-01 09:35:42 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-02-17 12:27:40 +00:00
|
|
|
"context"
|
2021-02-11 15:43:50 +00:00
|
|
|
"errors"
|
|
|
|
|
2021-05-20 14:31:47 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-01 09:35:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-12-01 09:35:42 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InhumePrm encapsulates parameters for inhume operation.
|
|
|
|
type InhumePrm struct {
|
2021-02-19 14:01:01 +00:00
|
|
|
tombstone *objectSDK.Address
|
|
|
|
addrs []*objectSDK.Address
|
2020-12-01 09:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InhumeRes encapsulates results of inhume operation.
|
|
|
|
type InhumeRes struct{}
|
|
|
|
|
2021-02-19 14:01:01 +00:00
|
|
|
// WithTarget sets list of objects that should be inhumed and tombstone address
|
2020-12-01 09:35:42 +00:00
|
|
|
// as the reason for inhume operation.
|
2021-02-19 14:01:01 +00:00
|
|
|
//
|
|
|
|
// tombstone should not be nil, addr should not be empty.
|
2021-02-19 14:08:41 +00:00
|
|
|
// Should not be called along with MarkAsGarbage.
|
2021-02-19 14:01:01 +00:00
|
|
|
func (p *InhumePrm) WithTarget(tombstone *objectSDK.Address, addrs ...*objectSDK.Address) *InhumePrm {
|
2020-12-01 09:35:42 +00:00
|
|
|
if p != nil {
|
2021-02-19 14:01:01 +00:00
|
|
|
p.addrs = addrs
|
2020-12-01 09:35:42 +00:00
|
|
|
p.tombstone = tombstone
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-02-19 14:08:41 +00:00
|
|
|
// MarkAsGarbage marks object to be physically removed from local storage.
|
|
|
|
//
|
|
|
|
// Should not be called along with WithTarget.
|
|
|
|
func (p *InhumePrm) MarkAsGarbage(addrs ...*objectSDK.Address) *InhumePrm {
|
|
|
|
if p != nil {
|
|
|
|
p.addrs = addrs
|
|
|
|
p.tombstone = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:43:50 +00:00
|
|
|
var errInhumeFailure = errors.New("inhume operation failed")
|
|
|
|
|
2020-12-01 09:35:42 +00:00
|
|
|
// Inhume calls metabase. Inhume method to mark object as removed. It won't be
|
|
|
|
// removed physically from shard until `Delete` operation.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
|
|
func (e *StorageEngine) Inhume(prm *InhumePrm) (res *InhumeRes, err error) {
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2021-11-09 15:46:12 +00:00
|
|
|
res, err = e.inhume(prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) inhume(prm *InhumePrm) (*InhumeRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddInhumeDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 14:01:01 +00:00
|
|
|
shPrm := new(shard.InhumePrm)
|
2020-12-01 09:35:42 +00:00
|
|
|
|
2021-02-19 14:01:01 +00:00
|
|
|
for i := range prm.addrs {
|
2021-02-19 14:08:41 +00:00
|
|
|
if prm.tombstone != nil {
|
|
|
|
shPrm.WithTarget(prm.tombstone, prm.addrs[i])
|
|
|
|
} else {
|
|
|
|
shPrm.MarkAsGarbage(prm.addrs[i])
|
|
|
|
}
|
2021-02-19 14:01:01 +00:00
|
|
|
|
2021-11-09 15:46:12 +00:00
|
|
|
ok := e.inhumeAddr(prm.addrs[i], shPrm, true)
|
2021-05-20 14:06:38 +00:00
|
|
|
if !ok {
|
2021-11-09 15:46:12 +00:00
|
|
|
ok = e.inhumeAddr(prm.addrs[i], shPrm, false)
|
2021-05-20 14:06:38 +00:00
|
|
|
if !ok {
|
2021-02-19 14:01:01 +00:00
|
|
|
return nil, errInhumeFailure
|
|
|
|
}
|
2021-02-11 15:43:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 14:01:01 +00:00
|
|
|
return new(InhumeRes), nil
|
2021-02-11 15:43:50 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 15:46:12 +00:00
|
|
|
func (e *StorageEngine) inhumeAddr(addr *objectSDK.Address, prm *shard.InhumePrm, checkExists bool) (ok bool) {
|
2021-05-20 14:31:47 +00:00
|
|
|
root := false
|
|
|
|
|
2021-02-11 15:43:50 +00:00
|
|
|
e.iterateOverSortedShards(addr, func(_ int, sh *shard.Shard) (stop bool) {
|
2021-05-20 14:31:47 +00:00
|
|
|
defer func() {
|
|
|
|
// if object is root we continue since information about it
|
|
|
|
// can be presented in other shards
|
|
|
|
if checkExists && root {
|
|
|
|
stop = false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-02-11 15:43:50 +00:00
|
|
|
if checkExists {
|
|
|
|
exRes, err := sh.Exists(new(shard.ExistsPrm).
|
|
|
|
WithAddress(addr),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-05-20 14:31:47 +00:00
|
|
|
if errors.Is(err, object.ErrAlreadyRemoved) {
|
|
|
|
// inhumed once - no need to be inhumed again
|
|
|
|
ok = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var siErr *objectSDK.SplitInfoError
|
|
|
|
if !errors.As(err, &siErr) {
|
|
|
|
// TODO: smth wrong with shard, need to be processed
|
|
|
|
e.log.Warn("could not check for presents in shard",
|
|
|
|
zap.Stringer("shard", sh.ID()),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
root = true
|
2021-02-11 15:43:50 +00:00
|
|
|
} else if !exRes.Exists() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := sh.Inhume(prm)
|
2020-12-01 09:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO: smth wrong with shard, need to be processed
|
|
|
|
e.log.Warn("could not inhume object in shard",
|
|
|
|
zap.Stringer("shard", sh.ID()),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2021-02-11 15:43:50 +00:00
|
|
|
} else {
|
2021-02-19 14:01:01 +00:00
|
|
|
ok = true
|
2020-12-01 09:35:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 15:43:50 +00:00
|
|
|
return err == nil
|
2020-12-01 09:35:42 +00:00
|
|
|
})
|
|
|
|
|
2021-02-11 15:43:50 +00:00
|
|
|
return
|
2020-12-01 09:35:42 +00:00
|
|
|
}
|
2021-02-17 12:27:40 +00:00
|
|
|
|
|
|
|
func (e *StorageEngine) processExpiredTombstones(ctx context.Context, addrs []*objectSDK.Address) {
|
|
|
|
tss := make(map[string]struct{}, len(addrs))
|
|
|
|
|
|
|
|
for i := range addrs {
|
|
|
|
tss[addrs[i].String()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.iterateOverUnsortedShards(func(sh *shard.Shard) (stop bool) {
|
|
|
|
sh.HandleExpiredTombstones(tss)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|