From 6e5f2b7cec3ec3942a914a2183a357ff025ff759 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Thu, 21 Nov 2024 13:38:49 +0100 Subject: [PATCH] ci: extra commit will be dropped This is extra commit will be dropped Signed-off-by: Madhu Rajanna --- internal/csi-addons/rbd/replication.go | 6 +- internal/rbd/group/group_mirror.go | 55 ++++++++- .../ceph/go-ceph/rbd/mirror_group.go | 115 ++++++++++++------ 3 files changed, 137 insertions(+), 39 deletions(-) diff --git a/internal/csi-addons/rbd/replication.go b/internal/csi-addons/rbd/replication.go index 70efb50007d..9c7a68714fe 100644 --- a/internal/csi-addons/rbd/replication.go +++ b/internal/csi-addons/rbd/replication.go @@ -859,6 +859,7 @@ func (rs *ReplicationServer) GetVolumeReplicationInfo(ctx context.Context, return nil, status.Error(codes.Internal, err.Error()) } + log.DebugLog(ctx, "mirrorStatus: %+v", mirrorStatus) remoteStatus, err := mirrorStatus.GetRemoteSiteStatus(ctx) if err != nil { log.ErrorLog(ctx, err.Error()) @@ -877,6 +878,7 @@ func (rs *ReplicationServer) GetVolumeReplicationInfo(ctx context.Context, return nil, status.Errorf(codes.Internal, "failed to get last sync info: %v", err) } + log.DebugLog(ctx, "Madhu the response is %v", resp) return resp, nil } @@ -901,7 +903,7 @@ func getLastSyncInfo(ctx context.Context, description string) (*replication.GetV if description == "" { return nil, fmt.Errorf("empty description: %w", corerbd.ErrLastSyncTimeNotFound) } - log.DebugLog(ctx, "description: %s", description) + log.DebugLog(ctx, "Madhu description: %s", description) splittedString := strings.SplitN(description, ",", 2) if len(splittedString) == 1 { return nil, fmt.Errorf("no snapshot details: %w", corerbd.ErrLastSyncTimeNotFound) @@ -918,6 +920,7 @@ func getLastSyncInfo(ctx context.Context, description string) (*replication.GetV return nil, fmt.Errorf("failed to unmarshal local snapshot info: %w", err) } + log.DebugLog(ctx, "Madhu the description after unmarshalling is %v", localSnapInfo) // 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 { @@ -943,6 +946,7 @@ func getLastSyncInfo(ctx context.Context, description string) (*replication.GetV response.LastSyncTime = lastSyncTime response.LastSyncBytes = localSnapInfo.LastSnapshotBytes + log.DebugLog(ctx, "Madhu the return response is %v", response) return &response, nil } diff --git a/internal/rbd/group/group_mirror.go b/internal/rbd/group/group_mirror.go index 5d68597e756..f4a3a61cf9d 100644 --- a/internal/rbd/group/group_mirror.go +++ b/internal/rbd/group/group_mirror.go @@ -15,8 +15,10 @@ package group import ( "context" + "encoding/json" "errors" "fmt" + "strings" "time" "github.com/ceph/go-ceph/rados" @@ -279,6 +281,12 @@ func (status globalMirrorGroupStatus) GetRemoteSiteStatus(ctx context.Context) ( err error = librbd.ErrNotExist ) + type localStatus struct { + LocalSnapshotTime int64 `json:"local_snapshot_timestamp"` + LastSnapshotBytes int64 `json:"last_snapshot_bytes"` + LastSnapshotDuration *int64 `json:"last_snapshot_sync_seconds"` + } + for i := range status.SiteStatuses { log.DebugLog( ctx, @@ -291,8 +299,53 @@ func (status globalMirrorGroupStatus) GetRemoteSiteStatus(ctx context.Context) ( if status.SiteStatuses[i].MirrorUUID != "" { ss = status.SiteStatuses[i] - err = nil + images := status.SiteStatuses[i].MirrorImages + + totalSnpshotTime := int64(0) + totalSnapshotBytes := int64(0) + totalSnapshotDuration := int64(0) + totalImages := len(images) + for _, image := range images { + log.DebugLog(ctx, "Madhu image: %s, state: %s, description: %s, lastUpdate: %v, up: %t", + image.MirrorUUID, + image.State, + image.Description, + image.LastUpdate, + image.Up) + description := image.Description + log.DebugLog(ctx, "[Madhu] description: %s", description) + splittedString := strings.SplitN(description, ",", 2) + if len(splittedString) == 1 { + log.DebugLog(ctx, "no snapshot details", splittedString[0]) + continue + } + var localSnapInfo localStatus + err := json.Unmarshal([]byte(splittedString[1]), &localSnapInfo) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal local snapshot info: %w", err) + } + log.DebugLog(ctx, "Madhu localStatus", localSnapInfo) + totalSnpshotTime += localSnapInfo.LocalSnapshotTime + totalSnapshotBytes += localSnapInfo.LastSnapshotBytes + totalSnapshotDuration += *localSnapInfo.LastSnapshotDuration + } + err = nil + totalDuration := int64(totalSnapshotDuration / int64(totalImages)) + // write the total snapshot time, bytes and duration to the description + d := localStatus{ + LocalSnapshotTime: int64(totalSnpshotTime / int64(totalImages)), + LastSnapshotBytes: int64(totalSnapshotBytes / int64(totalImages)), + LastSnapshotDuration: &totalDuration, + } + description, err := json.Marshal(d) + log.DebugLog(ctx, "description: %s", description) + log.DebugLog(ctx, "description: %v", d) + if err != nil { + + return nil, fmt.Errorf("failed to marshal local snapshot info: %w", err) + } + ss.Description = fmt.Sprintf("%s, %s", ss.Description, description) break } } diff --git a/vendor/github.com/ceph/go-ceph/rbd/mirror_group.go b/vendor/github.com/ceph/go-ceph/rbd/mirror_group.go index c295597932d..a33fd6ee273 100644 --- a/vendor/github.com/ceph/go-ceph/rbd/mirror_group.go +++ b/vendor/github.com/ceph/go-ceph/rbd/mirror_group.go @@ -7,6 +7,7 @@ package rbd // #include import "C" import ( + "errors" "fmt" "unsafe" @@ -156,11 +157,19 @@ func GetMirrorGroupInfo(groupIoctx *rados.IOContext, groupName string) (*MirrorG cGroupName, &cgInfo, C.sizeof_rbd_mirror_group_info_t) - + var err error if ret < 0 { - return nil, getError(ret) + err = getError(ret) + } + if err != nil { + if errors.Is(err, ErrNotFound) || errors.Is(err, rados.RadosErrorNotFound) { + return &MirrorGroupInfo{ + State: MirrorGroupDisabled, + MirrorImageMode: ImageMirrorModeSnapshot, + Primary: false, + }, nil + } } - info := convertMirrorGroupInfo(&cgInfo) // free C memory allocated by C.rbd_mirror_group_get_info call @@ -279,54 +288,86 @@ func GetGlobalMirrorGroupStatus(ioctx *rados.IOContext, groupName string) (Globa s := C.rbd_mirror_group_global_status_t{} cGroupName := C.CString(groupName) defer C.free(unsafe.Pointer(cGroupName)) - // ret := C.rbd_mirror_group_get_global_status( - // cephIoctx(ioctx), - // (*C.char)(cGroupName), - // &s, - // C.sizeof_rbd_mirror_group_global_status_t) - // if err := getError(ret); err != nil { - // return GlobalMirrorGroupStatus{}, err - // } + ret := C.rbd_mirror_group_get_global_status( + cephIoctx(ioctx), + (*C.char)(cGroupName), + &s, + C.sizeof_rbd_mirror_group_global_status_t) + if err := getError(ret); err != nil { + return GlobalMirrorGroupStatus{}, err + } status := newGlobalMirrorGroupStatus(&s) return status, nil } -func newGlobalMirrorGroupStatus( - s *C.rbd_mirror_group_global_status_t) GlobalMirrorGroupStatus { - +func newGlobalMirrorGroupStatus(s *C.rbd_mirror_group_global_status_t) GlobalMirrorGroupStatus { + // Initializing the status object status := GlobalMirrorGroupStatus{ Name: C.GoString(s.name), Info: convertMirrorGroupInfo(&s.info), SiteStatusesCount: int(s.site_statuses_count), SiteStatuses: make([]SiteMirrorGroupStatus, s.site_statuses_count), } - gsscs := (*groupSiteArray)(unsafe.Pointer(s.site_statuses))[:s.site_statuses_count:s.site_statuses_count] - for i := C.uint32_t(0); i < s.site_statuses_count; i++ { - gss := gsscs[i] - status.SiteStatuses[i] = SiteMirrorGroupStatus{ - MirrorUUID: C.GoString(gss.mirror_uuid), - MirrorImageGlobalIDs: C.GoString(*gss.mirror_image_global_ids), - MirrorImagePoolIDs: int64(*gss.mirror_image_pool_ids), - State: MirrorGroupStatusState(gss.state), - Description: C.GoString(gss.description), - MirrorImageCount: int(gss.mirror_image_count), - LastUpdate: int64(gss.last_update), - MirrorImages: make([]SiteMirrorImageStatus, gss.mirror_image_count), - Up: bool(gss.up), - } - sscs := (*siteArray)(unsafe.Pointer(gss.mirror_images))[:gss.mirror_image_count:gss.mirror_image_count] - for i := C.uint32_t(0); i < gss.mirror_image_count; i++ { - ss := sscs[i] - status.SiteStatuses[i].MirrorImages[i] = SiteMirrorImageStatus{ - MirrorUUID: C.GoString(ss.mirror_uuid), - State: MirrorImageStatusState(ss.state), - Description: C.GoString(ss.description), - LastUpdate: int64(ss.last_update), - Up: bool(ss.up), + // Print the count of site statuses for debugging + fmt.Println("status.SiteStatusesCount: ", s.site_statuses_count) + + fmt.Printf("s.site_statuses: %+v\n", s.site_statuses) + // Check if site statuses are not null before using them + if s.site_statuses != nil && s.site_statuses_count > 0 { + gsscs := (*groupSiteArray)(unsafe.Pointer(s.site_statuses))[:s.site_statuses_count:s.site_statuses_count] + for i := C.uint32_t(0); i < s.site_statuses_count; i++ { + gss := gsscs[i] + // Ensure that fields are valid before using them + if gss.mirror_uuid != nil && gss.mirror_image_global_ids != nil { + status.SiteStatuses[i] = SiteMirrorGroupStatus{ + MirrorUUID: C.GoString(gss.mirror_uuid), + MirrorImageGlobalIDs: C.GoString(*gss.mirror_image_global_ids), + MirrorImagePoolIDs: int64(*gss.mirror_image_pool_ids), + State: MirrorGroupStatusState(gss.state), + Description: C.GoString(gss.description), + MirrorImageCount: int(gss.mirror_image_count), + LastUpdate: int64(gss.last_update), + MirrorImages: make([]SiteMirrorImageStatus, gss.mirror_image_count), + Up: bool(gss.up), + } + + // Check if the mirror_images pointer is valid + if gss.mirror_images != nil && gss.mirror_image_count > 0 { + + sscs := (*siteArray)(unsafe.Pointer(gss.mirror_images))[:gss.mirror_image_count:gss.mirror_image_count] + fmt.Printf("sscs: siteArray %+v\n", sscs) + for j := C.uint32_t(0); j < gss.mirror_image_count; j++ { + ss := sscs[j] + + // Ensure that fields are valid before using them + if ss.mirror_uuid != nil { + status.SiteStatuses[i].MirrorImages[j] = SiteMirrorImageStatus{ + MirrorUUID: C.GoString(ss.mirror_uuid), + State: MirrorImageStatusState(ss.state), + Description: C.GoString(ss.description), + LastUpdate: int64(ss.last_update), + Up: bool(ss.up), + } + } else { + // Log if a field is invalid + fmt.Println("Warning: mirror_uuid is nil at index", i, j) + } + } + } else { + // Handle case where mirror_images is nil or mirror_image_count is 0 + fmt.Println("Warning: mirror_images is nil or mirror_image_count is 0 at index", i) + } + } else { + // Handle case where mirror_uuid or mirror_image_global_ids is nil + fmt.Println("Warning: mirror_uuid or mirror_image_global_ids is nil at index", i) } } + } else { + // Handle case where site statuses are nil or count is 0 + fmt.Println("Warning: site_statuses is nil or site_statuses_count is 0") } + // Return the populated status return status }