2023-03-31 14:38:38 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-04 10:20:57 +00:00
|
|
|
"crypto/ecdsa"
|
2023-03-31 14:38:38 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
2023-03-13 11:37:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/pkg/tracing"
|
2023-03-31 14:38:38 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
|
|
|
|
rpcclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal"
|
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-13 11:37:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-03-31 14:38:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type getRequestForwarder struct {
|
|
|
|
OnceResign *sync.Once
|
|
|
|
OnceHeaderSending *sync.Once
|
|
|
|
GlobalProgress int
|
2023-04-04 10:20:57 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
2023-03-31 14:38:38 +00:00
|
|
|
Request *objectV2.GetRequest
|
|
|
|
Stream *streamObjectWriter
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:17:59 +00:00
|
|
|
func (f *getRequestForwarder) forwardRequestToNode(ctx context.Context, addr network.Address, c client.MultiAddressClient, pubkey []byte) (*object.Object, error) {
|
2023-03-13 11:37:35 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "getRequestForwarder.forwardRequestToNode",
|
|
|
|
trace.WithAttributes(attribute.String("address", addr.String())),
|
|
|
|
)
|
|
|
|
defer span.End()
|
|
|
|
|
2023-04-04 10:20:57 +00:00
|
|
|
var err error
|
2023-03-31 14:38:38 +00:00
|
|
|
|
|
|
|
// once compose and resign forwarding request
|
|
|
|
f.OnceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(f.Request.GetMetaHeader().GetTTL() - 1)
|
|
|
|
// TODO: #1165 think how to set the other fields
|
|
|
|
metaHdr.SetOrigin(f.Request.GetMetaHeader())
|
|
|
|
writeCurrentVersion(metaHdr)
|
|
|
|
f.Request.SetMetaHeader(metaHdr)
|
2023-04-04 10:20:57 +00:00
|
|
|
err = signature.SignServiceMessage(f.Key, f.Request)
|
2023-03-31 14:38:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
getStream, err := f.openStream(ctx, addr, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, f.readStream(ctx, c, getStream, pubkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *getRequestForwarder) verifyResponse(resp *objectV2.GetResponse, pubkey []byte) error {
|
|
|
|
// verify response key
|
|
|
|
if err := internal.VerifyResponseKeyV2(pubkey, resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return errResponseVerificationFailed(err)
|
2023-03-31 14:38:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 08:31:41 +00:00
|
|
|
return checkStatus(resp.GetMetaHeader().GetStatus())
|
2023-03-31 14:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *getRequestForwarder) writeHeader(ctx context.Context, v *objectV2.GetObjectPartInit) error {
|
|
|
|
obj := new(objectV2.Object)
|
|
|
|
|
|
|
|
obj.SetObjectID(v.GetObjectID())
|
|
|
|
obj.SetSignature(v.GetSignature())
|
|
|
|
obj.SetHeader(v.GetHeader())
|
|
|
|
|
|
|
|
var err error
|
|
|
|
f.OnceHeaderSending.Do(func() {
|
|
|
|
err = f.Stream.WriteHeader(ctx, object.NewFromV2(obj))
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return errCouldNotWriteObjHeader(err)
|
2023-03-31 14:38:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *getRequestForwarder) openStream(ctx context.Context, addr network.Address, c client.MultiAddressClient) (*rpc.GetResponseReader, error) {
|
|
|
|
var getStream *rpc.GetResponseReader
|
2023-04-18 09:04:59 +00:00
|
|
|
err := c.RawForAddress(ctx, addr, func(cli *rpcclient.Client) error {
|
2023-03-31 14:38:38 +00:00
|
|
|
var e error
|
|
|
|
getStream, e = rpc.GetObject(cli, f.Request, rpcclient.WithContext(ctx))
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, errStreamOpenningFailed(err)
|
2023-03-31 14:38:38 +00:00
|
|
|
}
|
|
|
|
return getStream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *getRequestForwarder) readStream(ctx context.Context, c client.MultiAddressClient, getStream *rpc.GetResponseReader, pubkey []byte) error {
|
|
|
|
var (
|
|
|
|
headWas bool
|
|
|
|
resp = new(objectV2.GetResponse)
|
|
|
|
localProgress int
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
|
|
|
err := getStream.Read(resp)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
if !headWas {
|
|
|
|
return io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
internalclient.ReportError(c, err)
|
2023-04-24 10:11:44 +00:00
|
|
|
return errReadingResponseFailed(err)
|
2023-03-31 14:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.verifyResponse(resp, pubkey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetObjectPart().(type) {
|
|
|
|
default:
|
2023-04-24 10:11:44 +00:00
|
|
|
return errUnexpectedObjectPart(v)
|
2023-03-31 14:38:38 +00:00
|
|
|
case *objectV2.GetObjectPartInit:
|
|
|
|
if headWas {
|
|
|
|
return errWrongMessageSeq
|
|
|
|
}
|
|
|
|
headWas = true
|
|
|
|
if err := f.writeHeader(ctx, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *objectV2.GetObjectPartChunk:
|
|
|
|
if !headWas {
|
|
|
|
return errWrongMessageSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
origChunk := v.GetChunk()
|
|
|
|
|
|
|
|
chunk := chunkToSend(f.GlobalProgress, localProgress, origChunk)
|
|
|
|
if len(chunk) == 0 {
|
|
|
|
localProgress += len(origChunk)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = f.Stream.WriteChunk(ctx, chunk); err != nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return errCouldNotWriteObjChunk("Get", err)
|
2023-03-31 14:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
localProgress += len(origChunk)
|
|
|
|
f.GlobalProgress += len(chunk)
|
|
|
|
case *objectV2.SplitInfo:
|
|
|
|
si := object.NewSplitInfoFromV2(v)
|
|
|
|
return object.NewSplitInfoError(si)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|