2022-07-18 06:16:36 +00:00
|
|
|
package blobovniczatree
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
2023-09-27 13:25:15 +00:00
|
|
|
"errors"
|
2023-06-05 07:25:25 +00:00
|
|
|
"time"
|
2022-07-18 06:16:36 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobovnicza"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-09-27 08:02:06 +00:00
|
|
|
tracingPkg "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/tracing"
|
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-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-07-18 06:16:36 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-09-27 13:25:15 +00:00
|
|
|
var errObjectIsDeleteProtected = errors.New("object is delete protected")
|
|
|
|
|
2022-07-18 06:16:36 +00:00
|
|
|
// Delete deletes object from blobovnicza tree.
|
|
|
|
//
|
|
|
|
// If blobocvnicza ID is specified, only this blobovnicza is processed.
|
|
|
|
// Otherwise, all Blobovniczas are processed descending weight.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (b *Blobovniczas) Delete(ctx context.Context, prm common.DeletePrm) (res common.DeleteRes, err error) {
|
2023-06-05 07:25:25 +00:00
|
|
|
var (
|
|
|
|
success = false
|
|
|
|
startedAt = time.Now()
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
b.metrics.Delete(time.Since(startedAt), success, prm.StorageID != nil)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Blobovniczas.Delete",
|
|
|
|
trace.WithAttributes(
|
2023-06-05 07:25:25 +00:00
|
|
|
attribute.String("path", b.rootPath),
|
2023-04-12 14:01:29 +00:00
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
attribute.String("storage_id", hex.EncodeToString(prm.StorageID)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-23 13:04:01 +00:00
|
|
|
if b.readOnly {
|
|
|
|
return common.DeleteRes{}, common.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:07:32 +00:00
|
|
|
if b.rebuildGuard.TryRLock() {
|
|
|
|
defer b.rebuildGuard.RUnlock()
|
|
|
|
} else {
|
|
|
|
return common.DeleteRes{}, errRebuildInProgress
|
|
|
|
}
|
|
|
|
|
2023-09-27 13:25:15 +00:00
|
|
|
if b.deleteProtectedObjects.Contains(prm.Address) {
|
|
|
|
return common.DeleteRes{}, errObjectIsDeleteProtected
|
|
|
|
}
|
|
|
|
|
2022-07-18 06:16:36 +00:00
|
|
|
var bPrm blobovnicza.DeletePrm
|
|
|
|
bPrm.SetAddress(prm.Address)
|
|
|
|
|
|
|
|
if prm.StorageID != nil {
|
2023-09-19 15:08:38 +00:00
|
|
|
id := NewIDFromBytes(prm.StorageID)
|
2024-10-21 08:20:17 +00:00
|
|
|
shBlz := b.getBlobovnicza(ctx, id.Path())
|
|
|
|
blz, err := shBlz.Open(ctx)
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-10-21 13:27:28 +00:00
|
|
|
defer shBlz.Close(ctx)
|
2022-07-18 06:16:36 +00:00
|
|
|
|
2023-06-05 07:25:25 +00:00
|
|
|
if res, err = b.deleteObject(ctx, blz, bPrm); err == nil {
|
|
|
|
success = true
|
|
|
|
}
|
|
|
|
return res, err
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objectFound := false
|
|
|
|
|
2023-09-20 14:46:10 +00:00
|
|
|
err = b.iterateSortedDBPaths(ctx, prm.Address, func(p string) (bool, error) {
|
2023-08-30 20:36:48 +00:00
|
|
|
res, err = b.deleteObjectFromLevel(ctx, bPrm, p)
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
2023-08-04 11:14:07 +00:00
|
|
|
if !client.IsErrObjectNotFound(err) {
|
2024-10-21 07:22:54 +00:00
|
|
|
b.log.Debug(ctx, logs.BlobovniczatreeCouldNotRemoveObjectFromLevel,
|
2022-07-18 06:16:36 +00:00
|
|
|
zap.String("level", p),
|
|
|
|
zap.String("error", err.Error()),
|
2023-09-27 08:02:06 +00:00
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)),
|
2022-07-18 06:16:36 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
objectFound = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// abort iterator if found, otherwise process all Blobovniczas
|
|
|
|
return err == nil, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil && !objectFound {
|
|
|
|
// not found in any blobovnicza
|
2023-08-04 11:14:07 +00:00
|
|
|
return common.DeleteRes{}, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
2023-06-05 07:25:25 +00:00
|
|
|
success = err == nil
|
2022-07-18 06:16:36 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// tries to delete object from particular blobovnicza.
|
|
|
|
//
|
|
|
|
// returns no error if object was removed from some blobovnicza of the same level.
|
2023-08-30 20:36:48 +00:00
|
|
|
func (b *Blobovniczas) deleteObjectFromLevel(ctx context.Context, prm blobovnicza.DeletePrm, blzPath string) (common.DeleteRes, error) {
|
2024-10-21 08:20:17 +00:00
|
|
|
shBlz := b.getBlobovnicza(ctx, blzPath)
|
|
|
|
blz, err := shBlz.Open(ctx)
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.DeleteRes{}, err
|
|
|
|
}
|
2024-10-21 13:27:28 +00:00
|
|
|
defer shBlz.Close(ctx)
|
2022-07-18 06:16:36 +00:00
|
|
|
|
2023-04-26 08:24:40 +00:00
|
|
|
return b.deleteObject(ctx, blz, prm)
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removes object from blobovnicza and returns common.DeleteRes.
|
2023-04-26 08:24:40 +00:00
|
|
|
func (b *Blobovniczas) deleteObject(ctx context.Context, blz *blobovnicza.Blobovnicza, prm blobovnicza.DeletePrm) (common.DeleteRes, error) {
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := blz.Delete(ctx, prm)
|
2022-07-18 06:16:36 +00:00
|
|
|
return common.DeleteRes{}, err
|
|
|
|
}
|