2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2020-11-17 12:26:03 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util"
|
|
|
|
"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"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-03-13 11:37:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-02-22 07:20:33 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-17 12:26:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetRes groups the resulting values of Get operation.
|
2020-11-17 12:26:03 +00:00
|
|
|
type GetRes struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Get option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *GetPrm) WithAddress(addr oid.Address) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.addr = addr
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 10:41:12 +00:00
|
|
|
// Object returns the requested object.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (r GetRes) Object() *objectSDK.Object {
|
2020-11-17 12:26:03 +00:00
|
|
|
return r.obj
|
|
|
|
}
|
|
|
|
|
2020-12-01 10:41:12 +00:00
|
|
|
// Get reads an object from local storage.
|
2020-11-17 12:26:03 +00:00
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object part.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in local storage.
|
|
|
|
// Returns an error of type apistatus.ObjectAlreadyRemoved if the object has been marked as removed.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
2023-03-13 11:37:35 +00:00
|
|
|
func (e *StorageEngine) Get(ctx context.Context, prm GetPrm) (res GetRes, err error) {
|
2023-04-12 14:01:29 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.Get",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err = e.get(ctx, prm)
|
2021-11-09 15:46:12 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (e *StorageEngine) get(ctx context.Context, prm GetPrm) (GetRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
2023-06-13 16:48:15 +00:00
|
|
|
defer elapsed("Get", e.metrics.AddMethodDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
2020-12-01 10:41:12 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
var shPrm shard.GetPrm
|
|
|
|
shPrm.SetAddress(prm.addr)
|
2022-03-17 08:03:58 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
it := &getShardIterator{
|
|
|
|
OutError: errNotFound,
|
|
|
|
ShardPrm: shPrm,
|
|
|
|
Address: prm.addr,
|
|
|
|
Engine: e,
|
|
|
|
}
|
2022-02-22 07:20:33 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
it.tryGetWithMeta(ctx)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
if it.SplitInfo != nil {
|
|
|
|
return GetRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(it.SplitInfo))
|
|
|
|
}
|
2020-11-18 07:59:47 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
if it.ObjectExpired {
|
|
|
|
return GetRes{}, errNotFound
|
|
|
|
}
|
2022-07-05 04:55:46 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
if it.Object == nil {
|
|
|
|
if !it.HasDegraded && it.ShardWithMeta.Shard == nil || !shard.IsErrNotFound(it.OutError) {
|
|
|
|
return GetRes{}, it.OutError
|
|
|
|
}
|
2022-07-05 04:55:46 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
it.tryGetFromBlobstore(ctx)
|
2022-07-05 04:55:46 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
if it.Object == nil {
|
|
|
|
return GetRes{}, it.OutError
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
2023-03-30 14:32:33 +00:00
|
|
|
if it.ShardWithMeta.Shard != nil {
|
|
|
|
e.reportShardError(it.ShardWithMeta, "meta info was present, but object is missing",
|
|
|
|
it.MetaError, zap.Stringer("address", prm.addr))
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
return GetRes{
|
|
|
|
obj: it.Object,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-12-01 10:41:12 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
type getShardIterator struct {
|
|
|
|
Object *objectSDK.Object
|
|
|
|
SplitInfo *objectSDK.SplitInfo
|
|
|
|
OutError error
|
|
|
|
ShardWithMeta hashedShard
|
|
|
|
MetaError error
|
|
|
|
HasDegraded bool
|
|
|
|
ObjectExpired bool
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
ShardPrm shard.GetPrm
|
|
|
|
Address oid.Address
|
|
|
|
Engine *StorageEngine
|
2021-04-14 09:05:20 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
splitInfoErr *objectSDK.SplitInfoError
|
|
|
|
}
|
2023-01-25 15:48:13 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (i *getShardIterator) tryGetWithMeta(ctx context.Context) {
|
2023-03-30 14:32:33 +00:00
|
|
|
i.Engine.iterateOverSortedShards(i.Address, func(_ int, sh hashedShard) (stop bool) {
|
|
|
|
noMeta := sh.GetMode().NoMetabase()
|
|
|
|
i.ShardPrm.SetIgnoreMeta(noMeta)
|
|
|
|
|
|
|
|
i.HasDegraded = i.HasDegraded || noMeta
|
2022-02-22 07:20:33 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := sh.Get(ctx, i.ShardPrm)
|
2023-03-30 14:32:33 +00:00
|
|
|
if err == nil {
|
|
|
|
i.Object = res.Object()
|
|
|
|
return true
|
|
|
|
}
|
2022-02-22 07:20:33 +00:00
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
if res.HasMeta() {
|
|
|
|
i.ShardWithMeta = sh
|
|
|
|
i.MetaError = err
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case shard.IsErrNotFound(err):
|
|
|
|
return false // ignore, go to next shard
|
|
|
|
case errors.As(err, &i.splitInfoErr):
|
|
|
|
if i.SplitInfo == nil {
|
|
|
|
i.SplitInfo = objectSDK.NewSplitInfo()
|
2022-07-05 04:55:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 14:32:33 +00:00
|
|
|
util.MergeSplitInfo(i.splitInfoErr.SplitInfo(), i.SplitInfo)
|
|
|
|
|
|
|
|
_, withLink := i.SplitInfo.Link()
|
|
|
|
_, withLast := i.SplitInfo.LastPart()
|
|
|
|
|
|
|
|
// stop iterating over shards if SplitInfo structure is complete
|
|
|
|
return withLink && withLast
|
|
|
|
case shard.IsErrRemoved(err):
|
|
|
|
i.OutError = err
|
|
|
|
return true // stop, return it back
|
|
|
|
case shard.IsErrObjectExpired(err):
|
|
|
|
// object is found but should not be returned
|
|
|
|
i.ObjectExpired = true
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
i.Engine.reportShardError(sh, "could not get object from shard", err)
|
|
|
|
return false
|
2022-02-22 07:20:33 +00:00
|
|
|
}
|
2023-03-30 14:32:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (i *getShardIterator) tryGetFromBlobstore(ctx context.Context) {
|
2023-03-30 14:32:33 +00:00
|
|
|
// If the object is not found but is present in metabase,
|
|
|
|
// try to fetch it from blobstor directly. If it is found in any
|
|
|
|
// blobstor, increase the error counter for the shard which contains the meta.
|
|
|
|
i.ShardPrm.SetIgnoreMeta(true)
|
|
|
|
|
|
|
|
i.Engine.iterateOverSortedShards(i.Address, func(_ int, sh hashedShard) (stop bool) {
|
|
|
|
if sh.GetMode().NoMetabase() {
|
|
|
|
// Already visited.
|
|
|
|
return false
|
2022-07-05 04:55:46 +00:00
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := sh.Get(ctx, i.ShardPrm)
|
2023-03-30 14:32:33 +00:00
|
|
|
i.Object = res.Object()
|
|
|
|
return err == nil
|
|
|
|
})
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
2020-11-19 08:15:49 +00:00
|
|
|
|
|
|
|
// Get reads object from local storage by provided address.
|
2023-03-13 11:37:35 +00:00
|
|
|
func Get(ctx context.Context, storage *StorageEngine, addr oid.Address) (*objectSDK.Object, error) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var getPrm GetPrm
|
|
|
|
getPrm.WithAddress(addr)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := storage.Get(ctx, getPrm)
|
2020-11-19 08:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Object(), nil
|
|
|
|
}
|