2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2020-12-01 08:08:06 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
|
2020-11-17 17:39:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2020-12-08 09:56:14 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-06-28 14:05:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
|
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-03-03 14:19:05 +00:00
|
|
|
type storFetcher = func(stor *blobstor.BlobStor, id *blobovnicza.ID) (*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-05-31 11:50:39 +00:00
|
|
|
func (s *Shard) Get(prm GetPrm) (GetRes, error) {
|
2020-12-01 08:08:06 +00:00
|
|
|
var big, small storFetcher
|
2020-11-24 13:40:50 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
big = func(stor *blobstor.BlobStor, _ *blobovnicza.ID) (*objectSDK.Object, error) {
|
2022-05-23 14:15:16 +00:00
|
|
|
var getBigPrm blobstor.GetBigPrm
|
2020-11-24 13:40:50 +00:00
|
|
|
getBigPrm.SetAddress(prm.addr)
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
res, err := stor.GetBig(getBigPrm)
|
2020-11-17 17:39:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
return res.Object(), nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
small = func(stor *blobstor.BlobStor, id *blobovnicza.ID) (*objectSDK.Object, error) {
|
2022-05-23 14:15:16 +00:00
|
|
|
var getSmallPrm blobstor.GetSmallPrm
|
2020-12-01 08:08:06 +00:00
|
|
|
getSmallPrm.SetAddress(prm.addr)
|
|
|
|
getSmallPrm.SetBlobovniczaID(id)
|
|
|
|
|
|
|
|
res, err := stor.GetSmall(getSmallPrm)
|
2020-11-18 14:41:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
return res.Object(), nil
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
obj, hasMeta, err := s.fetchObjectData(prm.addr, prm.skipMeta, big, small)
|
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-05-31 17:00:41 +00:00
|
|
|
func (s *Shard) fetchObjectData(addr oid.Address, skipMeta bool, big, small storFetcher) (*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() {
|
2021-04-06 10:56:06 +00:00
|
|
|
res, err = s.writeCache.Get(addr)
|
2020-12-01 08:08:06 +00:00
|
|
|
if err == nil {
|
2022-02-22 07:20:33 +00:00
|
|
|
return res, false, nil
|
2020-12-01 08:08:06 +00:00
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
if writecache.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-06-28 14:05:08 +00:00
|
|
|
if err != nil && s.GetMode() != mode.Degraded {
|
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-02-22 07:20:33 +00:00
|
|
|
res, err = small(s.blobStor, nil)
|
2022-06-29 17:33:48 +00:00
|
|
|
if err == nil || IsErrOutOfRange(err) {
|
2022-02-22 07:20:33 +00:00
|
|
|
return res, false, err
|
|
|
|
}
|
|
|
|
res, err = big(s.blobStor, nil)
|
|
|
|
return res, false, err
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
if !exists {
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, false, errNotFound
|
2020-12-01 08:08:06 +00:00
|
|
|
}
|
2020-11-17 12:23:15 +00:00
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
var mPrm meta.IsSmallPrm
|
|
|
|
mPrm.WithAddress(addr)
|
|
|
|
|
|
|
|
mRes, err := s.metaBase.IsSmall(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-12 14:42:55 +00:00
|
|
|
if mRes.BlobovniczaID() != nil {
|
|
|
|
res, err = small(s.blobStor, mRes.BlobovniczaID())
|
2020-12-01 08:08:06 +00:00
|
|
|
} else {
|
|
|
|
res, err = big(s.blobStor, nil)
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
return res, true, err
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|