2020-09-21 14:31:31 +00:00
|
|
|
package putsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-14 10:31:30 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"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/services/object/util"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement"
|
2023-06-19 10:52:26 +00:00
|
|
|
pkgutil "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
2023-03-07 13:38:26 +00:00
|
|
|
containerSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-04-27 15:46:42 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2020-09-21 14:31:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Streamer struct {
|
|
|
|
*cfg
|
|
|
|
|
2023-03-14 10:31:30 +00:00
|
|
|
sessionKey *ecdsa.PrivateKey
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
target transformer.ObjectTarget
|
2021-05-27 14:25:29 +00:00
|
|
|
|
2023-04-12 08:02:25 +00:00
|
|
|
relay func(context.Context, client.NodeInfo, client.MultiAddressClient) error
|
2021-06-07 09:29:38 +00:00
|
|
|
|
|
|
|
maxPayloadSz uint64 // network config
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errNotInit = errors.New("stream not initialized")
|
|
|
|
|
|
|
|
var errInitRecall = errors.New("init recall")
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (p *Streamer) Init(ctx context.Context, prm *PutInitPrm) error {
|
2020-09-21 14:31:31 +00:00
|
|
|
// initialize destination target
|
|
|
|
if err := p.initTarget(prm); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not initialize object target: %w", p, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
if err := p.target.WriteHeader(ctx, prm.hdr); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not write header to target: %w", p, err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 09:29:38 +00:00
|
|
|
// MaxObjectSize returns maximum payload size for the streaming session.
|
|
|
|
//
|
|
|
|
// Must be called after the successful Init.
|
|
|
|
func (p *Streamer) MaxObjectSize() uint64 {
|
|
|
|
return p.maxPayloadSz
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
func (p *Streamer) initTarget(prm *PutInitPrm) error {
|
|
|
|
// prevent re-calling
|
|
|
|
if p.target != nil {
|
|
|
|
return errInitRecall
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare needed put parameters
|
|
|
|
if err := p.preparePrm(prm); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not prepare put parameters: %w", p, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 09:29:38 +00:00
|
|
|
p.maxPayloadSz = p.maxSizeSrc.MaxObjectSize()
|
|
|
|
if p.maxPayloadSz == 0 {
|
|
|
|
return fmt.Errorf("(%T) could not obtain max object size parameter", p)
|
|
|
|
}
|
|
|
|
|
2020-12-18 11:54:54 +00:00
|
|
|
if prm.hdr.Signature() != nil {
|
2023-04-03 12:40:52 +00:00
|
|
|
return p.initUntrustedTarget(prm)
|
|
|
|
}
|
|
|
|
return p.initTrustedTarget(prm)
|
|
|
|
}
|
2021-05-27 14:25:29 +00:00
|
|
|
|
2023-04-03 12:40:52 +00:00
|
|
|
func (p *Streamer) initUntrustedTarget(prm *PutInitPrm) error {
|
|
|
|
p.relay = prm.relay
|
2021-06-07 09:29:38 +00:00
|
|
|
|
2023-04-03 12:40:52 +00:00
|
|
|
// prepare untrusted-Put object target
|
2023-04-27 15:42:06 +00:00
|
|
|
p.target = &validatingPreparedTarget{
|
2023-04-03 12:40:52 +00:00
|
|
|
nextTarget: p.newCommonTarget(prm),
|
|
|
|
fmt: p.fmtValidator,
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2023-04-03 12:40:52 +00:00
|
|
|
maxPayloadSz: p.maxPayloadSz,
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 12:40:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Streamer) initTrustedTarget(prm *PutInitPrm) error {
|
2020-12-18 11:54:54 +00:00
|
|
|
sToken := prm.common.SessionToken()
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
// prepare trusted-Put object target
|
|
|
|
|
|
|
|
// get private token from local storage
|
2022-05-18 15:20:08 +00:00
|
|
|
var sessionInfo *util.SessionInfo
|
|
|
|
|
|
|
|
if sToken != nil {
|
|
|
|
sessionInfo = &util.SessionInfo{
|
|
|
|
ID: sToken.ID(),
|
2022-05-25 16:09:12 +00:00
|
|
|
Owner: sToken.Issuer(),
|
2022-05-18 15:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionKey, err := p.keyStorage.GetKey(sessionInfo)
|
2020-09-29 16:44:59 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not receive session key: %w", p, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 09:21:54 +00:00
|
|
|
// In case session token is missing, the line above returns the default key.
|
|
|
|
// If it isn't owner key, replication attempts will fail, thus this check.
|
2022-05-17 13:59:46 +00:00
|
|
|
if sToken == nil {
|
|
|
|
ownerObj := prm.hdr.OwnerID()
|
|
|
|
if ownerObj == nil {
|
|
|
|
return errors.New("missing object owner")
|
|
|
|
}
|
|
|
|
|
|
|
|
var ownerSession user.ID
|
|
|
|
user.IDFromKey(&ownerSession, sessionKey.PublicKey)
|
|
|
|
|
|
|
|
if !ownerObj.Equals(ownerSession) {
|
|
|
|
return fmt.Errorf("(%T) session token is missing but object owner id is different from the default key", p)
|
|
|
|
}
|
2022-04-04 09:21:54 +00:00
|
|
|
}
|
2022-05-17 13:59:46 +00:00
|
|
|
|
2023-03-14 10:31:30 +00:00
|
|
|
p.sessionKey = sessionKey
|
2022-04-04 13:04:18 +00:00
|
|
|
p.target = &validatingTarget{
|
2023-04-27 15:42:06 +00:00
|
|
|
fmt: p.fmtValidator,
|
2023-04-27 15:46:42 +00:00
|
|
|
nextTarget: transformer.NewPayloadSizeLimiter(transformer.Params{
|
|
|
|
Key: sessionKey,
|
|
|
|
NextTargetInit: func() transformer.ObjectTarget { return p.newCommonTarget(prm) },
|
|
|
|
NetworkState: p.networkState,
|
|
|
|
MaxSize: p.maxPayloadSz,
|
|
|
|
WithoutHomomorphicHash: containerSDK.IsHomomorphicHashingDisabled(prm.cnr),
|
|
|
|
SessionToken: sToken,
|
|
|
|
}),
|
2022-04-04 13:04:18 +00:00
|
|
|
}
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Streamer) preparePrm(prm *PutInitPrm) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// get latest network map
|
|
|
|
nm, err := netmap.GetLatestNetworkMap(p.netMapSrc)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not get latest network map: %w", p, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
idCnr, ok := prm.hdr.ContainerID()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("missing container ID")
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
// get container to store the object
|
2022-04-29 18:44:00 +00:00
|
|
|
cnrInfo, err := p.cnrSrc.Get(idCnr)
|
2020-09-21 14:31:31 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not get container by ID: %w", p, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 18:44:00 +00:00
|
|
|
prm.cnr = cnrInfo.Value
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
// add common options
|
|
|
|
prm.traverseOpts = append(prm.traverseOpts,
|
|
|
|
// set processing container
|
2022-04-29 18:44:00 +00:00
|
|
|
placement.ForContainer(prm.cnr),
|
2020-09-21 14:31:31 +00:00
|
|
|
)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
if id, ok := prm.hdr.ID(); ok {
|
|
|
|
prm.traverseOpts = append(prm.traverseOpts,
|
|
|
|
// set identifier of the processing object
|
2022-05-31 17:00:41 +00:00
|
|
|
placement.ForObject(id),
|
2022-05-12 16:37:46 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
// create placement builder from network map
|
|
|
|
builder := placement.NewNetworkMapBuilder(nm)
|
|
|
|
|
2020-09-29 15:05:22 +00:00
|
|
|
if prm.common.LocalOnly() {
|
2020-09-21 14:31:31 +00:00
|
|
|
// restrict success count to 1 stored copy (to local storage)
|
|
|
|
prm.traverseOpts = append(prm.traverseOpts, placement.SuccessAfter(1))
|
|
|
|
|
|
|
|
// use local-only placement builder
|
2021-09-06 12:17:14 +00:00
|
|
|
builder = util.NewLocalPlacement(builder, p.netmapKeys)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set placement builder
|
|
|
|
prm.traverseOpts = append(prm.traverseOpts, placement.UseBuilder(builder))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Streamer) newCommonTarget(prm *PutInitPrm) transformer.ObjectTarget {
|
2023-04-12 08:02:25 +00:00
|
|
|
var relay func(context.Context, nodeDesc) error
|
2021-05-27 14:25:29 +00:00
|
|
|
if p.relay != nil {
|
2023-04-12 08:02:25 +00:00
|
|
|
relay = func(ctx context.Context, node nodeDesc) error {
|
2021-09-28 04:46:10 +00:00
|
|
|
var info client.NodeInfo
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
client.NodeInfoFromNetmapElement(&info, node.info)
|
2021-09-28 04:46:10 +00:00
|
|
|
|
|
|
|
c, err := p.clientConstructor.Get(info)
|
2021-05-27 14:25:29 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return fmt.Errorf("could not create SDK client %s: %w", info.AddressGroup(), err)
|
2021-05-27 14:25:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 08:02:25 +00:00
|
|
|
return p.relay(ctx, info, c)
|
2021-05-27 14:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 10:43:13 +00:00
|
|
|
// enable additional container broadcast on non-local operation
|
2022-03-05 13:27:43 +00:00
|
|
|
// if object has TOMBSTONE or LOCK type.
|
|
|
|
typ := prm.hdr.Type()
|
|
|
|
withBroadcast := !prm.common.LocalOnly() && (typ == object.TypeTombstone || typ == object.TypeLock)
|
2022-03-05 10:43:13 +00:00
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
return &distributedTarget{
|
2022-03-05 10:32:11 +00:00
|
|
|
traversal: traversal{
|
|
|
|
opts: prm.traverseOpts,
|
2022-03-05 10:43:13 +00:00
|
|
|
|
|
|
|
extraBroadcastEnabled: withBroadcast,
|
2022-03-05 10:32:11 +00:00
|
|
|
},
|
2023-06-19 10:52:26 +00:00
|
|
|
payload: getPayload(),
|
|
|
|
getWorkerPool: p.getWorkerPool,
|
2022-04-28 07:19:26 +00:00
|
|
|
nodeTargetInitializer: func(node nodeDesc) preparedObjectTarget {
|
2021-09-24 10:28:58 +00:00
|
|
|
if node.local {
|
2020-09-21 14:31:31 +00:00
|
|
|
return &localTarget{
|
|
|
|
storage: p.localStore,
|
|
|
|
}
|
2021-01-11 15:21:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
rt := &remoteTarget{
|
2023-03-14 10:31:30 +00:00
|
|
|
privateKey: p.sessionKey,
|
2021-03-23 18:40:36 +00:00
|
|
|
commonPrm: prm.common,
|
|
|
|
clientConstructor: p.clientConstructor,
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
2021-09-28 05:32:30 +00:00
|
|
|
|
|
|
|
client.NodeInfoFromNetmapElement(&rt.nodeInfo, node.info)
|
|
|
|
|
|
|
|
return rt
|
2020-09-21 14:31:31 +00:00
|
|
|
},
|
2021-05-27 14:25:29 +00:00
|
|
|
relay: relay,
|
|
|
|
fmt: p.fmtValidator,
|
|
|
|
log: p.log,
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (p *Streamer) SendChunk(ctx context.Context, prm *PutChunkPrm) error {
|
2020-09-21 14:31:31 +00:00
|
|
|
if p.target == nil {
|
|
|
|
return errNotInit
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
if _, err := p.target.Write(ctx, prm.chunk); err != nil {
|
2021-05-19 15:36:03 +00:00
|
|
|
return fmt.Errorf("(%T) could not write payload chunk to target: %w", p, err)
|
|
|
|
}
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2021-05-19 15:36:03 +00:00
|
|
|
return nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (p *Streamer) Close(ctx context.Context) (*PutResponse, error) {
|
2020-09-21 14:31:31 +00:00
|
|
|
if p.target == nil {
|
|
|
|
return nil, errNotInit
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
ids, err := p.target.Close(ctx)
|
2020-09-21 14:31:31 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not close object target: %w", p, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 15:46:42 +00:00
|
|
|
id := ids.ParentID
|
2022-05-31 17:00:41 +00:00
|
|
|
if id != nil {
|
|
|
|
return &PutResponse{
|
|
|
|
id: *id,
|
|
|
|
}, nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &PutResponse{
|
2023-04-27 15:46:42 +00:00
|
|
|
id: ids.SelfID,
|
2020-09-21 14:31:31 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2023-06-19 10:52:26 +00:00
|
|
|
|
|
|
|
func (p *Streamer) getWorkerPool(pub []byte) (pkgutil.WorkerPool, bool) {
|
|
|
|
if p.netmapKeys.IsLocalKey(pub) {
|
|
|
|
return p.localPool, true
|
|
|
|
}
|
|
|
|
return p.remotePool, false
|
|
|
|
}
|