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-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
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/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"
|
2023-04-04 12:29:12 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-10-21 09:24:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Error(logs.PolicerCouldNotGetContainer,
|
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) {
|
2023-06-29 09:13:01 +00:00
|
|
|
err := p.buryFn(ctx, addrWithType.Address)
|
2021-10-16 11:21:03 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Error(logs.PolicerCouldNotInhumeObjectWithMissingContainer,
|
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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Error(logs.PolicerCouldNotBuildPlacementVectorForObject,
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-04 11:57:44 +00:00
|
|
|
c := &placementRequirements{}
|
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:
|
|
|
|
}
|
|
|
|
|
2023-04-04 11:57:44 +00:00
|
|
|
p.processNodes(ctx, 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 {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Info(logs.PolicerRedundantLocalObjectCopyDetected,
|
2022-05-20 11:31:55 +00:00
|
|
|
zap.Stringer("object", addr),
|
|
|
|
)
|
|
|
|
|
2023-04-04 11:40:01 +00:00
|
|
|
p.cbRedundantCopy(ctx, addr)
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 11:57:44 +00:00
|
|
|
type placementRequirements struct {
|
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-04-04 11:57:44 +00:00
|
|
|
func (p *Policer) processNodes(ctx context.Context, requirements *placementRequirements, addrWithType objectcore.AddressWithType,
|
2023-06-29 09:13:01 +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
|
|
|
|
2022-10-26 14:10:14 +00:00
|
|
|
// Number of copies that are stored on maintenance nodes.
|
|
|
|
var uncheckedCopies int
|
|
|
|
|
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()) {
|
2023-04-04 11:57:44 +00:00
|
|
|
requirements.needLocalCopy = true
|
2022-05-20 11:31:55 +00:00
|
|
|
|
|
|
|
shortage--
|
2022-10-07 09:33:37 +00:00
|
|
|
} else if nodes[i].IsMaintenance() {
|
2023-04-04 12:29:12 +00:00
|
|
|
shortage, uncheckedCopies = p.handleMaintenance(nodes[i], checkedNodes, shortage, uncheckedCopies)
|
2022-05-30 05:19:59 +00:00
|
|
|
} else {
|
2023-06-29 09:13:01 +00:00
|
|
|
if status := checkedNodes.processStatus(nodes[i]); status.Processed() {
|
|
|
|
if status == nodeHoldsObject {
|
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
|
|
|
|
2023-06-29 09:13:01 +00:00
|
|
|
_, err := p.remoteHeader(callCtx, nodes[i], addr)
|
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) {
|
2023-04-04 12:29:12 +00:00
|
|
|
shortage, uncheckedCopies = p.handleMaintenance(nodes[i], checkedNodes, shortage, uncheckedCopies)
|
2022-10-10 13:53:26 +00:00
|
|
|
} else if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Error(logs.PolicerReceiveObjectHeaderToCheckPolicyCompliance,
|
2022-06-02 17:20:27 +00:00
|
|
|
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--
|
|
|
|
}
|
|
|
|
|
2023-04-04 12:29:12 +00:00
|
|
|
p.handleProcessNodesResult(ctx, addr, requirements, nodes, checkedNodes, shortage, uncheckedCopies)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleMaintenance handles node in maintenance mode and returns new shortage and uncheckedCopies values
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
// However, additional copies should not be removed in this case,
|
|
|
|
// because we can remove the only copy this way.
|
2023-06-29 09:13:01 +00:00
|
|
|
func (p *Policer) handleMaintenance(node netmap.NodeInfo, checkedNodes nodeCache, shortage uint32, uncheckedCopies int) (uint32, int) {
|
2023-04-04 12:29:12 +00:00
|
|
|
checkedNodes.submitReplicaHolder(node)
|
|
|
|
shortage--
|
|
|
|
uncheckedCopies++
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Debug(logs.PolicerConsiderNodeUnderMaintenanceAsOK,
|
2023-04-04 12:29:12 +00:00
|
|
|
zap.String("node", netmap.StringifyPublicKey(node)),
|
|
|
|
)
|
|
|
|
return shortage, uncheckedCopies
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policer) handleProcessNodesResult(ctx context.Context, addr oid.Address, requirements *placementRequirements,
|
2023-06-29 09:13:01 +00:00
|
|
|
nodes []netmap.NodeInfo, checkedNodes nodeCache, shortage uint32, uncheckedCopies int) {
|
|
|
|
switch {
|
|
|
|
case shortage > 0:
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Debug(logs.PolicerShortageOfObjectCopiesDetected,
|
2022-06-02 17:20:27 +00:00
|
|
|
zap.Stringer("object", addr),
|
2020-10-21 09:24:02 +00:00
|
|
|
zap.Uint32("shortage", shortage),
|
|
|
|
)
|
2020-10-21 11:50:48 +00:00
|
|
|
|
2023-06-29 09:13:01 +00:00
|
|
|
task := replicator.Task{
|
|
|
|
NumCopies: shortage,
|
|
|
|
Addr: addr,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
2022-09-19 11:01:19 +00:00
|
|
|
|
|
|
|
p.replicator.HandleTask(ctx, task, checkedNodes)
|
2023-06-29 09:13:01 +00:00
|
|
|
|
|
|
|
case uncheckedCopies > 0:
|
2022-10-26 14:10:14 +00:00
|
|
|
// If we have more copies than needed, but some of them are from the maintenance nodes,
|
|
|
|
// save the local copy.
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Debug(logs.PolicerSomeOfTheCopiesAreStoredOnNodesUnderMaintenance,
|
2022-10-26 14:10:14 +00:00
|
|
|
zap.Int("count", uncheckedCopies))
|
2023-06-29 09:13:01 +00:00
|
|
|
|
|
|
|
case uncheckedCopies == 0:
|
2023-03-06 13:47:35 +00:00
|
|
|
// Safe to remove: checked all copies, shortage == 0.
|
2023-04-04 11:57:44 +00:00
|
|
|
requirements.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
|
|
|
|
}
|