2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2023-12-12 11:18:42 +00:00
|
|
|
"fmt"
|
2022-10-28 10:55:21 +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/blobstor/common"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
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-12-12 11:18:42 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-11-17 17:39:43 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-17 12:23:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeletePrm groups the parameters of Delete operation.
|
|
|
|
type DeletePrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr []oid.Address
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// DeleteRes groups the resulting values of Delete operation.
|
2023-05-29 14:32:13 +00:00
|
|
|
type DeleteRes struct {
|
|
|
|
deleted uint64
|
|
|
|
}
|
2020-11-17 12:23:15 +00:00
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
// SetAddresses is a Delete option to set the addresses of the objects to delete.
|
2020-11-17 12:23:15 +00:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-07-13 12:43:04 +00:00
|
|
|
func (p *DeletePrm) SetAddresses(addr ...oid.Address) {
|
|
|
|
p.addr = append(p.addr, addr...)
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 13:28:04 +00:00
|
|
|
// Delete removes data from the shard's metaBase and// blobStor.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) Delete(ctx context.Context, prm DeletePrm) (DeleteRes, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.Delete",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.Int("addr_count", len(prm.addr)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-12-07 17:42:35 +00:00
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
2023-10-13 13:28:04 +00:00
|
|
|
return s.delete(ctx, prm, false)
|
2022-12-07 17:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 13:28:04 +00:00
|
|
|
func (s *Shard) delete(ctx context.Context, prm DeletePrm, skipFailed bool) (DeleteRes, error) {
|
2022-12-07 17:42:35 +00:00
|
|
|
if s.info.Mode.ReadOnly() {
|
2022-05-31 11:50:39 +00:00
|
|
|
return DeleteRes{}, ErrReadOnlyMode
|
2022-12-07 17:42:35 +00:00
|
|
|
} else if s.info.Mode.NoMetabase() {
|
2022-06-29 11:27:36 +00:00
|
|
|
return DeleteRes{}, ErrDegradedMode
|
2021-12-27 11:04:07 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 14:32:13 +00:00
|
|
|
result := DeleteRes{}
|
2023-05-23 12:02:38 +00:00
|
|
|
for _, addr := range prm.addr {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2023-05-29 14:32:13 +00:00
|
|
|
return result, ctx.Err()
|
2023-05-23 12:02:38 +00:00
|
|
|
default:
|
2020-12-01 07:55:08 +00:00
|
|
|
}
|
|
|
|
|
2023-12-12 11:18:42 +00:00
|
|
|
if err := s.validateWritecacheDoesntContainObject(ctx, addr); err != nil {
|
|
|
|
if skipFailed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2023-10-13 13:28:04 +00:00
|
|
|
if err := s.deleteFromBlobstor(ctx, addr); err != nil {
|
|
|
|
if skipFailed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
2020-12-01 07:55:08 +00:00
|
|
|
|
2023-05-23 12:02:38 +00:00
|
|
|
if err := s.deleteFromMetabase(ctx, addr); err != nil {
|
2023-10-13 13:28:04 +00:00
|
|
|
if skipFailed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return result, err
|
2020-12-01 07:55:08 +00:00
|
|
|
}
|
2023-05-29 14:32:13 +00:00
|
|
|
result.deleted++
|
2023-05-23 12:02:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 14:32:13 +00:00
|
|
|
return result, nil
|
2023-05-23 12:02:38 +00:00
|
|
|
}
|
2020-12-01 07:55:08 +00:00
|
|
|
|
2023-12-12 11:18:42 +00:00
|
|
|
func (s *Shard) validateWritecacheDoesntContainObject(ctx context.Context, addr oid.Address) error {
|
|
|
|
if !s.hasWriteCache() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err := s.writeCache.Head(ctx, addr)
|
|
|
|
if err == nil {
|
|
|
|
s.log.Warn(logs.ObjectRemovalFailureExistsInWritecache, zap.Stringer("object_address", addr))
|
|
|
|
return fmt.Errorf("object %s must be flushed from writecache", addr)
|
|
|
|
}
|
|
|
|
if client.IsErrObjectNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-13 13:28:04 +00:00
|
|
|
func (s *Shard) deleteFromBlobstor(ctx context.Context, addr oid.Address) error {
|
2023-05-23 12:02:38 +00:00
|
|
|
var sPrm meta.StorageIDPrm
|
|
|
|
sPrm.SetAddress(addr)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
2023-05-23 12:02:38 +00:00
|
|
|
res, err := s.metaBase.StorageID(ctx, sPrm)
|
|
|
|
if err != nil {
|
2023-08-09 12:53:13 +00:00
|
|
|
s.log.Debug(logs.StorageIDRetrievalFailure,
|
2023-05-23 12:02:38 +00:00
|
|
|
zap.Stringer("object", addr),
|
2023-09-27 08:02:06 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)))
|
2023-10-13 13:28:04 +00:00
|
|
|
return err
|
2023-05-23 12:02:38 +00:00
|
|
|
}
|
|
|
|
storageID := res.StorageID()
|
|
|
|
|
|
|
|
var delPrm common.DeletePrm
|
|
|
|
delPrm.Address = addr
|
|
|
|
delPrm.StorageID = storageID
|
|
|
|
|
|
|
|
_, err = s.blobStor.Delete(ctx, delPrm)
|
2023-12-12 11:18:42 +00:00
|
|
|
if err != nil && !client.IsErrObjectNotFound(err) {
|
2023-08-09 12:53:13 +00:00
|
|
|
s.log.Debug(logs.ObjectRemovalFailureBlobStor,
|
2023-05-23 12:02:38 +00:00
|
|
|
zap.Stringer("object_address", addr),
|
2023-09-27 08:02:06 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)))
|
2023-12-12 11:18:42 +00:00
|
|
|
return err
|
2020-12-01 07:55:08 +00:00
|
|
|
}
|
2023-12-12 11:18:42 +00:00
|
|
|
return nil
|
2023-05-23 12:02:38 +00:00
|
|
|
}
|
2020-12-01 07:55:08 +00:00
|
|
|
|
2023-05-23 12:02:38 +00:00
|
|
|
func (s *Shard) deleteFromMetabase(ctx context.Context, addr oid.Address) error {
|
|
|
|
var delPrm meta.DeletePrm
|
|
|
|
delPrm.SetAddresses(addr)
|
2023-01-25 14:01:25 +00:00
|
|
|
|
2023-05-23 12:02:38 +00:00
|
|
|
res, err := s.metaBase.Delete(ctx, delPrm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-09 11:36:34 +00:00
|
|
|
s.decObjectCounterBy(physical, res.RawObjectsRemoved())
|
|
|
|
s.decObjectCounterBy(logical, res.AvailableObjectsRemoved())
|
2023-12-05 14:00:16 +00:00
|
|
|
s.decObjectCounterBy(user, res.UserObjectsRemoved())
|
2023-11-02 10:50:52 +00:00
|
|
|
s.decContainerObjectCounter(res.RemovedByCnrID())
|
2023-05-23 12:02:38 +00:00
|
|
|
removedPayload := res.RemovedPhysicalObjectSizes()[0]
|
|
|
|
logicalRemovedPayload := res.RemovedLogicalObjectSizes()[0]
|
|
|
|
if logicalRemovedPayload > 0 {
|
|
|
|
s.addToContainerSize(addr.Container().EncodeToString(), -int64(logicalRemovedPayload))
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
2023-05-23 12:02:38 +00:00
|
|
|
s.addToPayloadSize(-int64(removedPayload))
|
2020-11-17 12:23:15 +00:00
|
|
|
|
2023-05-23 12:02:38 +00:00
|
|
|
return nil
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|