2020-10-21 09:24:02 +00:00
|
|
|
package policer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-10-16 11:21:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/container"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
2020-10-21 09:24:02 +00:00
|
|
|
headsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/head"
|
2020-10-21 11:50:48 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/replicator"
|
2022-05-20 11:31:55 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2020-10-21 09:24:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *Policer) processObject(ctx context.Context, addr *addressSDK.Address) {
|
2022-05-12 16:37:46 +00:00
|
|
|
idCnr, _ := addr.ContainerID()
|
|
|
|
|
|
|
|
cnr, err := p.cnrSrc.Get(&idCnr)
|
2020-10-21 09:24:02 +00:00
|
|
|
if err != nil {
|
|
|
|
p.log.Error("could not get container",
|
2022-05-12 16:37:46 +00:00
|
|
|
zap.Stringer("cid", idCnr),
|
2020-10-21 09:24:02 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2022-03-17 08:17:45 +00:00
|
|
|
if container.IsErrNotFound(err) {
|
2021-10-16 11:21:03 +00:00
|
|
|
prm := new(engine.InhumePrm)
|
|
|
|
prm.MarkAsGarbage(addr)
|
|
|
|
_, err := p.jobQueue.localStorage.Inhume(prm)
|
|
|
|
if err != nil {
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := addr.ObjectID()
|
2021-10-16 11:21:03 +00:00
|
|
|
p.log.Error("could not inhume object with missing container",
|
2022-05-12 16:37:46 +00:00
|
|
|
zap.Stringer("cid", idCnr),
|
|
|
|
zap.Stringer("oid", id),
|
2021-10-16 11:21:03 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
policy := cnr.PlacementPolicy()
|
2020-10-21 09:24:02 +00:00
|
|
|
|
|
|
|
nn, err := p.placementBuilder.BuildPlacement(addr, policy)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error("could not build placement vector for object",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
replicas := policy.Replicas()
|
2022-05-20 11:31:55 +00:00
|
|
|
c := &processPlacementContext{
|
|
|
|
Context: ctx,
|
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
|
|
|
for i := range nn {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2022-05-20 11:31:55 +00:00
|
|
|
p.processNodes(c, addr, nn[i], replicas[i].Count())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.needLocalCopy {
|
|
|
|
p.log.Info("redundant local object copy detected",
|
|
|
|
zap.Stringer("object", addr),
|
|
|
|
)
|
|
|
|
|
|
|
|
p.cbRedundantCopy(addr)
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 11:31:55 +00:00
|
|
|
type processPlacementContext struct {
|
|
|
|
context.Context
|
|
|
|
|
|
|
|
needLocalCopy bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policer) processNodes(ctx *processPlacementContext, addr *addressSDK.Address, nodes netmap.Nodes, shortage uint32) {
|
2021-09-24 15:19:54 +00:00
|
|
|
log := p.log.With(
|
|
|
|
zap.Stringer("object", addr),
|
|
|
|
)
|
|
|
|
|
2020-10-21 09:24:02 +00:00
|
|
|
prm := new(headsvc.RemoteHeadPrm).WithObjectAddress(addr)
|
|
|
|
|
2022-05-20 11:31:55 +00:00
|
|
|
for i := 0; shortage > 0 && i < len(nodes); i++ {
|
2020-10-21 09:24:02 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
if p.netmapKeys.IsLocalKey(nodes[i].PublicKey()) {
|
2022-05-20 11:31:55 +00:00
|
|
|
ctx.needLocalCopy = true
|
|
|
|
|
|
|
|
shortage--
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-05-20 11:31:55 +00:00
|
|
|
callCtx, cancel := context.WithTimeout(ctx, p.headTimeout)
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-05-20 11:31:55 +00:00
|
|
|
_, err := p.remoteHeader.Head(callCtx, prm.WithNodeInfo(nodes[i].NodeInfo))
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-05-20 11:31:55 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
if !client.IsErrObjectNotFound(err) {
|
2020-10-21 09:24:02 +00:00
|
|
|
if err != nil {
|
2022-05-20 11:31:55 +00:00
|
|
|
log.Error("receive object header to check policy compliance",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2020-10-21 09:24:02 +00:00
|
|
|
} else {
|
|
|
|
shortage--
|
|
|
|
}
|
2022-05-20 11:31:55 +00:00
|
|
|
|
|
|
|
continue
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes = append(nodes[:i], nodes[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
if shortage > 0 {
|
2021-12-22 16:19:31 +00:00
|
|
|
log.Debug("shortage of object copies detected",
|
2020-10-21 09:24:02 +00:00
|
|
|
zap.Uint32("shortage", shortage),
|
|
|
|
)
|
2020-10-21 11:50:48 +00:00
|
|
|
|
2022-05-20 11:31:55 +00:00
|
|
|
// TODO(@cthulhu-rider): replace to processObject in order to prevent repetitions
|
|
|
|
// in nodes for single object, see #1410
|
2021-11-10 11:47:52 +00:00
|
|
|
p.replicator.HandleTask(ctx, new(replicator.Task).
|
2020-10-21 11:50:48 +00:00
|
|
|
WithObjectAddress(addr).
|
|
|
|
WithNodes(nodes).
|
|
|
|
WithCopiesNumber(shortage),
|
|
|
|
)
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|