[#188] engine: Refactor get range from engine

Resolve funlen linter for StorageEngine.getRange method

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-03-30 18:44:43 +03:00
parent 3010ca2649
commit 456bc097f7

View file

@ -65,122 +65,51 @@ func (e *StorageEngine) GetRange(prm RngPrm) (res RngRes, err error) {
return return
} }
// nolint: funlen
func (e *StorageEngine) getRange(prm RngPrm) (RngRes, error) { func (e *StorageEngine) getRange(prm RngPrm) (RngRes, error) {
if e.metrics != nil { if e.metrics != nil {
defer elapsed(e.metrics.AddRangeDuration)() defer elapsed(e.metrics.AddRangeDuration)()
} }
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 hasDegraded bool
var shPrm shard.RngPrm var shPrm shard.RngPrm
shPrm.SetAddress(prm.addr) shPrm.SetAddress(prm.addr)
shPrm.SetRange(prm.off, prm.ln) shPrm.SetRange(prm.off, prm.ln)
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) { it := &getRangeShardIterator{
noMeta := sh.GetMode().NoMetabase() OutError: errNotFound,
hasDegraded = hasDegraded || noMeta ShardPrm: shPrm,
shPrm.SetIgnoreMeta(noMeta) Address: prm.addr,
Engine: e,
res, err := sh.GetRange(shPrm)
if err != nil {
if res.HasMeta() {
shardWithMeta = sh
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()
} }
util.MergeSplitInfo(siErr.SplitInfo(), outSI) it.tryGetWithMeta()
_, withLink := outSI.Link() if it.SplitInfo != nil {
_, withLast := outSI.LastPart() return RngRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(it.SplitInfo))
// stop iterating over shards if SplitInfo structure is complete
if withLink && withLast {
return true
} }
return false if it.Object == nil {
case
shard.IsErrRemoved(err),
shard.IsErrOutOfRange(err):
outError = err
return true // stop, return it back
default:
e.reportShardError(sh, "could not get object from shard", err)
return false
}
}
obj = res.Object()
return true
})
if outSI != nil {
return RngRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(outSI))
}
if obj == nil {
// If any shard is in a degraded mode, we should assume that metabase could store // If any shard is in a degraded mode, we should assume that metabase could store
// info about some object. // info about some object.
if shardWithMeta.Shard == nil && !hasDegraded || !shard.IsErrNotFound(outError) { if it.ShardWithMeta.Shard == nil && !it.HasDegraded || !shard.IsErrNotFound(it.OutError) {
return RngRes{}, outError return RngRes{}, it.OutError
} }
// If the object is not found but is present in metabase, it.tryGetFromBlobstor()
// 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)
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) { if it.Object == nil {
if sh.GetMode().NoMetabase() { return RngRes{}, it.OutError
// Already processed it without a metabase.
return false
} }
if it.ShardWithMeta.Shard != nil {
res, err := sh.GetRange(shPrm) e.reportShardError(it.ShardWithMeta, "meta info was present, but object is missing",
if shard.IsErrOutOfRange(err) { it.MetaError,
var errOutOfRange apistatus.ObjectOutOfRange
outError = errOutOfRange
return true
}
obj = res.Object()
return err == nil
})
if obj == nil {
return RngRes{}, outError
}
if shardWithMeta.Shard != nil {
e.reportShardError(shardWithMeta, "meta info was present, but object is missing",
metaError,
zap.Stringer("address", prm.addr)) zap.Stringer("address", prm.addr))
} }
} }
return RngRes{ return RngRes{
obj: obj, obj: it.Object,
}, nil }, nil
} }
@ -197,3 +126,85 @@ func GetRange(storage *StorageEngine, addr oid.Address, rng *objectSDK.Range) ([
return res.Object().Payload(), nil return res.Object().Payload(), nil
} }
type getRangeShardIterator struct {
Object *objectSDK.Object
SplitInfoError *objectSDK.SplitInfoError
SplitInfo *objectSDK.SplitInfo
OutError error
ShardWithMeta hashedShard
MetaError error
HasDegraded bool
ShardPrm shard.RngPrm
Address oid.Address
Engine *StorageEngine
}
func (i *getRangeShardIterator) tryGetWithMeta() {
i.Engine.iterateOverSortedShards(i.Address, func(_ int, sh hashedShard) (stop bool) {
noMeta := sh.GetMode().NoMetabase()
i.HasDegraded = i.HasDegraded || noMeta
i.ShardPrm.SetIgnoreMeta(noMeta)
res, err := sh.GetRange(i.ShardPrm)
if err == nil {
i.Object = res.Object()
return true
}
if res.HasMeta() {
i.ShardWithMeta = sh
i.MetaError = err
}
switch {
case shard.IsErrNotFound(err):
return false // ignore, go to next shard
case errors.As(err, &i.SplitInfoError):
if i.SplitInfo == nil {
i.SplitInfo = objectSDK.NewSplitInfo()
}
util.MergeSplitInfo(i.SplitInfoError.SplitInfo(), i.SplitInfo)
_, withLink := i.SplitInfo.Link()
_, withLast := i.SplitInfo.LastPart()
// stop iterating over shards if SplitInfo structure is complete
return withLink && withLast
case
shard.IsErrRemoved(err),
shard.IsErrOutOfRange(err):
i.OutError = err
return true // stop, return it back
default:
i.Engine.reportShardError(sh, "could not get object from shard", err)
return false
}
})
}
func (i *getRangeShardIterator) tryGetFromBlobstor() {
// 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.
i.ShardPrm.SetIgnoreMeta(true)
i.Engine.iterateOverSortedShards(i.Address, func(_ int, sh hashedShard) (stop bool) {
if sh.GetMode().NoMetabase() {
// Already processed it without a metabase.
return false
}
res, err := sh.GetRange(i.ShardPrm)
if shard.IsErrOutOfRange(err) {
var errOutOfRange apistatus.ObjectOutOfRange
i.OutError = errOutOfRange
return true
}
i.Object = res.Object()
return err == nil
})
}