2020-12-01 10:41:54 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-04-15 07:23:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2020-12-01 10:41:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RngPrm groups the parameters of GetRange operation.
|
|
|
|
type RngPrm struct {
|
|
|
|
off, ln uint64
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-12-01 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// RngRes groups resulting values of GetRange operation.
|
2020-12-01 10:41:54 +00:00
|
|
|
type RngRes struct {
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a GetRng option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *RngPrm) WithAddress(addr *addressSDK.Address) *RngPrm {
|
2020-12-01 10:41:54 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2020-12-07 17:49:47 +00:00
|
|
|
func (p *RngPrm) WithPayloadRange(rng *objectSDK.Range) *RngPrm {
|
2020-12-01 10:41:54 +00:00
|
|
|
if p != nil {
|
2020-12-07 17:49:47 +00:00
|
|
|
p.off, p.ln = rng.GetOffset(), rng.GetLength()
|
2020-12-01 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object returns the requested object part.
|
|
|
|
//
|
|
|
|
// Instance payload contains the requested range of the original object.
|
|
|
|
func (r *RngRes) Object() *object.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 ErrNotFound if requested object is missing in local storage.
|
2020-12-08 16:36:54 +00:00
|
|
|
// Returns ErrAlreadyRemoved if requested object is inhumed.
|
|
|
|
// Returns ErrRangeOutOfBounds if requested object range is out of bounds.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
|
|
func (e *StorageEngine) GetRange(prm *RngPrm) (res *RngRes, err error) {
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2021-11-09 15:46:12 +00:00
|
|
|
res, err = e.getRange(prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) getRange(prm *RngPrm) (*RngRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddRangeDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 10:41:54 +00:00
|
|
|
var (
|
2020-12-08 09:47:05 +00:00
|
|
|
obj *object.Object
|
|
|
|
siErr *objectSDK.SplitInfoError
|
2020-12-01 10:41:54 +00:00
|
|
|
|
2021-04-14 09:05:53 +00:00
|
|
|
outSI *objectSDK.SplitInfo
|
2020-12-03 10:01:09 +00:00
|
|
|
outError = object.ErrNotFound
|
2020-12-01 10:41:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
shPrm := new(shard.RngPrm).
|
|
|
|
WithAddress(prm.addr).
|
|
|
|
WithRange(prm.off, prm.ln)
|
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
|
2020-12-01 10:41:54 +00:00
|
|
|
res, err := sh.GetRange(shPrm)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, object.ErrNotFound):
|
|
|
|
return false // ignore, go to next shard
|
2021-04-14 09:05:53 +00:00
|
|
|
case errors.As(err, &siErr):
|
|
|
|
siErr = err.(*objectSDK.SplitInfoError)
|
|
|
|
|
|
|
|
if outSI == nil {
|
|
|
|
outSI = objectSDK.NewSplitInfo()
|
|
|
|
}
|
|
|
|
|
2021-04-15 07:23:52 +00:00
|
|
|
util.MergeSplitInfo(siErr.SplitInfo(), outSI)
|
2021-04-14 09:05:53 +00:00
|
|
|
|
|
|
|
// stop iterating over shards if SplitInfo structure is complete
|
|
|
|
if outSI.Link() != nil && outSI.LastPart() != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-12-08 09:47:05 +00:00
|
|
|
case
|
|
|
|
errors.Is(err, object.ErrAlreadyRemoved),
|
2021-04-14 09:05:53 +00:00
|
|
|
errors.Is(err, object.ErrRangeOutOfBounds):
|
2020-12-03 10:01:09 +00:00
|
|
|
outError = err
|
2020-12-01 10:41:54 +00:00
|
|
|
|
|
|
|
return true // stop, return it back
|
|
|
|
default:
|
2022-01-31 14:58:32 +00:00
|
|
|
e.reportShardError(sh, "could not get object from shard", err)
|
2020-12-01 10:41:54 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = res.Object()
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:05:53 +00:00
|
|
|
if outSI != nil {
|
|
|
|
return nil, objectSDK.NewSplitInfoError(outSI)
|
|
|
|
}
|
|
|
|
|
2020-12-01 10:41:54 +00:00
|
|
|
if obj == nil {
|
2020-12-03 10:01:09 +00:00
|
|
|
return nil, outError
|
2020-12-01 10:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &RngRes{
|
|
|
|
obj: obj,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRange reads object payload range from local storage by provided address.
|
2022-01-26 12:11:13 +00:00
|
|
|
func GetRange(storage *StorageEngine, addr *addressSDK.Address, rng *objectSDK.Range) ([]byte, error) {
|
2020-12-01 10:41:54 +00:00
|
|
|
res, err := storage.GetRange(new(RngPrm).
|
|
|
|
WithAddress(addr).
|
2020-12-07 17:49:47 +00:00
|
|
|
WithPayloadRange(rng),
|
2020-12-01 10:41:54 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Object().Payload(), nil
|
|
|
|
}
|