From 1ca6f004a53632e9573ac907ff14bdaddddf1c1c Mon Sep 17 00:00:00 2001 From: Rakshith R Date: Fri, 23 Jun 2023 14:13:59 +0530 Subject: [PATCH] rbd: (lastSyncInfo)handle last sync duration being empty case This commit modifies code to handle last sync duration being empty & 0,returning nil & 0 on encountering it respectively. Earlier both case return 0. Test case is added too. Signed-off-by: Rakshith R --- internal/csi-addons/rbd/replication.go | 12 +++++------- internal/csi-addons/rbd/replication_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/internal/csi-addons/rbd/replication.go b/internal/csi-addons/rbd/replication.go index 167263fb7..0cc4ce58a 100644 --- a/internal/csi-addons/rbd/replication.go +++ b/internal/csi-addons/rbd/replication.go @@ -814,9 +814,9 @@ func getLastSyncInfo(description string) (*replication.GetVolumeReplicationInfoR return nil, fmt.Errorf("no snapshot details: %w", corerbd.ErrLastSyncTimeNotFound) } type localStatus struct { - LocalSnapshotTime int64 `json:"local_snapshot_timestamp"` - LastSnapshotBytes int64 `json:"last_snapshot_bytes"` - LastSnapshotDuration int64 `json:"last_snapshot_sync_seconds"` + LocalSnapshotTime int64 `json:"local_snapshot_timestamp"` + LastSnapshotBytes int64 `json:"last_snapshot_bytes"` + LastSnapshotDuration *int64 `json:"last_snapshot_sync_seconds"` } var localSnapInfo localStatus @@ -830,12 +830,10 @@ func getLastSyncInfo(description string) (*replication.GetVolumeReplicationInfoR if localSnapInfo.LocalSnapshotTime == 0 { return nil, fmt.Errorf("empty local snapshot timestamp: %w", corerbd.ErrLastSyncTimeNotFound) } - if localSnapInfo.LastSnapshotDuration == 0 { - response.LastSyncDuration = nil - } else { + if localSnapInfo.LastSnapshotDuration != nil { // converts localSnapshotDuration of type int64 to string format with // appended `s` seconds required for time.ParseDuration - lastDurationTime := fmt.Sprintf("%ds", localSnapInfo.LastSnapshotDuration) + lastDurationTime := fmt.Sprintf("%ds", *localSnapInfo.LastSnapshotDuration) // parse Duration from the lastDurationTime string lastDuration, err := time.ParseDuration(lastDurationTime) if err != nil { diff --git a/internal/csi-addons/rbd/replication_test.go b/internal/csi-addons/rbd/replication_test.go index 5a89e057c..8a7ea8806 100644 --- a/internal/csi-addons/rbd/replication_test.go +++ b/internal/csi-addons/rbd/replication_test.go @@ -512,6 +512,18 @@ func TestValidateLastSyncInfo(t *testing.T) { }, expectedErr: "", }, + { + name: "description with last_snapshot_sync_seconds = 0", + //nolint:lll // sample output cannot be split into multiple lines. + description: `replaying, {"bytes_per_second":0.0,"bytes_per_snapshot":81920.0,"last_snapshot_sync_seconds":0, + "last_snapshot_bytes":81920,"local_snapshot_timestamp":1684675261,"remote_snapshot_timestamp":1684675261,"replay_state":"idle"}`, + info: &replication.GetVolumeReplicationInfoResponse{ + LastSyncDuration: durationpb.New(time.Duration(0)), + LastSyncTime: timestamppb.New(time.Unix(1684675261, 0)), + LastSyncBytes: 81920, + }, + expectedErr: "", + }, { name: "description with invalid JSON", //nolint:lll // sample output cannot be split into multiple lines.