2022-07-18 06:16:36 +00:00
|
|
|
package blobovniczatree
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
2022-07-18 06:16:36 +00:00
|
|
|
"fmt"
|
2023-06-05 07:25:25 +00:00
|
|
|
"time"
|
2022-07-18 06:16:36 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"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"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-13 11:37:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-07-18 06:16:36 +00:00
|
|
|
"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.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (b *Blobovniczas) Get(ctx context.Context, prm common.GetPrm) (res common.GetRes, err error) {
|
2023-06-05 07:25:25 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
2023-06-06 06:05:52 +00:00
|
|
|
success = false
|
2023-06-05 07:25:25 +00:00
|
|
|
size = 0
|
|
|
|
)
|
|
|
|
defer func() {
|
2023-06-06 06:05:52 +00:00
|
|
|
b.metrics.Get(time.Since(startedAt), size, success, prm.StorageID != nil)
|
2023-06-05 07:25:25 +00:00
|
|
|
}()
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Blobovniczas.Get",
|
|
|
|
trace.WithAttributes(
|
2023-06-05 07:25:25 +00:00
|
|
|
attribute.String("path", b.rootPath),
|
2023-03-13 11:37:35 +00:00
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
attribute.String("storage_id", hex.EncodeToString(prm.StorageID)),
|
|
|
|
attribute.Bool("raw", prm.Raw),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-07-18 06:16:36 +00:00
|
|
|
var bPrm blobovnicza.GetPrm
|
|
|
|
bPrm.SetAddress(prm.Address)
|
|
|
|
|
|
|
|
if prm.StorageID != nil {
|
2023-09-19 15:08:38 +00:00
|
|
|
id := NewIDFromBytes(prm.StorageID)
|
|
|
|
shBlz := b.getBlobovnicza(id.Path())
|
2023-08-30 20:36:48 +00:00
|
|
|
blz, err := shBlz.Open()
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2023-08-30 20:36:48 +00:00
|
|
|
defer shBlz.Close()
|
2022-07-18 06:16:36 +00:00
|
|
|
|
2023-06-05 07:25:25 +00:00
|
|
|
res, err = b.getObject(ctx, blz, bPrm)
|
|
|
|
if err == nil {
|
2023-06-06 06:05:52 +00:00
|
|
|
success = true
|
2023-06-05 07:25:25 +00:00
|
|
|
size = len(res.RawData)
|
|
|
|
}
|
2023-06-21 13:42:58 +00:00
|
|
|
return res, err
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 14:46:10 +00:00
|
|
|
err = b.iterateSortedDBPaths(ctx, prm.Address, func(p string) (bool, error) {
|
2023-08-30 20:36:48 +00:00
|
|
|
res, err = b.getObjectFromLevel(ctx, bPrm, p)
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
2023-08-04 11:14:07 +00:00
|
|
|
if !client.IsErrObjectNotFound(err) {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeCouldNotGetObjectFromLevel,
|
2022-07-18 06:16:36 +00:00
|
|
|
zap.String("level", p),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// abort iterator if found, otherwise process all Blobovniczas
|
|
|
|
return err == nil, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil && res.Object == nil {
|
|
|
|
// not found in any blobovnicza
|
2023-08-04 11:14:07 +00:00
|
|
|
return res, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 06:05:52 +00:00
|
|
|
success = true
|
2023-06-05 07:25:25 +00:00
|
|
|
size = len(res.RawData)
|
|
|
|
|
2022-07-18 06:16:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// tries to read object from particular blobovnicza.
|
|
|
|
//
|
|
|
|
// returns error if object could not be read from any blobovnicza of the same level.
|
2023-08-30 20:36:48 +00:00
|
|
|
func (b *Blobovniczas) getObjectFromLevel(ctx context.Context, prm blobovnicza.GetPrm, blzPath string) (common.GetRes, error) {
|
2022-07-18 06:16:36 +00:00
|
|
|
// open blobovnicza (cached inside)
|
2023-08-31 08:32:09 +00:00
|
|
|
shBlz := b.getBlobovnicza(blzPath)
|
2023-08-30 20:36:48 +00:00
|
|
|
blz, err := shBlz.Open()
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
2023-08-30 20:36:48 +00:00
|
|
|
defer shBlz.Close()
|
2022-07-18 06:16:36 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
return b.getObject(ctx, blz, prm)
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reads object from blobovnicza and returns GetSmallRes.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (b *Blobovniczas) getObject(ctx context.Context, blz *blobovnicza.Blobovnicza, prm blobovnicza.GetPrm) (common.GetRes, error) {
|
|
|
|
res, err := blz.Get(ctx, prm)
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decompress the data
|
2022-08-19 14:29:53 +00:00
|
|
|
data, err := b.compression.Decompress(res.Object())
|
2022-07-18 06:16:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:16:35 +00:00
|
|
|
return common.GetRes{Object: obj, RawData: data}, nil
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|