2020-11-17 12:26:03 +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"
|
2020-11-17 12:26:03 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
|
|
|
addr *objectSDK.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRes groups resulting values of Get operation.
|
|
|
|
type GetRes struct {
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Get option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
|
|
|
func (p *GetPrm) WithAddress(addr *objectSDK.Address) *GetPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-01 10:41:12 +00:00
|
|
|
// Object returns the requested object.
|
2020-11-17 12:26:03 +00:00
|
|
|
func (r *GetRes) Object() *object.Object {
|
|
|
|
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.
|
|
|
|
//
|
2020-11-30 16:39:05 +00:00
|
|
|
// Returns ErrNotFound if requested object is missing in local storage.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
2020-12-03 10:01:09 +00:00
|
|
|
obj *object.Object
|
|
|
|
siErr *objectSDK.SplitInfoError
|
2020-12-01 10:41:12 +00:00
|
|
|
|
2021-04-14 09:05:20 +00:00
|
|
|
outSI *objectSDK.SplitInfo
|
2020-12-03 10:01:09 +00:00
|
|
|
outError = object.ErrNotFound
|
2022-02-22 07:20:33 +00:00
|
|
|
|
|
|
|
shardWithMeta hashedShard
|
2020-12-01 10:41:12 +00:00
|
|
|
)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
|
|
|
shPrm := new(shard.GetPrm).
|
2020-11-18 07:59:47 +00:00
|
|
|
WithAddress(prm.addr)
|
|
|
|
|
2020-12-01 10:18:25 +00:00
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh *shard.Shard) (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 = hashedShard{sh: sh}
|
|
|
|
}
|
2020-12-01 10:41:12 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, object.ErrNotFound):
|
|
|
|
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
|
|
|
|
|
|
|
// stop iterating over shards if SplitInfo structure is complete
|
|
|
|
if outSI.Link() != nil && outSI.LastPart() != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
case errors.Is(err, object.ErrAlreadyRemoved):
|
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:
|
|
|
|
// TODO: smth wrong with shard, need to be processed, but
|
|
|
|
// still go to next shard
|
2020-11-17 12:26:03 +00:00
|
|
|
e.log.Warn("could not get object from shard",
|
|
|
|
zap.Stringer("shard", sh.ID()),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
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 {
|
|
|
|
return nil, objectSDK.NewSplitInfoError(outSI)
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
if obj == nil {
|
2022-02-22 07:20:33 +00:00
|
|
|
if shardWithMeta.sh == nil || !errors.Is(outError, object.ErrNotFound) {
|
|
|
|
return nil, 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 = shPrm.WithIgnoreMeta(true)
|
|
|
|
|
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh *shard.Shard) (stop bool) {
|
|
|
|
res, err := sh.Get(shPrm)
|
|
|
|
obj = res.Object()
|
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
if obj == nil {
|
|
|
|
return nil, outError
|
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &GetRes{
|
|
|
|
obj: obj,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-11-19 08:15:49 +00:00
|
|
|
|
|
|
|
// Get reads object from local storage by provided address.
|
|
|
|
func Get(storage *StorageEngine, addr *objectSDK.Address) (*object.Object, error) {
|
|
|
|
res, err := storage.Get(new(GetPrm).
|
|
|
|
WithAddress(addr),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Object(), nil
|
|
|
|
}
|