2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"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"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
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) {
|
2020-11-17 12:26:03 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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).
|
2022-05-31 11:56:59 +00:00
|
|
|
func (e *StorageEngine) Get(prm GetPrm) (res GetRes, 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.get(prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
func (e *StorageEngine) get(prm GetPrm) (GetRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddGetDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 10:41:12 +00:00
|
|
|
var (
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2020-12-03 10:01:09 +00:00
|
|
|
siErr *objectSDK.SplitInfoError
|
2020-12-01 10:41:12 +00:00
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2021-04-14 09:05:20 +00:00
|
|
|
outSI *objectSDK.SplitInfo
|
2022-03-17 08:03:58 +00:00
|
|
|
outError error = errNotFound
|
2022-02-22 07:20:33 +00:00
|
|
|
|
|
|
|
shardWithMeta hashedShard
|
|
|
|
metaError error
|
2020-12-01 10:41:12 +00:00
|
|
|
)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var shPrm shard.GetPrm
|
|
|
|
shPrm.WithAddress(prm.addr)
|
2020-11-18 07:59:47 +00:00
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
|
2020-11-17 12:26:03 +00:00
|
|
|
res, err := sh.Get(shPrm)
|
|
|
|
if err != nil {
|
2022-02-22 07:20:33 +00:00
|
|
|
if res.HasMeta() {
|
|
|
|
shardWithMeta = sh
|
|
|
|
metaError = err
|
|
|
|
}
|
2020-12-01 10:41:12 +00:00
|
|
|
switch {
|
2022-03-17 08:03:58 +00:00
|
|
|
case shard.IsErrNotFound(err):
|
2020-12-01 10:41:12 +00:00
|
|
|
return false // ignore, go to next shard
|
2021-04-14 09:05:20 +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:20 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
_, withLink := outSI.Link()
|
|
|
|
_, withLast := outSI.LastPart()
|
|
|
|
|
2021-04-14 09:05:20 +00:00
|
|
|
// stop iterating over shards if SplitInfo structure is complete
|
2022-05-12 16:37:46 +00:00
|
|
|
if withLink && withLast {
|
2021-04-14 09:05:20 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2022-03-17 08:03:58 +00:00
|
|
|
case shard.IsErrRemoved(err):
|
2020-12-03 10:01:09 +00:00
|
|
|
outError = err
|
2020-12-01 10:41:12 +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:12 +00:00
|
|
|
return false
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 10:41:12 +00:00
|
|
|
obj = res.Object()
|
|
|
|
|
|
|
|
return true
|
2020-11-17 12:26:03 +00:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:05:20 +00:00
|
|
|
if outSI != nil {
|
2022-05-31 11:56:59 +00:00
|
|
|
return GetRes{}, objectSDK.NewSplitInfoError(outSI)
|
2021-04-14 09:05:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
if obj == nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
if shardWithMeta.Shard == nil || !shard.IsErrNotFound(outError) {
|
2022-05-31 11:56:59 +00:00
|
|
|
return GetRes{}, outError
|
2022-02-22 07:20: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.
|
2022-05-20 18:08:59 +00:00
|
|
|
shPrm.WithIgnoreMeta(true)
|
2022-02-22 07:20:33 +00:00
|
|
|
|
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
|
|
|
|
res, err := sh.Get(shPrm)
|
|
|
|
obj = res.Object()
|
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
if obj == nil {
|
2022-05-31 11:56:59 +00:00
|
|
|
return GetRes{}, outError
|
2022-02-22 07:20:33 +00:00
|
|
|
}
|
|
|
|
e.reportShardError(shardWithMeta, "meta info was present, but object is missing",
|
|
|
|
metaError, zap.Stringer("address", prm.addr))
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return GetRes{
|
2020-11-17 12:26:03 +00:00
|
|
|
obj: obj,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-11-19 08:15:49 +00:00
|
|
|
|
|
|
|
// Get reads object from local storage by provided address.
|
2022-05-31 17:00:41 +00:00
|
|
|
func Get(storage *StorageEngine, addr oid.Address) (*objectSDK.Object, error) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var getPrm GetPrm
|
|
|
|
getPrm.WithAddress(addr)
|
|
|
|
|
|
|
|
res, err := storage.Get(getPrm)
|
2020-11-19 08:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Object(), nil
|
|
|
|
}
|