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-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.
|
|
|
|
type storFetcher = func(stor *blobstor.BlobStor, id *blobovnicza.ID) (*object.Object, error)
|
|
|
|
|
2020-11-17 12:23:15 +00:00
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRes groups resulting values of Get operation.
|
|
|
|
type GetRes struct {
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
// Object returns the requested object.
|
2020-11-17 12:23:15 +00:00
|
|
|
func (r *GetRes) Object() *object.Object {
|
|
|
|
return r.obj
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
big = func(stor *blobstor.BlobStor, _ *blobovnicza.ID) (*object.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
|
|
|
|
}
|
|
|
|
|
|
|
|
small = func(stor *blobstor.BlobStor, id *blobovnicza.ID) (*object.Object, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
obj, err := s.fetchObjectData(prm.addr, big, small)
|
|
|
|
|
|
|
|
return &GetRes{
|
|
|
|
obj: obj,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchObjectData looks through writeCache and blobStor to find object.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (s *Shard) fetchObjectData(addr *addressSDK.Address, big, small storFetcher) (*object.Object, error) {
|
2020-12-01 08:08:06 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
res *object.Object
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return res, nil
|
|
|
|
}
|
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
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:08:06 +00:00
|
|
|
if !exists {
|
|
|
|
return nil, object.ErrNotFound
|
|
|
|
}
|
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 {
|
|
|
|
return nil, fmt.Errorf("can't fetch blobovnicza id from metabase: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if blobovniczaID != nil {
|
|
|
|
res, err = small(s.blobStor, blobovniczaID)
|
|
|
|
} else {
|
|
|
|
res, err = big(s.blobStor, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|