2022-09-13 10:28:08 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2022-09-19 10:57:59 +00:00
|
|
|
"bytes"
|
2022-09-13 10:28:08 +00:00
|
|
|
"context"
|
2022-09-19 10:57:59 +00:00
|
|
|
"crypto/sha256"
|
2024-02-06 14:34:32 +00:00
|
|
|
"encoding/hex"
|
2022-09-19 10:57:59 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-09-13 10:28:08 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine"
|
2024-02-06 14:34:32 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/pilorama"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
|
2024-05-16 09:11:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/server/ctrlmessage"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/replicator"
|
2024-02-06 14:34:32 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/tree"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-09-19 10:57:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-09-13 10:28:08 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2024-03-11 14:55:50 +00:00
|
|
|
var errFailedToBuildListOfContainerNodes = errors.New("can't build a list of container nodes")
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (s *Server) EvacuateShard(ctx context.Context, req *control.EvacuateShardRequest) (*control.EvacuateShardResponse, error) {
|
2022-09-13 10:28:08 +00:00
|
|
|
err := s.isValidRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-02-05 12:42:30 +00:00
|
|
|
prm := engine.EvacuateShardPrm{
|
2024-02-05 14:48:43 +00:00
|
|
|
ShardID: s.getShardIDList(req.GetBody().GetShard_ID()),
|
|
|
|
IgnoreErrors: req.GetBody().GetIgnoreErrors(),
|
2024-02-06 14:34:32 +00:00
|
|
|
ObjectsHandler: s.replicateObject,
|
2024-02-05 14:48:43 +00:00
|
|
|
Scope: engine.EvacuateScopeObjects,
|
2024-02-05 12:42:30 +00:00
|
|
|
}
|
2022-09-13 10:28:08 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := s.s.Evacuate(ctx, prm)
|
2022-09-13 10:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &control.EvacuateShardResponse{
|
|
|
|
Body: &control.EvacuateShardResponse_Body{
|
2024-02-05 14:48:43 +00:00
|
|
|
Count: uint32(res.ObjectsEvacuated()),
|
2022-09-13 10:28:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-16 09:11:57 +00:00
|
|
|
err = ctrlmessage.Sign(s.key, resp)
|
2022-09-13 10:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2022-09-19 10:57:59 +00:00
|
|
|
|
2024-07-03 06:55:04 +00:00
|
|
|
func (s *Server) replicateObject(ctx context.Context, addr oid.Address, obj *objectSDK.Object) (bool, error) {
|
2022-09-19 10:57:59 +00:00
|
|
|
cid, ok := obj.ContainerID()
|
|
|
|
if !ok {
|
|
|
|
// Return nil to prevent situations where a shard can't be evacuated
|
|
|
|
// because of a single bad/corrupted object.
|
2024-07-03 06:55:04 +00:00
|
|
|
return false, nil
|
2022-09-19 10:57:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 14:34:32 +00:00
|
|
|
nodes, err := s.getContainerNodes(cid)
|
2022-09-19 10:57:59 +00:00
|
|
|
if err != nil {
|
2024-07-03 06:55:04 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return false, nil
|
2022-09-19 10:57:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 14:34:32 +00:00
|
|
|
var res replicatorResult
|
|
|
|
task := replicator.Task{
|
|
|
|
NumCopies: 1,
|
|
|
|
Addr: addr,
|
|
|
|
Obj: obj,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
2024-05-13 13:50:21 +00:00
|
|
|
s.replicator.HandleReplicationTask(ctx, task, &res)
|
2024-02-06 14:34:32 +00:00
|
|
|
|
|
|
|
if res.count == 0 {
|
2024-07-03 06:55:04 +00:00
|
|
|
return false, errors.New("object was not replicated")
|
2024-02-06 14:34:32 +00:00
|
|
|
}
|
2024-07-03 06:55:04 +00:00
|
|
|
return true, nil
|
2024-02-06 14:34:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 08:47:50 +00:00
|
|
|
func (s *Server) replicateTree(ctx context.Context, contID cid.ID, treeID string, forest pilorama.Forest) (bool, string, error) {
|
2024-02-06 14:34:32 +00:00
|
|
|
nodes, err := s.getContainerNodes(contID)
|
2022-09-19 10:57:59 +00:00
|
|
|
if err != nil {
|
2024-07-03 08:47:50 +00:00
|
|
|
return false, "", err
|
2024-02-06 14:34:32 +00:00
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
2024-07-03 08:47:50 +00:00
|
|
|
return false, "", nil
|
2024-02-06 14:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
err = s.replicateTreeToNode(ctx, forest, contID, treeID, node)
|
|
|
|
if err == nil {
|
2024-07-03 08:47:50 +00:00
|
|
|
return true, hex.EncodeToString(node.PublicKey()), nil
|
2024-02-06 14:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-03 08:47:50 +00:00
|
|
|
return false, "", err
|
2024-02-06 14:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) replicateTreeToNode(ctx context.Context, forest pilorama.Forest, contID cid.ID, treeID string, node netmap.NodeInfo) error {
|
|
|
|
rawCID := make([]byte, sha256.Size)
|
|
|
|
contID.Encode(rawCID)
|
|
|
|
|
|
|
|
var height uint64
|
|
|
|
for {
|
|
|
|
op, err := forest.TreeGetOpLog(ctx, contID, treeID, height)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if op.Time == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &tree.ApplyRequest{
|
|
|
|
Body: &tree.ApplyRequest_Body{
|
|
|
|
ContainerId: rawCID,
|
|
|
|
TreeId: treeID,
|
|
|
|
Operation: &tree.LogMove{
|
|
|
|
ParentId: op.Parent,
|
|
|
|
Meta: op.Meta.Bytes(),
|
|
|
|
ChildId: op.Child,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tree.SignMessage(req, s.key)
|
|
|
|
if err != nil {
|
2024-05-16 09:11:57 +00:00
|
|
|
return fmt.Errorf("can't message apply request: %w", err)
|
2024-02-06 14:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.treeService.ReplicateTreeOp(ctx, node, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
height = op.Time + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getContainerNodes(contID cid.ID) ([]netmap.NodeInfo, error) {
|
|
|
|
nm, err := s.netMapSrc.GetNetMap(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := s.cnrSrc.Get(contID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-19 10:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binCnr := make([]byte, sha256.Size)
|
2024-02-06 14:34:32 +00:00
|
|
|
contID.Encode(binCnr)
|
2022-09-19 10:57:59 +00:00
|
|
|
|
|
|
|
ns, err := nm.ContainerNodes(c.Value.PlacementPolicy(), binCnr)
|
|
|
|
if err != nil {
|
2024-03-11 14:55:50 +00:00
|
|
|
return nil, errFailedToBuildListOfContainerNodes
|
2022-09-19 10:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes := placement.FlattenNodes(ns)
|
|
|
|
bs := (*keys.PublicKey)(&s.key.PublicKey).Bytes()
|
|
|
|
for i := 0; i < len(nodes); i++ {
|
|
|
|
if bytes.Equal(nodes[i].PublicKey(), bs) {
|
|
|
|
copy(nodes[i:], nodes[i+1:])
|
|
|
|
nodes = nodes[:len(nodes)-1]
|
|
|
|
}
|
|
|
|
}
|
2024-02-06 14:34:32 +00:00
|
|
|
return nodes, nil
|
2022-09-19 10:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type replicatorResult struct {
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitSuccessfulReplication implements the replicator.TaskResult interface.
|
2022-10-07 09:26:08 +00:00
|
|
|
func (r *replicatorResult) SubmitSuccessfulReplication(_ netmap.NodeInfo) {
|
2022-09-19 10:57:59 +00:00
|
|
|
r.count++
|
|
|
|
}
|