2021-02-11 10:01:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-09 11:26:41 +00:00
|
|
|
"encoding/json"
|
2021-02-11 10:01:38 +00:00
|
|
|
"errors"
|
2021-06-23 13:14:48 +00:00
|
|
|
"fmt"
|
2021-06-28 11:38:42 +00:00
|
|
|
"regexp"
|
2021-02-11 10:01:38 +00:00
|
|
|
"strconv"
|
2021-03-18 07:16:36 +00:00
|
|
|
"strings"
|
2021-08-05 09:11:28 +00:00
|
|
|
"time"
|
2021-02-11 10:01:38 +00:00
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
corerbd "github.com/ceph/ceph-csi/internal/rbd"
|
2021-02-11 10:01:38 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2021-02-11 10:01:38 +00:00
|
|
|
|
|
|
|
librbd "github.com/ceph/go-ceph/rbd"
|
2021-06-28 11:38:42 +00:00
|
|
|
"github.com/ceph/go-ceph/rbd/admin"
|
2021-04-06 06:46:35 +00:00
|
|
|
"github.com/csi-addons/spec/lib/go/replication"
|
2022-08-15 05:23:50 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-02-11 10:01:38 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-06-09 10:42:50 +00:00
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
2022-09-09 11:26:41 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2021-02-11 10:01:38 +00:00
|
|
|
)
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
// imageMirroringMode is used to indicate the mirroring mode for an RBD image.
|
2021-02-11 10:01:38 +00:00
|
|
|
type imageMirroringMode string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// imageMirrorModeSnapshot uses snapshots to propagate RBD images between
|
|
|
|
// ceph clusters.
|
|
|
|
imageMirrorModeSnapshot imageMirroringMode = "snapshot"
|
2021-11-26 06:38:21 +00:00
|
|
|
// imageMirrorModeJournal uses journaling to propagate RBD images between
|
|
|
|
// ceph clusters.
|
|
|
|
imageMirrorModeJournal imageMirroringMode = "journal"
|
2023-08-22 16:52:49 +00:00
|
|
|
|
|
|
|
// imageCreationTimeKey is the key to get/set the image creation timestamp
|
|
|
|
// on the image metadata. The key is starting with `.rbd` so that it will
|
|
|
|
// not get replicated to remote cluster.
|
|
|
|
imageCreationTimeKey = ".rbd.image.creation_time"
|
2021-02-11 10:01:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// mirroringMode + key to get the imageMirroringMode from parameters.
|
|
|
|
imageMirroringKey = "mirroringMode"
|
|
|
|
// forceKey + key to get the force option from parameters.
|
|
|
|
forceKey = "force"
|
2021-06-28 11:38:42 +00:00
|
|
|
|
|
|
|
// schedulingIntervalKey to get the schedulingInterval from the
|
|
|
|
// parameters.
|
|
|
|
// Interval of time between scheduled snapshots. Typically in the form
|
|
|
|
// <num><m,h,d>.
|
|
|
|
schedulingIntervalKey = "schedulingInterval"
|
|
|
|
|
|
|
|
// schedulingStartTimeKey to get the schedulingStartTime from the
|
|
|
|
// parameters.
|
|
|
|
// (optional) StartTime is the time the snapshot schedule
|
|
|
|
// begins, can be specified using the ISO 8601 time format.
|
|
|
|
schedulingStartTimeKey = "schedulingStartTime"
|
2021-02-11 10:01:38 +00:00
|
|
|
)
|
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
// ReplicationServer struct of rbd CSI driver with supported methods of Replication
|
|
|
|
// controller server spec.
|
|
|
|
type ReplicationServer struct {
|
|
|
|
// added UnimplementedControllerServer as a member of
|
|
|
|
// ControllerServer. if replication spec add more RPC services in the proto
|
|
|
|
// file, then we don't need to add all RPC methods leading to forward
|
|
|
|
// compatibility.
|
|
|
|
*replication.UnimplementedControllerServer
|
|
|
|
// Embed ControllerServer as it implements helper functions
|
2023-01-12 09:52:51 +00:00
|
|
|
*corerbd.ControllerServer
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 09:44:39 +00:00
|
|
|
// NewReplicationServer creates a new ReplicationServer which handles
|
|
|
|
// the Replication Service requests from the CSI-Addons specification.
|
|
|
|
func NewReplicationServer(c *corerbd.ControllerServer) *ReplicationServer {
|
|
|
|
return &ReplicationServer{ControllerServer: c}
|
|
|
|
}
|
|
|
|
|
2022-08-15 05:23:50 +00:00
|
|
|
func (rs *ReplicationServer) RegisterService(server grpc.ServiceRegistrar) {
|
|
|
|
replication.RegisterControllerServer(server, rs)
|
|
|
|
}
|
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
// getForceOption extracts the force option from the GRPC request parameters.
|
|
|
|
// If not set, the default will be set to false.
|
|
|
|
func getForceOption(ctx context.Context, parameters map[string]string) (bool, error) {
|
|
|
|
val, ok := parameters[forceKey]
|
|
|
|
if !ok {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(ctx, "%s is not set in parameters, setting to default (%v)", forceKey, false)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
force, err := strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
return false, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return force, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMirroringMode gets the mirroring mode from the input GRPC request parameters.
|
|
|
|
// mirroringMode is the key to check the mode in the parameters.
|
|
|
|
func getMirroringMode(ctx context.Context, parameters map[string]string) (librbd.ImageMirrorMode, error) {
|
|
|
|
val, ok := parameters[imageMirroringKey]
|
|
|
|
if !ok {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.WarningLog(
|
2021-06-25 11:52:34 +00:00
|
|
|
ctx,
|
|
|
|
"%s is not set in parameters, setting to mirroringMode to default (%s)",
|
|
|
|
imageMirroringKey,
|
|
|
|
imageMirrorModeSnapshot)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return librbd.ImageMirrorModeSnapshot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var mirroringMode librbd.ImageMirrorMode
|
|
|
|
switch imageMirroringMode(val) {
|
|
|
|
case imageMirrorModeSnapshot:
|
|
|
|
mirroringMode = librbd.ImageMirrorModeSnapshot
|
2021-11-26 06:38:21 +00:00
|
|
|
case imageMirrorModeJournal:
|
|
|
|
mirroringMode = librbd.ImageMirrorModeJournal
|
2021-02-11 10:01:38 +00:00
|
|
|
default:
|
|
|
|
return mirroringMode, status.Errorf(codes.InvalidArgument, "%s %s not supported", imageMirroringKey, val)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return mirroringMode, nil
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:00:13 +00:00
|
|
|
// validateSchedulingDetails gets the mirroring mode and scheduling details from the
|
2021-06-28 11:38:42 +00:00
|
|
|
// input GRPC request parameters and validates the scheduling is only supported
|
2021-08-09 04:48:32 +00:00
|
|
|
// for snapshot mirroring mode.
|
2021-11-26 06:38:21 +00:00
|
|
|
func validateSchedulingDetails(ctx context.Context, parameters map[string]string) error {
|
2021-06-28 11:38:42 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
val := parameters[imageMirroringKey]
|
|
|
|
|
|
|
|
switch imageMirroringMode(val) {
|
2021-11-26 06:38:21 +00:00
|
|
|
case imageMirrorModeJournal:
|
|
|
|
// journal mirror mode does not require scheduling parameters
|
|
|
|
if _, ok := parameters[schedulingIntervalKey]; ok {
|
|
|
|
log.WarningLog(ctx, "%s parameter cannot be used with %s mirror mode, ignoring it",
|
|
|
|
schedulingIntervalKey, string(imageMirrorModeJournal))
|
|
|
|
}
|
|
|
|
if _, ok := parameters[schedulingStartTimeKey]; ok {
|
|
|
|
log.WarningLog(ctx, "%s parameter cannot be used with %s mirror mode, ignoring it",
|
|
|
|
schedulingStartTimeKey, string(imageMirrorModeJournal))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-06-28 11:38:42 +00:00
|
|
|
case imageMirrorModeSnapshot:
|
2021-08-09 04:48:32 +00:00
|
|
|
// If mirroring mode is not set in parameters, we are defaulting mirroring
|
|
|
|
// mode to snapshot. Discard empty mirroring mode from validation as it is
|
|
|
|
// an optional parameter.
|
|
|
|
case "":
|
2021-06-28 11:38:42 +00:00
|
|
|
default:
|
2021-11-15 06:00:13 +00:00
|
|
|
return status.Error(codes.InvalidArgument, "scheduling is only supported for snapshot mode")
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate mandatory interval field
|
|
|
|
interval, ok := parameters[schedulingIntervalKey]
|
|
|
|
if ok && interval == "" {
|
2021-11-15 06:00:13 +00:00
|
|
|
return status.Error(codes.InvalidArgument, "scheduling interval cannot be empty")
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
2021-11-15 06:00:13 +00:00
|
|
|
adminStartTime := admin.StartTime(parameters[schedulingStartTimeKey])
|
2021-06-28 11:38:42 +00:00
|
|
|
if !ok {
|
|
|
|
// startTime is alone not supported it has to be present with interval
|
|
|
|
if adminStartTime != "" {
|
2021-11-15 06:00:13 +00:00
|
|
|
return status.Errorf(codes.InvalidArgument,
|
2021-06-28 11:38:42 +00:00
|
|
|
"%q parameter is supported only with %q",
|
|
|
|
schedulingStartTimeKey,
|
|
|
|
schedulingIntervalKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if interval != "" {
|
2021-11-15 06:00:13 +00:00
|
|
|
err = validateSchedulingInterval(interval)
|
2021-06-28 11:38:42 +00:00
|
|
|
if err != nil {
|
2021-11-15 06:00:13 +00:00
|
|
|
return status.Error(codes.InvalidArgument, err.Error())
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-11-15 06:00:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSchedulingDetails returns scheduling interval and scheduling startTime.
|
|
|
|
func getSchedulingDetails(parameters map[string]string) (admin.Interval, admin.StartTime) {
|
|
|
|
return admin.Interval(parameters[schedulingIntervalKey]),
|
|
|
|
admin.StartTime(parameters[schedulingStartTimeKey])
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validateSchedulingInterval return the interval as it is if its ending with
|
|
|
|
// `m|h|d` or else it will return error.
|
2021-11-15 06:00:13 +00:00
|
|
|
func validateSchedulingInterval(interval string) error {
|
2021-07-13 12:21:05 +00:00
|
|
|
re := regexp.MustCompile(`^\d+[mhd]$`)
|
2021-06-28 11:38:42 +00:00
|
|
|
if re.MatchString(interval) {
|
2021-11-15 06:00:13 +00:00
|
|
|
return nil
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-11-15 06:00:13 +00:00
|
|
|
return errors.New("interval specified without d, h, m suffix")
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
// EnableVolumeReplication extracts the RBD volume information from the
|
|
|
|
// volumeID, If the image is present it will enable the mirroring based on the
|
|
|
|
// user provided information.
|
2021-03-17 07:27:04 +00:00
|
|
|
func (rs *ReplicationServer) EnableVolumeReplication(ctx context.Context,
|
2021-02-11 10:01:38 +00:00
|
|
|
req *replication.EnableVolumeReplicationRequest,
|
|
|
|
) (*replication.EnableVolumeReplicationResponse, error) {
|
2021-03-17 07:27:04 +00:00
|
|
|
volumeID := req.GetVolumeId()
|
|
|
|
if volumeID == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty volume ID in request")
|
|
|
|
}
|
|
|
|
cr, err := util.NewUserCredentials(req.GetSecrets())
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
|
2021-11-26 06:38:21 +00:00
|
|
|
err = validateSchedulingDetails(ctx, req.GetParameters())
|
2021-06-28 11:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
if acquired := rs.VolumeLocks.TryAcquire(volumeID); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
|
|
|
}
|
|
|
|
defer rs.VolumeLocks.Release(volumeID)
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
|
2023-09-06 09:06:06 +00:00
|
|
|
defer func() {
|
|
|
|
if rbdVol != nil {
|
|
|
|
rbdVol.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
2021-03-17 07:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
switch {
|
2023-01-12 09:52:51 +00:00
|
|
|
case errors.Is(err, corerbd.ErrImageNotFound):
|
2021-03-17 07:27:04 +00:00
|
|
|
err = status.Errorf(codes.NotFound, "volume %s not found", volumeID)
|
|
|
|
case errors.Is(err, util.ErrPoolNotFound):
|
|
|
|
err = status.Errorf(codes.NotFound, "pool %s not found for %s", rbdVol.Pool, volumeID)
|
|
|
|
default:
|
|
|
|
err = status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// extract the mirroring mode
|
|
|
|
mirroringMode, err := getMirroringMode(ctx, req.GetParameters())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
mirroringInfo, err := rbdVol.GetImageMirroringInfo()
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if mirroringInfo.State != librbd.MirrorImageEnabled {
|
2023-01-12 09:52:51 +00:00
|
|
|
err = rbdVol.EnableImageMirroring(mirroringMode)
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2021-06-28 11:38:42 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return &replication.EnableVolumeReplicationResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableVolumeReplication extracts the RBD volume information from the
|
|
|
|
// volumeID, If the image is present and the mirroring is enabled on the RBD
|
|
|
|
// image it will disable the mirroring.
|
2021-03-17 07:27:04 +00:00
|
|
|
func (rs *ReplicationServer) DisableVolumeReplication(ctx context.Context,
|
2021-02-11 10:01:38 +00:00
|
|
|
req *replication.DisableVolumeReplicationRequest,
|
|
|
|
) (*replication.DisableVolumeReplicationResponse, error) {
|
2021-03-17 07:27:04 +00:00
|
|
|
volumeID := req.GetVolumeId()
|
|
|
|
if volumeID == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty volume ID in request")
|
|
|
|
}
|
|
|
|
cr, err := util.NewUserCredentials(req.GetSecrets())
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
2021-03-17 07:27:04 +00:00
|
|
|
defer cr.DeleteCredentials()
|
2021-02-11 10:01:38 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
if acquired := rs.VolumeLocks.TryAcquire(volumeID); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
|
|
|
}
|
|
|
|
defer rs.VolumeLocks.Release(volumeID)
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
|
2023-09-06 09:06:06 +00:00
|
|
|
defer func() {
|
|
|
|
if rbdVol != nil {
|
|
|
|
rbdVol.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
2021-03-17 07:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
switch {
|
2023-01-12 09:52:51 +00:00
|
|
|
case errors.Is(err, corerbd.ErrImageNotFound):
|
2021-03-17 07:27:04 +00:00
|
|
|
err = status.Errorf(codes.NotFound, "volume %s not found", volumeID)
|
|
|
|
case errors.Is(err, util.ErrPoolNotFound):
|
|
|
|
err = status.Errorf(codes.NotFound, "pool %s not found for %s", rbdVol.Pool, volumeID)
|
|
|
|
default:
|
|
|
|
err = status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-11 10:01:38 +00:00
|
|
|
// extract the force option
|
|
|
|
force, err := getForceOption(ctx, req.GetParameters())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
mirroringInfo, err := rbdVol.GetImageMirroringInfo()
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
switch mirroringInfo.State {
|
2021-03-30 04:44:32 +00:00
|
|
|
// image is already in disabled state
|
|
|
|
case librbd.MirrorImageDisabled:
|
|
|
|
// image mirroring is still disabling
|
|
|
|
case librbd.MirrorImageDisabling:
|
|
|
|
return nil, status.Errorf(codes.Aborted, "%s is in disabling state", volumeID)
|
2021-02-11 10:01:38 +00:00
|
|
|
case librbd.MirrorImageEnabled:
|
2023-06-05 16:18:29 +00:00
|
|
|
err = rbdVol.DisableVolumeReplication(mirroringInfo, force)
|
|
|
|
if err != nil {
|
|
|
|
return nil, getGRPCError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &replication.DisableVolumeReplicationResponse{}, nil
|
2021-08-12 07:05:56 +00:00
|
|
|
default:
|
2021-09-02 16:01:17 +00:00
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "image is in %s Mode", mirroringInfo.State)
|
2021-08-12 07:05:56 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-08-12 07:05:56 +00:00
|
|
|
return &replication.DisableVolumeReplicationResponse{}, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
// PromoteVolume extracts the RBD volume information from the volumeID, If the
|
|
|
|
// image is present, mirroring is enabled and the image is in demoted state it
|
|
|
|
// will promote the volume as primary.
|
|
|
|
// If the image is already primary it will return success.
|
2021-03-17 07:27:04 +00:00
|
|
|
func (rs *ReplicationServer) PromoteVolume(ctx context.Context,
|
2021-02-11 10:01:38 +00:00
|
|
|
req *replication.PromoteVolumeRequest,
|
|
|
|
) (*replication.PromoteVolumeResponse, error) {
|
2021-03-17 07:27:04 +00:00
|
|
|
volumeID := req.GetVolumeId()
|
|
|
|
if volumeID == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty volume ID in request")
|
|
|
|
}
|
|
|
|
cr, err := util.NewUserCredentials(req.GetSecrets())
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
2021-03-17 07:27:04 +00:00
|
|
|
defer cr.DeleteCredentials()
|
2021-02-11 10:01:38 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
if acquired := rs.VolumeLocks.TryAcquire(volumeID); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
|
|
|
}
|
|
|
|
defer rs.VolumeLocks.Release(volumeID)
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
|
2023-09-06 09:06:06 +00:00
|
|
|
defer func() {
|
|
|
|
if rbdVol != nil {
|
|
|
|
rbdVol.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
2021-03-17 07:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
switch {
|
2023-01-12 09:52:51 +00:00
|
|
|
case errors.Is(err, corerbd.ErrImageNotFound):
|
2021-03-17 07:27:04 +00:00
|
|
|
err = status.Errorf(codes.NotFound, "volume %s not found", volumeID)
|
|
|
|
case errors.Is(err, util.ErrPoolNotFound):
|
|
|
|
err = status.Errorf(codes.NotFound, "pool %s not found for %s", rbdVol.Pool, volumeID)
|
|
|
|
default:
|
|
|
|
err = status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-11 10:01:38 +00:00
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
mirroringInfo, err := rbdVol.GetImageMirroringInfo()
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if mirroringInfo.State != librbd.MirrorImageEnabled {
|
2021-06-25 11:52:34 +00:00
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
"mirroring is not enabled on %s, image is in %d Mode",
|
|
|
|
rbdVol.VolID,
|
|
|
|
mirroringInfo.State)
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// promote secondary to primary
|
|
|
|
if !mirroringInfo.Primary {
|
2021-12-22 03:36:37 +00:00
|
|
|
if req.GetForce() {
|
|
|
|
// workaround for https://github.com/ceph/ceph-csi/issues/2736
|
|
|
|
// TODO: remove this workaround when the issue is fixed
|
2023-01-12 09:52:51 +00:00
|
|
|
err = rbdVol.ForcePromoteImage(cr)
|
2021-12-22 03:36:37 +00:00
|
|
|
} else {
|
2023-01-12 09:52:51 +00:00
|
|
|
err = rbdVol.PromoteImage(req.GetForce())
|
2021-12-22 03:36:37 +00:00
|
|
|
}
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-04-06 05:48:01 +00:00
|
|
|
// In case of the DR the image on the primary site cannot be
|
|
|
|
// demoted as the cluster is down, during failover the image need
|
|
|
|
// to be force promoted. RBD returns `Device or resource busy`
|
|
|
|
// error message if the image cannot be promoted for above reason.
|
|
|
|
// Return FailedPrecondition so that replication operator can send
|
|
|
|
// request to force promote the image.
|
|
|
|
if strings.Contains(err.Error(), "Device or resource busy") {
|
|
|
|
return nil, status.Error(codes.FailedPrecondition, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:00:13 +00:00
|
|
|
interval, startTime := getSchedulingDetails(req.GetParameters())
|
|
|
|
if interval != admin.NoInterval {
|
2023-01-12 09:52:51 +00:00
|
|
|
err = rbdVol.AddSnapshotScheduling(interval, startTime)
|
2021-11-15 06:00:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.DebugLog(
|
|
|
|
ctx,
|
|
|
|
"Added scheduling at interval %s, start time %s for volume %s",
|
|
|
|
interval,
|
|
|
|
startTime,
|
|
|
|
rbdVol)
|
|
|
|
}
|
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return &replication.PromoteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DemoteVolume extracts the RBD volume information from the
|
|
|
|
// volumeID, If the image is present, mirroring is enabled and the
|
|
|
|
// image is in promoted state it will demote the volume as secondary.
|
|
|
|
// If the image is already secondary it will return success.
|
2021-03-17 07:27:04 +00:00
|
|
|
func (rs *ReplicationServer) DemoteVolume(ctx context.Context,
|
2021-02-11 10:01:38 +00:00
|
|
|
req *replication.DemoteVolumeRequest,
|
|
|
|
) (*replication.DemoteVolumeResponse, error) {
|
2021-03-17 07:27:04 +00:00
|
|
|
volumeID := req.GetVolumeId()
|
|
|
|
if volumeID == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty volume ID in request")
|
|
|
|
}
|
|
|
|
cr, err := util.NewUserCredentials(req.GetSecrets())
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
|
|
|
|
if acquired := rs.VolumeLocks.TryAcquire(volumeID); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
2021-03-17 07:27:04 +00:00
|
|
|
defer rs.VolumeLocks.Release(volumeID)
|
2021-02-11 10:01:38 +00:00
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
|
2023-09-06 09:06:06 +00:00
|
|
|
defer func() {
|
|
|
|
if rbdVol != nil {
|
|
|
|
rbdVol.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
2021-03-17 07:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
switch {
|
2023-01-12 09:52:51 +00:00
|
|
|
case errors.Is(err, corerbd.ErrImageNotFound):
|
2021-03-17 07:27:04 +00:00
|
|
|
err = status.Errorf(codes.NotFound, "volume %s not found", volumeID)
|
|
|
|
case errors.Is(err, util.ErrPoolNotFound):
|
|
|
|
err = status.Errorf(codes.NotFound, "pool %s not found for %s", rbdVol.Pool, volumeID)
|
|
|
|
default:
|
|
|
|
err = status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-22 16:52:49 +00:00
|
|
|
|
|
|
|
creationTime, err := rbdVol.GetImageCreationTime()
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, err.Error())
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
mirroringInfo, err := rbdVol.GetImageMirroringInfo()
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if mirroringInfo.State != librbd.MirrorImageEnabled {
|
2021-06-25 11:52:34 +00:00
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
"mirroring is not enabled on %s, image is in %d Mode",
|
|
|
|
rbdVol.VolID,
|
|
|
|
mirroringInfo.State)
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// demote image to secondary
|
|
|
|
if mirroringInfo.Primary {
|
2023-08-22 16:52:49 +00:00
|
|
|
// store the image creation time for resync
|
|
|
|
_, err = rbdVol.GetMetadata(imageCreationTimeKey)
|
|
|
|
if err != nil && errors.Is(err, librbd.ErrNotFound) {
|
2023-09-02 07:21:35 +00:00
|
|
|
log.DebugLog(ctx, "setting image creation time %s for %s", creationTime, rbdVol)
|
2023-08-22 16:52:49 +00:00
|
|
|
err = rbdVol.SetMetadata(imageCreationTimeKey, timestampToString(creationTime))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, err.Error())
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
err = rbdVol.DemoteImage()
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return &replication.DemoteVolumeResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:14:48 +00:00
|
|
|
// checkRemoteSiteStatus checks the state of the remote cluster.
|
|
|
|
// It returns true if the state of the remote cluster is up and unknown.
|
|
|
|
func checkRemoteSiteStatus(ctx context.Context, mirrorStatus *librbd.GlobalMirrorImageStatus) bool {
|
|
|
|
ready := true
|
2022-08-08 17:53:41 +00:00
|
|
|
found := false
|
2021-06-23 13:14:48 +00:00
|
|
|
for _, s := range mirrorStatus.SiteStatuses {
|
2021-10-25 10:28:37 +00:00
|
|
|
log.UsefulLog(
|
|
|
|
ctx,
|
2021-10-25 11:27:23 +00:00
|
|
|
"peer site mirrorUUID=%q, daemon up=%t, mirroring state=%q, description=%q and lastUpdate=%d",
|
2021-10-25 10:28:37 +00:00
|
|
|
s.MirrorUUID,
|
2021-10-25 11:27:23 +00:00
|
|
|
s.Up,
|
|
|
|
s.State,
|
2021-10-25 10:28:37 +00:00
|
|
|
s.Description,
|
|
|
|
s.LastUpdate)
|
2021-06-23 13:14:48 +00:00
|
|
|
if s.MirrorUUID != "" {
|
2022-08-08 17:53:41 +00:00
|
|
|
found = true
|
|
|
|
// If ready is already "false" do not flip it based on another remote peer status
|
|
|
|
if ready && (s.State != librbd.MirrorImageStatusStateUnknown || !s.Up) {
|
2021-06-23 13:14:48 +00:00
|
|
|
ready = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 17:53:41 +00:00
|
|
|
// Return readiness only if at least one remote peer status was processed
|
|
|
|
return found && ready
|
2021-06-23 13:14:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
// ResyncVolume extracts the RBD volume information from the volumeID, If the
|
|
|
|
// image is present, mirroring is enabled and the image is in demoted state.
|
|
|
|
// If yes it will resync the image to correct the split-brain.
|
2023-08-22 16:52:49 +00:00
|
|
|
//
|
|
|
|
//nolint:gocyclo,cyclop // TODO: reduce complexity
|
2021-03-17 07:27:04 +00:00
|
|
|
func (rs *ReplicationServer) ResyncVolume(ctx context.Context,
|
2021-02-11 10:01:38 +00:00
|
|
|
req *replication.ResyncVolumeRequest,
|
|
|
|
) (*replication.ResyncVolumeResponse, error) {
|
2021-03-17 07:27:04 +00:00
|
|
|
volumeID := req.GetVolumeId()
|
|
|
|
if volumeID == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty volume ID in request")
|
|
|
|
}
|
|
|
|
cr, err := util.NewUserCredentials(req.GetSecrets())
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
|
|
|
|
if acquired := rs.VolumeLocks.TryAcquire(volumeID); !acquired {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-17 07:27:04 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
|
|
|
}
|
|
|
|
defer rs.VolumeLocks.Release(volumeID)
|
2023-01-12 09:52:51 +00:00
|
|
|
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
|
2023-09-06 09:06:06 +00:00
|
|
|
defer func() {
|
|
|
|
if rbdVol != nil {
|
|
|
|
rbdVol.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
2021-03-17 07:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
switch {
|
2023-01-12 09:52:51 +00:00
|
|
|
case errors.Is(err, corerbd.ErrImageNotFound):
|
2021-03-17 07:27:04 +00:00
|
|
|
err = status.Errorf(codes.NotFound, "volume %s not found", volumeID)
|
|
|
|
case errors.Is(err, util.ErrPoolNotFound):
|
|
|
|
err = status.Errorf(codes.NotFound, "pool %s not found for %s", rbdVol.Pool, volumeID)
|
|
|
|
default:
|
|
|
|
err = status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
mirroringInfo, err := rbdVol.GetImageMirroringInfo()
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
|
|
|
// in case of Resync the image will get deleted and gets recreated and
|
2021-03-18 07:30:57 +00:00
|
|
|
// it takes time for this operation.
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2023-08-22 16:52:49 +00:00
|
|
|
return nil, status.Errorf(codes.Aborted, err.Error())
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if mirroringInfo.State != librbd.MirrorImageEnabled {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "image mirroring is not enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
// return error if the image is still primary
|
|
|
|
if mirroringInfo.Primary {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "image is in primary state")
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
mirrorStatus, err := rbdVol.GetImageMirroringStatus()
|
2021-02-11 10:01:38 +00:00
|
|
|
if err != nil {
|
2021-09-14 15:29:25 +00:00
|
|
|
// the image gets recreated after issuing resync
|
2023-01-12 09:52:51 +00:00
|
|
|
if errors.Is(err, corerbd.ErrImageNotFound) {
|
2021-09-14 15:29:25 +00:00
|
|
|
// caller retries till RBD syncs an initial version of the image to
|
|
|
|
// report its status in the resync call. Ideally, this line will not
|
|
|
|
// be executed as the error would get returned due to getImageMirroringInfo
|
|
|
|
// failing to find an image above.
|
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
ready := false
|
2021-06-23 13:14:48 +00:00
|
|
|
|
|
|
|
localStatus, err := mirrorStatus.LocalStatus()
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, err.Error())
|
2021-06-23 13:14:48 +00:00
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to get local status: %w", err)
|
|
|
|
}
|
2021-10-25 11:22:14 +00:00
|
|
|
|
|
|
|
// convert the last update time to UTC
|
|
|
|
lastUpdateTime := time.Unix(localStatus.LastUpdate, 0).UTC()
|
|
|
|
log.UsefulLog(
|
|
|
|
ctx,
|
2021-10-25 11:27:23 +00:00
|
|
|
"local status: daemon up=%t, image mirroring state=%q, description=%q and lastUpdate=%s",
|
|
|
|
localStatus.Up,
|
|
|
|
localStatus.State,
|
2021-10-25 11:22:14 +00:00
|
|
|
localStatus.Description,
|
|
|
|
lastUpdateTime)
|
|
|
|
|
2021-04-28 07:46:00 +00:00
|
|
|
// To recover from split brain (up+error) state the image need to be
|
|
|
|
// demoted and requested for resync on site-a and then the image on site-b
|
|
|
|
// should be demoted. The volume should be marked to ready=true when the
|
|
|
|
// image state on both the clusters are up+unknown because during the last
|
|
|
|
// snapshot syncing the data gets copied first and then image state on the
|
|
|
|
// site-a changes to up+unknown.
|
|
|
|
|
|
|
|
// If the image state on both the sites are up+unknown consider that
|
|
|
|
// complete data is synced as the last snapshot
|
|
|
|
// gets exchanged between the clusters.
|
2021-10-25 11:22:14 +00:00
|
|
|
if localStatus.State == librbd.MirrorImageStatusStateUnknown && localStatus.Up {
|
2021-06-23 13:14:48 +00:00
|
|
|
ready = checkRemoteSiteStatus(ctx, mirrorStatus)
|
2021-02-11 10:01:38 +00:00
|
|
|
}
|
2021-03-18 07:16:36 +00:00
|
|
|
|
2023-08-22 16:52:49 +00:00
|
|
|
creationTime, err := rbdVol.GetImageCreationTime()
|
2022-06-22 05:48:10 +00:00
|
|
|
if err != nil {
|
2023-08-22 16:52:49 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get image info for %s: %s", rbdVol, err.Error())
|
2021-03-18 07:16:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:52:49 +00:00
|
|
|
// image creation time is stored in the image metadata. it looks like
|
|
|
|
// `"seconds:1692879841 nanos:631526669"`
|
2023-09-02 07:21:35 +00:00
|
|
|
// If the image gets resynced the local image creation time will be
|
|
|
|
// lost, if the keys is not present in the image metadata then we can
|
|
|
|
// assume that the image is already resynced.
|
2023-08-22 16:52:49 +00:00
|
|
|
savedImageTime, err := rbdVol.GetMetadata(imageCreationTimeKey)
|
2023-09-02 07:21:35 +00:00
|
|
|
if err != nil && !errors.Is(err, librbd.ErrNotFound) {
|
2023-08-22 16:52:49 +00:00
|
|
|
return nil, status.Errorf(codes.Internal,
|
|
|
|
"failed to get %s key from image metadata for %s: %s",
|
|
|
|
imageCreationTimeKey,
|
|
|
|
rbdVol,
|
|
|
|
err.Error())
|
|
|
|
}
|
|
|
|
|
2023-09-02 07:21:35 +00:00
|
|
|
if savedImageTime != "" {
|
|
|
|
st, sErr := timestampFromString(savedImageTime)
|
|
|
|
if sErr != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to parse image creation time: %s", sErr.Error())
|
|
|
|
}
|
|
|
|
log.DebugLog(ctx, "image %s, savedImageTime=%v, currentImageTime=%v", rbdVol, st, creationTime.AsTime())
|
2024-04-04 08:51:19 +00:00
|
|
|
if req.GetForce() && st.Equal(creationTime.AsTime()) {
|
2023-09-02 07:21:35 +00:00
|
|
|
err = rbdVol.ResyncVol(localStatus)
|
|
|
|
if err != nil {
|
|
|
|
return nil, getGRPCError(err)
|
|
|
|
}
|
2023-08-22 16:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ready {
|
2024-02-13 08:29:33 +00:00
|
|
|
err = checkVolumeResyncStatus(ctx, localStatus)
|
2023-08-22 16:52:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-10-25 10:24:12 +00:00
|
|
|
}
|
2021-08-05 09:11:28 +00:00
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
err = rbdVol.RepairResyncedImageID(ctx, ready)
|
2021-11-18 10:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed to resync Image ID: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
resp := &replication.ResyncVolumeResponse{
|
|
|
|
Ready: ready,
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 10:01:38 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
2021-10-14 11:34:18 +00:00
|
|
|
|
2023-08-22 16:52:49 +00:00
|
|
|
// timestampToString converts the time.Time object to string.
|
|
|
|
func timestampToString(st *timestamppb.Timestamp) string {
|
2024-04-04 08:51:19 +00:00
|
|
|
return fmt.Sprintf("seconds:%d nanos:%d", st.GetSeconds(), st.GetNanos())
|
2023-08-22 16:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// timestampFromString parses the timestamp string and returns the time.Time
|
|
|
|
// object.
|
|
|
|
func timestampFromString(timestamp string) (time.Time, error) {
|
|
|
|
st := time.Time{}
|
|
|
|
parts := strings.Fields(timestamp)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return st, fmt.Errorf("failed to parse image creation time: %s", timestamp)
|
|
|
|
}
|
|
|
|
if len(strings.Split(parts[0], ":")) != 2 || len(strings.Split(parts[1], ":")) != 2 {
|
|
|
|
return st, fmt.Errorf("failed to parse image creation time: %s", timestamp)
|
|
|
|
}
|
|
|
|
secondsStr := strings.Split(parts[0], ":")[1]
|
|
|
|
nanosStr := strings.Split(parts[1], ":")[1]
|
|
|
|
|
|
|
|
seconds, err := strconv.ParseInt(secondsStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return st, fmt.Errorf("failed to parse image creation time seconds: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
nanos, err := strconv.ParseInt(nanosStr, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return st, fmt.Errorf("failed to parse image creation time nenos: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
st = time.Unix(seconds, nanos)
|
|
|
|
|
|
|
|
return st, nil
|
|
|
|
}
|
|
|
|
|
2023-06-05 16:18:29 +00:00
|
|
|
func getGRPCError(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return status.Error(codes.OK, codes.OK.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
errorStatusMap := map[error]codes.Code{
|
|
|
|
corerbd.ErrFetchingLocalState: codes.Internal,
|
|
|
|
corerbd.ErrResyncImageFailed: codes.Internal,
|
|
|
|
corerbd.ErrDisableImageMirroringFailed: codes.Internal,
|
|
|
|
corerbd.ErrFetchingMirroringInfo: codes.Internal,
|
|
|
|
corerbd.ErrInvalidArgument: codes.InvalidArgument,
|
|
|
|
corerbd.ErrAborted: codes.Aborted,
|
|
|
|
corerbd.ErrFailedPrecondition: codes.FailedPrecondition,
|
|
|
|
corerbd.ErrUnavailable: codes.Unavailable,
|
|
|
|
}
|
|
|
|
|
|
|
|
for e, code := range errorStatusMap {
|
|
|
|
if errors.Is(err, e) {
|
|
|
|
return status.Error(code, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle any other non nil error not listed in the map
|
|
|
|
return status.Error(codes.Unknown, err.Error())
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:26:41 +00:00
|
|
|
// GetVolumeReplicationInfo extracts the RBD volume information from the volumeID, If the
|
|
|
|
// image is present, mirroring is enabled and the image is in primary state.
|
|
|
|
func (rs *ReplicationServer) GetVolumeReplicationInfo(ctx context.Context,
|
|
|
|
req *replication.GetVolumeReplicationInfoRequest,
|
|
|
|
) (*replication.GetVolumeReplicationInfoResponse, error) {
|
|
|
|
volumeID := req.GetVolumeId()
|
|
|
|
if volumeID == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty volume ID in request")
|
|
|
|
}
|
|
|
|
cr, err := util.NewUserCredentials(req.GetSecrets())
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
defer cr.DeleteCredentials()
|
|
|
|
|
|
|
|
if acquired := rs.VolumeLocks.TryAcquire(volumeID); !acquired {
|
|
|
|
log.ErrorLog(ctx, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
|
|
|
|
|
|
|
return nil, status.Errorf(codes.Aborted, util.VolumeOperationAlreadyExistsFmt, volumeID)
|
|
|
|
}
|
|
|
|
defer rs.VolumeLocks.Release(volumeID)
|
2023-01-12 09:52:51 +00:00
|
|
|
rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets())
|
2023-09-06 09:06:06 +00:00
|
|
|
defer func() {
|
|
|
|
if rbdVol != nil {
|
|
|
|
rbdVol.Destroy()
|
|
|
|
}
|
|
|
|
}()
|
2022-09-09 11:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
switch {
|
2023-01-12 09:52:51 +00:00
|
|
|
case errors.Is(err, corerbd.ErrImageNotFound):
|
2022-09-09 11:26:41 +00:00
|
|
|
err = status.Errorf(codes.NotFound, "volume %s not found", volumeID)
|
|
|
|
case errors.Is(err, util.ErrPoolNotFound):
|
|
|
|
err = status.Errorf(codes.NotFound, "pool %s not found for %s", rbdVol.Pool, volumeID)
|
|
|
|
default:
|
|
|
|
err = status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
mirroringInfo, err := rbdVol.GetImageMirroringInfo()
|
2022-09-09 11:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, err.Error())
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if mirroringInfo.State != librbd.MirrorImageEnabled {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "image mirroring is not enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
// return error if the image is not in primary state
|
|
|
|
if !mirroringInfo.Primary {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "image is not in primary state")
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
mirrorStatus, err := rbdVol.GetImageMirroringStatus()
|
2022-09-09 11:26:41 +00:00
|
|
|
if err != nil {
|
2023-01-12 09:52:51 +00:00
|
|
|
if errors.Is(err, corerbd.ErrImageNotFound) {
|
2022-09-09 11:26:41 +00:00
|
|
|
return nil, status.Error(codes.Aborted, err.Error())
|
|
|
|
}
|
|
|
|
log.ErrorLog(ctx, err.Error())
|
|
|
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-02-13 08:29:33 +00:00
|
|
|
remoteStatus, err := RemoteStatus(ctx, mirrorStatus)
|
2022-09-09 11:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, err.Error())
|
|
|
|
|
2022-10-18 08:39:15 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get remote status: %v", err)
|
2022-09-09 11:26:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 11:09:35 +00:00
|
|
|
description := remoteStatus.Description
|
2024-02-13 08:29:33 +00:00
|
|
|
resp, err := getLastSyncInfo(ctx, description)
|
2022-09-09 11:26:41 +00:00
|
|
|
if err != nil {
|
2023-01-12 09:52:51 +00:00
|
|
|
if errors.Is(err, corerbd.ErrLastSyncTimeNotFound) {
|
2023-06-09 10:42:50 +00:00
|
|
|
return nil, status.Errorf(codes.NotFound, "failed to get last sync info: %v", err)
|
2022-10-28 07:47:33 +00:00
|
|
|
}
|
|
|
|
log.ErrorLog(ctx, err.Error())
|
|
|
|
|
2023-06-09 10:42:50 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to get last sync info: %v", err)
|
2022-09-09 11:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:09:35 +00:00
|
|
|
// RemoteStatus returns one SiteMirrorImageStatus item from the SiteStatuses
|
|
|
|
// slice that corresponds to the remote site's status. If the remote status
|
|
|
|
// is not found than the error ErrNotExist will be returned.
|
2024-02-13 08:29:33 +00:00
|
|
|
func RemoteStatus(ctx context.Context, gmis *librbd.GlobalMirrorImageStatus) (librbd.SiteMirrorImageStatus, error) {
|
2022-09-14 11:09:35 +00:00
|
|
|
var (
|
|
|
|
ss librbd.SiteMirrorImageStatus
|
|
|
|
err error = librbd.ErrNotExist
|
|
|
|
)
|
2024-02-13 08:29:33 +00:00
|
|
|
|
2022-09-14 11:09:35 +00:00
|
|
|
for i := range gmis.SiteStatuses {
|
2024-02-13 08:29:33 +00:00
|
|
|
log.DebugLog(
|
|
|
|
ctx,
|
|
|
|
"Site status of MirrorUUID: %s, state: %s, description: %s, lastUpdate: %v, up: %t",
|
|
|
|
gmis.SiteStatuses[i].MirrorUUID,
|
|
|
|
gmis.SiteStatuses[i].State,
|
|
|
|
gmis.SiteStatuses[i].Description,
|
|
|
|
gmis.SiteStatuses[i].LastUpdate,
|
|
|
|
gmis.SiteStatuses[i].Up)
|
|
|
|
|
2022-09-14 11:09:35 +00:00
|
|
|
if gmis.SiteStatuses[i].MirrorUUID != "" {
|
|
|
|
ss = gmis.SiteStatuses[i]
|
|
|
|
err = nil
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss, err
|
|
|
|
}
|
|
|
|
|
2023-06-09 10:42:50 +00:00
|
|
|
// This function gets the local snapshot time, last sync snapshot seconds
|
|
|
|
// and last sync bytes from the description of localStatus and convert
|
|
|
|
// it into required types.
|
2024-02-13 08:29:33 +00:00
|
|
|
func getLastSyncInfo(ctx context.Context, description string) (*replication.GetVolumeReplicationInfoResponse, error) {
|
2022-09-09 11:26:41 +00:00
|
|
|
// Format of the description will be as followed:
|
2023-06-09 10:42:50 +00:00
|
|
|
// description = `replaying, {"bytes_per_second":0.0,"bytes_per_snapshot":81920.0,
|
|
|
|
// "last_snapshot_bytes":81920,"last_snapshot_sync_seconds":0,
|
|
|
|
// "local_snapshot_timestamp":1684675261,
|
|
|
|
// "remote_snapshot_timestamp":1684675261,"replay_state":"idle"}`
|
|
|
|
// In case there is no last snapshot bytes returns 0 as the
|
|
|
|
// LastSyncBytes is optional.
|
|
|
|
// In case there is no last snapshot sync seconds, it returns nil as the
|
|
|
|
// LastSyncDuration is optional.
|
2022-10-28 07:47:33 +00:00
|
|
|
// In case there is no local snapshot timestamp return an error as the
|
|
|
|
// LastSyncTime is required.
|
2023-06-09 10:42:50 +00:00
|
|
|
|
|
|
|
var response replication.GetVolumeReplicationInfoResponse
|
|
|
|
|
2022-09-09 11:26:41 +00:00
|
|
|
if description == "" {
|
2023-01-12 09:52:51 +00:00
|
|
|
return nil, fmt.Errorf("empty description: %w", corerbd.ErrLastSyncTimeNotFound)
|
2022-09-09 11:26:41 +00:00
|
|
|
}
|
2024-02-13 08:29:33 +00:00
|
|
|
log.DebugLog(ctx, "description: %s", description)
|
2022-09-09 11:26:41 +00:00
|
|
|
splittedString := strings.SplitN(description, ",", 2)
|
2022-09-14 11:09:35 +00:00
|
|
|
if len(splittedString) == 1 {
|
2023-06-09 10:42:50 +00:00
|
|
|
return nil, fmt.Errorf("no snapshot details: %w", corerbd.ErrLastSyncTimeNotFound)
|
2022-09-14 11:09:35 +00:00
|
|
|
}
|
2022-09-09 11:26:41 +00:00
|
|
|
type localStatus struct {
|
2023-06-23 08:43:59 +00:00
|
|
|
LocalSnapshotTime int64 `json:"local_snapshot_timestamp"`
|
|
|
|
LastSnapshotBytes int64 `json:"last_snapshot_bytes"`
|
|
|
|
LastSnapshotDuration *int64 `json:"last_snapshot_sync_seconds"`
|
2022-09-09 11:26:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 10:42:50 +00:00
|
|
|
var localSnapInfo localStatus
|
|
|
|
err := json.Unmarshal([]byte(splittedString[1]), &localSnapInfo)
|
2022-09-09 11:26:41 +00:00
|
|
|
if err != nil {
|
2023-06-09 10:42:50 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal local snapshot info: %w", err)
|
2022-09-09 11:26:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 15:02:18 +00:00
|
|
|
// If the json unmarsal is successful but the local snapshot time is 0, we
|
|
|
|
// need to consider it as an error as the LastSyncTime is required.
|
2023-06-09 10:42:50 +00:00
|
|
|
if localSnapInfo.LocalSnapshotTime == 0 {
|
2023-01-12 09:52:51 +00:00
|
|
|
return nil, fmt.Errorf("empty local snapshot timestamp: %w", corerbd.ErrLastSyncTimeNotFound)
|
2022-11-01 15:02:18 +00:00
|
|
|
}
|
2023-06-23 08:43:59 +00:00
|
|
|
if localSnapInfo.LastSnapshotDuration != nil {
|
2023-06-09 10:42:50 +00:00
|
|
|
// converts localSnapshotDuration of type int64 to string format with
|
|
|
|
// appended `s` seconds required for time.ParseDuration
|
2023-06-23 08:43:59 +00:00
|
|
|
lastDurationTime := fmt.Sprintf("%ds", *localSnapInfo.LastSnapshotDuration)
|
2023-06-09 10:42:50 +00:00
|
|
|
// parse Duration from the lastDurationTime string
|
|
|
|
lastDuration, err := time.ParseDuration(lastDurationTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse last snapshot duration: %w", err)
|
|
|
|
}
|
|
|
|
// converts time.Duration to *durationpb.Duration
|
|
|
|
response.LastSyncDuration = durationpb.New(lastDuration)
|
|
|
|
}
|
2022-11-01 15:02:18 +00:00
|
|
|
|
2023-06-09 10:42:50 +00:00
|
|
|
// converts localSnapshotTime of type int64 to time.Time
|
|
|
|
lastUpdateTime := time.Unix(localSnapInfo.LocalSnapshotTime, 0)
|
2022-09-09 11:26:41 +00:00
|
|
|
lastSyncTime := timestamppb.New(lastUpdateTime)
|
|
|
|
|
2023-06-09 10:42:50 +00:00
|
|
|
response.LastSyncTime = lastSyncTime
|
|
|
|
response.LastSyncBytes = localSnapInfo.LastSnapshotBytes
|
|
|
|
|
|
|
|
return &response, nil
|
2022-09-09 11:26:41 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 08:29:33 +00:00
|
|
|
func checkVolumeResyncStatus(ctx context.Context, localStatus librbd.SiteMirrorImageStatus) error {
|
2023-08-22 16:52:49 +00:00
|
|
|
// we are considering local snapshot timestamp to check if the resync is
|
|
|
|
// started or not, if we dont see local_snapshot_timestamp in the
|
|
|
|
// description of localStatus, we are returning error. if we see the local
|
|
|
|
// snapshot timestamp in the description we return resyncing started.
|
|
|
|
description := localStatus.Description
|
2024-02-13 08:29:33 +00:00
|
|
|
resp, err := getLastSyncInfo(ctx, description)
|
2023-08-22 16:52:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get last sync info: %w", err)
|
|
|
|
}
|
2024-04-04 08:51:19 +00:00
|
|
|
if resp.GetLastSyncTime() == nil {
|
2023-08-22 16:52:49 +00:00
|
|
|
return errors.New("last sync time is nil")
|
2021-10-25 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|