2021-02-11 09:59:47 +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
|
|
|
|
|
2023-06-02 09:49:22 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2021-02-11 09:59:47 +00:00
|
|
|
|
|
|
|
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 (
|
2021-12-22 03:36:37 +00:00
|
|
|
"context"
|
2021-02-11 09:59:47 +00:00
|
|
|
"fmt"
|
2021-12-22 03:36:37 +00:00
|
|
|
"time"
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/rbd/types"
|
2021-12-22 03:36:37 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2024-07-26 08:01:02 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2021-02-11 09:59:47 +00:00
|
|
|
|
|
|
|
librbd "github.com/ceph/go-ceph/rbd"
|
|
|
|
)
|
|
|
|
|
2024-06-06 11:44:08 +00:00
|
|
|
// HandleParentImageExistence checks the image's parent.
|
|
|
|
// if the parent image does not exist and is not in trash, it returns nil.
|
|
|
|
// if the flattenMode is FlattenModeForce, it flattens the image itself.
|
|
|
|
// if the parent image is in trash, it returns an error.
|
|
|
|
// if the parent image exists and is not enabled for mirroring, it returns an error.
|
|
|
|
func (rv *rbdVolume) HandleParentImageExistence(
|
|
|
|
ctx context.Context,
|
2024-07-26 08:01:02 +00:00
|
|
|
mode types.FlattenMode,
|
2024-06-06 11:44:08 +00:00
|
|
|
) error {
|
|
|
|
if rv.ParentName == "" && !rv.ParentInTrash {
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-26 08:01:02 +00:00
|
|
|
if mode == types.FlattenModeForce {
|
2024-06-06 11:44:08 +00:00
|
|
|
// Delete temp image that exists for volume datasource since
|
|
|
|
// it is no longer required when the live image is flattened.
|
|
|
|
err := rv.DeleteTempImage(ctx)
|
|
|
|
if err != nil {
|
2024-07-30 16:29:33 +00:00
|
|
|
return fmt.Errorf("failed to delete temporary rbd image %s: %w", rv, err)
|
2024-06-06 11:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = rv.flattenRbdImage(ctx, true, 0, 0)
|
|
|
|
if err != nil {
|
2024-07-30 16:29:33 +00:00
|
|
|
return fmt.Errorf("failed to flatten image %s: %w", rv, err)
|
2024-06-06 11:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rv.ParentInTrash {
|
|
|
|
return fmt.Errorf("%w: failed to enable mirroring on image %q:"+
|
|
|
|
" parent is in trash",
|
|
|
|
ErrFailedPrecondition, rv)
|
|
|
|
}
|
|
|
|
|
|
|
|
parent, err := rv.getParent()
|
|
|
|
if err != nil {
|
2024-07-30 16:29:33 +00:00
|
|
|
return fmt.Errorf("failed to get parent of image %s: %w", rv, err)
|
2024-06-06 11:44:08 +00:00
|
|
|
}
|
2024-07-30 16:42:35 +00:00
|
|
|
parentMirroringInfo, err := parent.GetMirroringInfo(ctx)
|
2024-06-06 11:44:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to get mirroring info of parent %q of image %q: %w",
|
|
|
|
parent, rv, err)
|
|
|
|
}
|
2024-07-26 08:01:02 +00:00
|
|
|
if parentMirroringInfo.GetState() != librbd.MirrorImageEnabled.String() {
|
2024-06-06 11:44:08 +00:00
|
|
|
return fmt.Errorf("%w: failed to enable mirroring on image %q: "+
|
|
|
|
"parent image %q is not enabled for mirroring",
|
|
|
|
ErrFailedPrecondition, rv, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// check that rbdVolume implements the types.Mirror interface.
|
|
|
|
var _ types.Mirror = &rbdVolume{}
|
|
|
|
|
|
|
|
// EnableMirroring enables mirroring on an image.
|
2024-07-30 16:42:35 +00:00
|
|
|
func (ri *rbdImage) EnableMirroring(_ context.Context, mode librbd.ImageMirrorMode) error {
|
2021-03-17 07:54:19 +00:00
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
err = image.MirrorEnable(mode)
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to enable mirroring on %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// DisableMirroring disables mirroring on an image.
|
2024-07-30 16:42:35 +00:00
|
|
|
func (ri *rbdImage) DisableMirroring(_ context.Context, force bool) error {
|
2021-03-17 07:54:19 +00:00
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
err = image.MirrorDisable(force)
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to disable mirroring on %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// GetMirroringInfo gets mirroring information of an image.
|
2024-07-30 16:42:35 +00:00
|
|
|
func (ri *rbdImage) GetMirroringInfo(_ context.Context) (types.MirrorInfo, error) {
|
2021-03-17 07:54:19 +00:00
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
info, err := image.GetMirrorImageInfo()
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to get mirroring info of %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
return ImageStatus{MirrorImageInfo: info}, nil
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// Promote promotes image to primary.
|
2024-07-30 16:42:35 +00:00
|
|
|
func (ri *rbdImage) Promote(_ context.Context, force bool) error {
|
2021-03-17 07:54:19 +00:00
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorPromote(force)
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to promote image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// ForcePromote promotes image to primary with force option with 2 minutes
|
2022-03-29 09:52:27 +00:00
|
|
|
// timeout. If there is no response within 2 minutes,the rbd CLI process will be
|
2021-12-22 03:36:37 +00:00
|
|
|
// killed and an error is returned.
|
2024-07-30 16:42:35 +00:00
|
|
|
func (rv *rbdVolume) ForcePromote(ctx context.Context, cr *util.Credentials) error {
|
2021-12-22 03:36:37 +00:00
|
|
|
promoteArgs := []string{
|
|
|
|
"mirror", "image", "promote",
|
|
|
|
rv.String(),
|
|
|
|
"--force",
|
|
|
|
"--id", cr.ID,
|
|
|
|
"-m", rv.Monitors,
|
|
|
|
"--keyfile=" + cr.KeyFile,
|
|
|
|
}
|
|
|
|
_, stderr, err := util.ExecCommandWithTimeout(
|
2024-07-30 16:42:35 +00:00
|
|
|
ctx,
|
2022-03-29 09:52:27 +00:00
|
|
|
// 2 minutes timeout as the Replication RPC timeout is 2.5 minutes.
|
|
|
|
2*time.Minute,
|
2021-12-22 03:36:37 +00:00
|
|
|
"rbd",
|
|
|
|
promoteArgs...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to promote image %q with error: %w", rv, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stderr != "" {
|
|
|
|
return fmt.Errorf("failed to promote image %q with stderror: %s", rv, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// Demote demotes image to secondary.
|
2024-07-30 16:42:35 +00:00
|
|
|
func (ri *rbdImage) Demote(_ context.Context) error {
|
2021-03-17 07:54:19 +00:00
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorDemote()
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to demote image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-02-11 09:59:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// Resync resync image to correct the split-brain.
|
2024-07-30 16:42:35 +00:00
|
|
|
func (ri *rbdImage) Resync(_ context.Context) error {
|
2021-03-17 07:54:19 +00:00
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorResync()
|
|
|
|
if err != nil {
|
2021-05-07 05:30:37 +00:00
|
|
|
return fmt.Errorf("failed to resync image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// If we issued a resync, return a non-final error as image needs to be recreated
|
|
|
|
// locally. Caller retries till RBD syncs an initial version of the image to
|
|
|
|
// report its status in the resync request.
|
|
|
|
return fmt.Errorf("%w: awaiting initial resync due to split brain", ErrUnavailable)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// GetGlobalMirroringStatus get the mirroring status of an image.
|
2024-07-30 16:42:35 +00:00
|
|
|
func (ri *rbdImage) GetGlobalMirroringStatus(_ context.Context) (types.GlobalStatus, error) {
|
2021-06-23 13:14:48 +00:00
|
|
|
image, err := ri.open()
|
2021-02-11 09:59:47 +00:00
|
|
|
if err != nil {
|
2021-06-23 13:14:48 +00:00
|
|
|
return nil, fmt.Errorf("failed to open image %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-06-23 13:14:48 +00:00
|
|
|
defer image.Close()
|
|
|
|
statusInfo, err := image.GetGlobalMirrorStatus()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get image mirroring status %q with error: %w", ri, err)
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
return GlobalMirrorStatus{GlobalMirrorImageStatus: statusInfo}, nil
|
2021-02-11 09:59:47 +00:00
|
|
|
}
|
2021-08-12 05:17:25 +00:00
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
// ImageStatus is a wrapper around librbd.MirrorImageInfo that contains the
|
|
|
|
// image mirror status.
|
|
|
|
type ImageStatus struct {
|
|
|
|
*librbd.MirrorImageInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status ImageStatus) GetState() string {
|
|
|
|
return status.State.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status ImageStatus) IsPrimary() bool {
|
|
|
|
return status.Primary
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalMirrorStatus is a wrapper around librbd.GlobalMirrorImageStatus that contains the
|
|
|
|
// global mirror image status.
|
|
|
|
type GlobalMirrorStatus struct {
|
|
|
|
librbd.GlobalMirrorImageStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status GlobalMirrorStatus) GetState() string {
|
|
|
|
return status.GlobalMirrorImageStatus.Info.State.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status GlobalMirrorStatus) IsPrimary() bool {
|
|
|
|
return status.GlobalMirrorImageStatus.Info.Primary
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status GlobalMirrorStatus) GetLocalSiteStatus() (types.SiteStatus, error) {
|
|
|
|
s, err := status.GlobalMirrorImageStatus.LocalStatus()
|
2021-08-12 05:17:25 +00:00
|
|
|
if err != nil {
|
2024-07-26 08:01:02 +00:00
|
|
|
err = fmt.Errorf("failed to get local site status: %w", err)
|
2021-08-12 05:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
return SiteMirrorImageStatus{
|
|
|
|
SiteMirrorImageStatus: s,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status GlobalMirrorStatus) GetAllSitesStatus() []types.SiteStatus {
|
|
|
|
var siteStatuses []types.SiteStatus
|
|
|
|
for _, ss := range status.SiteStatuses {
|
|
|
|
siteStatuses = append(siteStatuses, SiteMirrorImageStatus{SiteMirrorImageStatus: ss})
|
2021-08-12 05:17:25 +00:00
|
|
|
}
|
2024-07-26 08:01:02 +00:00
|
|
|
|
|
|
|
return siteStatuses
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (status GlobalMirrorStatus) GetRemoteSiteStatus(ctx context.Context) (types.SiteStatus, error) {
|
|
|
|
var (
|
|
|
|
ss librbd.SiteMirrorImageStatus
|
|
|
|
err error = librbd.ErrNotExist
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := range status.SiteStatuses {
|
|
|
|
log.DebugLog(
|
|
|
|
ctx,
|
|
|
|
"Site status of MirrorUUID: %s, state: %s, description: %s, lastUpdate: %v, up: %t",
|
|
|
|
status.SiteStatuses[i].MirrorUUID,
|
|
|
|
status.SiteStatuses[i].State,
|
|
|
|
status.SiteStatuses[i].Description,
|
|
|
|
status.SiteStatuses[i].LastUpdate,
|
|
|
|
status.SiteStatuses[i].Up)
|
|
|
|
|
|
|
|
if status.SiteStatuses[i].MirrorUUID != "" {
|
|
|
|
ss = status.SiteStatuses[i]
|
|
|
|
err = nil
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2021-08-12 05:17:25 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 08:01:02 +00:00
|
|
|
return SiteMirrorImageStatus{SiteMirrorImageStatus: ss}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SiteMirrorImageStatus is a wrapper around librbd.SiteMirrorImageStatus that contains the
|
|
|
|
// site mirror image status.
|
|
|
|
type SiteMirrorImageStatus struct {
|
|
|
|
librbd.SiteMirrorImageStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status SiteMirrorImageStatus) GetMirrorUUID() string {
|
|
|
|
return status.MirrorUUID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status SiteMirrorImageStatus) GetState() string {
|
|
|
|
return status.State.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status SiteMirrorImageStatus) GetDescription() string {
|
|
|
|
return status.Description
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status SiteMirrorImageStatus) IsUP() bool {
|
|
|
|
return status.Up
|
|
|
|
}
|
|
|
|
|
|
|
|
func (status SiteMirrorImageStatus) GetLastUpdate() time.Time {
|
|
|
|
// convert the last update time to UTC
|
|
|
|
return time.Unix(status.LastUpdate, 0).UTC()
|
2021-08-12 05:17:25 +00:00
|
|
|
}
|