2020-10-21 09:24:02 +00:00
|
|
|
package policer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
|
|
|
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"
|
2020-10-21 09:24:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *Policer) processObject(ctx context.Context, addr *object.Address) {
|
2020-11-16 09:43:52 +00:00
|
|
|
cnr, err := p.cnrSrc.Get(addr.ContainerID())
|
2020-10-21 09:24:02 +00:00
|
|
|
if err != nil {
|
|
|
|
p.log.Error("could not get container",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2020-10-21 09:24:02 +00:00
|
|
|
|
|
|
|
for i := range nn {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
p.processNodes(ctx, addr, nn[i], replicas[i].Count())
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policer) processNodes(ctx context.Context, addr *object.Address, nodes netmap.Nodes, shortage uint32) {
|
|
|
|
prm := new(headsvc.RemoteHeadPrm).WithObjectAddress(addr)
|
2021-02-19 15:21:46 +00:00
|
|
|
redundantLocalCopy := false
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2021-02-19 15:21:46 +00:00
|
|
|
for i := 0; i < len(nodes); i++ {
|
2020-10-21 09:24:02 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
netAddr := nodes[i].Address()
|
2020-10-21 09:24:02 +00:00
|
|
|
|
|
|
|
log := p.log.With(zap.String("node", netAddr))
|
|
|
|
|
|
|
|
node, err := network.AddressFromString(netAddr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not parse network address")
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if network.IsLocalAddress(p.localAddrSrc, node) {
|
2021-02-19 15:21:46 +00:00
|
|
|
if shortage == 0 {
|
|
|
|
redundantLocalCopy = true
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
shortage--
|
|
|
|
}
|
|
|
|
} else if shortage > 0 {
|
2020-10-21 09:24:02 +00:00
|
|
|
callCtx, cancel := context.WithTimeout(ctx, p.headTimeout)
|
|
|
|
|
|
|
|
_, err = p.remoteHeader.Head(callCtx, prm.WithNodeAddress(node))
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// FIXME: this is a temporary solution to resolve 404 response from remote node
|
|
|
|
// We need to distinguish problem nodes from nodes without an object.
|
|
|
|
if strings.Contains(err.Error(), headsvc.ErrNotFound.Error()) {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
log.Error("could not receive object header",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2020-12-14 18:03:21 +00:00
|
|
|
|
|
|
|
continue
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
shortage--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = append(nodes[:i], nodes[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
if shortage > 0 {
|
|
|
|
p.log.Info("shortage of object copies detected",
|
|
|
|
zap.Uint32("shortage", shortage),
|
|
|
|
)
|
2020-10-21 11:50:48 +00:00
|
|
|
|
|
|
|
p.replicator.AddTask(new(replicator.Task).
|
|
|
|
WithObjectAddress(addr).
|
|
|
|
WithNodes(nodes).
|
|
|
|
WithCopiesNumber(shortage),
|
|
|
|
)
|
2021-02-19 15:21:46 +00:00
|
|
|
} else if redundantLocalCopy {
|
|
|
|
p.cbRedundantCopy(addr)
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|