2020-11-17 17:39:43 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2024-11-13 07:07:32 +00:00
|
|
|
"bytes"
|
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-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 {
|
2024-12-10 09:52:26 +00:00
|
|
|
addr oid.Address
|
|
|
|
ecParentAddr 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
|
2024-05-27 19:07:43 +00:00
|
|
|
locked bool
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-12-10 09:52:26 +00:00
|
|
|
// SetECParent is an Exists option to set objects parent.
|
|
|
|
func (p *ExistsPrm) SetECParent(addr oid.Address) {
|
|
|
|
p.ecParentAddr = addr
|
2024-05-27 19:07:43 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-27 19:07:43 +00:00
|
|
|
// Locked returns the fact that the object is locked.
|
|
|
|
func (p ExistsRes) Locked() bool {
|
|
|
|
return p.locked
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-12-10 09:52:26 +00:00
|
|
|
res.exists, res.locked, err = db.exists(tx, prm.addr, prm.ecParentAddr, 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
|
|
|
}
|
|
|
|
|
2024-12-10 09:52:26 +00:00
|
|
|
func (db *DB) exists(tx *bbolt.Tx, addr oid.Address, ecParent oid.Address, currEpoch uint64) (bool, bool, error) {
|
2024-05-27 19:07:43 +00:00
|
|
|
var locked bool
|
2024-12-10 09:52:26 +00:00
|
|
|
if !ecParent.Equals(oid.Address{}) {
|
2024-12-10 09:53:54 +00:00
|
|
|
st, err := objectStatus(tx, ecParent, currEpoch)
|
|
|
|
if err != nil {
|
|
|
|
return false, false, err
|
|
|
|
}
|
|
|
|
switch st {
|
|
|
|
case 2:
|
|
|
|
return false, locked, logicerr.Wrap(new(apistatus.ObjectAlreadyRemoved))
|
|
|
|
case 3:
|
|
|
|
return false, locked, ErrObjectIsExpired
|
|
|
|
}
|
|
|
|
|
2024-12-10 09:52:26 +00:00
|
|
|
locked = objectLocked(tx, ecParent.Container(), ecParent.Object())
|
2024-05-27 19:07:43 +00:00
|
|
|
}
|
2022-07-27 18:38:28 +00:00
|
|
|
// check graveyard and object expiration first
|
2024-08-20 08:59:42 +00:00
|
|
|
st, err := objectStatus(tx, addr, currEpoch)
|
|
|
|
if err != nil {
|
|
|
|
return false, false, err
|
|
|
|
}
|
|
|
|
switch st {
|
2021-09-24 15:18:04 +00:00
|
|
|
case 1:
|
2024-05-27 19:07:43 +00:00
|
|
|
return false, locked, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2021-09-24 15:18:04 +00:00
|
|
|
case 2:
|
2024-05-27 19:07:43 +00:00
|
|
|
return false, locked, logicerr.Wrap(new(apistatus.ObjectAlreadyRemoved))
|
2022-07-27 18:38:28 +00:00
|
|
|
case 3:
|
2024-05-27 19:07:43 +00:00
|
|
|
return false, locked, 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) {
|
2024-05-27 19:07:43 +00:00
|
|
|
return true, locked, nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2024-05-27 19:07:43 +00:00
|
|
|
return false, locked, err
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2024-05-27 19:07:43 +00:00
|
|
|
return false, locked, logicerr.Wrap(objectSDK.NewSplitInfoError(splitInfo))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2024-04-22 06:43:42 +00:00
|
|
|
// if parent bucket is empty, then check if object exists in ec bucket
|
|
|
|
if data := getFromBucket(tx, ecInfoBucketName(cnr, key), objKey); len(data) != 0 {
|
2024-05-27 19:07:43 +00:00
|
|
|
return false, locked, getECInfoError(tx, cnr, data)
|
2024-04-22 06:43:42 +00:00
|
|
|
}
|
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
|
2024-05-27 19:07:43 +00:00
|
|
|
return firstIrregularObjectType(tx, cnr, objKey) != objectSDK.TypeRegular, locked, 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.
|
2024-08-20 08:59:42 +00:00
|
|
|
func objectStatus(tx *bbolt.Tx, addr oid.Address, currEpoch uint64) (uint8, error) {
|
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()) {
|
2024-08-20 08:59:42 +00:00
|
|
|
return 0, nil
|
2023-01-30 10:23:44 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 08:59:42 +00:00
|
|
|
expired, err := isExpired(tx, addr, currEpoch)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2023-03-06 13:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if expired {
|
2024-08-20 08:59:42 +00:00
|
|
|
return 3, nil
|
2023-03-06 13:11:42 +00:00
|
|
|
}
|
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))
|
2024-08-20 08:59:42 +00:00
|
|
|
return inGraveyardWithKey(addrKey, graveyardBkt, garbageBkt), nil
|
2023-03-06 13:11:42 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2024-11-13 07:07:32 +00:00
|
|
|
err := splitInfo.Unmarshal(bytes.Clone(rawSplitInfo))
|
2020-12-08 11:23:23 +00:00
|
|
|
if err != nil {
|
2024-12-18 06:38:14 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal split info from root index: %w", err)
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return splitInfo, nil
|
|
|
|
}
|