2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-12-01 08:08:06 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-17 12:23:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-01 08:08:06 +00:00
|
|
|
"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-03-03 14:19:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
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-02-22 07:20:33 +00:00
|
|
|
addr *addressSDK.Address
|
|
|
|
skipMeta bool
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRes groups resulting values of Get operation.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Get option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *GetPrm) WithAddress(addr *addressSDK.Address) *GetPrm {
|
2020-11-17 12:23:15 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-02-22 07:20:33 +00:00
|
|
|
// WithIgnoreMeta is a Get option try to fetch object from blobstor directly,
|
|
|
|
// without accessing metabase.
|
|
|
|
func (p *GetPrm) WithIgnoreMeta(ignore bool) *GetPrm {
|
|
|
|
p.skipMeta = ignore
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
// Object returns the requested object.
|
2022-03-03 14:19:05 +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.
|
|
|
|
func (r *GetRes) HasMeta() bool {
|
|
|
|
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.
|
|
|
|
//
|
2020-12-01 08:08:06 +00:00
|
|
|
// Returns object.ErrNotFound if requested object is missing in shard.
|
2020-11-17 12:23:15 +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) {
|
2020-11-24 13:40:50 +00:00
|
|
|
getBigPrm := new(blobstor.GetBigPrm)
|
|
|
|
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) {
|
2020-12-01 08:08:06 +00:00
|
|
|
getSmallPrm := new(blobstor.GetSmallPrm)
|
|
|
|
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
|
|
|
|
|
|
|
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-03-03 14:19:05 +00:00
|
|
|
func (s *Shard) fetchObjectData(addr *addressSDK.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
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
if errors.Is(err, object.ErrNotFound) {
|
|
|
|
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-02-22 07:20:33 +00:00
|
|
|
if skipMeta {
|
|
|
|
res, err = small(s.blobStor, nil)
|
2022-03-04 12:57:43 +00:00
|
|
|
if err == nil || errors.Is(err, object.ErrRangeOutOfBounds) {
|
2022-02-22 07:20:33 +00:00
|
|
|
return res, false, err
|
|
|
|
}
|
|
|
|
res, err = big(s.blobStor, nil)
|
|
|
|
return res, false, err
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
exists, err := meta.Exists(s.metaBase, addr)
|
2020-11-17 17:39:43 +00:00
|
|
|
if err != nil {
|
2022-02-22 07:20:33 +00:00
|
|
|
return nil, false, err
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
if !exists {
|
2022-02-22 07:20:33 +00:00
|
|
|
return nil, false, object.ErrNotFound
|
2020-12-01 08:08:06 +00:00
|
|
|
}
|
2020-11-17 12:23:15 +00:00
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
blobovniczaID, err := meta.IsSmall(s.metaBase, addr)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if blobovniczaID != nil {
|
|
|
|
res, err = small(s.blobStor, blobovniczaID)
|
|
|
|
} 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
|
|
|
}
|