2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-02-15 22:25:27 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2022-02-15 22:25:27 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-07-01 11:21: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-10-05 10:48:09 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-17 12:26:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeletePrm groups the parameters of Delete operation.
|
|
|
|
type DeletePrm struct {
|
2022-09-14 14:18:11 +00:00
|
|
|
addr oid.Address
|
2022-05-31 20:11:42 +00:00
|
|
|
|
|
|
|
forceRemoval bool
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// DeleteRes groups the resulting values of Delete operation.
|
2020-11-17 12:26:03 +00:00
|
|
|
type DeleteRes struct{}
|
|
|
|
|
2022-09-14 14:18:11 +00:00
|
|
|
// WithAddress is a Delete option to set the addresses of the objects to delete.
|
2020-11-17 12:26:03 +00:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-09-14 14:18:11 +00:00
|
|
|
func (p *DeletePrm) WithAddress(addr oid.Address) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.addr = addr
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 20:11:42 +00:00
|
|
|
// WithForceRemoval is a Delete option to remove an object despite any
|
|
|
|
// restrictions imposed on deleting that object. Expected to be used
|
|
|
|
// only in control service.
|
|
|
|
func (p *DeletePrm) WithForceRemoval() {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.forceRemoval = true
|
2022-05-31 20:11:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 11:42:54 +00:00
|
|
|
// Delete marks the objects to be removed.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
2022-02-15 22:25:27 +00:00
|
|
|
//
|
|
|
|
// Returns apistatus.ObjectLocked if at least one object is locked.
|
|
|
|
// In this case no object from the list is marked to be deleted.
|
2022-07-07 12:33:27 +00:00
|
|
|
//
|
|
|
|
// NOTE: Marks any object to be deleted (despite any prohibitions
|
|
|
|
// on operations with that object) if WithForceRemoval option has
|
|
|
|
// been provided.
|
2022-05-31 11:56:59 +00:00
|
|
|
func (e *StorageEngine) Delete(prm DeletePrm) (res DeleteRes, 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.delete(prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
func (e *StorageEngine) delete(prm DeletePrm) (DeleteRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddDeleteDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 22:25:27 +00:00
|
|
|
var locked struct {
|
|
|
|
is bool
|
|
|
|
err apistatus.ObjectLocked
|
|
|
|
}
|
2021-02-15 11:42:54 +00:00
|
|
|
|
2022-09-14 14:18:11 +00:00
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
|
|
|
|
var existsPrm shard.ExistsPrm
|
|
|
|
existsPrm.SetAddress(prm.addr)
|
2021-02-15 11:42:54 +00:00
|
|
|
|
2022-09-14 14:18:11 +00:00
|
|
|
resExists, err := sh.Exists(existsPrm)
|
|
|
|
if err != nil {
|
|
|
|
_, ok := err.(*objectSDK.SplitInfoError)
|
|
|
|
if ok || shard.IsErrRemoved(err) || shard.IsErrObjectExpired(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !shard.IsErrNotFound(err) {
|
|
|
|
e.reportShardError(sh, "could not check object existence", err)
|
2022-05-31 20:11:42 +00:00
|
|
|
}
|
2022-09-14 14:18:11 +00:00
|
|
|
return false
|
|
|
|
} else if !resExists.Exists() {
|
|
|
|
return false
|
|
|
|
}
|
2022-05-20 18:08:59 +00:00
|
|
|
|
2022-09-14 14:18:11 +00:00
|
|
|
var shPrm shard.InhumePrm
|
|
|
|
shPrm.MarkAsGarbage(prm.addr)
|
|
|
|
if prm.forceRemoval {
|
|
|
|
shPrm.ForceRemoval()
|
|
|
|
}
|
2022-02-15 22:25:27 +00:00
|
|
|
|
2022-09-14 14:18:11 +00:00
|
|
|
_, err = sh.Inhume(shPrm)
|
|
|
|
if err != nil {
|
2022-10-05 10:48:09 +00:00
|
|
|
if errors.Is(err, shard.ErrReadOnlyMode) || errors.Is(err, shard.ErrDegradedMode) {
|
|
|
|
e.log.Warn("could not inhume object in shard",
|
|
|
|
zap.Stringer("shard_id", sh.ID()),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
} else {
|
|
|
|
e.reportShardError(sh, "could not inhume object in shard", err)
|
|
|
|
}
|
2022-02-15 22:25:27 +00:00
|
|
|
|
2022-09-14 14:18:11 +00:00
|
|
|
locked.is = errors.As(err, &locked.err)
|
2021-02-15 11:42:54 +00:00
|
|
|
|
2022-09-14 14:18:11 +00:00
|
|
|
return locked.is
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-02-15 22:25:27 +00:00
|
|
|
if locked.is {
|
2022-05-31 11:56:59 +00:00
|
|
|
return DeleteRes{}, locked.err
|
2022-02-15 22:25:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return DeleteRes{}, nil
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|