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-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
|
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 {
|
|
|
|
deletedLockObj []oid.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-21 11:28:05 +00:00
|
|
|
// WithAddresses sets a list of object addresses that should be inhumed.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (p *InhumePrm) WithAddresses(addrs ...oid.Address) {
|
2020-12-08 09:56:14 +00:00
|
|
|
if p != nil {
|
2021-02-15 11:50:21 +00:00
|
|
|
p.target = addrs
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTombstoneAddress sets tombstone address as the reason for inhume operation.
|
2021-02-15 11:11:10 +00:00
|
|
|
//
|
|
|
|
// addr should not be nil.
|
|
|
|
// Should not be called along with WithGCMark.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (p *InhumePrm) WithTombstoneAddress(addr oid.Address) {
|
2020-12-08 09:56:14 +00:00
|
|
|
if p != nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
p.tomb = &addr
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 11:11:10 +00:00
|
|
|
// WithGCMark marks the object to be physically removed.
|
|
|
|
//
|
|
|
|
// Should not be called along with WithTombstoneAddress.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (p *InhumePrm) WithGCMark() {
|
2021-02-15 11:11:10 +00:00
|
|
|
if p != nil {
|
|
|
|
p.tomb = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:38:32 +00:00
|
|
|
// WithLockObjectHandling checks if there were
|
|
|
|
// any LOCK object among the targets set via WithAddresses.
|
|
|
|
func (p *InhumePrm) WithLockObjectHandling() {
|
|
|
|
if p != nil {
|
|
|
|
p.lockObjectHandling = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// Inhume inhumes the object by specified address.
|
2021-02-15 11:11:10 +00:00
|
|
|
//
|
|
|
|
// tomb should not be nil.
|
2022-05-31 17:00:41 +00:00
|
|
|
func Inhume(db *DB, target, tomb oid.Address) error {
|
2022-05-20 16:48:14 +00:00
|
|
|
var inhumePrm InhumePrm
|
|
|
|
inhumePrm.WithAddresses(target)
|
|
|
|
inhumePrm.WithTombstoneAddress(tomb)
|
|
|
|
|
|
|
|
_, err := db.Inhume(inhumePrm)
|
2020-12-08 09:56:14 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-19 09:49:23 +00:00
|
|
|
var errBreakBucketForEach = errors.New("bucket ForEach break")
|
|
|
|
|
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
|
|
|
|
// if at least one object is locked.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (db *DB) Inhume(prm InhumePrm) (res InhumeRes, err error) {
|
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)
|
|
|
|
|
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 {
|
2021-04-08 14:19:31 +00:00
|
|
|
bkt = tx.Bucket(graveyardBucketName)
|
2022-05-31 17:00:41 +00:00
|
|
|
tombKey := addressKey(*prm.tomb)
|
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
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
}
|
|
|
|
|
2021-02-15 11:50:21 +00:00
|
|
|
obj, err := db.get(tx, prm.target[i], false, true)
|
|
|
|
|
|
|
|
// if object is stored and it is regular object then update bucket
|
|
|
|
// with container size estimations
|
2022-03-03 14:19:05 +00:00
|
|
|
if err == nil && obj.Type() == object.TypeRegular {
|
2022-05-31 17:00:41 +00:00
|
|
|
err := changeContainerSize(tx, cnr, obj.PayloadSize(), false)
|
2021-02-15 11:50:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 09:49:23 +00:00
|
|
|
targetKey := addressKey(prm.target[i])
|
|
|
|
|
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 {
|
|
|
|
if isLockObject(tx, cnr, id) {
|
|
|
|
res.deletedLockObj = append(res.deletedLockObj, prm.target[i])
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 11:07:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 11:50:21 +00:00
|
|
|
return nil
|
2020-11-23 13:30:56 +00:00
|
|
|
})
|
2020-12-08 09:56:14 +00:00
|
|
|
|
|
|
|
return
|
2020-11-23 13:30:56 +00:00
|
|
|
}
|