2020-11-17 15:23:15 +03:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2020-12-01 11:08:06 +03:00
|
|
|
"fmt"
|
|
|
|
|
2022-12-23 20:35:35 +03:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
meta "github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
|
|
|
apistatus "github.com/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
objectSDK "github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-04-06 13:56:06 +03:00
|
|
|
"go.uber.org/zap"
|
2020-11-17 15:23:15 +03:00
|
|
|
)
|
|
|
|
|
2020-12-01 11:08:06 +03:00
|
|
|
// storFetcher is a type to unify object fetching mechanism in `fetchObjectData`
|
|
|
|
// method. It represents generalization of `getSmall` and `getBig` methods.
|
2022-07-06 17:09:50 +03:00
|
|
|
type storFetcher = func(stor *blobstor.BlobStor, id []byte) (*objectSDK.Object, error)
|
2020-12-01 11:08:06 +03:00
|
|
|
|
2020-11-17 15:23:15 +03:00
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
2022-05-31 20:00:41 +03:00
|
|
|
addr oid.Address
|
2022-02-22 10:20:33 +03:00
|
|
|
skipMeta bool
|
2020-11-17 15:23:15 +03:00
|
|
|
}
|
|
|
|
|
2022-04-21 14:28:05 +03:00
|
|
|
// GetRes groups the resulting values of Get operation.
|
2020-11-17 15:23:15 +03:00
|
|
|
type GetRes struct {
|
2022-03-03 17:19:05 +03:00
|
|
|
obj *objectSDK.Object
|
2022-02-22 10:20:33 +03:00
|
|
|
hasMeta bool
|
2020-11-17 15:23:15 +03:00
|
|
|
}
|
|
|
|
|
2022-07-13 15:43:04 +03:00
|
|
|
// SetAddress is a Get option to set the address of the requested object.
|
2020-11-17 15:23:15 +03:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-07-13 15:43:04 +03:00
|
|
|
func (p *GetPrm) SetAddress(addr oid.Address) {
|
|
|
|
p.addr = addr
|
2020-11-17 15:23:15 +03:00
|
|
|
}
|
|
|
|
|
2022-07-13 15:43:04 +03:00
|
|
|
// SetIgnoreMeta is a Get option try to fetch object from blobstor directly,
|
2022-02-22 10:20:33 +03:00
|
|
|
// without accessing metabase.
|
2022-07-13 15:43:04 +03:00
|
|
|
func (p *GetPrm) SetIgnoreMeta(ignore bool) {
|
2022-02-22 10:20:33 +03:00
|
|
|
p.skipMeta = ignore
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:08:06 +03:00
|
|
|
// Object returns the requested object.
|
2022-05-31 14:50:39 +03:00
|
|
|
func (r GetRes) Object() *objectSDK.Object {
|
2020-11-17 15:23:15 +03:00
|
|
|
return r.obj
|
|
|
|
}
|
|
|
|
|
2022-02-22 10:20:33 +03:00
|
|
|
// HasMeta returns true if info about the object was found in the metabase.
|
2022-05-31 14:50:39 +03:00
|
|
|
func (r GetRes) HasMeta() bool {
|
2022-02-22 10:20:33 +03:00
|
|
|
return r.hasMeta
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:08:06 +03:00
|
|
|
// Get reads an object from shard.
|
2020-11-17 15:23:15 +03:00
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object part.
|
|
|
|
//
|
2022-04-21 14:28:05 +03: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 21:38:28 +03:00
|
|
|
// Returns the object.ErrObjectIsExpired if the object is presented but already expired.
|
2022-05-31 14:50:39 +03:00
|
|
|
func (s *Shard) Get(prm GetPrm) (GetRes, error) {
|
2022-12-07 20:42:35 +03:00
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
2022-07-06 17:09:50 +03:00
|
|
|
cb := func(stor *blobstor.BlobStor, id []byte) (*objectSDK.Object, error) {
|
2022-07-06 15:39:35 +03:00
|
|
|
var getPrm common.GetPrm
|
|
|
|
getPrm.Address = prm.addr
|
2022-07-06 17:09:50 +03:00
|
|
|
getPrm.StorageID = id
|
2020-11-24 16:40:50 +03:00
|
|
|
|
2022-07-06 15:39:35 +03:00
|
|
|
res, err := stor.Get(getPrm)
|
2020-11-18 17:41:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-05 17:07:40 +03:00
|
|
|
return res.Object, nil
|
2020-11-17 20:39:43 +03:00
|
|
|
}
|
|
|
|
|
2022-07-29 10:54:49 +03:00
|
|
|
wc := func(c writecache.Cache) (*objectSDK.Object, error) {
|
|
|
|
return c.Get(prm.addr)
|
|
|
|
}
|
|
|
|
|
2022-12-07 20:42:35 +03:00
|
|
|
skipMeta := prm.skipMeta || s.info.Mode.NoMetabase()
|
2022-07-06 15:39:35 +03:00
|
|
|
obj, hasMeta, err := s.fetchObjectData(prm.addr, skipMeta, cb, wc)
|
2020-12-01 11:08:06 +03:00
|
|
|
|
2022-05-31 14:50:39 +03:00
|
|
|
return GetRes{
|
2022-02-22 10:20:33 +03:00
|
|
|
obj: obj,
|
|
|
|
hasMeta: hasMeta,
|
2020-12-01 11:08:06 +03:00
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2023-01-17 18:58:26 +03: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 11:08:06 +03:00
|
|
|
// fetchObjectData looks through writeCache and blobStor to find object.
|
2022-07-06 15:39:35 +03:00
|
|
|
func (s *Shard) fetchObjectData(addr oid.Address, skipMeta bool, cb storFetcher, wc func(w writecache.Cache) (*objectSDK.Object, error)) (*objectSDK.Object, bool, error) {
|
2020-12-01 11:08:06 +03:00
|
|
|
var (
|
2023-01-17 18:48:16 +03:00
|
|
|
mErr error
|
|
|
|
mRes meta.ExistsRes
|
2020-12-01 11:08:06 +03:00
|
|
|
)
|
|
|
|
|
2022-03-14 12:49:41 +03:00
|
|
|
var exists bool
|
|
|
|
if !skipMeta {
|
2022-07-12 17:42:55 +03:00
|
|
|
var mPrm meta.ExistsPrm
|
2022-07-12 17:59:37 +03:00
|
|
|
mPrm.SetAddress(addr)
|
2022-07-12 17:42:55 +03:00
|
|
|
|
2023-01-17 18:48:16 +03:00
|
|
|
mRes, mErr = s.metaBase.Exists(mPrm)
|
|
|
|
if mErr != nil && !s.info.Mode.NoMetabase() {
|
|
|
|
return nil, false, mErr
|
2022-03-14 12:49:41 +03:00
|
|
|
}
|
2022-07-12 17:42:55 +03:00
|
|
|
exists = mRes.Exists()
|
2022-03-14 12:49:41 +03:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:27:03 +03:00
|
|
|
if s.hasWriteCache() {
|
2023-01-17 18:48:16 +03:00
|
|
|
res, err := wc(s.writeCache)
|
2022-12-20 16:27:03 +03:00
|
|
|
if err == nil || IsErrOutOfRange(err) {
|
|
|
|
return res, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsErrNotFound(err) {
|
|
|
|
s.log.Debug("object is missing in write-cache")
|
|
|
|
} else {
|
|
|
|
s.log.Error("failed to fetch object from write-cache", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 18:48:16 +03:00
|
|
|
if skipMeta || mErr != nil {
|
|
|
|
res, err := cb(s.blobStor, nil)
|
2022-02-22 10:20:33 +03:00
|
|
|
return res, false, err
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:08:06 +03:00
|
|
|
if !exists {
|
2022-10-26 15:23:12 +03:00
|
|
|
return nil, false, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2020-12-01 11:08:06 +03:00
|
|
|
}
|
2020-11-17 15:23:15 +03:00
|
|
|
|
2022-07-06 17:09:50 +03:00
|
|
|
var mPrm meta.StorageIDPrm
|
|
|
|
mPrm.SetAddress(addr)
|
2022-07-12 17:42:55 +03:00
|
|
|
|
2023-01-17 18:48:16 +03:00
|
|
|
mExRes, err := s.metaBase.StorageID(mPrm)
|
2020-12-01 11:08:06 +03:00
|
|
|
if err != nil {
|
2022-02-22 10:20:33 +03:00
|
|
|
return nil, true, fmt.Errorf("can't fetch blobovnicza id from metabase: %w", err)
|
2020-12-01 11:08:06 +03:00
|
|
|
}
|
|
|
|
|
2023-01-17 18:58:26 +03: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 15:39:35 +03:00
|
|
|
|
2022-02-22 10:20:33 +03:00
|
|
|
return res, true, err
|
2020-11-17 15:23:15 +03:00
|
|
|
}
|