package blobovniczatree import ( "context" "encoding/hex" "fmt" "strconv" "time" "git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobovnicza" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr" tracingPkg "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/tracing" "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client" apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status" objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" "go.uber.org/zap" ) // GetRange reads range of object payload data from blobovnicza tree. // // If blobocvnicza ID is specified, only this blobovnicza is processed. // Otherwise, all Blobovniczas are processed descending weight. func (b *Blobovniczas) GetRange(ctx context.Context, prm common.GetRangePrm) (res common.GetRangeRes, err error) { var ( startedAt = time.Now() success = false size = 0 ) defer func() { b.metrics.GetRange(time.Since(startedAt), size, success, prm.StorageID != nil) }() ctx, span := tracing.StartSpanFromContext(ctx, "Blobovniczas.GetRange", trace.WithAttributes( attribute.String("path", b.rootPath), attribute.String("address", prm.Address.EncodeToString()), attribute.String("storage_id", hex.EncodeToString(prm.StorageID)), attribute.String("offset", strconv.FormatUint(prm.Range.GetOffset(), 10)), attribute.String("length", strconv.FormatUint(prm.Range.GetLength(), 10)), )) defer span.End() if prm.StorageID != nil { id := NewIDFromBytes(prm.StorageID) shBlz := b.getBlobovnicza(id.Path()) blz, err := shBlz.Open() if err != nil { return common.GetRangeRes{}, err } defer shBlz.Close() res, err := b.getObjectRange(ctx, blz, prm) if err == nil { size = len(res.Data) success = true } return res, err } objectFound := false err = b.iterateSortedDBPaths(ctx, prm.Address, func(p string) (bool, error) { res, err = b.getRangeFromLevel(ctx, prm, p) if err != nil { outOfBounds := isErrOutOfRange(err) if !outOfBounds && !client.IsErrObjectNotFound(err) { b.log.Debug(logs.BlobovniczatreeCouldNotGetObjectFromLevel, zap.String("level", p), zap.String("error", err.Error()), zap.String("trace_id", tracingPkg.GetTraceID(ctx))) } if outOfBounds { return true, err } } objectFound = err == nil // abort iterator if found, otherwise process all Blobovniczas return err == nil, nil }) if err == nil && !objectFound { // not found in any blobovnicza return common.GetRangeRes{}, logicerr.Wrap(new(apistatus.ObjectNotFound)) } if err == nil { success = true size = len(res.Data) } return } // tries to read range of object payload data from particular blobovnicza. // // returns error if object could not be read from any blobovnicza of the same level. func (b *Blobovniczas) getRangeFromLevel(ctx context.Context, prm common.GetRangePrm, blzPath string) (common.GetRangeRes, error) { // open blobovnicza (cached inside) shBlz := b.getBlobovnicza(blzPath) blz, err := shBlz.Open() if err != nil { return common.GetRangeRes{}, err } defer shBlz.Close() return b.getObjectRange(ctx, blz, prm) } // reads range of object payload data from blobovnicza and returns GetRangeSmallRes. func (b *Blobovniczas) getObjectRange(ctx context.Context, blz *blobovnicza.Blobovnicza, prm common.GetRangePrm) (common.GetRangeRes, error) { var gPrm blobovnicza.GetPrm gPrm.SetAddress(prm.Address) // we don't use GetRange call for now since blobovnicza // stores data that is compressed on BlobStor side. // If blobovnicza learns to do the compression itself, // we can start using GetRange. res, err := blz.Get(ctx, gPrm) if err != nil { return common.GetRangeRes{}, err } // decompress the data data, err := b.compression.Decompress(res.Object()) if err != nil { return common.GetRangeRes{}, fmt.Errorf("could not decompress object data: %w", err) } // unmarshal the object obj := objectSDK.New() if err := obj.Unmarshal(data); err != nil { return common.GetRangeRes{}, fmt.Errorf("could not unmarshal the object: %w", err) } from := prm.Range.GetOffset() to := from + prm.Range.GetLength() payload := obj.Payload() if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to { return common.GetRangeRes{}, logicerr.Wrap(new(apistatus.ObjectOutOfRange)) } return common.GetRangeRes{ Data: payload[from:to], }, nil }