2020-12-02 23:45:25 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2020-12-18 11:48:28 +00:00
|
|
|
"errors"
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2021-05-31 11:03:17 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-12-02 23:45:25 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2021-01-12 14:55:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-12-02 23:45:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type statusError struct {
|
|
|
|
status int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type execCtx struct {
|
|
|
|
svc *Service
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
prm RangePrm
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
statusError
|
|
|
|
|
|
|
|
infoSplit *objectSDK.SplitInfo
|
|
|
|
|
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
collectedObject *object.Object
|
2020-12-07 17:49:47 +00:00
|
|
|
|
|
|
|
curOff uint64
|
2020-12-09 10:32:33 +00:00
|
|
|
|
2021-05-20 08:04:20 +00:00
|
|
|
head bool
|
2021-01-12 14:55:02 +00:00
|
|
|
|
|
|
|
curProcEpoch uint64
|
2021-04-29 12:18:29 +00:00
|
|
|
|
|
|
|
// true when the processing of the initial request
|
|
|
|
// is turned to assembling stage. When false,
|
|
|
|
// initial request can be forwarded during network
|
|
|
|
// communication.
|
|
|
|
assembling bool
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type execOption func(*execCtx)
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
const (
|
|
|
|
statusUndefined int = iota
|
|
|
|
statusOK
|
|
|
|
statusINHUMED
|
|
|
|
statusVIRTUAL
|
2020-12-07 17:49:47 +00:00
|
|
|
statusOutOfRange
|
2020-12-02 23:45:25 +00:00
|
|
|
)
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func headOnly() execOption {
|
|
|
|
return func(c *execCtx) {
|
|
|
|
c.head = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withPayloadRange(r *objectSDK.Range) execOption {
|
|
|
|
return func(c *execCtx) {
|
|
|
|
c.prm.rng = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (exec *execCtx) setLogger(l *logger.Logger) {
|
2020-12-07 17:49:47 +00:00
|
|
|
req := "GET"
|
2020-12-09 10:32:33 +00:00
|
|
|
if exec.headOnly() {
|
|
|
|
req = "HEAD"
|
|
|
|
} else if exec.ctxRange() != nil {
|
2020-12-07 17:49:47 +00:00
|
|
|
req = "GET_RANGE"
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
exec.log = l.With(
|
2020-12-07 17:49:47 +00:00
|
|
|
zap.String("request", req),
|
2020-12-02 23:45:25 +00:00
|
|
|
zap.Stringer("address", exec.address()),
|
|
|
|
zap.Bool("raw", exec.isRaw()),
|
|
|
|
zap.Bool("local", exec.isLocal()),
|
|
|
|
zap.Bool("with session", exec.prm.common.SessionToken() != nil),
|
|
|
|
zap.Bool("with bearer", exec.prm.common.BearerToken() != nil),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec execCtx) context() context.Context {
|
|
|
|
return exec.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec execCtx) isLocal() bool {
|
|
|
|
return exec.prm.common.LocalOnly()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec execCtx) isRaw() bool {
|
2020-12-07 17:49:47 +00:00
|
|
|
return exec.prm.RawFlag()
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (exec execCtx) address() *objectSDK.Address {
|
2020-12-07 17:49:47 +00:00
|
|
|
return exec.prm.Address()
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 11:48:28 +00:00
|
|
|
func (exec execCtx) isChild(obj *object.Object) bool {
|
|
|
|
par := obj.GetParent()
|
|
|
|
return par != nil && equalAddresses(exec.address(), par.Address())
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (exec execCtx) key() *ecdsa.PrivateKey {
|
2020-12-11 11:59:16 +00:00
|
|
|
return exec.prm.common.PrivateKey()
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (exec execCtx) callOptions() []client.CallOption {
|
2021-01-12 14:55:02 +00:00
|
|
|
return exec.prm.common.RemoteCallOptions(
|
|
|
|
util.WithNetmapEpoch(exec.curProcEpoch),
|
2021-03-13 15:22:21 +00:00
|
|
|
util.WithKey(exec.key()))
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (exec execCtx) remotePrm() *client.GetObjectParams {
|
|
|
|
return new(client.GetObjectParams).
|
2020-12-07 17:49:47 +00:00
|
|
|
WithAddress(exec.prm.Address()).
|
|
|
|
WithRawFlag(exec.prm.RawFlag())
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) canAssemble() bool {
|
2020-12-09 10:32:33 +00:00
|
|
|
return exec.svc.assembly && !exec.isRaw() && !exec.headOnly()
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) splitInfo() *objectSDK.SplitInfo {
|
|
|
|
return exec.infoSplit
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (exec *execCtx) containerID() *cid.ID {
|
2020-12-02 23:45:25 +00:00
|
|
|
return exec.address().ContainerID()
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (exec *execCtx) ctxRange() *objectSDK.Range {
|
|
|
|
return exec.prm.rng
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (exec *execCtx) headOnly() bool {
|
|
|
|
return exec.head
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
func (exec *execCtx) netmapEpoch() uint64 {
|
|
|
|
return exec.prm.common.NetmapEpoch()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) netmapLookupDepth() uint64 {
|
|
|
|
return exec.prm.common.NetmapLookupDepth()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) initEpoch() bool {
|
|
|
|
exec.curProcEpoch = exec.netmapEpoch()
|
|
|
|
if exec.curProcEpoch > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
e, err := exec.svc.currentEpochReceiver.currentEpoch()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
exec.status = statusUndefined
|
|
|
|
exec.err = err
|
|
|
|
|
|
|
|
exec.log.Debug("could not get current epoch number",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return false
|
|
|
|
case err == nil:
|
|
|
|
exec.curProcEpoch = e
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (exec *execCtx) generateTraverser(addr *objectSDK.Address) (*placement.Traverser, bool) {
|
2021-01-12 14:55:02 +00:00
|
|
|
t, err := exec.svc.traverserGenerator.GenerateTraverser(addr, exec.curProcEpoch)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
exec.status = statusUndefined
|
|
|
|
exec.err = err
|
|
|
|
|
|
|
|
exec.log.Debug("could not generate container traverser",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
case err == nil:
|
|
|
|
return t, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 11:48:28 +00:00
|
|
|
func (exec *execCtx) getChild(id *objectSDK.ID, rng *objectSDK.Range, withHdr bool) (*object.Object, bool) {
|
2020-12-09 10:32:33 +00:00
|
|
|
w := NewSimpleObjectWriter()
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
p := exec.prm
|
|
|
|
p.common = p.common.WithLocalOnly(false)
|
2020-12-07 17:49:47 +00:00
|
|
|
p.objWriter = w
|
|
|
|
p.SetRange(rng)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
addr := objectSDK.NewAddress()
|
|
|
|
addr.SetContainerID(exec.address().ContainerID())
|
|
|
|
addr.SetObjectID(id)
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
p.WithAddress(addr)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
exec.statusError = exec.svc.get(exec.context(), p.commonPrm, withPayloadRange(rng))
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2020-12-18 11:48:28 +00:00
|
|
|
child := w.Object()
|
|
|
|
ok := exec.status == statusOK
|
|
|
|
|
|
|
|
if ok && withHdr && !exec.isChild(child) {
|
|
|
|
exec.status = statusUndefined
|
|
|
|
exec.err = errors.New("wrong child header")
|
|
|
|
|
|
|
|
exec.log.Debug("parent address in child object differs")
|
|
|
|
}
|
|
|
|
|
|
|
|
return child, ok
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) headChild(id *objectSDK.ID) (*object.Object, bool) {
|
|
|
|
childAddr := objectSDK.NewAddress()
|
|
|
|
childAddr.SetContainerID(exec.containerID())
|
|
|
|
childAddr.SetObjectID(id)
|
|
|
|
|
|
|
|
p := exec.prm
|
|
|
|
p.common = p.common.WithLocalOnly(false)
|
2020-12-07 17:49:47 +00:00
|
|
|
p.WithAddress(childAddr)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
prm := HeadPrm{
|
2020-12-07 17:49:47 +00:00
|
|
|
commonPrm: p.commonPrm,
|
2020-12-09 10:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w := NewSimpleObjectWriter()
|
|
|
|
prm.SetHeaderWriter(w)
|
|
|
|
|
|
|
|
err := exec.svc.Head(exec.context(), prm)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
exec.status = statusUndefined
|
|
|
|
exec.err = err
|
|
|
|
|
|
|
|
exec.log.Debug("could not get child object header",
|
|
|
|
zap.Stringer("child ID", id),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
case err == nil:
|
2020-12-18 11:48:28 +00:00
|
|
|
child := w.Object()
|
|
|
|
|
|
|
|
if child.ParentID() != nil && !exec.isChild(child) {
|
|
|
|
exec.status = statusUndefined
|
|
|
|
|
|
|
|
exec.log.Debug("parent address in child object differs")
|
|
|
|
} else {
|
|
|
|
exec.status = statusOK
|
|
|
|
exec.err = nil
|
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2020-12-18 11:48:28 +00:00
|
|
|
return child, true
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
func (exec execCtx) remoteClient(node network.Address) (getClient, bool) {
|
2020-12-02 23:45:25 +00:00
|
|
|
log := exec.log.With(zap.Stringer("node", node))
|
|
|
|
|
2021-05-20 15:17:16 +00:00
|
|
|
c, err := exec.svc.clientCache.get(node)
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
exec.status = statusUndefined
|
|
|
|
exec.err = err
|
|
|
|
|
2021-05-20 15:17:16 +00:00
|
|
|
log.Debug("could not construct remote node client")
|
2020-12-02 23:45:25 +00:00
|
|
|
case err == nil:
|
2021-05-20 15:17:16 +00:00
|
|
|
return c, true
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeSplitInfo(dst, src *objectSDK.SplitInfo) {
|
|
|
|
if last := src.LastPart(); last != nil {
|
|
|
|
dst.SetLastPart(last)
|
|
|
|
}
|
|
|
|
|
|
|
|
if link := src.Link(); link != nil {
|
|
|
|
dst.SetLink(link)
|
|
|
|
}
|
|
|
|
|
|
|
|
if splitID := src.SplitID(); splitID != nil {
|
|
|
|
dst.SetSplitID(splitID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) writeCollectedHeader() bool {
|
2020-12-07 17:49:47 +00:00
|
|
|
if exec.ctxRange() != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
err := exec.prm.objWriter.WriteHeader(
|
|
|
|
object.NewRawFromObject(exec.collectedObject).CutPayload().Object(),
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
exec.status = statusUndefined
|
|
|
|
exec.err = err
|
|
|
|
|
|
|
|
exec.log.Debug("could not write header",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
case err == nil:
|
|
|
|
exec.status = statusOK
|
|
|
|
exec.err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec.status == statusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) writeObjectPayload(obj *object.Object) bool {
|
2020-12-09 10:32:33 +00:00
|
|
|
if exec.headOnly() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
err := exec.prm.objWriter.WriteChunk(obj.Payload())
|
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
exec.status = statusUndefined
|
|
|
|
exec.err = err
|
|
|
|
|
|
|
|
exec.log.Debug("could not write payload chunk",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
case err == nil:
|
|
|
|
exec.status = statusOK
|
|
|
|
exec.err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) writeCollectedObject() {
|
|
|
|
if ok := exec.writeCollectedHeader(); ok {
|
|
|
|
exec.writeObjectPayload(exec.collectedObject)
|
|
|
|
}
|
|
|
|
}
|