2020-09-21 14:31:31 +00:00
|
|
|
package putsvc
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-21 14:31:31 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
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"
|
2020-09-21 14:31:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type distributedTarget struct {
|
|
|
|
traverseOpts []placement.Option
|
|
|
|
|
|
|
|
workerPool util.WorkerPool
|
|
|
|
|
|
|
|
obj *object.RawObject
|
|
|
|
|
|
|
|
chunks [][]byte
|
|
|
|
|
|
|
|
nodeTargetInitializer func(*network.Address) transformer.ObjectTarget
|
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
|
|
|
}
|
|
|
|
|
|
|
|
var errIncompletePut = errors.New("incomplete object put")
|
|
|
|
|
|
|
|
func (t *distributedTarget) WriteHeader(obj *object.RawObject) error {
|
|
|
|
t.obj = obj
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *distributedTarget) Write(p []byte) (n int, err error) {
|
|
|
|
t.chunks = append(t.chunks, p)
|
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *distributedTarget) Close() (*transformer.AccessIdentifiers, error) {
|
|
|
|
sz := 0
|
|
|
|
|
|
|
|
for i := range t.chunks {
|
|
|
|
sz += len(t.chunks[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := make([]byte, 0, sz)
|
|
|
|
|
|
|
|
for i := range t.chunks {
|
|
|
|
payload = append(payload, t.chunks[i]...)
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:23:28 +00:00
|
|
|
t.obj.SetPayload(payload)
|
|
|
|
|
2020-12-11 08:03:27 +00:00
|
|
|
if err := t.fmt.ValidateContent(t.obj.Object()); 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *distributedTarget) sendObject(addr *network.Address) error {
|
|
|
|
target := t.nodeTargetInitializer(addr)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *distributedTarget) iteratePlacement(f func(*network.Address) error) (*transformer.AccessIdentifiers, error) {
|
|
|
|
traverser, err := placement.NewTraverser(
|
|
|
|
append(t.traverseOpts, placement.ForObject(t.obj.ID()))...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("(%T) could not create object placement traverser: %w", t, err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
addr := addrs[i]
|
|
|
|
if err := t.workerPool.Submit(func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2021-05-28 07:34:31 +00:00
|
|
|
if err := f(addr); err != nil {
|
|
|
|
svcutil.LogServiceError(t.log, "PUT", addr, 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() {
|
|
|
|
return nil, errIncompletePut
|
|
|
|
}
|
|
|
|
|
|
|
|
return new(transformer.AccessIdentifiers).
|
2020-11-16 09:43:52 +00:00
|
|
|
WithSelfID(t.obj.ID()), nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|