[#188] engine: Refactor get object from engine

Resolve funlen linter for StorageEngine.get method

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-03-30 17:32:33 +03:00
parent 0739c36a3b
commit 3010ca2649

View file

@ -52,122 +52,132 @@ func (e *StorageEngine) Get(prm GetPrm) (res GetRes, err error) {
return
}
// nolint: funlen
func (e *StorageEngine) get(prm GetPrm) (GetRes, error) {
if e.metrics != nil {
defer elapsed(e.metrics.AddGetDuration)()
}
var (
obj *objectSDK.Object
siErr *objectSDK.SplitInfoError
errNotFound apistatus.ObjectNotFound
outSI *objectSDK.SplitInfo
outError error = errNotFound
shardWithMeta hashedShard
metaError error
)
var errNotFound apistatus.ObjectNotFound
var shPrm shard.GetPrm
shPrm.SetAddress(prm.addr)
var hasDegraded bool
var objectExpired bool
it := &getShardIterator{
OutError: errNotFound,
ShardPrm: shPrm,
Address: prm.addr,
Engine: e,
}
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
it.tryGetWithMeta()
if it.SplitInfo != nil {
return GetRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(it.SplitInfo))
}
if it.ObjectExpired {
return GetRes{}, errNotFound
}
if it.Object == nil {
if !it.HasDegraded && it.ShardWithMeta.Shard == nil || !shard.IsErrNotFound(it.OutError) {
return GetRes{}, it.OutError
}
it.tryGetFromBlobstore()
if it.Object == nil {
return GetRes{}, it.OutError
}
if it.ShardWithMeta.Shard != nil {
e.reportShardError(it.ShardWithMeta, "meta info was present, but object is missing",
it.MetaError, zap.Stringer("address", prm.addr))
}
}
return GetRes{
obj: it.Object,
}, nil
}
type getShardIterator struct {
Object *objectSDK.Object
SplitInfo *objectSDK.SplitInfo
OutError error
ShardWithMeta hashedShard
MetaError error
HasDegraded bool
ObjectExpired bool
ShardPrm shard.GetPrm
Address oid.Address
Engine *StorageEngine
splitInfoErr *objectSDK.SplitInfoError
}
func (i *getShardIterator) tryGetWithMeta() {
i.Engine.iterateOverSortedShards(i.Address, func(_ int, sh hashedShard) (stop bool) {
noMeta := sh.GetMode().NoMetabase()
shPrm.SetIgnoreMeta(noMeta)
i.ShardPrm.SetIgnoreMeta(noMeta)
hasDegraded = hasDegraded || noMeta
i.HasDegraded = i.HasDegraded || noMeta
res, err := sh.Get(i.ShardPrm)
if err == nil {
i.Object = res.Object()
return true
}
res, err := sh.Get(shPrm)
if err != nil {
if res.HasMeta() {
shardWithMeta = sh
metaError = err
i.ShardWithMeta = sh
i.MetaError = err
}
switch {
case shard.IsErrNotFound(err):
return false // ignore, go to next shard
case errors.As(err, &siErr):
if outSI == nil {
outSI = objectSDK.NewSplitInfo()
case errors.As(err, &i.splitInfoErr):
if i.SplitInfo == nil {
i.SplitInfo = objectSDK.NewSplitInfo()
}
util.MergeSplitInfo(siErr.SplitInfo(), outSI)
util.MergeSplitInfo(i.splitInfoErr.SplitInfo(), i.SplitInfo)
_, withLink := outSI.Link()
_, withLast := outSI.LastPart()
_, withLink := i.SplitInfo.Link()
_, withLast := i.SplitInfo.LastPart()
// stop iterating over shards if SplitInfo structure is complete
if withLink && withLast {
return true
}
return false
return withLink && withLast
case shard.IsErrRemoved(err):
outError = err
i.OutError = err
return true // stop, return it back
case shard.IsErrObjectExpired(err):
// object is found but should not
// be returned
objectExpired = true
// object is found but should not be returned
i.ObjectExpired = true
return true
default:
e.reportShardError(sh, "could not get object from shard", err)
i.Engine.reportShardError(sh, "could not get object from shard", err)
return false
}
}
obj = res.Object()
return true
})
if outSI != nil {
return GetRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(outSI))
}
if objectExpired {
return GetRes{}, errNotFound
}
if obj == nil {
if !hasDegraded && shardWithMeta.Shard == nil || !shard.IsErrNotFound(outError) {
return GetRes{}, outError
}
func (i *getShardIterator) tryGetFromBlobstore() {
// 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.SetIgnoreMeta(true)
i.ShardPrm.SetIgnoreMeta(true)
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
i.Engine.iterateOverSortedShards(i.Address, func(_ int, sh hashedShard) (stop bool) {
if sh.GetMode().NoMetabase() {
// Already visited.
return false
}
res, err := sh.Get(shPrm)
obj = res.Object()
res, err := sh.Get(i.ShardPrm)
i.Object = res.Object()
return err == nil
})
if obj == nil {
return GetRes{}, outError
}
if shardWithMeta.Shard != nil {
e.reportShardError(shardWithMeta, "meta info was present, but object is missing",
metaError, zap.Stringer("address", prm.addr))
}
}
return GetRes{
obj: obj,
}, nil
}
// Get reads object from local storage by provided address.