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 <rar@redhat.com>
This commit is contained in:
Rakshith R 2023-06-23 14:13:59 +05:30 committed by mergify[bot]
parent 2e2e9044c0
commit 1ca6f004a5
2 changed files with 17 additions and 7 deletions

View File

@ -814,9 +814,9 @@ func getLastSyncInfo(description string) (*replication.GetVolumeReplicationInfoR
return nil, fmt.Errorf("no snapshot details: %w", corerbd.ErrLastSyncTimeNotFound) return nil, fmt.Errorf("no snapshot details: %w", corerbd.ErrLastSyncTimeNotFound)
} }
type localStatus struct { type localStatus struct {
LocalSnapshotTime int64 `json:"local_snapshot_timestamp"` LocalSnapshotTime int64 `json:"local_snapshot_timestamp"`
LastSnapshotBytes int64 `json:"last_snapshot_bytes"` LastSnapshotBytes int64 `json:"last_snapshot_bytes"`
LastSnapshotDuration int64 `json:"last_snapshot_sync_seconds"` LastSnapshotDuration *int64 `json:"last_snapshot_sync_seconds"`
} }
var localSnapInfo localStatus var localSnapInfo localStatus
@ -830,12 +830,10 @@ func getLastSyncInfo(description string) (*replication.GetVolumeReplicationInfoR
if localSnapInfo.LocalSnapshotTime == 0 { if localSnapInfo.LocalSnapshotTime == 0 {
return nil, fmt.Errorf("empty local snapshot timestamp: %w", corerbd.ErrLastSyncTimeNotFound) return nil, fmt.Errorf("empty local snapshot timestamp: %w", corerbd.ErrLastSyncTimeNotFound)
} }
if localSnapInfo.LastSnapshotDuration == 0 { if localSnapInfo.LastSnapshotDuration != nil {
response.LastSyncDuration = nil
} else {
// converts localSnapshotDuration of type int64 to string format with // converts localSnapshotDuration of type int64 to string format with
// appended `s` seconds required for time.ParseDuration // 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 // parse Duration from the lastDurationTime string
lastDuration, err := time.ParseDuration(lastDurationTime) lastDuration, err := time.ParseDuration(lastDurationTime)
if err != nil { if err != nil {

View File

@ -512,6 +512,18 @@ func TestValidateLastSyncInfo(t *testing.T) {
}, },
expectedErr: "", 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", name: "description with invalid JSON",
//nolint:lll // sample output cannot be split into multiple lines. //nolint:lll // sample output cannot be split into multiple lines.