[#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 return
} }
// nolint: funlen
func (e *StorageEngine) get(prm GetPrm) (GetRes, error) { func (e *StorageEngine) get(prm GetPrm) (GetRes, error) {
if e.metrics != nil { if e.metrics != nil {
defer elapsed(e.metrics.AddGetDuration)() defer elapsed(e.metrics.AddGetDuration)()
} }
var ( var errNotFound apistatus.ObjectNotFound
obj *objectSDK.Object
siErr *objectSDK.SplitInfoError
errNotFound apistatus.ObjectNotFound
outSI *objectSDK.SplitInfo
outError error = errNotFound
shardWithMeta hashedShard
metaError error
)
var shPrm shard.GetPrm var shPrm shard.GetPrm
shPrm.SetAddress(prm.addr) shPrm.SetAddress(prm.addr)
var hasDegraded bool it := &getShardIterator{
var objectExpired bool 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() 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() { if res.HasMeta() {
shardWithMeta = sh i.ShardWithMeta = sh
metaError = err i.MetaError = err
} }
switch { switch {
case shard.IsErrNotFound(err): case shard.IsErrNotFound(err):
return false // ignore, go to next shard return false // ignore, go to next shard
case errors.As(err, &siErr): case errors.As(err, &i.splitInfoErr):
if outSI == nil { if i.SplitInfo == nil {
outSI = objectSDK.NewSplitInfo() i.SplitInfo = objectSDK.NewSplitInfo()
} }
util.MergeSplitInfo(siErr.SplitInfo(), outSI) util.MergeSplitInfo(i.splitInfoErr.SplitInfo(), i.SplitInfo)
_, withLink := outSI.Link() _, withLink := i.SplitInfo.Link()
_, withLast := outSI.LastPart() _, withLast := i.SplitInfo.LastPart()
// stop iterating over shards if SplitInfo structure is complete // stop iterating over shards if SplitInfo structure is complete
if withLink && withLast { return withLink && withLast
return true
}
return false
case shard.IsErrRemoved(err): case shard.IsErrRemoved(err):
outError = err i.OutError = err
return true // stop, return it back return true // stop, return it back
case shard.IsErrObjectExpired(err): case shard.IsErrObjectExpired(err):
// object is found but should not // object is found but should not be returned
// be returned i.ObjectExpired = true
objectExpired = true
return true return true
default: default:
e.reportShardError(sh, "could not get object from shard", err) i.Engine.reportShardError(sh, "could not get object from shard", err)
return false 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, // If the object is not found but is present in metabase,
// try to fetch it from blobstor directly. If it is found in any // 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. // 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() { if sh.GetMode().NoMetabase() {
// Already visited. // Already visited.
return false return false
} }
res, err := sh.Get(shPrm) res, err := sh.Get(i.ShardPrm)
obj = res.Object() i.Object = res.Object()
return err == nil 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. // Get reads object from local storage by provided address.