2020-12-02 23:45:25 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
2023-03-09 08:02:27 +00:00
|
|
|
"context"
|
2022-10-12 13:41:23 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
2021-01-11 13:50:49 +00:00
|
|
|
"io"
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
coreclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine"
|
|
|
|
internal "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal/client"
|
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal/client"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2020-12-02 23:45:25 +00:00
|
|
|
)
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type SimpleObjectWriter struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *object.Object
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
pld []byte
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientCacheWrapper struct {
|
2021-03-23 18:40:36 +00:00
|
|
|
cache ClientConstructor
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientWrapper struct {
|
2022-01-13 15:01:50 +00:00
|
|
|
client coreclient.MultiAddressClient
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storageEngineWrapper struct {
|
|
|
|
engine *engine.StorageEngine
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type partWriter struct {
|
2020-12-07 17:49:47 +00:00
|
|
|
ObjectWriter
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
headWriter HeaderWriter
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
chunkWriter ChunkWriter
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
type hasherWrapper struct {
|
2021-01-11 13:50:49 +00:00
|
|
|
hash io.Writer
|
2020-12-08 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
type nmSrcWrapper struct {
|
|
|
|
nmSrc netmap.Source
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func NewSimpleObjectWriter() *SimpleObjectWriter {
|
|
|
|
return &SimpleObjectWriter{
|
2022-03-03 14:19:05 +00:00
|
|
|
obj: object.New(),
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (s *SimpleObjectWriter) WriteHeader(_ context.Context, obj *object.Object) error {
|
2022-03-03 14:19:05 +00:00
|
|
|
s.obj = obj
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
s.pld = make([]byte, 0, obj.PayloadSize())
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (s *SimpleObjectWriter) WriteChunk(_ context.Context, p []byte) error {
|
2020-12-07 17:49:47 +00:00
|
|
|
s.pld = append(s.pld, p...)
|
2020-12-02 23:45:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (s *SimpleObjectWriter) Object() *object.Object {
|
2020-12-07 17:49:47 +00:00
|
|
|
if len(s.pld) > 0 {
|
|
|
|
s.obj.SetPayload(s.pld)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return s.obj
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
func (c *clientCacheWrapper) get(info coreclient.NodeInfo) (getClient, error) {
|
|
|
|
clt, err := c.cache.Get(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
return &clientWrapper{
|
|
|
|
client: clt,
|
2021-09-28 04:46:10 +00:00
|
|
|
}, nil
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 11:49:40 +00:00
|
|
|
func (c *clientWrapper) getObject(ctx context.Context, exec *execCtx, info coreclient.NodeInfo) (*object.Object, error) {
|
2021-09-27 06:52:03 +00:00
|
|
|
if exec.isForwardingEnabled() {
|
2023-04-03 08:17:59 +00:00
|
|
|
return exec.prm.forwarder(ctx, info, c.client)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
key, err := exec.key()
|
2021-10-26 12:07:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
if exec.headOnly() {
|
2023-03-31 12:44:18 +00:00
|
|
|
return c.getHeadOnly(ctx, exec, key)
|
2020-12-09 10:32:33 +00:00
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
// we don't specify payload writer because we accumulate
|
|
|
|
// the object locally (even huge).
|
2020-12-09 10:32:33 +00:00
|
|
|
if rng := exec.ctxRange(); rng != nil {
|
2023-03-31 12:44:18 +00:00
|
|
|
// Current spec allows other storage node to deny access,
|
|
|
|
// fallback to GET here.
|
|
|
|
return c.getRange(ctx, exec, key, rng)
|
|
|
|
}
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2023-03-31 12:44:18 +00:00
|
|
|
return c.get(ctx, exec, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientWrapper) getRange(ctx context.Context, exec *execCtx, key *ecdsa.PrivateKey, rng *object.Range) (*object.Object, error) {
|
|
|
|
var prm internalclient.PayloadRangePrm
|
|
|
|
|
|
|
|
prm.SetContext(ctx)
|
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
|
|
|
prm.SetAddress(exec.address())
|
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
prm.SetRange(rng)
|
|
|
|
|
|
|
|
if exec.isRaw() {
|
|
|
|
prm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internalclient.PayloadRange(prm)
|
|
|
|
if err != nil {
|
|
|
|
var errAccessDenied *apistatus.ObjectAccessDenied
|
|
|
|
if errors.As(err, &errAccessDenied) {
|
|
|
|
obj, err := c.get(ctx, exec, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-10-12 13:41:23 +00:00
|
|
|
}
|
2023-03-31 12:44:18 +00:00
|
|
|
|
|
|
|
payload := obj.Payload()
|
|
|
|
from := rng.GetOffset()
|
|
|
|
to := from + rng.GetLength()
|
|
|
|
|
|
|
|
if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to {
|
|
|
|
return nil, new(apistatus.ObjectOutOfRange)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payloadOnlyObject(payload[from:to]), nil
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
2023-03-31 12:44:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2023-03-31 12:44:18 +00:00
|
|
|
return payloadOnlyObject(res.PayloadRange()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientWrapper) getHeadOnly(ctx context.Context, exec *execCtx, key *ecdsa.PrivateKey) (*object.Object, error) {
|
|
|
|
var prm internalclient.HeadObjectPrm
|
|
|
|
|
|
|
|
prm.SetContext(ctx)
|
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
|
|
|
prm.SetAddress(exec.address())
|
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
|
|
|
|
if exec.isRaw() {
|
|
|
|
prm.SetRawFlag()
|
2021-11-01 08:35:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 12:44:18 +00:00
|
|
|
res, err := internalclient.HeadObject(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Header(), nil
|
2022-10-12 13:41:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 11:49:40 +00:00
|
|
|
func (c *clientWrapper) get(ctx context.Context, exec *execCtx, key *ecdsa.PrivateKey) (*object.Object, error) {
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.GetObjectPrm
|
|
|
|
|
2023-03-31 11:49:40 +00:00
|
|
|
prm.SetContext(ctx)
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
|
|
|
prm.SetAddress(exec.address())
|
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
|
|
|
|
if exec.isRaw() {
|
|
|
|
prm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internal.GetObject(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
2021-01-11 15:21:06 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return res.Object(), nil
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (e *storageEngineWrapper) get(exec *execCtx) (*object.Object, error) {
|
|
|
|
if exec.headOnly() {
|
2022-05-23 13:12:32 +00:00
|
|
|
var headPrm engine.HeadPrm
|
|
|
|
headPrm.WithAddress(exec.address())
|
|
|
|
headPrm.WithRaw(exec.isRaw())
|
|
|
|
|
|
|
|
r, err := e.engine.Head(headPrm)
|
2020-12-09 10:32:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Header(), nil
|
|
|
|
} else if rng := exec.ctxRange(); rng != nil {
|
2022-05-23 13:12:32 +00:00
|
|
|
var getRange engine.RngPrm
|
|
|
|
getRange.WithAddress(exec.address())
|
|
|
|
getRange.WithPayloadRange(rng)
|
|
|
|
|
|
|
|
r, err := e.engine.GetRange(getRange)
|
2020-12-07 17:49:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Object(), nil
|
|
|
|
} else {
|
2022-05-23 13:12:32 +00:00
|
|
|
var getPrm engine.GetPrm
|
|
|
|
getPrm.WithAddress(exec.address())
|
|
|
|
|
|
|
|
r, err := e.engine.Get(getPrm)
|
2020-12-07 17:49:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Object(), nil
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (w *partWriter) WriteChunk(ctx context.Context, p []byte) error {
|
|
|
|
return w.chunkWriter.WriteChunk(ctx, p)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (w *partWriter) WriteHeader(ctx context.Context, o *object.Object) error {
|
|
|
|
return w.headWriter.WriteHeader(ctx, o)
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func payloadOnlyObject(payload []byte) *object.Object {
|
|
|
|
obj := object.New()
|
|
|
|
obj.SetPayload(payload)
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return obj
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
2020-12-08 16:18:24 +00:00
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (h *hasherWrapper) WriteChunk(_ context.Context, p []byte) error {
|
2020-12-08 16:18:24 +00:00
|
|
|
_, err := h.hash.Write(p)
|
|
|
|
return err
|
|
|
|
}
|
2021-01-12 14:55:02 +00:00
|
|
|
|
|
|
|
func (n *nmSrcWrapper) currentEpoch() (uint64, error) {
|
|
|
|
return n.nmSrc.Epoch()
|
|
|
|
}
|