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"
|
|
|
|
"path/filepath"
|
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 {
|
|
|
|
id := blobovnicza.NewIDFromBytes(prm.StorageID)
|
|
|
|
blz, err := b.openBlobovnicza(id.String())
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
activeCache := make(map[string]struct{})
|
|
|
|
|
2023-05-28 19:37:37 +00:00
|
|
|
err = b.iterateSortedLeaves(ctx, &prm.Address, func(p string) (bool, error) {
|
2022-07-18 06:16:36 +00:00
|
|
|
dirPath := filepath.Dir(p)
|
|
|
|
|
|
|
|
_, ok := activeCache[dirPath]
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err = b.getObjectFromLevel(ctx, bPrm, p, !ok)
|
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()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
activeCache[dirPath] = struct{}{}
|
|
|
|
|
|
|
|
// 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-03-13 11:37:35 +00:00
|
|
|
func (b *Blobovniczas) getObjectFromLevel(ctx context.Context, prm blobovnicza.GetPrm, blzPath string, tryActive bool) (common.GetRes, error) {
|
2022-07-18 06:16:36 +00:00
|
|
|
lvlPath := filepath.Dir(blzPath)
|
|
|
|
|
|
|
|
// try to read from blobovnicza if it is opened
|
|
|
|
b.lruMtx.Lock()
|
|
|
|
v, ok := b.opened.Get(blzPath)
|
|
|
|
b.lruMtx.Unlock()
|
|
|
|
if ok {
|
2023-03-13 11:37:35 +00:00
|
|
|
if res, err := b.getObject(ctx, v, prm); err == nil {
|
2022-07-18 06:16:36 +00:00
|
|
|
return res, err
|
2023-08-04 11:14:07 +00:00
|
|
|
} else if !client.IsErrObjectNotFound(err) {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeCouldNotReadObjectFromOpenedBlobovnicza,
|
2022-07-18 06:16:36 +00:00
|
|
|
zap.String("path", blzPath),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// therefore the object is possibly placed in a lighter blobovnicza
|
|
|
|
|
|
|
|
// next we check in the active level blobobnicza:
|
|
|
|
// * the freshest objects are probably the most demanded;
|
|
|
|
// * the active blobovnicza is always opened.
|
|
|
|
b.activeMtx.RLock()
|
|
|
|
active, ok := b.active[lvlPath]
|
|
|
|
b.activeMtx.RUnlock()
|
|
|
|
|
|
|
|
if ok && tryActive {
|
2023-03-13 11:37:35 +00:00
|
|
|
if res, err := b.getObject(ctx, active.blz, prm); err == nil {
|
2022-07-18 06:16:36 +00:00
|
|
|
return res, err
|
2023-08-04 11:14:07 +00:00
|
|
|
} else if !client.IsErrObjectNotFound(err) {
|
2023-04-12 14:35:10 +00:00
|
|
|
b.log.Debug(logs.BlobovniczatreeCouldNotGetObjectFromActiveBlobovnicza,
|
2022-07-18 06:16:36 +00:00
|
|
|
zap.String("path", blzPath),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then object is possibly placed in closed blobovnicza
|
|
|
|
|
|
|
|
// check if it makes sense to try to open the blob
|
|
|
|
// (Blobovniczas "after" the active one are empty anyway,
|
|
|
|
// and it's pointless to open them).
|
|
|
|
if u64FromHexString(filepath.Base(blzPath)) > active.ind {
|
2023-08-04 11:14:07 +00:00
|
|
|
return common.GetRes{}, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open blobovnicza (cached inside)
|
|
|
|
blz, err := b.openBlobovnicza(blzPath)
|
|
|
|
if err != nil {
|
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|