2020-10-21 09:24:02 +00:00
|
|
|
package policer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-10 13:53:26 +00:00
|
|
|
"errors"
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine"
|
|
|
|
headsvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/head"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/replicator"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2020-10-21 09:24:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-10-17 12:03:55 +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)
|
|
|
|
}
|
|
|
|
|
2022-10-07 09:26:08 +00:00
|
|
|
func (n *nodeCache) set(node netmap.NodeInfo, val bool) {
|
|
|
|
(*n)[node.Hash()] = val
|
|
|
|
}
|
|
|
|
|
2022-10-07 09:19:31 +00:00
|
|
|
// submits storage node as a candidate to store the object replica in case of
|
|
|
|
// shortage.
|
|
|
|
func (n *nodeCache) submitReplicaCandidate(node netmap.NodeInfo) {
|
2022-10-07 09:26:08 +00:00
|
|
|
n.set(node, false)
|
2022-10-07 09:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// submits storage node as a current object replica holder.
|
|
|
|
func (n *nodeCache) submitReplicaHolder(node netmap.NodeInfo) {
|
2022-10-07 09:26:08 +00:00
|
|
|
n.set(node, true)
|
2022-10-07 09:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-10-07 09:26:08 +00:00
|
|
|
// SubmitSuccessfulReplication marks given storage node as a current object
|
|
|
|
// replica holder.
|
|
|
|
//
|
|
|
|
// SubmitSuccessfulReplication implements replicator.TaskResult.
|
|
|
|
func (n *nodeCache) SubmitSuccessfulReplication(node netmap.NodeInfo) {
|
|
|
|
n.submitReplicaHolder(node)
|
2022-06-09 19:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 13:48:44 +00:00
|
|
|
func (p *Policer) processObject(ctx context.Context, addrWithType objectcore.AddressWithType) {
|
|
|
|
addr := addrWithType.Address
|
2022-05-31 17:00:41 +00:00
|
|
|
idCnr := addr.Container()
|
2022-11-12 13:48:44 +00:00
|
|
|
idObj := addr.Object()
|
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
|
2022-11-12 13:48:44 +00:00
|
|
|
prm.MarkAsGarbage(addrWithType.Address)
|
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-11-12 13:48:44 +00:00
|
|
|
zap.Stringer("oid", idObj),
|
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()
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-11-12 13:48:44 +00:00
|
|
|
nn, err := p.placementBuilder.BuildPlacement(idCnr, &idObj, 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-11-12 13:48:44 +00:00
|
|
|
p.processNodes(c, addrWithType, nn[i], policy.ReplicaNumberByIndex(i), checkedNodes)
|
2022-05-20 11:31:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 13:47:35 +00:00
|
|
|
if !c.needLocalCopy && c.removeLocalCopy {
|
2022-05-20 11:31:55 +00:00
|
|
|
p.log.Info("redundant local object copy detected",
|
|
|
|
zap.Stringer("object", addr),
|
|
|
|
)
|
|
|
|
|
|
|
|
p.cbRedundantCopy(addr)
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:39:44 +00:00
|
|
|
// nolint: containedctx
|
2022-05-20 11:31:55 +00:00
|
|
|
type processPlacementContext struct {
|
|
|
|
context.Context
|
|
|
|
|
2023-03-06 13:47:35 +00:00
|
|
|
// needLocalCopy is true if the current node must store an object according to the storage policy.
|
2022-05-20 11:31:55 +00:00
|
|
|
needLocalCopy bool
|
2023-03-06 13:47:35 +00:00
|
|
|
// removeLocalCopy is true if all copies are stored according to the storage policy
|
|
|
|
// and the current node doesn't need to store an object.
|
|
|
|
removeLocalCopy bool
|
2022-05-20 11:31:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 07:01:27 +00:00
|
|
|
// nolint: funlen
|
2022-11-12 13:48:44 +00:00
|
|
|
func (p *Policer) processNodes(ctx *processPlacementContext, addrWithType objectcore.AddressWithType,
|
2022-10-07 09:19:31 +00:00
|
|
|
nodes []netmap.NodeInfo, shortage uint32, checkedNodes *nodeCache) {
|
2022-11-12 13:48:44 +00:00
|
|
|
addr := addrWithType.Address
|
|
|
|
typ := addrWithType.Type
|
2020-10-21 09:24:02 +00:00
|
|
|
prm := new(headsvc.RemoteHeadPrm).WithObjectAddress(addr)
|
|
|
|
|
2022-10-26 14:10:14 +00:00
|
|
|
// Number of copies that are stored on maintenance nodes.
|
|
|
|
var uncheckedCopies int
|
|
|
|
|
2022-10-07 09:33:37 +00:00
|
|
|
handleMaintenance := func(node netmap.NodeInfo) {
|
|
|
|
// consider remote nodes under maintenance as problem OK. Such
|
|
|
|
// nodes MAY not respond with object, however, this is how we
|
|
|
|
// prevent spam with new replicas.
|
2022-10-26 14:10:14 +00:00
|
|
|
// However, additional copies should not be removed in this case,
|
|
|
|
// because we can remove the only copy this way.
|
2022-10-07 09:33:37 +00:00
|
|
|
checkedNodes.submitReplicaHolder(node)
|
|
|
|
shortage--
|
2022-10-26 14:10:14 +00:00
|
|
|
uncheckedCopies++
|
2022-10-07 09:33:37 +00:00
|
|
|
|
|
|
|
p.log.Debug("consider node under maintenance as OK",
|
2022-10-11 12:49:34 +00:00
|
|
|
zap.String("node", netmap.StringifyPublicKey(node)),
|
2022-10-07 09:33:37 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-12 13:48:44 +00:00
|
|
|
if typ == object.TypeLock {
|
|
|
|
// all nodes of a container must store the `LOCK` objects
|
|
|
|
// for correct object removal protection:
|
|
|
|
// - `LOCK` objects are broadcast on their PUT requests;
|
|
|
|
// - `LOCK` object removal is a prohibited action in the GC.
|
|
|
|
shortage = uint32(len(nodes))
|
|
|
|
}
|
|
|
|
|
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-10-07 09:33:37 +00:00
|
|
|
} else if nodes[i].IsMaintenance() {
|
|
|
|
handleMaintenance(nodes[i])
|
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
|
|
|
|
2022-10-10 13:53:26 +00:00
|
|
|
if isClientErrMaintenance(err) {
|
|
|
|
handleMaintenance(nodes[i])
|
|
|
|
} else 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)
|
2022-10-26 14:10:14 +00:00
|
|
|
} else if uncheckedCopies > 0 {
|
|
|
|
// If we have more copies than needed, but some of them are from the maintenance nodes,
|
|
|
|
// save the local copy.
|
|
|
|
p.log.Debug("some of the copies are stored on nodes under maintenance, save local copy",
|
|
|
|
zap.Int("count", uncheckedCopies))
|
2023-03-06 13:47:35 +00:00
|
|
|
} else if uncheckedCopies == 0 {
|
|
|
|
// Safe to remove: checked all copies, shortage == 0.
|
|
|
|
ctx.removeLocalCopy = true
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 13:53:26 +00:00
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// isClientErrMaintenance checks if err corresponds to FrostFS status return
|
2022-10-10 13:53:26 +00:00
|
|
|
// which tells that node is currently under maintenance. Supports wrapped
|
|
|
|
// errors.
|
|
|
|
//
|
2023-02-05 15:59:38 +00:00
|
|
|
// Similar to client.IsErr___ errors, consider replacing to FrostFS SDK.
|
2022-10-10 13:53:26 +00:00
|
|
|
func isClientErrMaintenance(err error) bool {
|
|
|
|
switch unwrapErr(err).(type) {
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
case
|
|
|
|
apistatus.NodeUnderMaintenance,
|
|
|
|
*apistatus.NodeUnderMaintenance:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unwrapErr unwraps error using errors.Unwrap.
|
|
|
|
func unwrapErr(err error) error {
|
|
|
|
for e := errors.Unwrap(err); e != nil; e = errors.Unwrap(err) {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|