2020-09-21 14:31:31 +00:00
|
|
|
package putsvc
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-21 14:31:31 +00:00
|
|
|
"sync"
|
2021-09-08 23:13:55 +00:00
|
|
|
"sync/atomic"
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-11-23 11:51:02 +00:00
|
|
|
svcutil "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"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2020-11-23 11:51:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2022-03-03 14:19:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-03-05 10:32:11 +00:00
|
|
|
"go.uber.org/zap"
|
2020-09-21 14:31:31 +00:00
|
|
|
)
|
|
|
|
|
2022-04-28 07:19:26 +00:00
|
|
|
type preparedObjectTarget interface {
|
|
|
|
WriteHeader(*objectSDK.Object) error
|
|
|
|
Close() (*transformer.AccessIdentifiers, error)
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
type distributedTarget struct {
|
2022-03-05 10:32:11 +00:00
|
|
|
traversal traversal
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2021-09-24 10:36:54 +00:00
|
|
|
remotePool, localPool util.WorkerPool
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2022-04-28 07:18:14 +00:00
|
|
|
payload []byte
|
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
|
|
|
|
2021-09-24 10:28:58 +00:00
|
|
|
isLocalKey func([]byte) bool
|
|
|
|
|
|
|
|
relay func(nodeDesc) error
|
2021-05-27 14:25:29 +00:00
|
|
|
|
2020-09-30 17:54:25 +00:00
|
|
|
fmt *object.FormatValidator
|
2020-11-23 11:51:02 +00:00
|
|
|
|
|
|
|
log *logger.Logger
|
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
|
|
|
|
mExclude map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (x *traversal) submitProcessed(n placement.Node) {
|
|
|
|
if x.extraBroadcastEnabled {
|
|
|
|
if x.mExclude == nil {
|
|
|
|
x.mExclude = make(map[string]struct{}, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mExclude[string(n.PublicKey())] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks if specified node was processed during the primary object placement.
|
|
|
|
func (x traversal) processed(n placement.Node) bool {
|
|
|
|
_, ok := x.mExclude[string(n.PublicKey())]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func (t *distributedTarget) WriteHeader(obj *objectSDK.Object) error {
|
2020-09-21 14:31:31 +00:00
|
|
|
t.obj = obj
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *distributedTarget) Write(p []byte) (n int, err error) {
|
2022-04-28 07:18:14 +00:00
|
|
|
t.payload = append(t.payload, p...)
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *distributedTarget) Close() (*transformer.AccessIdentifiers, error) {
|
2022-04-28 07:18:14 +00:00
|
|
|
t.obj.SetPayload(t.payload)
|
2020-12-01 11:23:28 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
if err := t.fmt.ValidateContent(t.obj); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not validate payload content: %w", t, err)
|
2020-09-30 17:54:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:34:31 +00:00
|
|
|
return t.iteratePlacement(t.sendObject)
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:28:58 +00:00
|
|
|
func (t *distributedTarget) sendObject(node nodeDesc) error {
|
|
|
|
if !node.local && t.relay != nil {
|
|
|
|
return t.relay(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
|
|
|
|
|
|
|
if err := target.WriteHeader(t.obj); err != nil {
|
|
|
|
return fmt.Errorf("could not write header: %w", err)
|
|
|
|
} else if _, err := target.Close(); err != nil {
|
|
|
|
return fmt.Errorf("could not close object stream: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:28:58 +00:00
|
|
|
func (t *distributedTarget) iteratePlacement(f func(nodeDesc) error) (*transformer.AccessIdentifiers, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := t.obj.ID()
|
|
|
|
|
2021-05-28 07:34:31 +00:00
|
|
|
traverser, err := placement.NewTraverser(
|
2022-05-12 16:37:46 +00:00
|
|
|
append(t.traversal.opts, placement.ForObject(&id))...,
|
2021-05-28 07:34:31 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("(%T) could not create object placement traverser: %w", t, err)
|
|
|
|
}
|
|
|
|
|
2021-09-08 23:13:55 +00:00
|
|
|
var resErr atomic.Value
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
addrs := traverser.Next()
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
|
|
|
|
for i := range addrs {
|
2022-03-05 10:32:11 +00:00
|
|
|
if t.traversal.processed(addrs[i]) {
|
|
|
|
// it can happen only during additional container broadcast
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
addr := addrs[i]
|
2021-09-24 10:28:58 +00:00
|
|
|
|
2021-09-28 05:17:11 +00:00
|
|
|
isLocal := t.isLocalKey(addr.PublicKey())
|
2021-09-24 10:28:58 +00:00
|
|
|
|
2021-09-24 10:36:54 +00:00
|
|
|
var workerPool util.WorkerPool
|
|
|
|
|
|
|
|
if isLocal {
|
|
|
|
workerPool = t.localPool
|
|
|
|
} else {
|
|
|
|
workerPool = t.remotePool
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := workerPool.Submit(func() {
|
2020-09-21 14:31:31 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-03-05 10:32:11 +00:00
|
|
|
err := f(nodeDesc{local: isLocal, info: addr})
|
|
|
|
|
|
|
|
// mark the container node as processed in order to exclude it
|
|
|
|
// in subsequent container broadcast. Note that we don't
|
|
|
|
// process this node during broadcast if primary placement
|
|
|
|
// on it failed.
|
|
|
|
t.traversal.submitProcessed(addr)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-08 23:13:55 +00:00
|
|
|
resErr.Store(err)
|
2021-09-06 11:35:06 +00:00
|
|
|
svcutil.LogServiceError(t.log, "PUT", addr.Addresses(), err)
|
2020-09-21 14:31:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
traverser.SubmitSuccess()
|
|
|
|
}); err != nil {
|
|
|
|
wg.Done()
|
2020-11-23 12:23:32 +00:00
|
|
|
|
|
|
|
svcutil.LogWorkerPoolError(t.log, "PUT", err)
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !traverser.Success() {
|
2021-09-10 13:39:50 +00:00
|
|
|
var err errIncompletePut
|
|
|
|
|
|
|
|
err.singleErr, _ = resErr.Load().(error)
|
2021-09-08 23:13:55 +00:00
|
|
|
|
2021-09-10 13:39:50 +00:00
|
|
|
return nil, err
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 10:32:11 +00:00
|
|
|
// perform additional container broadcast if needed
|
|
|
|
if t.traversal.submitPrimaryPlacementFinish() {
|
|
|
|
_, err = t.iteratePlacement(f)
|
|
|
|
if err != nil {
|
|
|
|
t.log.Error("additional container broadcast failure",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
|
|
|
|
// we don't fail primary operation because of broadcast failure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ = t.obj.ID()
|
|
|
|
|
2020-09-21 14:31:31 +00:00
|
|
|
return new(transformer.AccessIdentifiers).
|
2022-05-12 16:37:46 +00:00
|
|
|
WithSelfID(&id), nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|