2021-06-09 15:40:02 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2023-05-03 13:19:46 +00:00
|
|
|
"bytes"
|
2021-06-09 15:40:02 +00:00
|
|
|
"context"
|
2023-04-25 12:09:20 +00:00
|
|
|
"fmt"
|
2021-06-09 15:40:02 +00:00
|
|
|
|
2023-10-16 15:15:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/container"
|
2023-05-03 13:19:46 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
2023-03-07 13:38:26 +00:00
|
|
|
control "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir"
|
2023-10-16 15:15:04 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-06-09 15:40:02 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HealthCheck returns health status of the local IR node.
|
|
|
|
//
|
|
|
|
// If request is not signed with a key from white list, permission error returns.
|
|
|
|
func (s *Server) HealthCheck(_ context.Context, req *control.HealthCheckRequest) (*control.HealthCheckResponse, error) {
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(control.HealthCheckResponse)
|
|
|
|
|
|
|
|
body := new(control.HealthCheckResponse_Body)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetHealthStatus(s.prm.healthChecker.HealthStatus())
|
|
|
|
|
2023-04-25 12:09:20 +00:00
|
|
|
if err := SignMessage(&s.prm.key.PrivateKey, resp); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TickEpoch forces a new epoch.
|
|
|
|
//
|
|
|
|
// If request is not signed with a key from white list, permission error returns.
|
|
|
|
func (s *Server) TickEpoch(_ context.Context, req *control.TickEpochRequest) (*control.TickEpochResponse, error) {
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(control.TickEpochResponse)
|
|
|
|
resp.SetBody(new(control.TickEpochResponse_Body))
|
|
|
|
|
|
|
|
epoch, err := s.netmapClient.Epoch()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting current epoch: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-11-13 14:22:31 +00:00
|
|
|
vub, err := s.netmapClient.NewEpochControl(epoch+1, req.GetBody().GetVub())
|
2023-11-08 09:05:03 +00:00
|
|
|
if err != nil {
|
2023-04-25 12:09:20 +00:00
|
|
|
return nil, fmt.Errorf("forcing new epoch: %w", err)
|
|
|
|
}
|
2023-11-08 09:05:03 +00:00
|
|
|
resp.Body.Vub = vub
|
2023-04-25 12:09:20 +00:00
|
|
|
|
2021-06-09 15:49:10 +00:00
|
|
|
if err := SignMessage(&s.prm.key.PrivateKey, resp); err != nil {
|
2021-06-09 15:40:02 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
2023-05-03 13:19:46 +00:00
|
|
|
|
|
|
|
// RemoveNode forces a node removal.
|
|
|
|
//
|
|
|
|
// If request is not signed with a key from white list, permission error returns.
|
|
|
|
func (s *Server) RemoveNode(_ context.Context, req *control.RemoveNodeRequest) (*control.RemoveNodeResponse, error) {
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(control.RemoveNodeResponse)
|
|
|
|
resp.SetBody(new(control.RemoveNodeResponse_Body))
|
|
|
|
|
|
|
|
nm, err := s.netmapClient.NetMap()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting netmap: %w", err)
|
|
|
|
}
|
|
|
|
var nodeInfo netmap.NodeInfo
|
|
|
|
for _, info := range nm.Nodes() {
|
|
|
|
if bytes.Equal(info.PublicKey(), req.GetBody().GetKey()) {
|
|
|
|
nodeInfo = info
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(nodeInfo.PublicKey()) == 0 {
|
|
|
|
return nil, status.Error(codes.NotFound, "no such node")
|
|
|
|
}
|
|
|
|
if nodeInfo.IsOffline() {
|
|
|
|
return nil, status.Error(codes.FailedPrecondition, "node is already offline")
|
|
|
|
}
|
|
|
|
|
2023-11-08 09:05:03 +00:00
|
|
|
vub, err := s.netmapClient.ForceRemovePeer(nodeInfo, req.GetBody().GetVub())
|
|
|
|
if err != nil {
|
2023-05-03 13:19:46 +00:00
|
|
|
return nil, fmt.Errorf("forcing node removal: %w", err)
|
|
|
|
}
|
2023-11-08 09:05:03 +00:00
|
|
|
resp.Body.Vub = vub
|
2023-05-03 13:19:46 +00:00
|
|
|
|
|
|
|
if err := SignMessage(&s.prm.key.PrivateKey, resp); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
2023-10-16 15:15:04 +00:00
|
|
|
|
|
|
|
// RemoveContainer forces a container removal.
|
|
|
|
func (s *Server) RemoveContainer(_ context.Context, req *control.RemoveContainerRequest) (*control.RemoveContainerResponse, error) {
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-12-11 10:18:34 +00:00
|
|
|
if len(req.GetBody().GetContainerId()) > 0 && len(req.GetBody().GetOwner()) > 0 {
|
2023-10-16 15:15:04 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "specify the owner and container at the same time is not allowed")
|
|
|
|
}
|
2023-11-08 09:05:03 +00:00
|
|
|
var vub uint32
|
2023-12-11 10:18:34 +00:00
|
|
|
if len(req.GetBody().GetContainerId()) > 0 {
|
2023-10-16 15:15:04 +00:00
|
|
|
var containerID cid.ID
|
2023-12-11 10:18:34 +00:00
|
|
|
if err := containerID.Decode(req.GetBody().GetContainerId()); err != nil {
|
2023-10-16 15:15:04 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("failed to parse container ID: %s", err.Error()))
|
|
|
|
}
|
2023-11-08 09:05:03 +00:00
|
|
|
var err error
|
|
|
|
vub, err = s.removeContainer(containerID, req.GetBody().GetVub())
|
|
|
|
if err != nil {
|
2023-10-16 15:15:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var ownerID refs.OwnerID
|
|
|
|
if err := ownerID.Unmarshal(req.GetBody().GetOwner()); err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("failed to parse ownerID: %s", err.Error()))
|
|
|
|
}
|
|
|
|
var owner user.ID
|
|
|
|
if err := owner.ReadFromV2(ownerID); err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("failed to read owner: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
cids, err := s.containerClient.ContainersOf(&owner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get owner's containers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, containerID := range cids {
|
2023-11-08 09:05:03 +00:00
|
|
|
vub, err = s.removeContainer(containerID, req.GetBody().GetVub())
|
|
|
|
if err != nil {
|
2023-10-16 15:15:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &control.RemoveContainerResponse{
|
2023-11-08 09:05:03 +00:00
|
|
|
Body: &control.RemoveContainerResponse_Body{
|
|
|
|
Vub: vub,
|
|
|
|
},
|
2023-10-16 15:15:04 +00:00
|
|
|
}
|
|
|
|
if err := SignMessage(&s.prm.key.PrivateKey, resp); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 09:05:03 +00:00
|
|
|
func (s *Server) removeContainer(containerID cid.ID, vub uint32) (uint32, error) {
|
2023-10-16 15:15:04 +00:00
|
|
|
var prm container.DeletePrm
|
|
|
|
prm.SetCID(containerID[:])
|
|
|
|
prm.SetControlTX(true)
|
2023-11-08 09:05:03 +00:00
|
|
|
prm.SetVUB(vub)
|
2023-10-16 15:15:04 +00:00
|
|
|
|
2023-11-08 09:05:03 +00:00
|
|
|
vub, err := s.containerClient.Delete(prm)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("forcing container removal: %w", err)
|
2023-10-16 15:15:04 +00:00
|
|
|
}
|
2023-11-08 09:05:03 +00:00
|
|
|
return vub, nil
|
2023-10-16 15:15:04 +00:00
|
|
|
}
|