2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2022-10-28 10:55:21 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/pkg/tracing"
|
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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-01 07:55:08 +00:00
|
|
|
// Delete removes data from the shard's writeCache, 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-04-12 14:01:29 +00:00
|
|
|
return s.delete(ctx, prm)
|
2022-12-07 17:42:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) delete(ctx context.Context, prm DeletePrm) (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-05-23 12:02:38 +00:00
|
|
|
s.deleteObjectFromWriteCacheSafe(ctx, addr)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
2023-05-23 12:02:38 +00:00
|
|
|
s.deleteFromBlobstorSafe(ctx, addr)
|
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-05-29 14:32:13 +00:00
|
|
|
return result, err // stop on metabase error ?
|
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-05-23 12:02:38 +00:00
|
|
|
func (s *Shard) deleteObjectFromWriteCacheSafe(ctx context.Context, addr oid.Address) {
|
|
|
|
if s.hasWriteCache() {
|
|
|
|
err := s.writeCache.Delete(ctx, addr)
|
|
|
|
if err != nil && !IsErrNotFound(err) && !errors.Is(err, writecache.ErrReadOnly) {
|
|
|
|
s.log.Warn(logs.ShardCantDeleteObjectFromWriteCache, zap.Error(err))
|
2020-12-01 07:55:08 +00:00
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
2023-05-23 12:02:38 +00:00
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2023-05-23 12:02:38 +00:00
|
|
|
func (s *Shard) deleteFromBlobstorSafe(ctx context.Context, addr oid.Address) {
|
|
|
|
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 {
|
|
|
|
s.log.Debug("can't get storage ID from metabase",
|
|
|
|
zap.Stringer("object", addr),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
storageID := res.StorageID()
|
|
|
|
|
|
|
|
var delPrm common.DeletePrm
|
|
|
|
delPrm.Address = addr
|
|
|
|
delPrm.StorageID = storageID
|
|
|
|
|
|
|
|
_, err = s.blobStor.Delete(ctx, delPrm)
|
2020-12-01 07:55:08 +00:00
|
|
|
if err != nil {
|
2023-05-23 12:02:38 +00:00
|
|
|
s.log.Debug("can't remove object from blobStor",
|
|
|
|
zap.Stringer("object_address", addr),
|
|
|
|
zap.String("error", err.Error()))
|
2020-12-01 07:55:08 +00:00
|
|
|
}
|
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-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
|
|
|
}
|