2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2020-12-01 08:08:06 +00:00
|
|
|
"fmt"
|
|
|
|
|
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/blobstor"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
2023-09-27 08:02:06 +00:00
|
|
|
tracingPkg "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/tracing"
|
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"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-03-13 11:37:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-17 12:23:15 +00:00
|
|
|
)
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
// storFetcher is a type to unify object fetching mechanism in `fetchObjectData`
|
|
|
|
// method. It represents generalization of `getSmall` and `getBig` methods.
|
2022-07-06 14:09:50 +00:00
|
|
|
type storFetcher = func(stor *blobstor.BlobStor, id []byte) (*objectSDK.Object, error)
|
2020-12-01 08:08:06 +00:00
|
|
|
|
2020-11-17 12:23:15 +00:00
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2022-02-22 07:20:33 +00:00
|
|
|
skipMeta bool
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetRes groups the resulting values of Get operation.
|
2020-11-17 12:23:15 +00:00
|
|
|
type GetRes struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2022-02-22 07:20:33 +00:00
|
|
|
hasMeta bool
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
// SetAddress is a Get option to set the address of the requested object.
|
2020-11-17 12:23:15 +00:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-07-13 12:43:04 +00:00
|
|
|
func (p *GetPrm) SetAddress(addr oid.Address) {
|
|
|
|
p.addr = addr
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
// SetIgnoreMeta is a Get option try to fetch object from blobstor directly,
|
2022-02-22 07:20:33 +00:00
|
|
|
// without accessing metabase.
|
2022-07-13 12:43:04 +00:00
|
|
|
func (p *GetPrm) SetIgnoreMeta(ignore bool) {
|
2022-02-22 07:20:33 +00:00
|
|
|
p.skipMeta = ignore
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
// Object returns the requested object.
|
2022-05-31 11:50:39 +00:00
|
|
|
func (r GetRes) Object() *objectSDK.Object {
|
2020-11-17 12:23:15 +00:00
|
|
|
return r.obj
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
// HasMeta returns true if info about the object was found in the metabase.
|
2022-05-31 11:50:39 +00:00
|
|
|
func (r GetRes) HasMeta() bool {
|
2022-02-22 07:20:33 +00:00
|
|
|
return r.hasMeta
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
// Get reads an object from shard.
|
2020-11-17 12:23:15 +00:00
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object part.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in shard.
|
|
|
|
// Returns an error of type apistatus.ObjectAlreadyRemoved if the requested object has been marked as removed in shard.
|
2022-07-27 18:38:28 +00:00
|
|
|
// Returns the object.ErrObjectIsExpired if the object is presented but already expired.
|
2023-06-20 08:59:18 +00:00
|
|
|
// Returns the ErrShardDisabled if the shard is disabled.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (s *Shard) Get(ctx context.Context, prm GetPrm) (GetRes, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.Get",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
|
|
attribute.Bool("skip_meta", prm.skipMeta),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-12-07 17:42:35 +00:00
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
2023-06-20 08:59:18 +00:00
|
|
|
if s.info.Mode.Disabled() {
|
|
|
|
return GetRes{}, ErrShardDisabled
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
cb := func(stor *blobstor.BlobStor, id []byte) (*objectSDK.Object, error) {
|
2022-07-06 12:39:35 +00:00
|
|
|
var getPrm common.GetPrm
|
|
|
|
getPrm.Address = prm.addr
|
2022-07-06 14:09:50 +00:00
|
|
|
getPrm.StorageID = id
|
2020-11-24 13:40:50 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := stor.Get(ctx, getPrm)
|
2020-11-18 14:41:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
return res.Object, nil
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-07-29 07:54:49 +00:00
|
|
|
wc := func(c writecache.Cache) (*objectSDK.Object, error) {
|
2023-03-13 11:37:35 +00:00
|
|
|
return c.Get(ctx, prm.addr)
|
2022-07-29 07:54:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 17:42:35 +00:00
|
|
|
skipMeta := prm.skipMeta || s.info.Mode.NoMetabase()
|
2023-04-12 14:01:29 +00:00
|
|
|
obj, hasMeta, err := s.fetchObjectData(ctx, prm.addr, skipMeta, cb, wc)
|
2020-12-01 08:08:06 +00:00
|
|
|
|
2022-05-31 11:50:39 +00:00
|
|
|
return GetRes{
|
2022-02-22 07:20:33 +00:00
|
|
|
obj: obj,
|
|
|
|
hasMeta: hasMeta,
|
2020-12-01 08:08:06 +00:00
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2023-01-17 15:58:26 +00:00
|
|
|
// emptyStorageID is an empty storageID that indicates that
|
|
|
|
// an object is big (and is stored in an FSTree, not in a blobovnicza).
|
|
|
|
var emptyStorageID = make([]byte, 0)
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
// fetchObjectData looks through writeCache and blobStor to find object.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) fetchObjectData(ctx context.Context, addr oid.Address, skipMeta bool, cb storFetcher, wc func(w writecache.Cache) (*objectSDK.Object, error)) (*objectSDK.Object, bool, error) {
|
2020-12-01 08:08:06 +00:00
|
|
|
var (
|
2023-01-17 15:48:16 +00:00
|
|
|
mErr error
|
|
|
|
mRes meta.ExistsRes
|
2020-12-01 08:08:06 +00:00
|
|
|
)
|
|
|
|
|
2022-03-14 09:49:41 +00:00
|
|
|
if !skipMeta {
|
2022-07-12 14:42:55 +00:00
|
|
|
var mPrm meta.ExistsPrm
|
2022-07-12 14:59:37 +00:00
|
|
|
mPrm.SetAddress(addr)
|
2023-04-12 14:01:29 +00:00
|
|
|
mRes, mErr = s.metaBase.Exists(ctx, mPrm)
|
2023-01-17 15:48:16 +00:00
|
|
|
if mErr != nil && !s.info.Mode.NoMetabase() {
|
|
|
|
return nil, false, mErr
|
2022-03-14 09:49:41 +00:00
|
|
|
}
|
2023-02-17 14:23:09 +00:00
|
|
|
|
|
|
|
if !mRes.Exists() {
|
2023-08-04 11:14:07 +00:00
|
|
|
return nil, false, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2023-02-17 14:23:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Warn(logs.ShardFetchingObjectWithoutMeta, zap.Stringer("addr", addr))
|
2022-03-14 09:49:41 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 13:27:03 +00:00
|
|
|
if s.hasWriteCache() {
|
2023-01-17 15:48:16 +00:00
|
|
|
res, err := wc(s.writeCache)
|
2022-12-20 13:27:03 +00:00
|
|
|
if err == nil || IsErrOutOfRange(err) {
|
|
|
|
return res, false, err
|
|
|
|
}
|
2023-08-04 11:14:07 +00:00
|
|
|
if client.IsErrObjectNotFound(err) {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Debug(logs.ShardObjectIsMissingInWritecache,
|
2023-02-07 13:40:07 +00:00
|
|
|
zap.Stringer("addr", addr),
|
2023-09-27 08:02:06 +00:00
|
|
|
zap.Bool("skip_meta", skipMeta),
|
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)))
|
2022-12-20 13:27:03 +00:00
|
|
|
} else {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Error(logs.ShardFailedToFetchObjectFromWritecache,
|
2023-02-07 13:40:07 +00:00
|
|
|
zap.Error(err),
|
|
|
|
zap.Stringer("addr", addr),
|
2023-09-27 08:02:06 +00:00
|
|
|
zap.Bool("skip_meta", skipMeta),
|
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)))
|
2022-12-20 13:27:03 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 15:48:16 +00:00
|
|
|
if skipMeta || mErr != nil {
|
|
|
|
res, err := cb(s.blobStor, nil)
|
2022-02-22 07:20:33 +00:00
|
|
|
return res, false, err
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
var mPrm meta.StorageIDPrm
|
|
|
|
mPrm.SetAddress(addr)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
mExRes, err := s.metaBase.StorageID(ctx, mPrm)
|
2020-12-01 08:08:06 +00:00
|
|
|
if err != nil {
|
2022-02-22 07:20:33 +00:00
|
|
|
return nil, true, fmt.Errorf("can't fetch blobovnicza id from metabase: %w", err)
|
2020-12-01 08:08:06 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 15:58:26 +00:00
|
|
|
storageID := mExRes.StorageID()
|
|
|
|
if storageID == nil {
|
|
|
|
// `nil` storageID returned without any error
|
|
|
|
// means that object is big, `cb` expects an
|
|
|
|
// empty (but non-nil) storageID in such cases
|
|
|
|
storageID = emptyStorageID
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := cb(s.blobStor, storageID)
|
2022-07-06 12:39:35 +00:00
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
return res, true, err
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|