2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2024-08-19 15:28:53 +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-10-28 14:49:30 +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-10-28 14:49:30 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-12-08 11:23:23 +00:00
|
|
|
raw bool
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetRes groups the resulting values of Get operation.
|
2020-12-08 09:56:14 +00:00
|
|
|
type GetRes struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
hdr *objectSDK.Object
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetAddress is a Get option to set the address of the requested object.
|
2020-12-08 09:56:14 +00:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (p *GetPrm) SetAddress(addr oid.Address) {
|
|
|
|
p.addr = addr
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetRaw is a Get option to set raw flag value. If flag is unset, then Get
|
2020-12-08 11:23:23 +00:00
|
|
|
// returns header of virtual object, otherwise it returns SplitInfo of virtual
|
|
|
|
// object.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (p *GetPrm) SetRaw(raw bool) {
|
|
|
|
p.raw = raw
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// Header returns the requested object header.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (r GetRes) Header() *objectSDK.Object {
|
2020-12-08 09:56:14 +00:00
|
|
|
return r.hdr
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
// Get returns object header for specified address.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-03-17 13:26:17 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if object is missing in DB.
|
|
|
|
// 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) Get(ctx context.Context, prm GetPrm) (res GetRes, err error) {
|
2023-06-06 09:27:19 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("Get", time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.Get",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
|
|
attribute.Bool("raw", prm.raw),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-11-15 12:43:38 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return res, ErrDegradedMode
|
|
|
|
}
|
2022-07-21 13:26:25 +00:00
|
|
|
|
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-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, addressKeySize)
|
2023-04-20 16:17:08 +00:00
|
|
|
res.hdr, err = db.get(tx, prm.addr, key, true, prm.raw, 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-09-08 11:54:21 +00:00
|
|
|
func (db *DB) get(tx *bbolt.Tx, addr oid.Address, key []byte, checkStatus, raw bool, currEpoch uint64) (*objectSDK.Object, error) {
|
2022-07-27 18:38:28 +00:00
|
|
|
if checkStatus {
|
2024-08-20 08:59:42 +00:00
|
|
|
st, err := objectStatus(tx, addr, currEpoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch st {
|
2021-09-24 15:18:04 +00:00
|
|
|
case 1:
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2021-09-24 15:18:04 +00:00
|
|
|
case 2:
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, logicerr.Wrap(new(apistatus.ObjectAlreadyRemoved))
|
2022-07-27 18:38:28 +00:00
|
|
|
case 3:
|
2022-10-31 07:08:30 +00:00
|
|
|
return nil, ErrObjectIsExpired
|
2021-09-24 15:18:04 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
key = objectKey(addr.Object(), key)
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := addr.Container()
|
2022-05-12 16:37:46 +00:00
|
|
|
obj := objectSDK.New()
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// check in primary index
|
2022-09-08 11:54:21 +00:00
|
|
|
data := getFromBucket(tx, primaryBucketName(cnr, bucketName), key)
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(data) != 0 {
|
2024-08-19 15:28:53 +00:00
|
|
|
return obj, obj.Unmarshal(bytes.Clone(data))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2024-04-22 06:43:42 +00:00
|
|
|
data = getFromBucket(tx, ecInfoBucketName(cnr, bucketName), key)
|
|
|
|
if len(data) != 0 {
|
|
|
|
return nil, getECInfoError(tx, cnr, data)
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// if not found then check in tombstone index
|
2022-09-08 11:54:21 +00:00
|
|
|
data = getFromBucket(tx, tombstoneBucketName(cnr, bucketName), key)
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(data) != 0 {
|
2024-08-19 15:28:53 +00:00
|
|
|
return obj, obj.Unmarshal(bytes.Clone(data))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-02-15 12:51:56 +00:00
|
|
|
// if not found then check in locker index
|
2022-09-08 11:54:21 +00:00
|
|
|
data = getFromBucket(tx, bucketNameLockers(cnr, bucketName), key)
|
2022-02-15 12:51:56 +00:00
|
|
|
if len(data) != 0 {
|
2024-08-19 15:28:53 +00:00
|
|
|
return obj, obj.Unmarshal(bytes.Clone(data))
|
2022-02-15 12:51:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// if not found then check if object is a virtual
|
2022-05-31 17:00:41 +00:00
|
|
|
return getVirtualObject(tx, cnr, key, raw)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-11-16 09:43:52 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func getFromBucket(tx *bbolt.Tx, name, key []byte) []byte {
|
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return bkt.Get(key)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func getVirtualObject(tx *bbolt.Tx, cnr cid.ID, key []byte, raw bool) (*objectSDK.Object, error) {
|
2020-12-08 11:23:23 +00:00
|
|
|
if raw {
|
2022-05-31 17:00:41 +00:00
|
|
|
return nil, getSplitInfoError(tx, cnr, key)
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
|
|
|
parentBucket := tx.Bucket(parentBucketName(cnr, bucketName))
|
2020-12-08 07:51:34 +00:00
|
|
|
if parentBucket == nil {
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
relativeLst, err := decodeList(parentBucket.Get(key))
|
|
|
|
if err != nil {
|
2020-10-28 14:49:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(relativeLst) == 0 { // this should never happen though
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 06:32:40 +00:00
|
|
|
var data []byte
|
|
|
|
for i := 0; i < len(relativeLst) && len(data) == 0; i++ {
|
|
|
|
virtualOID := relativeLst[len(relativeLst)-i-1]
|
|
|
|
data = getFromBucket(tx, primaryBucketName(cnr, bucketName), virtualOID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
// check if any of the relatives is an EC object
|
|
|
|
for _, relative := range relativeLst {
|
|
|
|
data = getFromBucket(tx, ecInfoBucketName(cnr, bucketName), relative)
|
|
|
|
if len(data) > 0 {
|
|
|
|
// we can't return object headers, but can return error,
|
|
|
|
// so assembler can try to assemble complex object
|
|
|
|
return nil, getSplitInfoError(tx, cnr, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
child := objectSDK.New()
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2024-11-13 07:07:32 +00:00
|
|
|
err = child.Unmarshal(bytes.Clone(data))
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2024-12-18 06:38:14 +00:00
|
|
|
return nil, fmt.Errorf("unmarshal child with parent: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
par := child.Parent()
|
|
|
|
|
|
|
|
if par == nil { // this should never happen though
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return par, nil
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
2020-12-08 11:23:23 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func getSplitInfoError(tx *bbolt.Tx, cnr cid.ID, key []byte) error {
|
|
|
|
splitInfo, err := getSplitInfo(tx, cnr, key)
|
2020-12-08 11:23:23 +00:00
|
|
|
if err == nil {
|
2022-10-26 12:23:12 +00:00
|
|
|
return logicerr.Wrap(objectSDK.NewSplitInfoError(splitInfo))
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 11:14:07 +00:00
|
|
|
return logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|
2024-04-22 06:43:42 +00:00
|
|
|
|
|
|
|
func getECInfoError(tx *bbolt.Tx, cnr cid.ID, data []byte) error {
|
2024-06-04 10:30:30 +00:00
|
|
|
keys, err := decodeList(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-22 06:43:42 +00:00
|
|
|
ecInfo := objectSDK.NewECInfo()
|
2024-06-04 10:30:30 +00:00
|
|
|
for _, key := range keys {
|
2024-04-22 06:43:42 +00:00
|
|
|
// check in primary index
|
2024-11-13 07:02:38 +00:00
|
|
|
objData := getFromBucket(tx, primaryBucketName(cnr, make([]byte, bucketKeySize)), key)
|
|
|
|
if len(objData) != 0 {
|
2024-04-22 06:43:42 +00:00
|
|
|
obj := objectSDK.New()
|
2024-11-13 07:07:32 +00:00
|
|
|
if err := obj.Unmarshal(bytes.Clone(objData)); err != nil {
|
2024-04-22 06:43:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
chunk := objectSDK.ECChunk{}
|
|
|
|
id, _ := obj.ID()
|
|
|
|
chunk.SetID(id)
|
|
|
|
chunk.Index = obj.ECHeader().Index()
|
|
|
|
chunk.Total = obj.ECHeader().Total()
|
|
|
|
ecInfo.AddChunk(chunk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return logicerr.Wrap(objectSDK.NewECInfoError(ecInfo))
|
|
|
|
}
|