forked from TrueCloudLab/frostfs-node
[#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:
parent
3010ca2649
commit
456bc097f7
1 changed files with 104 additions and 93 deletions
|
@ -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)
|
|
||||||
|
|
||||||
_, withLink := outSI.Link()
|
|
||||||
_, withLast := outSI.LastPart()
|
|
||||||
|
|
||||||
// stop iterating over shards if SplitInfo structure is complete
|
|
||||||
if withLink && withLast {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
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 {
|
it.tryGetWithMeta()
|
||||||
|
|
||||||
|
if it.SplitInfo != nil {
|
||||||
|
return RngRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(it.SplitInfo))
|
||||||
|
}
|
||||||
|
|
||||||
|
if it.Object == 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
|
|
||||||
}
|
|
||||||
|
|
||||||
res, err := sh.GetRange(shPrm)
|
|
||||||
if shard.IsErrOutOfRange(err) {
|
|
||||||
var errOutOfRange apistatus.ObjectOutOfRange
|
|
||||||
|
|
||||||
outError = errOutOfRange
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
obj = res.Object()
|
|
||||||
return err == nil
|
|
||||||
})
|
|
||||||
if obj == nil {
|
|
||||||
return RngRes{}, outError
|
|
||||||
}
|
}
|
||||||
if shardWithMeta.Shard != nil {
|
if it.ShardWithMeta.Shard != nil {
|
||||||
e.reportShardError(shardWithMeta, "meta info was present, but object is missing",
|
e.reportShardError(it.ShardWithMeta, "meta info was present, but object is missing",
|
||||||
metaError,
|
it.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
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue