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"
|
|
|
|
"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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/replicator"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
var prm engine.EvacuateShardPrm
|
2022-10-10 18:14:12 +00:00
|
|
|
prm.WithShardIDList(s.getShardIDList(req.GetBody().GetShard_ID()))
|
2022-09-13 10:28:08 +00:00
|
|
|
prm.WithIgnoreErrors(req.GetBody().GetIgnoreErrors())
|
2022-09-19 10:57:59 +00:00
|
|
|
prm.WithFaultHandler(s.replicate)
|
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{
|
2023-05-04 10:58:26 +00:00
|
|
|
Count: uint32(res.Evacuated()),
|
2022-09-13 10:28:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = SignMessage(s.key, resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
2022-09-19 10:57:59 +00:00
|
|
|
|
2023-05-02 11:16:13 +00:00
|
|
|
func (s *Server) replicate(ctx context.Context, addr oid.Address, obj *objectSDK.Object) 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.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nm, err := s.netMapSrc.GetNetMap(0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := s.cnrSrc.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
binCnr := make([]byte, sha256.Size)
|
|
|
|
cid.Encode(binCnr)
|
|
|
|
|
|
|
|
ns, err := nm.ContainerNodes(c.Value.PlacementPolicy(), binCnr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't build a list of container nodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var res replicatorResult
|
2023-06-29 09:13:01 +00:00
|
|
|
task := replicator.Task{
|
|
|
|
NumCopies: 1,
|
|
|
|
Addr: addr,
|
|
|
|
Obj: obj,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
2023-05-02 11:16:13 +00:00
|
|
|
s.replicator.HandleTask(ctx, task, &res)
|
2022-09-19 10:57:59 +00:00
|
|
|
|
|
|
|
if res.count == 0 {
|
|
|
|
return errors.New("object was not replicated")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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++
|
|
|
|
}
|