2020-10-29 12:03:55 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"bytes"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-12-08 07:51:34 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-03-02 09:17:32 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
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"
|
2020-10-29 12:03:55 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// DeletePrm groups the parameters of Delete operation.
|
|
|
|
type DeletePrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrs []oid.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// DeleteRes groups the resulting values of Delete operation.
|
2022-08-19 16:49:09 +00:00
|
|
|
type DeleteRes struct {
|
2022-09-09 11:33:38 +00:00
|
|
|
rawRemoved uint64
|
|
|
|
availableRemoved uint64
|
2022-08-19 16:49:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// AvailableObjectsRemoved returns the number of removed available
|
|
|
|
// objects.
|
|
|
|
func (d DeleteRes) AvailableObjectsRemoved() uint64 {
|
|
|
|
return d.availableRemoved
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawObjectsRemoved returns the number of removed raw objects.
|
|
|
|
func (d DeleteRes) RawObjectsRemoved() uint64 {
|
|
|
|
return d.rawRemoved
|
2022-08-19 16:49:09 +00:00
|
|
|
}
|
2020-12-08 09:56:14 +00:00
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetAddresses is a Delete option to set the addresses of the objects to delete.
|
2020-12-08 09:56:14 +00:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (p *DeletePrm) SetAddresses(addrs ...oid.Address) {
|
|
|
|
p.addrs = addrs
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:39:48 +00:00
|
|
|
type referenceNumber struct {
|
|
|
|
all, cur int
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2021-02-11 17:39:48 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type referenceCounter map[string]*referenceNumber
|
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
// Delete removed object records from metabase indexes.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (db *DB) Delete(prm DeletePrm) (DeleteRes, error) {
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-08-19 16:49:09 +00:00
|
|
|
var rawRemoved uint64
|
2022-09-09 11:33:38 +00:00
|
|
|
var availableRemoved uint64
|
2022-08-19 16:49:09 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
err = db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2022-09-09 11:33:38 +00:00
|
|
|
rawRemoved, availableRemoved, err = db.deleteGroup(tx, prm.addrs)
|
2022-08-19 16:49:09 +00:00
|
|
|
return err
|
2021-02-11 17:39:48 +00:00
|
|
|
})
|
2022-03-02 09:17:32 +00:00
|
|
|
if err == nil {
|
|
|
|
for i := range prm.addrs {
|
|
|
|
storagelog.Write(db.log,
|
|
|
|
storagelog.AddressField(prm.addrs[i]),
|
|
|
|
storagelog.OpField("metabase DELETE"))
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 11:33:38 +00:00
|
|
|
return DeleteRes{
|
|
|
|
rawRemoved: rawRemoved,
|
|
|
|
availableRemoved: availableRemoved,
|
|
|
|
}, err
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// deleteGroup deletes object from the metabase. Handles removal of the
|
|
|
|
// references of the split objects.
|
|
|
|
// The first return value is a physical objects removed number: physical
|
|
|
|
// objects that were stored. The second return value is a logical objects
|
|
|
|
// removed number: objects that were available (without Tombstones, GCMarks
|
|
|
|
// non-expired, etc.)
|
|
|
|
func (db *DB) deleteGroup(tx *bbolt.Tx, addrs []oid.Address) (uint64, uint64, error) {
|
2021-02-11 17:39:48 +00:00
|
|
|
refCounter := make(referenceCounter, len(addrs))
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
2021-02-11 17:39:48 +00:00
|
|
|
|
2022-08-19 12:36:37 +00:00
|
|
|
var rawDeleted uint64
|
2022-09-09 11:33:38 +00:00
|
|
|
var availableDeleted uint64
|
|
|
|
|
2021-02-11 17:39:48 +00:00
|
|
|
for i := range addrs {
|
2022-09-09 11:33:38 +00:00
|
|
|
removed, available, err := db.delete(tx, addrs[i], refCounter, currEpoch)
|
2021-02-11 17:39:48 +00:00
|
|
|
if err != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
return 0, 0, err // maybe log and continue?
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
2022-08-19 12:36:37 +00:00
|
|
|
|
|
|
|
if removed {
|
|
|
|
rawDeleted++
|
|
|
|
}
|
2022-09-09 11:33:38 +00:00
|
|
|
|
|
|
|
if available {
|
|
|
|
availableDeleted++
|
|
|
|
}
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
if rawDeleted > 0 {
|
|
|
|
err := db.updateCounter(tx, phy, rawDeleted, false)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("could not decrease phy object counter: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if availableDeleted > 0 {
|
|
|
|
err := db.updateCounter(tx, logical, availableDeleted, false)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("could not decrease logical object counter: %w", err)
|
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, refNum := range refCounter {
|
|
|
|
if refNum.cur == refNum.all {
|
|
|
|
err := db.deleteObject(tx, refNum.obj, true)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
return rawDeleted, availableDeleted, err // maybe log and continue?
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
return rawDeleted, availableDeleted, nil
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// delete removes object indexes from the metabase. Counts the references
|
|
|
|
// of the object that is being removed.
|
|
|
|
// The first return value indicates if an object has been removed. (removing a
|
|
|
|
// non-exist object is error-free). The second return value indicates if an
|
|
|
|
// object was available before the removal (for calculating the logical object
|
|
|
|
// counter).
|
|
|
|
func (db *DB) delete(tx *bbolt.Tx, addr oid.Address, refCounter referenceCounter, currEpoch uint64) (bool, bool, error) {
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, addressKeySize)
|
|
|
|
addrKey := addressKey(addr, key)
|
2021-04-08 14:19:31 +00:00
|
|
|
garbageBKT := tx.Bucket(garbageBucketName)
|
2022-09-09 11:33:38 +00:00
|
|
|
graveyardBKT := tx.Bucket(graveyardBucketName)
|
|
|
|
|
|
|
|
removeAvailableObject := inGraveyardWithKey(addrKey, graveyardBKT, garbageBKT) == 0
|
|
|
|
|
|
|
|
// remove record from the garbage bucket
|
2021-04-08 14:19:31 +00:00
|
|
|
if garbageBKT != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
err := garbageBKT.Delete(addrKey)
|
2021-04-06 14:07:42 +00:00
|
|
|
if err != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
return false, false, fmt.Errorf("could not remove from garbage bucket: %w", err)
|
2021-04-06 14:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
// unmarshal object, work only with physically stored (raw == true) objects
|
2022-09-08 11:54:21 +00:00
|
|
|
obj, err := db.get(tx, addr, key, false, true, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-09-04 08:52:01 +00:00
|
|
|
var siErr *objectSDK.SplitInfoError
|
|
|
|
var notFoundErr apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
if errors.As(err, ¬FoundErr) || errors.As(err, &siErr) {
|
2022-09-09 11:33:38 +00:00
|
|
|
return false, false, nil
|
2021-04-06 14:07:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
return false, false, err
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if object is an only link to a parent, then remove parent
|
2022-03-03 14:19:05 +00:00
|
|
|
if parent := obj.Parent(); parent != nil {
|
|
|
|
parAddr := object.AddressOf(parent)
|
2022-09-08 11:54:21 +00:00
|
|
|
sParAddr := addressKey(parAddr, key)
|
|
|
|
k := string(sParAddr)
|
2021-02-11 17:39:48 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
nRef, ok := refCounter[k]
|
2021-02-11 17:39:48 +00:00
|
|
|
if !ok {
|
|
|
|
nRef = &referenceNumber{
|
2022-03-03 14:19:05 +00:00
|
|
|
all: parentLength(tx, parAddr),
|
2021-02-11 17:39:48 +00:00
|
|
|
addr: parAddr,
|
|
|
|
obj: parent,
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
refCounter[k] = nRef
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
|
|
|
|
nRef.cur++
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove object
|
2022-08-19 12:36:37 +00:00
|
|
|
err = db.deleteObject(tx, obj, false)
|
|
|
|
if err != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
return false, false, fmt.Errorf("could not remove object: %w", err)
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
return true, removeAvailableObject, nil
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func (db *DB) deleteObject(
|
|
|
|
tx *bbolt.Tx,
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object,
|
2020-12-08 07:51:34 +00:00
|
|
|
isParent bool,
|
|
|
|
) error {
|
2022-03-21 13:59:05 +00:00
|
|
|
err := delUniqueIndexes(tx, obj, isParent)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't remove unique indexes")
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = updateListIndexes(tx, obj, delListIndexItem)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't remove list indexes: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = updateFKBTIndexes(tx, obj, delFKBTIndexItem)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't remove fake bucket tree indexes: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// parentLength returns amount of available children from parentid index.
|
2022-05-31 17:00:41 +00:00
|
|
|
func parentLength(tx *bbolt.Tx, addr oid.Address) int {
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
|
|
|
|
|
|
|
bkt := tx.Bucket(parentBucketName(addr.Container(), bucketName[:]))
|
2020-12-08 07:51:34 +00:00
|
|
|
if bkt == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
lst, err := decodeList(bkt.Get(objectKey(addr.Object(), bucketName[:])))
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(lst)
|
|
|
|
}
|
|
|
|
|
|
|
|
func delUniqueIndexItem(tx *bbolt.Tx, item namedBucketItem) {
|
|
|
|
bkt := tx.Bucket(item.name)
|
|
|
|
if bkt != nil {
|
|
|
|
_ = bkt.Delete(item.key) // ignore error, best effort there
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
func delFKBTIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
2020-12-08 07:51:34 +00:00
|
|
|
bkt := tx.Bucket(item.name)
|
|
|
|
if bkt == nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fkbtRoot := bkt.Bucket(item.key)
|
|
|
|
if fkbtRoot == nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = fkbtRoot.Delete(item.val) // ignore error, best effort there
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
func delListIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
2020-12-08 07:51:34 +00:00
|
|
|
bkt := tx.Bucket(item.name)
|
|
|
|
if bkt == nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lst, err := decodeList(bkt.Get(item.key))
|
|
|
|
if err != nil || len(lst) == 0 {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove element from the list
|
|
|
|
for i := range lst {
|
2022-03-21 15:39:46 +00:00
|
|
|
if bytes.Equal(item.val, lst[i]) {
|
|
|
|
copy(lst[i:], lst[i+1:])
|
|
|
|
lst = lst[:len(lst)-1]
|
|
|
|
break
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// if list empty, remove the key from <list> bucket
|
2022-03-21 15:39:46 +00:00
|
|
|
if len(lst) == 0 {
|
2020-12-08 07:51:34 +00:00
|
|
|
_ = bkt.Delete(item.key) // ignore error, best effort there
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if list is not empty, then update it
|
|
|
|
encodedLst, err := encodeList(lst)
|
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil // ignore error, best effort there
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = bkt.Put(item.key, encodedLst) // ignore error, best effort there
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
func delUniqueIndexes(tx *bbolt.Tx, obj *objectSDK.Object, isParent bool) error {
|
2022-03-03 14:19:05 +00:00
|
|
|
addr := object.AddressOf(obj)
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
objKey := objectKey(addr.Object(), make([]byte, objectKeySize))
|
|
|
|
addrKey := addressKey(addr, make([]byte, addressKeySize))
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := addr.Container()
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// add value to primary unique bucket
|
|
|
|
if !isParent {
|
|
|
|
switch obj.Type() {
|
|
|
|
case objectSDK.TypeRegular:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName = primaryBucketName(cnr, bucketName)
|
2020-12-08 07:51:34 +00:00
|
|
|
case objectSDK.TypeTombstone:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName = tombstoneBucketName(cnr, bucketName)
|
2020-12-08 07:51:34 +00:00
|
|
|
case objectSDK.TypeStorageGroup:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName = storageGroupBucketName(cnr, bucketName)
|
2022-02-15 12:51:56 +00:00
|
|
|
case objectSDK.TypeLock:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName = bucketNameLockers(cnr, bucketName)
|
2020-12-08 07:51:34 +00:00
|
|
|
default:
|
2022-03-21 13:59:05 +00:00
|
|
|
return ErrUnknownObjectType
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
delUniqueIndexItem(tx, namedBucketItem{
|
2020-12-08 07:51:34 +00:00
|
|
|
name: bucketName,
|
|
|
|
key: objKey,
|
|
|
|
})
|
2021-02-11 17:39:48 +00:00
|
|
|
} else {
|
2022-03-21 13:59:05 +00:00
|
|
|
delUniqueIndexItem(tx, namedBucketItem{
|
2022-09-08 11:54:21 +00:00
|
|
|
name: parentBucketName(cnr, bucketName),
|
2021-02-11 17:39:48 +00:00
|
|
|
key: objKey,
|
|
|
|
})
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from storage id index
|
2022-09-08 11:54:21 +00:00
|
|
|
name: smallBucketName(cnr, bucketName),
|
2022-03-21 13:59:05 +00:00
|
|
|
key: objKey,
|
|
|
|
})
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from root index
|
2022-09-08 11:54:21 +00:00
|
|
|
name: rootBucketName(cnr, bucketName),
|
2022-03-21 13:59:05 +00:00
|
|
|
key: objKey,
|
|
|
|
})
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from ToMoveIt index
|
|
|
|
name: toMoveItBucketName,
|
|
|
|
key: addrKey,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|