2020-10-29 12:03:55 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"bytes"
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-12-08 07:51:34 +00:00
|
|
|
"fmt"
|
2023-06-06 09:27:19 +00:00
|
|
|
"time"
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
storagelog "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-11-01 17:53:24 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-03-07 13:38:26 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-10-29 12:03:55 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-10-29 12:03:55 +00:00
|
|
|
)
|
|
|
|
|
2024-03-11 14:55:50 +00:00
|
|
|
var errFailedToRemoveUniqueIndexes = errors.New("can't remove unique indexes")
|
|
|
|
|
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 {
|
2023-12-25 07:15:26 +00:00
|
|
|
phyCount uint64
|
|
|
|
logicCount uint64
|
|
|
|
userCount uint64
|
|
|
|
phySize uint64
|
|
|
|
logicSize uint64
|
|
|
|
removedByCnrID map[cid.ID]ObjectCounters
|
2022-08-19 16:49:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
// LogicCount returns the number of removed logic
|
2022-09-09 11:33:38 +00:00
|
|
|
// objects.
|
2023-12-25 07:15:26 +00:00
|
|
|
func (d DeleteRes) LogicCount() uint64 {
|
|
|
|
return d.logicCount
|
2022-09-09 11:33:38 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
func (d DeleteRes) UserCount() uint64 {
|
|
|
|
return d.userCount
|
2023-12-05 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
// RemovedByCnrID returns the number of removed objects by container ID.
|
|
|
|
func (d DeleteRes) RemovedByCnrID() map[cid.ID]ObjectCounters {
|
|
|
|
return d.removedByCnrID
|
|
|
|
}
|
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
// PhyCount returns the number of removed physical objects.
|
|
|
|
func (d DeleteRes) PhyCount() uint64 {
|
|
|
|
return d.phyCount
|
2022-08-19 16:49:09 +00:00
|
|
|
}
|
2020-12-08 09:56:14 +00:00
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
// PhySize returns the size of removed physical objects.
|
|
|
|
func (d DeleteRes) PhySize() uint64 {
|
|
|
|
return d.phySize
|
2022-12-01 11:59:22 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
// LogicSize returns the size of removed logical objects.
|
|
|
|
func (d DeleteRes) LogicSize() uint64 {
|
|
|
|
return d.logicSize
|
2023-02-02 20:28:42 +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.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (db *DB) Delete(ctx context.Context, prm DeletePrm) (DeleteRes, error) {
|
2023-06-06 09:27:19 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
deleted = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("Delete", time.Since(startedAt), deleted)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.Delete",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.Int("addr_count", len(prm.addrs)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return DeleteRes{}, ErrDegradedMode
|
2022-11-15 14:53:23 +00:00
|
|
|
} else if db.mode.ReadOnly() {
|
|
|
|
return DeleteRes{}, ErrReadOnlyMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:49:09 +00:00
|
|
|
var err error
|
2023-11-02 10:50:52 +00:00
|
|
|
var res DeleteRes
|
2022-08-19 16:49:09 +00:00
|
|
|
|
|
|
|
err = db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2023-11-02 10:50:52 +00:00
|
|
|
res, 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 {
|
2023-06-06 09:27:19 +00:00
|
|
|
deleted = true
|
2022-03-02 09:17:32 +00:00
|
|
|
for i := range prm.addrs {
|
|
|
|
storagelog.Write(db.log,
|
|
|
|
storagelog.AddressField(prm.addrs[i]),
|
|
|
|
storagelog.OpField("metabase DELETE"))
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 10:50:52 +00:00
|
|
|
return res, metaerr.Wrap(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.
|
2023-11-02 10:50:52 +00:00
|
|
|
func (db *DB) deleteGroup(tx *bbolt.Tx, addrs []oid.Address) (DeleteRes, error) {
|
|
|
|
res := DeleteRes{
|
|
|
|
removedByCnrID: make(map[cid.ID]ObjectCounters),
|
|
|
|
}
|
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
|
|
|
|
|
|
|
for i := range addrs {
|
2023-12-05 14:00:16 +00:00
|
|
|
r, err := db.delete(tx, addrs[i], refCounter, currEpoch)
|
2021-02-11 17:39:48 +00:00
|
|
|
if err != nil {
|
2023-12-06 10:02:14 +00:00
|
|
|
return DeleteRes{}, err
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
2022-08-19 12:36:37 +00:00
|
|
|
|
2023-12-06 10:02:14 +00:00
|
|
|
applyDeleteSingleResult(r, &res, addrs, i)
|
|
|
|
}
|
2023-11-01 17:53:24 +00:00
|
|
|
|
2023-12-06 10:02:14 +00:00
|
|
|
if err := db.updateCountersDelete(tx, res); err != nil {
|
|
|
|
return DeleteRes{}, err
|
|
|
|
}
|
2023-12-05 14:00:16 +00:00
|
|
|
|
2023-12-06 10:02:14 +00:00
|
|
|
for _, refNum := range refCounter {
|
|
|
|
if refNum.cur == refNum.all {
|
|
|
|
err := db.deleteObject(tx, refNum.obj, true)
|
|
|
|
if err != nil {
|
|
|
|
return DeleteRes{}, err
|
2023-12-05 14:00:16 +00:00
|
|
|
}
|
2022-09-09 11:33:38 +00:00
|
|
|
}
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 10:02:14 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) updateCountersDelete(tx *bbolt.Tx, res DeleteRes) error {
|
2023-12-25 07:15:26 +00:00
|
|
|
if res.phyCount > 0 {
|
|
|
|
err := db.updateShardObjectCounter(tx, phy, res.phyCount, false)
|
2022-09-09 11:33:38 +00:00
|
|
|
if err != nil {
|
2023-12-06 10:02:14 +00:00
|
|
|
return fmt.Errorf("could not decrease phy object counter: %w", err)
|
2022-09-09 11:33:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
if res.logicCount > 0 {
|
|
|
|
err := db.updateShardObjectCounter(tx, logical, res.logicCount, false)
|
2022-09-09 11:33:38 +00:00
|
|
|
if err != nil {
|
2023-12-06 10:02:14 +00:00
|
|
|
return fmt.Errorf("could not decrease logical object counter: %w", err)
|
2022-09-09 11:33:38 +00:00
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
if res.userCount > 0 {
|
|
|
|
err := db.updateShardObjectCounter(tx, user, res.userCount, false)
|
2023-12-05 14:00:16 +00:00
|
|
|
if err != nil {
|
2023-12-06 10:02:14 +00:00
|
|
|
return fmt.Errorf("could not decrease user object counter: %w", err)
|
2023-12-05 14:00:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
if err := db.updateContainerCounter(tx, res.removedByCnrID, false); err != nil {
|
2023-12-06 10:02:14 +00:00
|
|
|
return fmt.Errorf("could not decrease container object counter: %w", err)
|
2023-11-01 17:53:24 +00:00
|
|
|
}
|
2023-12-06 10:02:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-11-01 17:53:24 +00:00
|
|
|
|
2023-12-06 10:02:14 +00:00
|
|
|
func applyDeleteSingleResult(r deleteSingleResult, res *DeleteRes, addrs []oid.Address, i int) {
|
2023-12-25 07:15:26 +00:00
|
|
|
if r.Phy {
|
2023-12-06 10:02:14 +00:00
|
|
|
if v, ok := res.removedByCnrID[addrs[i].Container()]; ok {
|
|
|
|
v.Phy++
|
|
|
|
res.removedByCnrID[addrs[i].Container()] = v
|
|
|
|
} else {
|
|
|
|
res.removedByCnrID[addrs[i].Container()] = ObjectCounters{
|
|
|
|
Phy: 1,
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-06 10:02:14 +00:00
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
res.phyCount++
|
|
|
|
res.phySize += r.Size
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
if r.Logic {
|
2023-12-06 10:02:14 +00:00
|
|
|
if v, ok := res.removedByCnrID[addrs[i].Container()]; ok {
|
|
|
|
v.Logic++
|
|
|
|
res.removedByCnrID[addrs[i].Container()] = v
|
|
|
|
} else {
|
|
|
|
res.removedByCnrID[addrs[i].Container()] = ObjectCounters{
|
|
|
|
Logic: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
res.logicCount++
|
|
|
|
res.logicSize += r.Size
|
2023-12-06 10:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.User {
|
|
|
|
if v, ok := res.removedByCnrID[addrs[i].Container()]; ok {
|
|
|
|
v.User++
|
|
|
|
res.removedByCnrID[addrs[i].Container()] = v
|
|
|
|
} else {
|
|
|
|
res.removedByCnrID[addrs[i].Container()] = ObjectCounters{
|
|
|
|
User: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 07:15:26 +00:00
|
|
|
res.userCount++
|
2023-12-06 10:02:14 +00:00
|
|
|
}
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
type deleteSingleResult struct {
|
2023-12-25 07:15:26 +00:00
|
|
|
Phy bool
|
|
|
|
Logic bool
|
|
|
|
User bool
|
|
|
|
Size uint64
|
2023-12-05 14:00:16 +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
|
2023-12-05 14:00:16 +00:00
|
|
|
// counter). The third return value The fourth return value is removed object payload size.
|
|
|
|
func (db *DB) delete(tx *bbolt.Tx, addr oid.Address, refCounter referenceCounter, currEpoch uint64) (deleteSingleResult, 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
|
|
|
|
|
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 {
|
2024-02-06 16:25:24 +00:00
|
|
|
if client.IsErrObjectNotFound(err) {
|
|
|
|
addrKey = addressKey(addr, key)
|
|
|
|
if garbageBKT != nil {
|
|
|
|
err := garbageBKT.Delete(addrKey)
|
|
|
|
if err != nil {
|
|
|
|
return deleteSingleResult{}, fmt.Errorf("could not remove from garbage bucket: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deleteSingleResult{}, nil
|
|
|
|
}
|
2022-09-04 08:52:01 +00:00
|
|
|
var siErr *objectSDK.SplitInfoError
|
2024-05-27 19:07:43 +00:00
|
|
|
var ecErr *objectSDK.ECInfoError
|
|
|
|
if errors.As(err, &siErr) || errors.As(err, &ecErr) {
|
2024-01-10 08:56:24 +00:00
|
|
|
// if object is virtual (parent) then do nothing, it will be deleted with last child
|
2024-05-27 19:07:43 +00:00
|
|
|
// if object is erasure-coded it will be deleted with the last chunk presented on the shard
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{}, nil
|
2021-04-06 14:07:42 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{}, err
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 15:41:02 +00:00
|
|
|
addrKey = addressKey(addr, key)
|
2024-01-10 08:56:24 +00:00
|
|
|
// remove record from the garbage bucket
|
|
|
|
if garbageBKT != nil {
|
|
|
|
err := garbageBKT.Delete(addrKey)
|
|
|
|
if err != nil {
|
|
|
|
return deleteSingleResult{}, fmt.Errorf("could not remove from garbage bucket: %w", 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
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
isUserObject := IsUserObject(obj)
|
|
|
|
|
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 {
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{}, fmt.Errorf("could not remove object: %w", err)
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 13:14:36 +00:00
|
|
|
if err := deleteECRelatedInfo(tx, garbageBKT, obj, addr.Container(), refCounter); err != nil {
|
|
|
|
return deleteSingleResult{}, err
|
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{
|
2023-12-25 07:15:26 +00:00
|
|
|
Phy: true,
|
|
|
|
Logic: removeAvailableObject,
|
|
|
|
User: isUserObject && removeAvailableObject,
|
|
|
|
Size: obj.PayloadSize(),
|
2023-12-05 14:00:16 +00:00
|
|
|
}, 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 {
|
2024-03-11 14:55:50 +00:00
|
|
|
return errFailedToRemoveUniqueIndexes
|
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
|
|
|
}
|
|
|
|
|
2024-01-10 08:56:24 +00:00
|
|
|
if isParent {
|
|
|
|
// remove record from the garbage bucket, because regular object deletion does nothing for virtual object
|
|
|
|
garbageBKT := tx.Bucket(garbageBucketName)
|
|
|
|
if garbageBKT != nil {
|
|
|
|
key := make([]byte, addressKeySize)
|
|
|
|
addrKey := addressKey(object.AddressOf(obj), key)
|
|
|
|
err := garbageBKT.Delete(addrKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not remove from garbage bucket: %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 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))
|
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)
|
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,
|
|
|
|
})
|
|
|
|
|
2024-08-20 08:59:42 +00:00
|
|
|
if expEpoch, ok := hasExpirationEpoch(obj); ok {
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{
|
|
|
|
name: expEpochToObjectBucketName,
|
|
|
|
key: expirationEpochKey(expEpoch, cnr, addr.Object()),
|
|
|
|
})
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{
|
|
|
|
name: objectToExpirationEpochBucketName(cnr, make([]byte, bucketKeySize)),
|
|
|
|
key: objKey,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|
2024-07-18 13:14:36 +00:00
|
|
|
|
|
|
|
func deleteECRelatedInfo(tx *bbolt.Tx, garbageBKT *bbolt.Bucket, obj *objectSDK.Object, cnr cid.ID, refCounter referenceCounter) error {
|
|
|
|
ech := obj.ECHeader()
|
|
|
|
if ech == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hasAnyChunks := hasAnyECChunks(tx, ech, cnr)
|
|
|
|
// drop EC parent GC mark if current EC chunk is the last one
|
|
|
|
if !hasAnyChunks && garbageBKT != nil {
|
|
|
|
var ecParentAddress oid.Address
|
|
|
|
ecParentAddress.SetContainer(cnr)
|
|
|
|
ecParentAddress.SetObject(ech.Parent())
|
|
|
|
addrKey := addressKey(ecParentAddress, make([]byte, addressKeySize))
|
|
|
|
err := garbageBKT.Delete(addrKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not remove EC parent from garbage bucket: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// also drop EC parent root info if current EC chunk is the last one
|
|
|
|
if !hasAnyChunks {
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{
|
|
|
|
name: rootBucketName(cnr, make([]byte, bucketKeySize)),
|
|
|
|
key: objectKey(ech.Parent(), make([]byte, objectKeySize)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if ech.ParentSplitParentID() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var splitParentAddress oid.Address
|
|
|
|
splitParentAddress.SetContainer(cnr)
|
|
|
|
splitParentAddress.SetObject(*ech.ParentSplitParentID())
|
|
|
|
|
|
|
|
if ref, ok := refCounter[string(addressKey(splitParentAddress, make([]byte, addressKeySize)))]; ok {
|
|
|
|
// linking object is already processing
|
|
|
|
// so just inform that one more reference was deleted
|
|
|
|
// split info and gc marks will be deleted after linking object delete
|
|
|
|
ref.cur++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentLength(tx, splitParentAddress) > 0 {
|
|
|
|
// linking object still exists, so leave split info and gc mark deletion for linking object processing
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop split parent gc mark
|
|
|
|
if garbageBKT != nil {
|
|
|
|
addrKey := addressKey(splitParentAddress, make([]byte, addressKeySize))
|
|
|
|
err := garbageBKT.Delete(addrKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not remove EC parent from garbage bucket: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop split info
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{
|
|
|
|
name: rootBucketName(cnr, make([]byte, bucketKeySize)),
|
|
|
|
key: objectKey(*ech.ParentSplitParentID(), make([]byte, objectKeySize)),
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasAnyECChunks(tx *bbolt.Tx, ech *objectSDK.ECHeader, cnr cid.ID) bool {
|
|
|
|
data := getFromBucket(tx, ecInfoBucketName(cnr, make([]byte, bucketKeySize)),
|
|
|
|
objectKey(ech.Parent(), make([]byte, objectKeySize)))
|
|
|
|
return len(data) > 0
|
|
|
|
}
|