2020-11-23 13:30:56 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2021-02-19 09:49:23 +00:00
|
|
|
"bytes"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-19 09:49:23 +00:00
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr"
|
2022-02-15 22:16:07 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-03-03 14:19:05 +00:00
|
|
|
"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-11-23 13:30:56 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// InhumePrm encapsulates parameters for Inhume operation.
|
|
|
|
type InhumePrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
tomb *oid.Address
|
2021-02-15 11:50:21 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
target []oid.Address
|
2022-05-26 17:38:32 +00:00
|
|
|
|
|
|
|
lockObjectHandling bool
|
2022-05-31 20:11:42 +00:00
|
|
|
|
|
|
|
forceRemoval bool
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InhumeRes encapsulates results of Inhume operation.
|
2022-05-26 17:38:32 +00:00
|
|
|
type InhumeRes struct {
|
2022-09-09 11:33:38 +00:00
|
|
|
deletedLockObj []oid.Address
|
|
|
|
availableImhumed uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// AvailableInhumed return number of available object
|
|
|
|
// that have been inhumed.
|
|
|
|
func (i InhumeRes) AvailableInhumed() uint64 {
|
|
|
|
return i.availableImhumed
|
2022-05-26 17:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeletedLockObjects returns deleted object of LOCK
|
|
|
|
// type. Returns always nil if WithoutLockObjectHandling
|
|
|
|
// was provided to the InhumePrm.
|
|
|
|
func (i InhumeRes) DeletedLockObjects() []oid.Address {
|
|
|
|
return i.deletedLockObj
|
|
|
|
}
|
2020-12-08 09:56:14 +00:00
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetAddresses sets a list of object addresses that should be inhumed.
|
|
|
|
func (p *InhumePrm) SetAddresses(addrs ...oid.Address) {
|
|
|
|
p.target = addrs
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetTombstoneAddress sets tombstone address as the reason for inhume operation.
|
2021-02-15 11:11:10 +00:00
|
|
|
//
|
|
|
|
// addr should not be nil.
|
2022-07-12 14:59:37 +00:00
|
|
|
// Should not be called along with SetGCMark.
|
|
|
|
func (p *InhumePrm) SetTombstoneAddress(addr oid.Address) {
|
|
|
|
p.tomb = &addr
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetGCMark marks the object to be physically removed.
|
2021-02-15 11:11:10 +00:00
|
|
|
//
|
2022-07-12 14:59:37 +00:00
|
|
|
// Should not be called along with SetTombstoneAddress.
|
|
|
|
func (p *InhumePrm) SetGCMark() {
|
|
|
|
p.tomb = nil
|
2021-02-15 11:11:10 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetLockObjectHandling checks if there were
|
2022-05-26 17:38:32 +00:00
|
|
|
// any LOCK object among the targets set via WithAddresses.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (p *InhumePrm) SetLockObjectHandling() {
|
|
|
|
p.lockObjectHandling = true
|
2022-05-26 17:38:32 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetForceGCMark allows removal any object. Expected to be
|
2022-05-31 20:11:42 +00:00
|
|
|
// called only in control service.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (p *InhumePrm) SetForceGCMark() {
|
|
|
|
p.tomb = nil
|
|
|
|
p.forceRemoval = true
|
2022-05-31 20:11:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 09:49:23 +00:00
|
|
|
var errBreakBucketForEach = errors.New("bucket ForEach break")
|
|
|
|
|
2022-06-06 14:44:37 +00:00
|
|
|
// ErrLockObjectRemoval is returned when inhume operation is being
|
|
|
|
// performed on lock object, and it is not a forced object removal.
|
2022-10-26 12:23:12 +00:00
|
|
|
var ErrLockObjectRemoval = logicerr.Wrap(errors.New("lock object removal"))
|
2022-06-06 14:44:37 +00:00
|
|
|
|
2020-11-23 13:30:56 +00:00
|
|
|
// Inhume marks objects as removed but not removes it from metabase.
|
2022-02-15 22:16:07 +00:00
|
|
|
//
|
|
|
|
// Allows inhuming non-locked objects only. Returns apistatus.ObjectLocked
|
2022-07-07 12:33:27 +00:00
|
|
|
// if at least one object is locked. Returns ErrLockObjectRemoval if inhuming
|
|
|
|
// is being performed on lock (not locked) object.
|
|
|
|
//
|
|
|
|
// NOTE: Marks any object with GC mark (despite any prohibitions on operations
|
|
|
|
// with that object) if WithForceGCMark option has been provided.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (db *DB) Inhume(prm InhumePrm) (res InhumeRes, err error) {
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-10-26 06:12:09 +00:00
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return InhumeRes{}, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
2022-09-09 11:33:38 +00:00
|
|
|
var inhumed uint64
|
2022-07-27 18:38:28 +00:00
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
err = db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2021-04-08 17:06:13 +00:00
|
|
|
garbageBKT := tx.Bucket(garbageBucketName)
|
2022-09-09 11:33:38 +00:00
|
|
|
graveyardBKT := tx.Bucket(graveyardBucketName)
|
2021-04-08 17:06:13 +00:00
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
var (
|
|
|
|
// target bucket of the operation, one of the:
|
|
|
|
// 1. Graveyard if Inhume was called with a Tombstone
|
|
|
|
// 2. Garbage if Inhume was called with a GC mark
|
|
|
|
bkt *bbolt.Bucket
|
|
|
|
// value that will be put in the bucket, one of the:
|
|
|
|
// 1. tombstone address if Inhume was called with
|
|
|
|
// a Tombstone
|
|
|
|
// 2. zeroValue if Inhume was called with a GC mark
|
|
|
|
value []byte
|
|
|
|
)
|
2020-11-23 13:30:56 +00:00
|
|
|
|
2021-02-19 09:49:23 +00:00
|
|
|
if prm.tomb != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
bkt = graveyardBKT
|
2022-09-08 11:54:21 +00:00
|
|
|
tombKey := addressKey(*prm.tomb, make([]byte, addressKeySize))
|
2021-02-19 09:49:23 +00:00
|
|
|
|
|
|
|
// it is forbidden to have a tomb-on-tomb in NeoFS,
|
|
|
|
// so graveyard keys must not be addresses of tombstones
|
2021-04-08 14:19:31 +00:00
|
|
|
data := bkt.Get(tombKey)
|
|
|
|
if data != nil {
|
|
|
|
err := bkt.Delete(tombKey)
|
2021-02-19 09:49:23 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not remove grave with tombstone key: %w", err)
|
2021-02-19 09:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 14:19:31 +00:00
|
|
|
|
|
|
|
value = tombKey
|
2021-02-19 09:49:23 +00:00
|
|
|
} else {
|
2021-04-08 17:06:13 +00:00
|
|
|
bkt = garbageBKT
|
2021-04-08 14:19:31 +00:00
|
|
|
value = zeroValue
|
2021-02-19 09:49:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
buf := make([]byte, addressKeySize)
|
2021-02-15 11:50:21 +00:00
|
|
|
for i := range prm.target {
|
2022-05-31 17:00:41 +00:00
|
|
|
id := prm.target[i].Object()
|
|
|
|
cnr := prm.target[i].Container()
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2022-02-15 22:16:07 +00:00
|
|
|
// prevent locked objects to be inhumed
|
2022-05-12 16:37:46 +00:00
|
|
|
if objectLocked(tx, cnr, id) {
|
2022-02-15 22:16:07 +00:00
|
|
|
return apistatus.ObjectLocked{}
|
|
|
|
}
|
|
|
|
|
2022-05-31 20:11:42 +00:00
|
|
|
var lockWasChecked bool
|
|
|
|
|
|
|
|
// prevent lock objects to be inhumed
|
|
|
|
// if `Inhume` was called not with the
|
|
|
|
// `WithForceGCMark` option
|
|
|
|
if !prm.forceRemoval {
|
|
|
|
if isLockObject(tx, cnr, id) {
|
2022-06-06 14:44:37 +00:00
|
|
|
return ErrLockObjectRemoval
|
2022-05-31 20:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lockWasChecked = true
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
obj, err := db.get(tx, prm.target[i], buf, false, true, currEpoch)
|
|
|
|
targetKey := addressKey(prm.target[i], buf)
|
2022-09-09 11:33:38 +00:00
|
|
|
if err == nil {
|
|
|
|
if inGraveyardWithKey(targetKey, graveyardBKT, garbageBKT) == 0 {
|
|
|
|
// object is available, decrement the
|
|
|
|
// logical counter
|
|
|
|
inhumed++
|
|
|
|
}
|
2021-02-15 11:50:21 +00:00
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// if object is stored, and it is regular object then update bucket
|
|
|
|
// with container size estimations
|
|
|
|
if obj.Type() == object.TypeRegular {
|
|
|
|
err := changeContainerSize(tx, cnr, obj.PayloadSize(), false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-15 11:50:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.tomb != nil {
|
2021-02-19 09:49:23 +00:00
|
|
|
targetIsTomb := false
|
|
|
|
|
|
|
|
// iterate over graveyard and check if target address
|
|
|
|
// is the address of tombstone in graveyard.
|
2021-04-08 14:19:31 +00:00
|
|
|
err = bkt.ForEach(func(k, v []byte) error {
|
2021-02-19 09:49:23 +00:00
|
|
|
// check if graveyard has record with key corresponding
|
|
|
|
// to tombstone address (at least one)
|
|
|
|
targetIsTomb = bytes.Equal(v, targetKey)
|
|
|
|
|
|
|
|
if targetIsTomb {
|
|
|
|
// break bucket iterator
|
|
|
|
return errBreakBucketForEach
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil && !errors.Is(err, errBreakBucketForEach) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not add grave if target is a tombstone
|
|
|
|
if targetIsTomb {
|
|
|
|
continue
|
|
|
|
}
|
2021-04-08 17:06:13 +00:00
|
|
|
|
|
|
|
// if tombstone appears object must be
|
|
|
|
// additionally marked with GC
|
|
|
|
err = garbageBKT.Put(targetKey, zeroValue)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-15 11:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// consider checking if target is already in graveyard?
|
2021-04-08 14:19:31 +00:00
|
|
|
err = bkt.Put(targetKey, value)
|
2021-01-22 11:07:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-26 17:38:32 +00:00
|
|
|
|
|
|
|
if prm.lockObjectHandling {
|
2022-05-31 20:11:42 +00:00
|
|
|
// do not perform lock check if
|
|
|
|
// it was already called
|
|
|
|
if lockWasChecked {
|
|
|
|
// inhumed object is not of
|
|
|
|
// the LOCK type
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:38:32 +00:00
|
|
|
if isLockObject(tx, cnr, id) {
|
|
|
|
res.deletedLockObj = append(res.deletedLockObj, prm.target[i])
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 11:07:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
return db.updateCounter(tx, logical, inhumed, false)
|
2020-11-23 13:30:56 +00:00
|
|
|
})
|
2020-12-08 09:56:14 +00:00
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
res.availableImhumed = inhumed
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
return
|
2020-11-23 13:30:56 +00:00
|
|
|
}
|