2020-09-21 14:31:31 +00:00
|
|
|
package putsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2021-06-21 14:13:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2020-09-21 14:31:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-09-22 06:48:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-09-21 14:31:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/transformer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Streamer struct {
|
|
|
|
*cfg
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
target transformer.ObjectTarget
|
2021-05-27 14:25:29 +00:00
|
|
|
|
2021-09-28 05:36:41 +00:00
|
|
|
relay func(client.NodeInfo, client.Client) 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")
|
|
|
|
|
|
|
|
func (p *Streamer) Init(prm *PutInitPrm) error {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
if err := p.target.WriteHeader(prm.hdr); err != nil {
|
|
|
|
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 {
|
2021-05-27 14:25:29 +00:00
|
|
|
p.relay = prm.relay
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
// prepare untrusted-Put object target
|
2020-09-29 12:38:35 +00:00
|
|
|
p.target = &validatingTarget{
|
|
|
|
nextTarget: p.newCommonTarget(prm),
|
|
|
|
fmt: p.fmtValidator,
|
2021-06-07 09:29:38 +00:00
|
|
|
|
|
|
|
maxPayloadSz: p.maxPayloadSz,
|
2020-09-29 12:38:35 +00:00
|
|
|
}
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2020-09-29 16:44:59 +00:00
|
|
|
sessionKey, err := p.keyStorage.GetKey(sToken)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
p.target = transformer.NewPayloadSizeLimiter(
|
2021-06-07 09:29:38 +00:00
|
|
|
p.maxPayloadSz,
|
2020-09-21 14:31:31 +00:00
|
|
|
func() transformer.ObjectTarget {
|
2020-10-21 15:10:22 +00:00
|
|
|
return transformer.NewFormatTarget(&transformer.FormatterParams{
|
|
|
|
Key: sessionKey,
|
|
|
|
NextTarget: p.newCommonTarget(prm),
|
|
|
|
SessionToken: sToken,
|
2020-10-21 15:11:50 +00:00
|
|
|
NetworkState: p.networkState,
|
2020-10-21 15:10:22 +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
|
|
|
}
|
|
|
|
|
|
|
|
// get container to store the object
|
2020-11-16 09:43:52 +00:00
|
|
|
cnr, err := p.cnrSrc.Get(prm.hdr.ContainerID())
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// add common options
|
|
|
|
prm.traverseOpts = append(prm.traverseOpts,
|
|
|
|
// set processing container
|
|
|
|
placement.ForContainer(cnr),
|
|
|
|
|
|
|
|
// set identifier of the processing object
|
2020-11-16 09:43:52 +00:00
|
|
|
placement.ForObject(prm.hdr.ID()),
|
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 {
|
2021-09-24 10:28:58 +00:00
|
|
|
var relay func(nodeDesc) error
|
2021-05-27 14:25:29 +00:00
|
|
|
if p.relay != nil {
|
2021-09-24 10:28:58 +00:00
|
|
|
relay = func(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
|
|
|
}
|
|
|
|
|
2021-09-28 05:36:41 +00:00
|
|
|
return p.relay(info, c)
|
2021-05-27 14:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
return &distributedTarget{
|
|
|
|
traverseOpts: prm.traverseOpts,
|
2021-09-24 10:36:54 +00:00
|
|
|
remotePool: p.remotePool,
|
|
|
|
localPool: p.localPool,
|
2021-09-24 10:28:58 +00:00
|
|
|
nodeTargetInitializer: func(node nodeDesc) transformer.ObjectTarget {
|
|
|
|
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{
|
2021-03-23 18:40:36 +00:00
|
|
|
ctx: p.ctx,
|
|
|
|
keyStorage: p.keyStorage,
|
|
|
|
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,
|
2021-09-24 10:28:58 +00:00
|
|
|
|
|
|
|
isLocalKey: p.netmapKeys.IsLocalKey,
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Streamer) SendChunk(prm *PutChunkPrm) error {
|
|
|
|
if p.target == nil {
|
|
|
|
return errNotInit
|
|
|
|
}
|
|
|
|
|
2021-05-19 15:36:03 +00:00
|
|
|
if _, err := p.target.Write(prm.chunk); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Streamer) Close() (*PutResponse, error) {
|
|
|
|
if p.target == nil {
|
|
|
|
return nil, errNotInit
|
|
|
|
}
|
|
|
|
|
|
|
|
ids, err := p.target.Close()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
id := ids.ParentID()
|
|
|
|
if id == nil {
|
|
|
|
id = ids.SelfID()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PutResponse{
|
|
|
|
id: id,
|
|
|
|
}, nil
|
|
|
|
}
|