2020-11-17 17:39:43 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
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
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-12-08 07:51:34 +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-11-17 17:39:43 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// ExistsPrm groups the parameters of Exists operation.
|
|
|
|
type ExistsPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ExistsRes groups the resulting values of Exists operation.
|
2020-12-08 09:56:14 +00:00
|
|
|
type ExistsRes struct {
|
|
|
|
exists bool
|
|
|
|
}
|
|
|
|
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrLackSplitInfo = logicerr.New("no split info on parent object")
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetAddress is an Exists option to set object checked for existence.
|
|
|
|
func (p *ExistsPrm) SetAddress(addr oid.Address) {
|
|
|
|
p.addr = addr
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exists returns the fact that the object is in the metabase.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (p ExistsRes) Exists() bool {
|
2020-12-08 09:56:14 +00:00
|
|
|
return p.exists
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// Exists returns ErrAlreadyRemoved if addr was marked as removed. Otherwise it
|
|
|
|
// returns true if addr is in primary index or false if it is not.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-03-17 13:26:17 +00:00
|
|
|
// Returns an error of type apistatus.ObjectAlreadyRemoved if object has been placed in graveyard.
|
2022-07-27 18:38:28 +00:00
|
|
|
// Returns the object.ErrObjectIsExpired if the object is presented but already expired.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (db *DB) Exists(ctx context.Context, prm ExistsPrm) (res ExistsRes, err error) {
|
2023-06-06 09:27:19 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("Exists", time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.Exists",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
|
|
))
|
|
|
|
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 res, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
2022-07-27 18:38:28 +00:00
|
|
|
res.exists, err = db.exists(tx, prm.addr, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
2023-06-06 09:27:19 +00:00
|
|
|
success = err == nil
|
2023-06-15 10:19:36 +00:00
|
|
|
return res, metaerr.Wrap(err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
func (db *DB) exists(tx *bbolt.Tx, addr oid.Address, currEpoch uint64) (exists bool, err error) {
|
|
|
|
// check graveyard and object expiration first
|
|
|
|
switch objectStatus(tx, addr, currEpoch) {
|
2021-09-24 15:18:04 +00:00
|
|
|
case 1:
|
2022-10-26 12:23:12 +00:00
|
|
|
return false, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2021-09-24 15:18:04 +00:00
|
|
|
case 2:
|
2022-10-26 12:23:12 +00:00
|
|
|
return false, logicerr.Wrap(apistatus.ObjectAlreadyRemoved{})
|
2022-07-27 18:38:28 +00:00
|
|
|
case 3:
|
2022-10-31 07:08:30 +00:00
|
|
|
return false, ErrObjectIsExpired
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
objKey := objectKey(addr.Object(), make([]byte, objectKeySize))
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := addr.Container()
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, bucketKeySize)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// if graveyard is empty, then check if object exists in primary bucket
|
2022-09-08 11:54:21 +00:00
|
|
|
if inBucket(tx, primaryBucketName(cnr, key), objKey) {
|
2020-12-08 07:51:34 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if primary bucket is empty, then check if object exists in parent bucket
|
2022-09-08 11:54:21 +00:00
|
|
|
if inBucket(tx, parentBucketName(cnr, key), objKey) {
|
2022-05-31 17:00:41 +00:00
|
|
|
splitInfo, err := getSplitInfo(tx, cnr, objKey)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2020-12-08 11:23:23 +00:00
|
|
|
return false, err
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
return false, logicerr.Wrap(objectSDK.NewSplitInfoError(splitInfo))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 12:51:56 +00:00
|
|
|
// if parent bucket is empty, then check if object exists in typed buckets
|
2022-05-12 16:37:46 +00:00
|
|
|
return firstIrregularObjectType(tx, cnr, objKey) != objectSDK.TypeRegular, nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
// objectStatus returns:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - 0 if object is available;
|
|
|
|
// - 1 if object with GC mark;
|
|
|
|
// - 2 if object is covered with tombstone;
|
|
|
|
// - 3 if object is expired.
|
2022-07-27 18:38:28 +00:00
|
|
|
func objectStatus(tx *bbolt.Tx, addr oid.Address, currEpoch uint64) uint8 {
|
2023-01-30 10:23:44 +00:00
|
|
|
// locked object could not be removed/marked with GC/expired
|
|
|
|
if objectLocked(tx, addr.Container(), addr.Object()) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
// we check only if the object is expired in the current
|
|
|
|
// epoch since it is considered the only corner case: the
|
|
|
|
// GC is expected to collect all the objects that have
|
|
|
|
// expired previously for less than the one epoch duration
|
|
|
|
|
2023-03-06 13:11:42 +00:00
|
|
|
expired := isExpiredWithAttribute(tx, objectV2.SysAttributeExpEpoch, addr, currEpoch)
|
|
|
|
if !expired {
|
|
|
|
expired = isExpiredWithAttribute(tx, objectV2.SysAttributeExpEpochNeoFS, addr, currEpoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expired {
|
|
|
|
return 3
|
|
|
|
}
|
2022-07-27 18:38:28 +00:00
|
|
|
|
2023-03-06 13:11:42 +00:00
|
|
|
graveyardBkt := tx.Bucket(graveyardBucketName)
|
|
|
|
garbageBkt := tx.Bucket(garbageBucketName)
|
|
|
|
addrKey := addressKey(addr, make([]byte, addressKeySize))
|
|
|
|
return inGraveyardWithKey(addrKey, graveyardBkt, garbageBkt)
|
|
|
|
}
|
|
|
|
|
2022-06-14 16:09:06 +00:00
|
|
|
func inGraveyardWithKey(addrKey []byte, graveyard, garbageBCK *bbolt.Bucket) uint8 {
|
2020-12-08 07:51:34 +00:00
|
|
|
if graveyard == nil {
|
2021-04-08 14:19:31 +00:00
|
|
|
// incorrect metabase state, does not make
|
|
|
|
// sense to check garbage bucket
|
2021-09-24 15:18:04 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:53:09 +00:00
|
|
|
val := graveyard.Get(addrKey)
|
2021-09-24 15:18:04 +00:00
|
|
|
if val == nil {
|
2021-04-08 14:19:31 +00:00
|
|
|
if garbageBCK == nil {
|
|
|
|
// incorrect node state
|
|
|
|
return 0
|
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2022-06-14 15:53:09 +00:00
|
|
|
val = garbageBCK.Get(addrKey)
|
2021-04-08 14:19:31 +00:00
|
|
|
if val != nil {
|
|
|
|
// object has been marked with GC
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// neither in the graveyard
|
|
|
|
// nor was marked with GC mark
|
|
|
|
return 0
|
2021-09-24 15:18:04 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
// object in the graveyard
|
2021-09-24 15:18:04 +00:00
|
|
|
return 2
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// inBucket checks if key <key> is present in bucket <name>.
|
|
|
|
func inBucket(tx *bbolt.Tx, name, key []byte) bool {
|
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// using `get` as `exists`: https://github.com/boltdb/bolt/issues/321
|
|
|
|
val := bkt.Get(key)
|
|
|
|
|
|
|
|
return len(val) != 0
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
2020-12-08 11:23:23 +00:00
|
|
|
|
|
|
|
// getSplitInfo returns SplitInfo structure from root index. Returns error
|
|
|
|
// if there is no `key` record in root index.
|
2022-05-31 17:00:41 +00:00
|
|
|
func getSplitInfo(tx *bbolt.Tx, cnr cid.ID, key []byte) (*objectSDK.SplitInfo, error) {
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := rootBucketName(cnr, make([]byte, bucketKeySize))
|
|
|
|
rawSplitInfo := getFromBucket(tx, bucketName, key)
|
2020-12-08 11:23:23 +00:00
|
|
|
if len(rawSplitInfo) == 0 {
|
|
|
|
return nil, ErrLackSplitInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
splitInfo := objectSDK.NewSplitInfo()
|
|
|
|
|
|
|
|
err := splitInfo.Unmarshal(rawSplitInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't unmarshal split info from root index: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return splitInfo, nil
|
|
|
|
}
|