2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2020-12-01 08:08:06 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-17 17:39:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2022-07-05 14:07:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2020-12-08 09:56:14 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-10-26 12:23:12 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr"
|
2022-03-17 08:03:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache"
|
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-03-03 14:19:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
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.
|
2022-05-31 11:50:39 +00:00
|
|
|
func (s *Shard) Get(prm GetPrm) (GetRes, error) {
|
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
|
|
|
|
2022-07-06 12:39:35 +00:00
|
|
|
res, err := stor.Get(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) {
|
|
|
|
return c.Get(prm.addr)
|
|
|
|
}
|
|
|
|
|
2022-07-05 04:55:46 +00:00
|
|
|
skipMeta := prm.skipMeta || s.GetMode().NoMetabase()
|
2022-07-06 12:39:35 +00:00
|
|
|
obj, hasMeta, err := s.fetchObjectData(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
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchObjectData looks through writeCache and blobStor to find object.
|
2022-07-06 12:39:35 +00: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 08:08:06 +00:00
|
|
|
var (
|
|
|
|
err error
|
2022-03-03 14:19:05 +00:00
|
|
|
res *objectSDK.Object
|
2020-12-01 08:08:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if s.hasWriteCache() {
|
2022-07-29 07:54:49 +00:00
|
|
|
res, err := wc(s.writeCache)
|
|
|
|
if err == nil || IsErrOutOfRange(err) {
|
|
|
|
return res, false, err
|
2020-12-01 08:08:06 +00:00
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2022-09-01 06:16:50 +00:00
|
|
|
if IsErrNotFound(err) {
|
2021-04-06 10:56:06 +00:00
|
|
|
s.log.Debug("object is missing in write-cache")
|
|
|
|
} else {
|
|
|
|
s.log.Error("failed to fetch object from write-cache", zap.Error(err))
|
2020-12-01 08:08:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-24 14:15:58 +00:00
|
|
|
|
2022-03-14 09:49:41 +00:00
|
|
|
var exists bool
|
|
|
|
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)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
|
|
|
mRes, err := s.metaBase.Exists(mPrm)
|
2022-07-18 11:44:29 +00:00
|
|
|
if err != nil && !s.GetMode().NoMetabase() {
|
2022-03-14 09:49:41 +00:00
|
|
|
return res, false, err
|
|
|
|
}
|
2022-07-12 14:42:55 +00:00
|
|
|
exists = mRes.Exists()
|
2022-03-14 09:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if skipMeta || err != nil {
|
2022-07-06 12:39:35 +00:00
|
|
|
res, err = cb(s.blobStor, nil)
|
2022-02-22 07:20:33 +00:00
|
|
|
return res, false, err
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
if !exists {
|
2022-10-26 12:23:12 +00:00
|
|
|
return nil, false, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2020-12-01 08:08:06 +00:00
|
|
|
}
|
2020-11-17 12:23:15 +00:00
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
var mPrm meta.StorageIDPrm
|
|
|
|
mPrm.SetAddress(addr)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
mRes, err := s.metaBase.StorageID(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
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
res, err = cb(s.blobStor, mRes.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
|
|
|
}
|