2024-08-30 09:09:14 +00:00
|
|
|
package writer
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
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"
|
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
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
type distributedWriter struct {
|
|
|
|
cfg *Config
|
|
|
|
|
2024-03-27 08:36:58 +00:00
|
|
|
placementOpts []placement.Option
|
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
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
nodeTargetInitializer func(NodeDescriptor) preparedObjectTarget
|
2020-09-30 17:54:25 +00:00
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
relay func(context.Context, NodeDescriptor) error
|
2024-08-23 09:28:09 +00:00
|
|
|
|
|
|
|
resetSuccessAfterOnBroadcast bool
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
// parameters and state of container Traversal.
|
|
|
|
type Traversal struct {
|
|
|
|
Opts []placement.Option
|
2022-03-05 10:32:11 +00:00
|
|
|
|
|
|
|
// need of additional broadcast after the object is saved
|
2024-08-30 09:09:14 +00:00
|
|
|
ExtraBroadcastEnabled bool
|
2022-03-05 10:32:11 +00:00
|
|
|
|
|
|
|
// container nodes which was processed during the primary object placement
|
2024-08-30 09:09:14 +00:00
|
|
|
Exclude map[string]*bool
|
2024-08-23 09:28:09 +00:00
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
ResetSuccessAfterOnBroadcast 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.
|
2024-08-30 09:09:14 +00:00
|
|
|
func (x *Traversal) submitPrimaryPlacementFinish() bool {
|
|
|
|
if x.ExtraBroadcastEnabled {
|
2022-03-05 10:32:11 +00:00
|
|
|
// do not track success during container broadcast (best-effort)
|
2024-08-30 09:09:14 +00:00
|
|
|
x.Opts = append(x.Opts, placement.WithoutSuccessTracking())
|
2022-03-05 10:32:11 +00:00
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
if x.ResetSuccessAfterOnBroadcast {
|
|
|
|
x.Opts = append(x.Opts, placement.ResetSuccessAfter())
|
2024-08-23 09:28:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 10:32:11 +00:00
|
|
|
// avoid 2nd broadcast
|
2024-08-30 09:09:14 +00:00
|
|
|
x.ExtraBroadcastEnabled = false
|
2022-03-05 10:32:11 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// marks the container node as processed during the primary object placement.
|
2024-08-30 09:09:14 +00:00
|
|
|
func (x *Traversal) submitProcessed(n placement.Node, item *bool) {
|
|
|
|
if x.ExtraBroadcastEnabled {
|
2022-11-10 06:48:46 +00:00
|
|
|
key := string(n.PublicKey())
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
if x.Exclude == nil {
|
|
|
|
x.Exclude = make(map[string]*bool, 1)
|
2022-03-05 10:32:11 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
x.Exclude[key] = item
|
2022-03-05 10:32:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
type NodeDescriptor struct {
|
|
|
|
Local bool
|
2021-09-24 10:28:58 +00:00
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
Info placement.Node
|
2021-09-24 10:28:58 +00:00
|
|
|
}
|
|
|
|
|
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-07-11 14:32:00 +00:00
|
|
|
// WriteObject implements the transformer.ObjectWriter interface.
|
2024-08-30 09:09:14 +00:00
|
|
|
func (t *distributedWriter) WriteObject(ctx context.Context, obj *objectSDK.Object) error {
|
2023-07-11 14:32:00 +00:00
|
|
|
t.obj = obj
|
|
|
|
|
2022-11-01 17:32:43 +00:00
|
|
|
var err error
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
if t.objMeta, err = t.cfg.FormatValidator.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
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
func (t *distributedWriter) sendObject(ctx context.Context, node NodeDescriptor) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
func (t *distributedWriter) iteratePlacement(ctx context.Context) error {
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := t.obj.ID()
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
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)
|
2023-04-03 09:24:01 +00:00
|
|
|
}
|