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/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
|
|
|
}
|
|
|
|
|
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-20 07:01:27 +00:00
|
|
|
// nolint: funlen
|
2022-03-03 14:19:05 +00:00
|
|
|
func (c *clientWrapper) getObject(exec *execCtx, info coreclient.NodeInfo) (*object.Object, error) {
|
2022-12-30 11:00:01 +00:00
|
|
|
if exec.prm.forwarder != nil {
|
2021-09-28 04:46:10 +00:00
|
|
|
return exec.prm.forwarder(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
|
|
|
|
}
|
|
|
|
|
2022-12-30 11:00:01 +00:00
|
|
|
if exec.headOnly {
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.HeadObjectPrm
|
|
|
|
|
2022-12-30 11:00:01 +00:00
|
|
|
prm.SetContext(exec.ctx)
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
2022-12-30 11:00:01 +00:00
|
|
|
prm.SetAddress(exec.prm.addr)
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
|
2022-12-30 11:00:01 +00:00
|
|
|
if exec.prm.raw {
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internalclient.HeadObject(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Header(), nil
|
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).
|
2022-12-30 11:00:01 +00:00
|
|
|
if rng := exec.prm.rng; rng != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.PayloadRangePrm
|
|
|
|
|
2022-12-30 11:00:01 +00:00
|
|
|
prm.SetContext(exec.ctx)
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
2022-12-30 11:00:01 +00:00
|
|
|
prm.SetAddress(exec.prm.addr)
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
prm.SetRange(rng)
|
|
|
|
|
2022-12-30 11:00:01 +00:00
|
|
|
if exec.prm.raw {
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internalclient.PayloadRange(prm)
|
2020-12-07 17:49:47 +00:00
|
|
|
if err != nil {
|
2022-10-12 13:41:23 +00:00
|
|
|
var errAccessDenied *apistatus.ObjectAccessDenied
|
|
|
|
if errors.As(err, &errAccessDenied) {
|
|
|
|
// Current spec allows other storage node to deny access,
|
2022-11-02 08:09:55 +00:00
|
|
|
// fallback to GET here.
|
2022-10-12 13:41:23 +00:00
|
|
|
obj, err := c.get(exec, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := obj.Payload()
|
|
|
|
from := rng.GetOffset()
|
|
|
|
to := from + rng.GetLength()
|
|
|
|
|
|
|
|
if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to {
|
2022-11-08 12:12:52 +00:00
|
|
|
return nil, new(apistatus.ObjectOutOfRange)
|
2022-10-12 13:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return payloadOnlyObject(payload[from:to]), nil
|
|
|
|
}
|
2020-12-07 17:49:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return payloadOnlyObject(res.PayloadRange()), nil
|
|
|
|
}
|
|
|
|
|
2022-10-12 13:41:23 +00:00
|
|
|
return c.get(exec, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientWrapper) get(exec *execCtx, key *ecdsa.PrivateKey) (*object.Object, error) {
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.GetObjectPrm
|
|
|
|
|
2022-12-30 11:00:01 +00:00
|
|
|
prm.SetContext(exec.ctx)
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
2022-12-30 11:00:01 +00:00
|
|
|
prm.SetAddress(exec.prm.addr)
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
|
2022-12-30 11:00:01 +00:00
|
|
|
if exec.prm.raw {
|
2021-11-01 08:35:33 +00:00
|
|
|
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) {
|
2022-12-30 11:00:01 +00:00
|
|
|
if exec.headOnly {
|
2022-05-23 13:12:32 +00:00
|
|
|
var headPrm engine.HeadPrm
|
2022-12-30 11:00:01 +00:00
|
|
|
headPrm.WithAddress(exec.prm.addr)
|
|
|
|
headPrm.WithRaw(exec.prm.raw)
|
2022-05-23 13:12:32 +00:00
|
|
|
|
|
|
|
r, err := e.engine.Head(headPrm)
|
2020-12-09 10:32:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Header(), nil
|
2022-12-30 11:00:01 +00:00
|
|
|
} else if rng := exec.prm.rng; rng != nil {
|
2022-05-23 13:12:32 +00:00
|
|
|
var getRange engine.RngPrm
|
2022-12-30 11:00:01 +00:00
|
|
|
getRange.WithAddress(exec.prm.addr)
|
2022-05-23 13:12:32 +00:00
|
|
|
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
|
2022-12-30 11:00:01 +00:00
|
|
|
getPrm.WithAddress(exec.prm.addr)
|
2022-05-23 13:12:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|