2020-09-21 14:31:31 +00:00
|
|
|
package putsvc
|
|
|
|
|
|
|
|
import (
|
2023-04-03 11:23:53 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"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/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement"
|
|
|
|
objectSDK "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"
|
2020-09-21 14:31:31 +00:00
|
|
|
)
|
|
|
|
|
2022-04-28 07:19:26 +00:00
|
|
|
type preparedObjectTarget interface {
|
2023-01-10 12:10:54 +00:00
|
|
|
WriteObject(context.Context, *objectSDK.Object, object.ContentMeta) error
|
2022-04-28 07:19:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
type distributedTarget struct {
|
2023-08-23 17:31:03 +00:00
|
|
|
placementOpts []placement.Option
|
|
|
|
extraBroadcastEnabled bool
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2022-11-01 17:32:43 +00:00
|
|
|
obj *objectSDK.Object
|
|
|
|
objMeta object.ContentMeta
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2023-08-23 17:14:37 +00:00
|
|
|
*cfg
|
|
|
|
|
2023-04-03 10:09:44 +00:00
|
|
|
payload *payload
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2022-04-28 07:19:26 +00:00
|
|
|
nodeTargetInitializer func(nodeDesc) preparedObjectTarget
|
2020-09-30 17:54:25 +00:00
|
|
|
|
2023-04-12 08:02:25 +00:00
|
|
|
relay func(context.Context, nodeDesc) error
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 10:32:11 +00:00
|
|
|
// parameters and state of container traversal.
|
|
|
|
type traversal struct {
|
|
|
|
opts []placement.Option
|
|
|
|
|
|
|
|
// need of additional broadcast after the object is saved
|
|
|
|
extraBroadcastEnabled bool
|
|
|
|
|
|
|
|
// container nodes which was processed during the primary object placement
|
2023-08-03 07:39:48 +00:00
|
|
|
mExclude map[string]*bool
|
2022-03-05 10:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// updates traversal parameters after the primary placement finish and
|
|
|
|
// returns true if additional container broadcast is needed.
|
|
|
|
func (x *traversal) submitPrimaryPlacementFinish() bool {
|
|
|
|
if x.extraBroadcastEnabled {
|
|
|
|
// do not track success during container broadcast (best-effort)
|
|
|
|
x.opts = append(x.opts, placement.WithoutSuccessTracking())
|
|
|
|
|
|
|
|
// avoid 2nd broadcast
|
|
|
|
x.extraBroadcastEnabled = false
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// marks the container node as processed during the primary object placement.
|
2023-08-03 07:39:48 +00:00
|
|
|
func (x *traversal) submitProcessed(n placement.Node, item *bool) {
|
2022-03-05 10:32:11 +00:00
|
|
|
if x.extraBroadcastEnabled {
|
2022-11-10 06:48:46 +00:00
|
|
|
key := string(n.PublicKey())
|
|
|
|
|
2022-03-05 10:32:11 +00:00
|
|
|
if x.mExclude == nil {
|
2023-08-03 07:39:48 +00:00
|
|
|
x.mExclude = make(map[string]*bool, 1)
|
2022-03-05 10:32:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 07:39:48 +00:00
|
|
|
x.mExclude[key] = item
|
2022-03-05 10:32:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:28:58 +00:00
|
|
|
type nodeDesc struct {
|
|
|
|
local bool
|
|
|
|
|
|
|
|
info placement.Node
|
|
|
|
}
|
|
|
|
|
2021-09-10 13:39:50 +00:00
|
|
|
// errIncompletePut is returned if processing on a container fails.
|
|
|
|
type errIncompletePut struct {
|
|
|
|
singleErr error // error from the last responding node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x errIncompletePut) Error() string {
|
|
|
|
const commonMsg = "incomplete object PUT by placement"
|
|
|
|
|
|
|
|
if x.singleErr != nil {
|
|
|
|
return fmt.Sprintf("%s: %v", commonMsg, x.singleErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return commonMsg
|
|
|
|
}
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (t *distributedTarget) WriteHeader(_ context.Context, obj *objectSDK.Object) error {
|
2020-09-21 14:31:31 +00:00
|
|
|
t.obj = obj
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (t *distributedTarget) Write(_ context.Context, p []byte) (n int, err error) {
|
2023-04-03 10:09:44 +00:00
|
|
|
t.payload.Data = append(t.payload.Data, p...)
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (t *distributedTarget) Close(ctx context.Context) (*transformer.AccessIdentifiers, error) {
|
2022-12-06 10:25:19 +00:00
|
|
|
defer func() {
|
|
|
|
putPayload(t.payload)
|
|
|
|
t.payload = nil
|
|
|
|
}()
|
|
|
|
|
2023-04-03 10:09:44 +00:00
|
|
|
t.obj.SetPayload(t.payload.Data)
|
2020-12-01 11:23:28 +00:00
|
|
|
|
2023-07-11 14:32:00 +00:00
|
|
|
if err := t.WriteObject(ctx, t.obj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := t.obj.ID()
|
|
|
|
return &transformer.AccessIdentifiers{
|
|
|
|
SelfID: id,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteObject implements the transformer.ObjectWriter interface.
|
|
|
|
func (t *distributedTarget) WriteObject(ctx context.Context, obj *objectSDK.Object) error {
|
|
|
|
t.obj = obj
|
|
|
|
|
2022-11-01 17:32:43 +00:00
|
|
|
var err error
|
|
|
|
|
2023-08-23 17:14:37 +00:00
|
|
|
if t.objMeta, err = t.fmtValidator.ValidateContent(t.obj); err != nil {
|
2023-07-11 14:32:00 +00:00
|
|
|
return fmt.Errorf("(%T) could not validate payload content: %w", t, err)
|
2020-09-30 17:54:25 +00:00
|
|
|
}
|
2023-07-11 14:32:00 +00:00
|
|
|
return t.iteratePlacement(ctx)
|
2021-05-28 07:34:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (t *distributedTarget) sendObject(ctx context.Context, node nodeDesc) error {
|
2021-09-24 10:28:58 +00:00
|
|
|
if !node.local && t.relay != nil {
|
2023-04-12 08:02:25 +00:00
|
|
|
return t.relay(ctx, node)
|
2021-05-27 14:25:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
target := t.nodeTargetInitializer(node)
|
2021-05-28 07:34:31 +00:00
|
|
|
|
2023-01-10 12:10:54 +00:00
|
|
|
err := target.WriteObject(ctx, t.obj, t.objMeta)
|
|
|
|
if err != nil {
|
2021-05-28 07:34:31 +00:00
|
|
|
return fmt.Errorf("could not write header: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-11 14:33:13 +00:00
|
|
|
func (t *distributedTarget) iteratePlacement(ctx context.Context) error {
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := t.obj.ID()
|
|
|
|
|
2023-08-23 17:31:03 +00:00
|
|
|
iter := t.cfg.newNodeIterator(append(t.placementOpts, placement.ForObject(id)))
|
2023-08-23 17:38:25 +00:00
|
|
|
iter.extraBroadcastEnabled = needAdditionalBroadcast(t.obj, false /* Distributed target is for cluster-wide PUT */)
|
2023-08-23 17:31:03 +00:00
|
|
|
return iter.forEachNode(ctx, t.sendObject)
|
2023-04-03 09:24:01 +00:00
|
|
|
}
|