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-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-10-21 09:24:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-10-07 09:19:31 +00:00
|
|
|
// tracks Policer's check progress
|
2022-06-09 19:11:35 +00:00
|
|
|
type nodeCache map[uint64]bool
|
|
|
|
|
2022-10-07 09:19:31 +00:00
|
|
|
func newNodeCache() *nodeCache {
|
|
|
|
m := make(map[uint64]bool)
|
|
|
|
return (*nodeCache)(&m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// submits storage node as a candidate to store the object replica in case of
|
|
|
|
// shortage.
|
|
|
|
func (n *nodeCache) submitReplicaCandidate(node netmap.NodeInfo) {
|
|
|
|
(*n)[node.Hash()] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// submits storage node as a current object replica holder.
|
|
|
|
func (n *nodeCache) submitReplicaHolder(node netmap.NodeInfo) {
|
|
|
|
(*n)[node.Hash()] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// processStatus returns current processing status of the storage node
|
|
|
|
//
|
|
|
|
// >0 if node does not currently hold the object
|
|
|
|
// 0 if node already holds the object
|
|
|
|
// <0 if node has not been processed yet
|
|
|
|
func (n *nodeCache) processStatus(node netmap.NodeInfo) int8 {
|
|
|
|
val, ok := (*n)[node.Hash()]
|
|
|
|
if !ok {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if val {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nodeCache) SubmitSuccessfulReplication(id uint64) {
|
|
|
|
(*n)[id] = true
|
2022-06-09 19:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (p *Policer) processObject(ctx context.Context, addr oid.Address) {
|
|
|
|
idCnr := addr.Container()
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
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) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var prm engine.InhumePrm
|
2021-10-16 11:21:03 +00:00
|
|
|
prm.MarkAsGarbage(addr)
|
2022-06-14 17:48:33 +00:00
|
|
|
prm.WithForceRemoval()
|
|
|
|
|
2021-10-16 11:21:03 +00:00
|
|
|
_, err := p.jobQueue.localStorage.Inhume(prm)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error("could not inhume object with missing container",
|
2022-05-12 16:37:46 +00:00
|
|
|
zap.Stringer("cid", idCnr),
|
2022-05-31 17:00:41 +00:00
|
|
|
zap.Stringer("oid", addr.Object()),
|
2021-10-16 11:21:03 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
policy := cnr.Value.PlacementPolicy()
|
2022-05-31 17:00:41 +00:00
|
|
|
obj := addr.Object()
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
nn, err := p.placementBuilder.BuildPlacement(idCnr, &obj, policy)
|
2020-10-21 09:24:02 +00:00
|
|
|
if err != nil {
|
|
|
|
p.log.Error("could not build placement vector for object",
|
2022-10-06 15:49:50 +00:00
|
|
|
zap.Stringer("cid", idCnr),
|
2020-10-21 09:24:02 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 11:31:55 +00:00
|
|
|
c := &processPlacementContext{
|
|
|
|
Context: ctx,
|
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-06-09 19:11:35 +00:00
|
|
|
var numOfContainerNodes int
|
|
|
|
for i := range nn {
|
|
|
|
numOfContainerNodes += len(nn[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// cached info about already checked nodes
|
2022-10-07 09:19:31 +00:00
|
|
|
checkedNodes := newNodeCache()
|
2022-06-09 19:11:35 +00:00
|
|
|
|
2020-10-21 09:24:02 +00:00
|
|
|
for i := range nn {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
p.processNodes(c, addr, nn[i], policy.ReplicaNumberByIndex(i), checkedNodes)
|
2022-05-20 11:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-09 19:11:35 +00:00
|
|
|
func (p *Policer) processNodes(ctx *processPlacementContext, addr oid.Address,
|
2022-10-07 09:19:31 +00:00
|
|
|
nodes []netmap.NodeInfo, shortage uint32, checkedNodes *nodeCache) {
|
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--
|
2022-05-30 05:19:59 +00:00
|
|
|
} else {
|
2022-10-07 09:19:31 +00:00
|
|
|
if status := checkedNodes.processStatus(nodes[i]); status >= 0 {
|
|
|
|
if status == 0 {
|
2022-06-09 19:11:35 +00:00
|
|
|
// node already contains replica, no need to replicate
|
|
|
|
nodes = append(nodes[:i], nodes[i+1:]...)
|
|
|
|
i--
|
|
|
|
shortage--
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-30 05:19:59 +00:00
|
|
|
callCtx, cancel := context.WithTimeout(ctx, p.headTimeout)
|
2022-05-20 11:31:55 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
_, err := p.remoteHeader.Head(callCtx, prm.WithNodeInfo(nodes[i]))
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-05-30 05:19:59 +00:00
|
|
|
cancel()
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-05-30 05:19:59 +00:00
|
|
|
if client.IsErrObjectNotFound(err) {
|
2022-10-07 09:19:31 +00:00
|
|
|
checkedNodes.submitReplicaCandidate(nodes[i])
|
2022-05-30 05:19:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-05-20 11:31:55 +00:00
|
|
|
|
2020-10-21 09:24:02 +00:00
|
|
|
if err != nil {
|
2022-06-02 17:20:27 +00:00
|
|
|
p.log.Error("receive object header to check policy compliance",
|
|
|
|
zap.Stringer("object", addr),
|
2022-05-20 11:31:55 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2020-10-21 09:24:02 +00:00
|
|
|
} else {
|
|
|
|
shortage--
|
2022-10-07 09:19:31 +00:00
|
|
|
checkedNodes.submitReplicaHolder(nodes[i])
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = append(nodes[:i], nodes[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
if shortage > 0 {
|
2022-06-02 17:20:27 +00:00
|
|
|
p.log.Debug("shortage of object copies detected",
|
|
|
|
zap.Stringer("object", addr),
|
2020-10-21 09:24:02 +00:00
|
|
|
zap.Uint32("shortage", shortage),
|
|
|
|
)
|
2020-10-21 11:50:48 +00:00
|
|
|
|
2022-09-19 11:01:19 +00:00
|
|
|
var task replicator.Task
|
|
|
|
task.SetObjectAddress(addr)
|
|
|
|
task.SetNodes(nodes)
|
|
|
|
task.SetCopiesNumber(shortage)
|
|
|
|
|
|
|
|
p.replicator.HandleTask(ctx, task, checkedNodes)
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|