Dmitrii Stepanov
08593f664b
All checks were successful
DCO action / DCO (pull_request) Successful in 54s
Pre-commit hooks / Pre-commit (pull_request) Successful in 1m28s
Vulncheck / Vulncheck (pull_request) Successful in 1m35s
Build / Build Components (pull_request) Successful in 1m48s
Tests and linters / Staticcheck (pull_request) Successful in 2m17s
Tests and linters / Lint (pull_request) Successful in 3m2s
Tests and linters / Tests (pull_request) Successful in 1m33s
Tests and linters / gopls check (pull_request) Successful in 2m58s
Tests and linters / Tests with -race (pull_request) Successful in 2m50s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
131 lines
3.3 KiB
Go
131 lines
3.3 KiB
Go
package putsvc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"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"
|
|
)
|
|
|
|
type preparedObjectTarget interface {
|
|
WriteObject(context.Context, *objectSDK.Object, object.ContentMeta) error
|
|
}
|
|
|
|
type distributedTarget struct {
|
|
placementOpts []placement.Option
|
|
|
|
obj *objectSDK.Object
|
|
objMeta object.ContentMeta
|
|
|
|
*cfg
|
|
|
|
nodeTargetInitializer func(nodeDesc) preparedObjectTarget
|
|
|
|
relay func(context.Context, nodeDesc) error
|
|
|
|
resetSuccessAfterOnBroadcast bool
|
|
}
|
|
|
|
// 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
|
|
mExclude map[string]*bool
|
|
|
|
resetSuccessAfterOnBroadcast bool
|
|
}
|
|
|
|
// 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())
|
|
|
|
if x.resetSuccessAfterOnBroadcast {
|
|
x.opts = append(x.opts, placement.ResetSuccessAfter())
|
|
}
|
|
|
|
// avoid 2nd broadcast
|
|
x.extraBroadcastEnabled = false
|
|
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// marks the container node as processed during the primary object placement.
|
|
func (x *traversal) submitProcessed(n placement.Node, item *bool) {
|
|
if x.extraBroadcastEnabled {
|
|
key := string(n.PublicKey())
|
|
|
|
if x.mExclude == nil {
|
|
x.mExclude = make(map[string]*bool, 1)
|
|
}
|
|
|
|
x.mExclude[key] = item
|
|
}
|
|
}
|
|
|
|
type nodeDesc struct {
|
|
local bool
|
|
|
|
info placement.Node
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// WriteObject implements the transformer.ObjectWriter interface.
|
|
func (t *distributedTarget) WriteObject(ctx context.Context, obj *objectSDK.Object) error {
|
|
t.obj = obj
|
|
|
|
var err error
|
|
|
|
if t.objMeta, err = t.fmtValidator.ValidateContent(t.obj); err != nil {
|
|
return fmt.Errorf("(%T) could not validate payload content: %w", t, err)
|
|
}
|
|
return t.iteratePlacement(ctx)
|
|
}
|
|
|
|
func (t *distributedTarget) sendObject(ctx context.Context, node nodeDesc) error {
|
|
if !node.local && t.relay != nil {
|
|
return t.relay(ctx, node)
|
|
}
|
|
|
|
target := t.nodeTargetInitializer(node)
|
|
|
|
err := target.WriteObject(ctx, t.obj, t.objMeta)
|
|
if err != nil {
|
|
return fmt.Errorf("could not write header: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *distributedTarget) iteratePlacement(ctx context.Context) error {
|
|
id, _ := t.obj.ID()
|
|
|
|
iter := t.cfg.newNodeIterator(append(t.placementOpts, placement.ForObject(id)))
|
|
iter.extraBroadcastEnabled = needAdditionalBroadcast(t.obj, false /* Distributed target is for cluster-wide PUT */)
|
|
iter.resetSuccessAfterOnBroadcast = t.resetSuccessAfterOnBroadcast
|
|
return iter.forEachNode(ctx, t.sendObject)
|
|
}
|