2021-02-11 15:29:47 +05:30
|
|
|
/*
|
|
|
|
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 11:49:22 +02:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2021-02-11 15:29:47 +05:30
|
|
|
|
|
|
|
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 09:06:37 +05:30
|
|
|
"context"
|
2025-04-15 11:40:55 +02:00
|
|
|
"encoding/json"
|
2021-02-11 15:29:47 +05:30
|
|
|
"fmt"
|
2025-04-15 11:40:55 +02:00
|
|
|
"strings"
|
2021-12-22 09:06:37 +05:30
|
|
|
"time"
|
|
|
|
|
2025-03-03 11:45:43 +05:30
|
|
|
rbderrors "github.com/ceph/ceph-csi/internal/rbd/errors"
|
2024-07-26 10:01:02 +02:00
|
|
|
"github.com/ceph/ceph-csi/internal/rbd/types"
|
2021-12-22 09:06:37 +05:30
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2024-07-26 10:01:02 +02:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2021-02-11 15:29:47 +05:30
|
|
|
|
|
|
|
librbd "github.com/ceph/go-ceph/rbd"
|
|
|
|
)
|
|
|
|
|
2024-06-06 17:14:08 +05:30
|
|
|
// 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 10:01:02 +02:00
|
|
|
mode types.FlattenMode,
|
2024-06-06 17:14:08 +05:30
|
|
|
) error {
|
|
|
|
if rv.ParentName == "" && !rv.ParentInTrash {
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-26 10:01:02 +02:00
|
|
|
if mode == types.FlattenModeForce {
|
2024-06-06 17:14:08 +05:30
|
|
|
// 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 18:29:33 +02:00
|
|
|
return fmt.Errorf("failed to delete temporary rbd image %s: %w", rv, err)
|
2024-06-06 17:14:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
err = rv.flattenRbdImage(ctx, true, 0, 0)
|
|
|
|
if err != nil {
|
2024-07-30 18:29:33 +02:00
|
|
|
return fmt.Errorf("failed to flatten image %s: %w", rv, err)
|
2024-06-06 17:14:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rv.ParentInTrash {
|
|
|
|
return fmt.Errorf("%w: failed to enable mirroring on image %q:"+
|
|
|
|
" parent is in trash",
|
2025-03-03 11:45:43 +05:30
|
|
|
rbderrors.ErrFailedPrecondition, rv)
|
2024-06-06 17:14:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
parent, err := rv.getParent()
|
|
|
|
if err != nil {
|
2024-07-30 18:29:33 +02:00
|
|
|
return fmt.Errorf("failed to get parent of image %s: %w", rv, err)
|
2024-06-06 17:14:08 +05:30
|
|
|
}
|
2025-04-15 14:15:53 +02:00
|
|
|
|
|
|
|
pm, err := parent.ToMirror()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert parent image %s to mirror type: %w", parent, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parentMirroringInfo, err := pm.GetMirroringInfo(ctx)
|
2024-06-06 17:14:08 +05:30
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to get mirroring info of parent %q of image %q: %w",
|
2025-04-15 14:15:53 +02:00
|
|
|
pm, rv, err)
|
2024-06-06 17:14:08 +05:30
|
|
|
}
|
2024-07-26 10:01:02 +02:00
|
|
|
if parentMirroringInfo.GetState() != librbd.MirrorImageEnabled.String() {
|
2024-06-06 17:14:08 +05:30
|
|
|
return fmt.Errorf("%w: failed to enable mirroring on image %q: "+
|
|
|
|
"parent image %q is not enabled for mirroring",
|
2025-04-15 14:15:53 +02:00
|
|
|
rbderrors.ErrFailedPrecondition, rv, pm)
|
2024-06-06 17:14:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-04-15 14:15:53 +02:00
|
|
|
// rbdMirror is an extended rbdImage type that implements the types.Mirror interface.
|
|
|
|
type rbdMirror struct {
|
|
|
|
rbdImage
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
// check that rbdVolume implements the types.Mirror interface.
|
2025-04-15 14:15:53 +02:00
|
|
|
var _ types.Mirror = &rbdMirror{}
|
|
|
|
|
|
|
|
func (ri *rbdImage) ToMirror() (types.Mirror, error) {
|
|
|
|
return &rbdMirror{
|
|
|
|
rbdImage: *ri,
|
|
|
|
}, nil
|
|
|
|
}
|
2024-07-26 10:01:02 +02:00
|
|
|
|
|
|
|
// EnableMirroring enables mirroring on an image.
|
2025-04-15 14:15:53 +02:00
|
|
|
func (rm *rbdMirror) EnableMirroring(_ context.Context, mode librbd.ImageMirrorMode) error {
|
|
|
|
image, err := rm.open()
|
2021-02-11 15:29:47 +05:30
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
err = image.MirrorEnable(mode)
|
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to enable mirroring on %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-07-22 11:15:17 +05:30
|
|
|
|
2021-02-11 15:29:47 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
// DisableMirroring disables mirroring on an image.
|
2025-04-15 14:15:53 +02:00
|
|
|
func (rm *rbdMirror) DisableMirroring(_ context.Context, force bool) error {
|
|
|
|
image, err := rm.open()
|
2021-02-11 15:29:47 +05:30
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
err = image.MirrorDisable(force)
|
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to disable mirroring on %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-07-22 11:15:17 +05:30
|
|
|
|
2021-02-11 15:29:47 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
// GetMirroringInfo gets mirroring information of an image.
|
2025-04-15 14:15:53 +02:00
|
|
|
func (rm *rbdMirror) GetMirroringInfo(_ context.Context) (types.MirrorInfo, error) {
|
|
|
|
image, err := rm.open()
|
2021-02-11 15:29:47 +05:30
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return nil, fmt.Errorf("failed to open image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
info, err := image.GetMirrorImageInfo()
|
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return nil, fmt.Errorf("failed to get mirroring info of %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-07-22 11:15:17 +05:30
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
return ImageStatus{MirrorImageInfo: info}, nil
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
// Promote promotes image to primary.
|
2025-04-15 14:15:53 +02:00
|
|
|
func (rm *rbdMirror) Promote(_ context.Context, force bool) error {
|
|
|
|
image, err := rm.open()
|
2021-02-11 15:29:47 +05:30
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorPromote(force)
|
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to promote image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-07-22 11:15:17 +05:30
|
|
|
|
2021-02-11 15:29:47 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
// ForcePromote promotes image to primary with force option with 2 minutes
|
2022-03-29 15:22:27 +05:30
|
|
|
// timeout. If there is no response within 2 minutes,the rbd CLI process will be
|
2021-12-22 09:06:37 +05:30
|
|
|
// killed and an error is returned.
|
2025-04-15 14:15:53 +02:00
|
|
|
func (rm *rbdMirror) ForcePromote(ctx context.Context, cr *util.Credentials) error {
|
2021-12-22 09:06:37 +05:30
|
|
|
promoteArgs := []string{
|
|
|
|
"mirror", "image", "promote",
|
2025-04-15 14:15:53 +02:00
|
|
|
rm.String(),
|
2021-12-22 09:06:37 +05:30
|
|
|
"--force",
|
|
|
|
"--id", cr.ID,
|
2025-04-15 14:15:53 +02:00
|
|
|
"-m", rm.Monitors,
|
2021-12-22 09:06:37 +05:30
|
|
|
"--keyfile=" + cr.KeyFile,
|
|
|
|
}
|
|
|
|
_, stderr, err := util.ExecCommandWithTimeout(
|
2024-07-30 18:42:35 +02:00
|
|
|
ctx,
|
2022-03-29 15:22:27 +05:30
|
|
|
// 2 minutes timeout as the Replication RPC timeout is 2.5 minutes.
|
|
|
|
2*time.Minute,
|
2021-12-22 09:06:37 +05:30
|
|
|
"rbd",
|
|
|
|
promoteArgs...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to promote image %q with error: %w", rm, err)
|
2021-12-22 09:06:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if stderr != "" {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to promote image %q with stderror: %s", rm, stderr)
|
2021-12-22 09:06:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
// Demote demotes image to secondary.
|
2025-04-15 14:15:53 +02:00
|
|
|
func (rm *rbdMirror) Demote(_ context.Context) error {
|
|
|
|
image, err := rm.open()
|
2021-02-11 15:29:47 +05:30
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorDemote()
|
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to demote image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-07-22 11:15:17 +05:30
|
|
|
|
2021-02-11 15:29:47 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
// Resync resync image to correct the split-brain.
|
2025-04-15 14:15:53 +02:00
|
|
|
func (rm *rbdMirror) Resync(_ context.Context) error {
|
|
|
|
image, err := rm.open()
|
2021-02-11 15:29:47 +05:30
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to open image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
err = image.MirrorResync()
|
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return fmt.Errorf("failed to resync image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-07-22 11:15:17 +05:30
|
|
|
|
2024-07-26 10:01:02 +02: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.
|
2025-03-03 11:45:43 +05:30
|
|
|
return fmt.Errorf("%w: awaiting initial resync due to split brain", rbderrors.ErrUnavailable)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
// GetGlobalMirroringStatus get the mirroring status of an image.
|
2025-04-15 14:15:53 +02:00
|
|
|
func (rm *rbdMirror) GetGlobalMirroringStatus(_ context.Context) (types.GlobalStatus, error) {
|
|
|
|
image, err := rm.open()
|
2021-02-11 15:29:47 +05:30
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return nil, fmt.Errorf("failed to open image %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-06-23 18:44:48 +05:30
|
|
|
defer image.Close()
|
|
|
|
statusInfo, err := image.GetGlobalMirrorStatus()
|
|
|
|
if err != nil {
|
2025-04-15 14:15:53 +02:00
|
|
|
return nil, fmt.Errorf("failed to get image mirroring status %q with error: %w", rm, err)
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-07-22 11:15:17 +05:30
|
|
|
|
2024-07-26 10:01:02 +02:00
|
|
|
return GlobalMirrorStatus{GlobalMirrorImageStatus: statusInfo}, nil
|
2021-02-11 15:29:47 +05:30
|
|
|
}
|
2021-08-12 10:47:25 +05:30
|
|
|
|
2024-07-26 10:01:02 +02: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 10:47:25 +05:30
|
|
|
if err != nil {
|
2024-07-26 10:01:02 +02:00
|
|
|
err = fmt.Errorf("failed to get local site status: %w", err)
|
2021-08-12 10:47:25 +05:30
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02: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 10:47:25 +05:30
|
|
|
}
|
2024-07-26 10:01:02 +02: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 10:47:25 +05:30
|
|
|
}
|
|
|
|
|
2024-07-26 10:01:02 +02: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 10:47:25 +05:30
|
|
|
}
|
2025-04-15 11:40:55 +02:00
|
|
|
|
|
|
|
func (status SiteMirrorImageStatus) GetLastSyncInfo(ctx context.Context) (types.SyncInfo, error) {
|
|
|
|
return newSyncInfo(ctx, status.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
type syncInfo struct {
|
2025-04-15 17:52:30 +02:00
|
|
|
LocalSnapshotTime int64 `json:"local_snapshot_timestamp"`
|
|
|
|
LastSnapshotBytes int64 `json:"last_snapshot_bytes"`
|
|
|
|
LastSnapshotDuration *int64 `json:"last_snapshot_sync_seconds"`
|
|
|
|
ReplayState replayState `json:"replay_state"`
|
2025-04-15 11:40:55 +02:00
|
|
|
}
|
|
|
|
|
2025-04-15 17:52:30 +02:00
|
|
|
type replayState string
|
|
|
|
|
|
|
|
const (
|
|
|
|
idle replayState = "idle"
|
|
|
|
syncing replayState = "syncing"
|
|
|
|
)
|
|
|
|
|
2025-04-15 11:40:55 +02:00
|
|
|
// Type assertion for ensuring an implementation of the full SyncInfo interface.
|
|
|
|
var _ types.SyncInfo = &syncInfo{}
|
|
|
|
|
|
|
|
func newSyncInfo(ctx context.Context, description string) (types.SyncInfo, error) {
|
|
|
|
// Format of the description will be as followed:
|
|
|
|
// 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.
|
|
|
|
// In case there is no local snapshot timestamp return an error as the
|
|
|
|
// LastSyncTime is required.
|
|
|
|
|
|
|
|
if description == "" {
|
|
|
|
return nil, fmt.Errorf("empty description: %w", rbderrors.ErrLastSyncTimeNotFound)
|
|
|
|
}
|
|
|
|
log.DebugLog(ctx, "description: %s", description)
|
|
|
|
splittedString := strings.SplitN(description, ",", 2)
|
|
|
|
if len(splittedString) == 1 {
|
|
|
|
return nil, fmt.Errorf("no snapshot details: %w", rbderrors.ErrLastSyncTimeNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
var localSnapInfo syncInfo
|
|
|
|
err := json.Unmarshal([]byte(splittedString[1]), &localSnapInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal description %q into syncInfo: %w", description, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if localSnapInfo.LocalSnapshotTime == 0 {
|
|
|
|
return nil, fmt.Errorf("empty local snapshot timestamp: %w", rbderrors.ErrLastSyncTimeNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &localSnapInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (si *syncInfo) GetLastSyncTime() time.Time {
|
|
|
|
// converts localSnapshotTime of type int64 to time.Time
|
|
|
|
return time.Unix(si.LocalSnapshotTime, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (si *syncInfo) GetLastSyncBytes() int64 {
|
|
|
|
return si.LastSnapshotBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (si *syncInfo) GetLastSyncDuration() *time.Duration {
|
|
|
|
var duration time.Duration
|
|
|
|
|
|
|
|
if si.LastSnapshotDuration == nil {
|
|
|
|
duration = time.Duration(0)
|
|
|
|
} else {
|
|
|
|
// time.Duration is in nanoseconds
|
|
|
|
duration = time.Duration(*si.LastSnapshotDuration) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
return &duration
|
|
|
|
}
|
2025-04-15 17:52:30 +02:00
|
|
|
|
|
|
|
func (si *syncInfo) IsSyncing() bool {
|
|
|
|
return si.ReplayState == syncing
|
|
|
|
}
|