forked from TrueCloudLab/frostfs-node
96 lines
2.5 KiB
Go
96 lines
2.5 KiB
Go
package writecache
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
storagelog "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
"go.etcd.io/bbolt"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// Delete removes object from write-cache.
|
|
//
|
|
// Returns an error of type apistatus.ObjectNotFound if object is missing in write-cache.
|
|
// Returns ErrNotInitialized if write-cache has not been initialized yet.
|
|
// Returns ErrDegraded if write-cache is in DEGRADED mode.
|
|
func (c *cache) Delete(ctx context.Context, addr oid.Address) error {
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "writecache.Delete",
|
|
trace.WithAttributes(
|
|
attribute.String("address", addr.EncodeToString()),
|
|
))
|
|
defer span.End()
|
|
|
|
deleted := false
|
|
storageType := StorageTypeUndefined
|
|
startedAt := time.Now()
|
|
defer func() {
|
|
c.metrics.Delete(time.Since(startedAt), deleted, storageType)
|
|
}()
|
|
|
|
if !c.modeMtx.TryRLock() {
|
|
return ErrNotInitialized
|
|
}
|
|
defer c.modeMtx.RUnlock()
|
|
if c.readOnly() {
|
|
return ErrReadOnly
|
|
}
|
|
if c.noMetabase() {
|
|
return ErrDegraded
|
|
}
|
|
|
|
saddr := addr.EncodeToString()
|
|
|
|
var dataSize int
|
|
_ = c.db.View(func(tx *bbolt.Tx) error {
|
|
b := tx.Bucket(defaultBucket)
|
|
dataSize = len(b.Get([]byte(saddr)))
|
|
return nil
|
|
})
|
|
|
|
if dataSize > 0 {
|
|
storageType = StorageTypeDB
|
|
var recordDeleted bool
|
|
err := c.db.Update(func(tx *bbolt.Tx) error {
|
|
b := tx.Bucket(defaultBucket)
|
|
key := []byte(saddr)
|
|
recordDeleted = b.Get(key) != nil
|
|
err := b.Delete(key)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
storagelog.Write(c.log,
|
|
storagelog.AddressField(saddr),
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
storagelog.OpField("db DELETE"),
|
|
)
|
|
if recordDeleted {
|
|
c.objCounters.cDB.Add(math.MaxUint64)
|
|
c.estimateCacheSize()
|
|
}
|
|
deleted = true
|
|
return nil
|
|
}
|
|
|
|
storageType = StorageTypeFSTree
|
|
_, err := c.fsTree.Delete(ctx, common.DeletePrm{Address: addr})
|
|
if err == nil {
|
|
storagelog.Write(c.log,
|
|
storagelog.AddressField(saddr),
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
storagelog.OpField("fstree DELETE"),
|
|
)
|
|
deleted = true
|
|
// counter changed by fstree
|
|
c.estimateCacheSize()
|
|
}
|
|
return metaerr.Wrap(err)
|
|
}
|