2020-12-01 10:41:54 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2020-12-01 10:41:54 +00:00
|
|
|
"errors"
|
2023-03-13 11:37:35 +00:00
|
|
|
"strconv"
|
2020-12-01 10:41:54 +00:00
|
|
|
|
2023-09-13 08:25:59 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
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-09-27 08:02:06 +00:00
|
|
|
tracingPkg "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/tracing"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
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-03-04 12:57:43 +00:00
|
|
|
"go.uber.org/zap"
|
2020-12-01 10:41:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RngPrm groups the parameters of GetRange operation.
|
|
|
|
type RngPrm struct {
|
|
|
|
off, ln uint64
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-12-01 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// RngRes groups the resulting values of GetRange operation.
|
2020-12-01 10:41:54 +00:00
|
|
|
type RngRes struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2020-12-01 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a GetRng option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *RngPrm) WithAddress(addr oid.Address) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.addr = addr
|
2020-12-01 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithPayloadRange is a GetRange option to set range of requested payload data.
|
|
|
|
//
|
|
|
|
// Missing an option or calling with zero length is equivalent
|
|
|
|
// to getting the full payload range.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *RngPrm) WithPayloadRange(rng *objectSDK.Range) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.off, p.ln = rng.GetOffset(), rng.GetLength()
|
2020-12-01 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object returns the requested object part.
|
|
|
|
//
|
|
|
|
// Instance payload contains the requested range of the original object.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (r RngRes) Object() *objectSDK.Object {
|
2020-12-01 10:41:54 +00:00
|
|
|
return r.obj
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRange reads part of an object from local storage.
|
|
|
|
//
|
|
|
|
// 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 requested object is inhumed.
|
|
|
|
// Returns ErrRangeOutOfBounds if the requested object range is out of bounds.
|
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) GetRange(ctx context.Context, prm RngPrm) (res RngRes, err error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.getRange",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
|
|
attribute.String("offset", strconv.FormatUint(prm.off, 10)),
|
|
|
|
attribute.String("length", strconv.FormatUint(prm.ln, 10)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
2024-10-03 07:40:56 +00:00
|
|
|
defer elapsed("GetRange", e.metrics.AddMethodDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
|
2024-11-11 09:46:55 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
|
|
|
res, err = e.getRange(ctx, prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) getRange(ctx context.Context, prm RngPrm) (RngRes, error) {
|
2022-05-20 18:08:59 +00:00
|
|
|
var shPrm shard.RngPrm
|
2022-07-13 12:43:04 +00:00
|
|
|
shPrm.SetAddress(prm.addr)
|
|
|
|
shPrm.SetRange(prm.off, prm.ln)
|
2020-12-01 10:41:54 +00:00
|
|
|
|
2023-03-30 15:44:43 +00:00
|
|
|
it := &getRangeShardIterator{
|
2023-08-04 11:14:07 +00:00
|
|
|
OutError: new(apistatus.ObjectNotFound),
|
2023-03-30 15:44:43 +00:00
|
|
|
ShardPrm: shPrm,
|
|
|
|
Address: prm.addr,
|
|
|
|
Engine: e,
|
|
|
|
}
|
2020-12-01 10:41:54 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
it.tryGetWithMeta(ctx)
|
2020-12-01 10:41:54 +00:00
|
|
|
|
2023-03-30 15:44:43 +00:00
|
|
|
if it.SplitInfo != nil {
|
|
|
|
return RngRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(it.SplitInfo))
|
2021-04-14 09:05:53 +00:00
|
|
|
}
|
2024-05-02 11:29:59 +00:00
|
|
|
if it.ECInfo != nil {
|
|
|
|
return RngRes{}, logicerr.Wrap(objectSDK.NewECInfoError(it.ECInfo))
|
|
|
|
}
|
2021-04-14 09:05:53 +00:00
|
|
|
|
2023-03-30 15:44:43 +00:00
|
|
|
if it.Object == nil {
|
2022-06-29 11:27:36 +00:00
|
|
|
// If any shard is in a degraded mode, we should assume that metabase could store
|
|
|
|
// info about some object.
|
2023-08-04 11:14:07 +00:00
|
|
|
if it.ShardWithMeta.Shard == nil && !it.HasDegraded || !client.IsErrObjectNotFound(it.OutError) {
|
2023-03-30 15:44:43 +00:00
|
|
|
return RngRes{}, it.OutError
|
2022-03-04 12:57:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
it.tryGetFromBlobstor(ctx)
|
2022-06-29 11:27:36 +00:00
|
|
|
|
2023-03-30 15:44:43 +00:00
|
|
|
if it.Object == nil {
|
|
|
|
return RngRes{}, it.OutError
|
2022-03-04 12:57:43 +00:00
|
|
|
}
|
2023-09-13 08:25:59 +00:00
|
|
|
if it.ShardWithMeta.Shard != nil && it.MetaError != nil {
|
|
|
|
e.log.Warn(logs.ShardMetaInfoPresentButObjectNotFound,
|
|
|
|
zap.Stringer("shard_id", it.ShardWithMeta.ID()),
|
|
|
|
zap.String("error", it.MetaError.Error()),
|
2023-09-27 08:02:06 +00:00
|
|
|
zap.Stringer("address", prm.addr),
|
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)))
|
2022-06-29 11:27:36 +00:00
|
|
|
}
|
2020-12-01 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return RngRes{
|
2023-03-30 15:44:43 +00:00
|
|
|
obj: it.Object,
|
2020-12-01 10:41:54 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRange reads object payload range from local storage by provided address.
|
2023-03-13 11:37:35 +00:00
|
|
|
func GetRange(ctx context.Context, storage *StorageEngine, addr oid.Address, rng *objectSDK.Range) ([]byte, error) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var rangePrm RngPrm
|
|
|
|
rangePrm.WithAddress(addr)
|
|
|
|
rangePrm.WithPayloadRange(rng)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := storage.GetRange(ctx, rangePrm)
|
2020-12-01 10:41:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Object().Payload(), nil
|
|
|
|
}
|
2023-03-30 15:44:43 +00:00
|
|
|
|
|
|
|
type getRangeShardIterator struct {
|
|
|
|
Object *objectSDK.Object
|
|
|
|
SplitInfoError *objectSDK.SplitInfoError
|
|
|
|
SplitInfo *objectSDK.SplitInfo
|
2024-05-02 11:29:59 +00:00
|
|
|
ECInfoError *objectSDK.ECInfoError
|
|
|
|
ECInfo *objectSDK.ECInfo
|
2023-03-30 15:44:43 +00:00
|
|
|
OutError error
|
|
|
|
ShardWithMeta hashedShard
|
|
|
|
MetaError error
|
|
|
|
HasDegraded bool
|
|
|
|
|
|
|
|
ShardPrm shard.RngPrm
|
|
|
|
Address oid.Address
|
|
|
|
Engine *StorageEngine
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (i *getRangeShardIterator) tryGetWithMeta(ctx context.Context) {
|
2023-03-30 15:44:43 +00:00
|
|
|
i.Engine.iterateOverSortedShards(i.Address, func(_ int, sh hashedShard) (stop bool) {
|
|
|
|
noMeta := sh.GetMode().NoMetabase()
|
|
|
|
i.HasDegraded = i.HasDegraded || noMeta
|
|
|
|
i.ShardPrm.SetIgnoreMeta(noMeta)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := sh.GetRange(ctx, i.ShardPrm)
|
2023-03-30 15:44:43 +00:00
|
|
|
if err == nil {
|
|
|
|
i.Object = res.Object()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.HasMeta() {
|
|
|
|
i.ShardWithMeta = sh
|
|
|
|
i.MetaError = err
|
|
|
|
}
|
|
|
|
switch {
|
2023-08-04 11:14:07 +00:00
|
|
|
case client.IsErrObjectNotFound(err):
|
2023-03-30 15:44:43 +00:00
|
|
|
return false // ignore, go to next shard
|
|
|
|
case errors.As(err, &i.SplitInfoError):
|
|
|
|
if i.SplitInfo == nil {
|
|
|
|
i.SplitInfo = objectSDK.NewSplitInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
util.MergeSplitInfo(i.SplitInfoError.SplitInfo(), i.SplitInfo)
|
|
|
|
|
|
|
|
_, withLink := i.SplitInfo.Link()
|
|
|
|
_, withLast := i.SplitInfo.LastPart()
|
|
|
|
|
|
|
|
// stop iterating over shards if SplitInfo structure is complete
|
|
|
|
return withLink && withLast
|
2024-05-02 11:29:59 +00:00
|
|
|
case errors.As(err, &i.ECInfoError):
|
|
|
|
if i.ECInfo == nil {
|
|
|
|
i.ECInfo = objectSDK.NewECInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
util.MergeECInfo(i.ECInfoError.ECInfo(), i.ECInfo)
|
|
|
|
// stop iterating over shards if ECInfo structure is complete
|
|
|
|
return len(i.ECInfo.Chunks) == int(i.ECInfo.Chunks[0].Total)
|
2023-03-30 15:44:43 +00:00
|
|
|
case
|
2023-08-04 11:14:07 +00:00
|
|
|
client.IsErrObjectAlreadyRemoved(err),
|
2023-03-30 15:44:43 +00:00
|
|
|
shard.IsErrOutOfRange(err):
|
|
|
|
i.OutError = err
|
|
|
|
|
|
|
|
return true // stop, return it back
|
|
|
|
default:
|
2024-08-05 10:24:48 +00:00
|
|
|
i.Engine.reportShardError(sh, "could not get object from shard", err, zap.Stringer("address", i.Address))
|
2023-03-30 15:44:43 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (i *getRangeShardIterator) tryGetFromBlobstor(ctx context.Context) {
|
2023-03-30 15:44:43 +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 processed it without a metabase.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := sh.GetRange(ctx, i.ShardPrm)
|
2023-03-30 15:44:43 +00:00
|
|
|
if shard.IsErrOutOfRange(err) {
|
2023-08-04 11:14:07 +00:00
|
|
|
i.OutError = new(apistatus.ObjectOutOfRange)
|
2023-03-30 15:44:43 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
i.Object = res.Object()
|
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
}
|