frostfs-node/pkg/local_object_storage/engine/range.go
Pavel Karpy 9f7a22e2aa [#1561] object: Return OUT_OF_RANGE status
Replace `ErrRangeOutOfBounds` error from `pkg/core/object` package with
`ObjectOutOfRange` from `apistatus` package. That error is returned by
storage node's server as NeoFS API statuses.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
2022-07-19 13:29:45 +03:00

189 lines
4.6 KiB
Go

package engine
import (
"errors"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util"
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"go.uber.org/zap"
)
// RngPrm groups the parameters of GetRange operation.
type RngPrm struct {
off, ln uint64
addr oid.Address
}
// RngRes groups the resulting values of GetRange operation.
type RngRes struct {
obj *objectSDK.Object
}
// WithAddress is a GetRng option to set the address of the requested object.
//
// Option is required.
func (p *RngPrm) WithAddress(addr oid.Address) {
if p != nil {
p.addr = addr
}
}
// 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.
func (p *RngPrm) WithPayloadRange(rng *objectSDK.Range) {
if p != nil {
p.off, p.ln = rng.GetOffset(), rng.GetLength()
}
}
// Object returns the requested object part.
//
// Instance payload contains the requested range of the original object.
func (r RngRes) Object() *objectSDK.Object {
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.
//
// 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.
//
// Returns an error if executions are blocked (see BlockExecution).
func (e *StorageEngine) GetRange(prm RngPrm) (res RngRes, err error) {
err = e.execIfNotBlocked(func() error {
res, err = e.getRange(prm)
return err
})
return
}
func (e *StorageEngine) getRange(prm RngPrm) (RngRes, error) {
if e.metrics != nil {
defer elapsed(e.metrics.AddRangeDuration)()
}
var (
obj *objectSDK.Object
siErr *objectSDK.SplitInfoError
errNotFound apistatus.ObjectNotFound
outSI *objectSDK.SplitInfo
outError error = errNotFound
shardWithMeta hashedShard
metaError error
)
var shPrm shard.RngPrm
shPrm.WithAddress(prm.addr)
shPrm.WithRange(prm.off, prm.ln)
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
res, err := sh.GetRange(shPrm)
if err != nil {
if res.HasMeta() {
shardWithMeta = sh
metaError = err
}
switch {
case shard.IsErrNotFound(err):
return false // ignore, go to next shard
case errors.As(err, &siErr):
siErr = err.(*objectSDK.SplitInfoError)
if outSI == nil {
outSI = objectSDK.NewSplitInfo()
}
util.MergeSplitInfo(siErr.SplitInfo(), outSI)
_, withLink := outSI.Link()
_, withLast := outSI.LastPart()
// stop iterating over shards if SplitInfo structure is complete
if withLink && withLast {
return true
}
return false
case
shard.IsErrRemoved(err),
shard.IsErrOutOfRange(err):
outError = err
return true // stop, return it back
default:
e.reportShardError(sh, "could not get object from shard", err)
return false
}
}
obj = res.Object()
return true
})
if outSI != nil {
return RngRes{}, objectSDK.NewSplitInfoError(outSI)
}
if obj == nil {
if shardWithMeta.Shard == nil || !shard.IsErrNotFound(outError) {
return RngRes{}, outError
}
// 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.
shPrm.WithIgnoreMeta(true)
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
res, err := sh.GetRange(shPrm)
if shard.IsErrOutOfRange(err) {
var errOutOfRange apistatus.ObjectOutOfRange
outError = errOutOfRange
return true
}
obj = res.Object()
return err == nil
})
if obj == nil {
return RngRes{}, outError
}
e.reportShardError(shardWithMeta, "meta info was present, but object is missing",
metaError,
zap.Stringer("address", prm.addr),
)
}
return RngRes{
obj: obj,
}, nil
}
// GetRange reads object payload range from local storage by provided address.
func GetRange(storage *StorageEngine, addr oid.Address, rng *objectSDK.Range) ([]byte, error) {
var rangePrm RngPrm
rangePrm.WithAddress(addr)
rangePrm.WithPayloadRange(rng)
res, err := storage.GetRange(rangePrm)
if err != nil {
return nil, err
}
return res.Object().Payload(), nil
}