forked from TrueCloudLab/frostfs-node
128 lines
3.7 KiB
Go
128 lines
3.7 KiB
Go
package blobovniczatree
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"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"
|
|
)
|
|
|
|
// Get reads object from blobovnicza tree.
|
|
//
|
|
// If blobocvnicza ID is specified, only this blobovnicza is processed.
|
|
// Otherwise, all Blobovniczas are processed descending weight.
|
|
func (b *Blobovniczas) Get(ctx context.Context, prm common.GetPrm) (res common.GetRes, err error) {
|
|
var (
|
|
startedAt = time.Now()
|
|
success = false
|
|
size = 0
|
|
)
|
|
defer func() {
|
|
b.metrics.Get(time.Since(startedAt), size, success, prm.StorageID != nil)
|
|
}()
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Blobovniczas.Get",
|
|
trace.WithAttributes(
|
|
attribute.String("path", b.rootPath),
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
attribute.String("storage_id", hex.EncodeToString(prm.StorageID)),
|
|
attribute.Bool("raw", prm.Raw),
|
|
))
|
|
defer span.End()
|
|
|
|
var bPrm blobovnicza.GetPrm
|
|
bPrm.SetAddress(prm.Address)
|
|
|
|
if prm.StorageID != nil {
|
|
id := blobovnicza.NewIDFromBytes(prm.StorageID)
|
|
shBlz := b.getBlobovnicza(id.String())
|
|
blz, err := shBlz.Open()
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
defer shBlz.Close()
|
|
|
|
res, err = b.getObject(ctx, blz, bPrm)
|
|
if err == nil {
|
|
success = true
|
|
size = len(res.RawData)
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
err = b.iterateSortedLeaves(ctx, &prm.Address, func(p string) (bool, error) {
|
|
res, err = b.getObjectFromLevel(ctx, bPrm, p)
|
|
if err != nil {
|
|
if !client.IsErrObjectNotFound(err) {
|
|
b.log.Debug(logs.BlobovniczatreeCouldNotGetObjectFromLevel,
|
|
zap.String("level", p),
|
|
zap.String("error", err.Error()),
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)),
|
|
)
|
|
}
|
|
}
|
|
|
|
// abort iterator if found, otherwise process all Blobovniczas
|
|
return err == nil, nil
|
|
})
|
|
|
|
if err == nil && res.Object == nil {
|
|
// not found in any blobovnicza
|
|
return res, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
|
}
|
|
|
|
success = true
|
|
size = len(res.RawData)
|
|
|
|
return
|
|
}
|
|
|
|
// tries to read object from particular blobovnicza.
|
|
//
|
|
// returns error if object could not be read from any blobovnicza of the same level.
|
|
func (b *Blobovniczas) getObjectFromLevel(ctx context.Context, prm blobovnicza.GetPrm, blzPath string) (common.GetRes, error) {
|
|
// open blobovnicza (cached inside)
|
|
shBlz := b.getBlobovnicza(blzPath)
|
|
blz, err := shBlz.Open()
|
|
if err != nil {
|
|
return common.GetRes{}, err
|
|
}
|
|
defer shBlz.Close()
|
|
|
|
return b.getObject(ctx, blz, prm)
|
|
}
|
|
|
|
// reads object from blobovnicza and returns GetSmallRes.
|
|
func (b *Blobovniczas) getObject(ctx context.Context, blz *blobovnicza.Blobovnicza, prm blobovnicza.GetPrm) (common.GetRes, error) {
|
|
res, err := blz.Get(ctx, prm)
|
|
if err != nil {
|
|
return common.GetRes{}, err
|
|
}
|
|
|
|
// decompress the data
|
|
data, err := b.compression.Decompress(res.Object())
|
|
if err != nil {
|
|
return common.GetRes{}, fmt.Errorf("could not decompress object data: %w", err)
|
|
}
|
|
|
|
// unmarshal the object
|
|
obj := objectSDK.New()
|
|
if err := obj.Unmarshal(data); err != nil {
|
|
return common.GetRes{}, fmt.Errorf("could not unmarshal the object: %w", err)
|
|
}
|
|
|
|
return common.GetRes{Object: obj, RawData: data}, nil
|
|
}
|