2020-09-30 10:52:14 +00:00
|
|
|
package deletesvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-10-01 10:54:18 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-09-30 10:52:14 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-10-01 10:54:18 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-09-30 10:52:14 +00:00
|
|
|
headsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/head"
|
|
|
|
putsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/put"
|
|
|
|
objutil "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-10-05 09:42:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
2020-11-23 11:19:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2020-09-30 10:52:14 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-11-23 11:19:49 +00:00
|
|
|
"go.uber.org/zap"
|
2020-09-30 10:52:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
*cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
2020-10-01 10:54:18 +00:00
|
|
|
type RelationHeader interface {
|
2020-10-14 18:14:54 +00:00
|
|
|
HeadRelation(context.Context, *objectSDK.Address, *objutil.CommonPrm) (*object.Object, error)
|
2020-10-01 10:54:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 10:52:14 +00:00
|
|
|
type cfg struct {
|
|
|
|
ownerID *owner.ID
|
|
|
|
|
|
|
|
keyStorage *objutil.KeyStorage
|
|
|
|
|
|
|
|
putSvc *putsvc.Service
|
|
|
|
|
|
|
|
headSvc *headsvc.Service
|
2020-10-01 10:54:18 +00:00
|
|
|
|
|
|
|
hdrLinking RelationHeader
|
2020-11-23 11:19:49 +00:00
|
|
|
|
|
|
|
log *logger.Logger
|
2020-09-30 10:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
2020-11-23 11:19:49 +00:00
|
|
|
return &cfg{
|
|
|
|
log: zap.L(),
|
|
|
|
}
|
2020-09-30 10:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewService(opts ...Option) *Service {
|
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
cfg: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Delete(ctx context.Context, prm *Prm) (*Response, error) {
|
|
|
|
ownerID := s.ownerID
|
|
|
|
if token := prm.common.SessionToken(); token != nil {
|
|
|
|
ownerID = token.OwnerID()
|
|
|
|
}
|
|
|
|
|
|
|
|
if ownerID == nil {
|
|
|
|
return nil, errors.Errorf("(%T) missing owner identifier", s)
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
addrList, err := s.getRelations(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not get object relations", s)
|
2020-09-30 10:52:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
content := object.NewTombstoneContent()
|
|
|
|
content.SetAddressList(addrList...)
|
2020-10-01 10:54:18 +00:00
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
data, err := content.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not marshal tombstone content", s)
|
|
|
|
}
|
2020-10-01 10:54:18 +00:00
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
r, err := s.putSvc.Put(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not open put stream", s)
|
|
|
|
}
|
|
|
|
|
2020-10-05 09:42:45 +00:00
|
|
|
// `WithoutSuccessTracking` option broadcast message to all container nodes.
|
|
|
|
// For now there is no better solution to distributed tombstones with
|
|
|
|
// content address storage (CAS) and one tombstone for several split
|
|
|
|
// objects.
|
2020-10-01 17:15:28 +00:00
|
|
|
if err := r.Init(new(putsvc.PutInitPrm).
|
2020-11-16 09:43:52 +00:00
|
|
|
WithObject(newTombstone(ownerID, prm.addr.ContainerID())).
|
2020-10-05 09:42:45 +00:00
|
|
|
WithCommonPrm(prm.common).
|
|
|
|
WithTraverseOption(placement.WithoutSuccessTracking()), // broadcast tombstone, maybe one
|
2020-10-01 17:15:28 +00:00
|
|
|
); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not initialize tombstone stream", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.SendChunk(new(putsvc.PutChunkPrm).
|
|
|
|
WithChunk(data),
|
|
|
|
); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not send tombstone payload", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := r.Close(); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not close tombstone stream", s)
|
2020-09-30 10:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new(Response), nil
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
func (s *Service) getRelations(ctx context.Context, prm *Prm) ([]*objectSDK.Address, error) {
|
|
|
|
var res []*objectSDK.Address
|
|
|
|
|
2020-10-14 18:14:54 +00:00
|
|
|
if linking, err := s.hdrLinking.HeadRelation(ctx, prm.addr, prm.common); err != nil {
|
2020-11-16 09:43:52 +00:00
|
|
|
cid := prm.addr.ContainerID()
|
2020-10-01 17:15:28 +00:00
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
for prev := prm.addr.ObjectID(); prev != nil; {
|
2020-10-01 17:15:28 +00:00
|
|
|
addr := objectSDK.NewAddress()
|
|
|
|
addr.SetObjectID(prev)
|
|
|
|
addr.SetContainerID(cid)
|
|
|
|
|
|
|
|
headResult, err := s.headSvc.Head(ctx, new(headsvc.Prm).
|
|
|
|
WithAddress(addr).
|
|
|
|
WithCommonPrm(prm.common),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not receive Head result", s)
|
|
|
|
}
|
2020-09-30 10:52:14 +00:00
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
hdr := headResult.Header()
|
2020-11-16 09:43:52 +00:00
|
|
|
id := hdr.ID()
|
|
|
|
prev = hdr.PreviousID()
|
2020-09-30 10:52:14 +00:00
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
addr.SetObjectID(id)
|
2020-09-30 10:52:14 +00:00
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
res = append(res, addr)
|
2020-09-30 10:52:14 +00:00
|
|
|
}
|
2020-10-01 17:15:28 +00:00
|
|
|
} else {
|
2020-11-16 09:43:52 +00:00
|
|
|
childList := linking.Children()
|
2020-11-09 12:47:32 +00:00
|
|
|
res = make([]*objectSDK.Address, 0, len(childList)+2) // 1 for parent, 1 for linking
|
2020-09-30 10:52:14 +00:00
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
for i := range childList {
|
|
|
|
addr := objectSDK.NewAddress()
|
|
|
|
addr.SetObjectID(childList[i])
|
2020-11-16 09:43:52 +00:00
|
|
|
addr.SetContainerID(prm.addr.ContainerID())
|
2020-10-01 17:15:28 +00:00
|
|
|
|
|
|
|
res = append(res, addr)
|
|
|
|
}
|
2020-09-30 10:52:14 +00:00
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
addr := objectSDK.NewAddress()
|
2020-11-16 09:43:52 +00:00
|
|
|
addr.SetObjectID(linking.ID())
|
|
|
|
addr.SetContainerID(prm.addr.ContainerID())
|
2020-09-30 10:52:14 +00:00
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
res = append(res, addr)
|
2020-09-30 10:52:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 12:47:32 +00:00
|
|
|
res = append(res, prm.addr)
|
|
|
|
|
2020-10-01 17:15:28 +00:00
|
|
|
return res, nil
|
2020-09-30 10:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func WithOwnerID(v *owner.ID) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.ownerID = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithKeyStorage(v *objutil.KeyStorage) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.keyStorage = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithPutService(v *putsvc.Service) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.putSvc = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WitHeadService(v *headsvc.Service) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.headSvc = v
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 10:54:18 +00:00
|
|
|
|
|
|
|
func WithLinkingHeader(v RelationHeader) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.hdrLinking = v
|
|
|
|
}
|
|
|
|
}
|
2020-11-23 11:19:49 +00:00
|
|
|
|
|
|
|
func WithLogger(l *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = l
|
|
|
|
}
|
|
|
|
}
|