Skip to content

Commit

Permalink
WIP: review fix1
Browse files Browse the repository at this point in the history
Signed-off-by: Shinya Hayashi <shinya-hayashi@cybozu.co.jp>
  • Loading branch information
peng225 committed Oct 16, 2024
1 parent 331f0c4 commit 6be7ae4
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions internal/controller/mantlebackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"
"time"

mantlev1 "github.com/cybozu-go/mantle/api/v1"
Expand Down Expand Up @@ -612,17 +611,20 @@ func (r *MantleBackupReconciler) finalize(
}

type dataSyncPrepareResult struct {
isIncremental bool
isIncremental bool // This field is valid iff isSecondaryMantleBackupReadyToUse is false.
isSecondaryMantleBackupReadyToUse bool
diffFrom *mantlev1.MantleBackup // non-nil value iff isIncremental is true.
diffFrom *mantlev1.MantleBackup // non-nil value iff isIncremental or isSecondaryMantleBackupReadyToUse is true.
}

func (r *MantleBackupReconciler) prepareForDataSynchronization(
ctx context.Context,
backup *mantlev1.MantleBackup,
msc proto.MantleServiceClient,
) (*dataSyncPrepareResult, error) {
exportTargetPVCUID := backup.GetLabels()[labelLocalBackupTargetPVCUID]
exportTargetPVCUID, ok := backup.GetLabels()[labelLocalBackupTargetPVCUID]
if !ok {
return nil, fmt.Errorf(`"%s" label is missing`, labelLocalBackupTargetPVCUID)
}
resp, err := msc.ListMantleBackup(
ctx,
&proto.ListMantleBackupRequest{
Expand All @@ -638,10 +640,10 @@ func (r *MantleBackupReconciler) prepareForDataSynchronization(
if err != nil {
return nil, err
}
secondaryBackupSet := convertToMap(secondaryBackups)
secondaryBackupMap := convertToMap(secondaryBackups)

isSecondaryMantleBackupReadyToUse := false
secondaryBackup, ok := secondaryBackupSet[backup.GetName()]
secondaryBackup, ok := secondaryBackupMap[backup.GetName()]
if !ok {
return nil, fmt.Errorf("secondary MantleBackup not found: %s, %s",
backup.GetName(), backup.GetNamespace())
Expand All @@ -651,6 +653,14 @@ func (r *MantleBackupReconciler) prepareForDataSynchronization(
mantlev1.BackupConditionReadyToUse,
)

if isSecondaryMantleBackupReadyToUse {
return &dataSyncPrepareResult{
isIncremental: false,
isSecondaryMantleBackupReadyToUse: true,
diffFrom: nil,
}, nil
}

if syncMode, ok := backup.GetAnnotations()[annotSyncMode]; ok {
switch syncMode {
case syncModeFull:
Expand Down Expand Up @@ -694,7 +704,7 @@ func (r *MantleBackupReconciler) prepareForDataSynchronization(
return nil, err
}

diffFrom := searchForDiffOriginMantleBackup(backup, primaryBackupList.Items, secondaryBackupSet)
diffFrom := searchForDiffOriginMantleBackup(backup, primaryBackupList.Items, secondaryBackupMap)
isIncremental := (diffFrom != nil)

return &dataSyncPrepareResult{
Expand All @@ -718,7 +728,7 @@ func searchForDiffOriginMantleBackup(
primaryBackups []mantlev1.MantleBackup,
secondaryBackupSet map[string]*mantlev1.MantleBackup,
) *mantlev1.MantleBackup {
candidates := make([]*mantlev1.MantleBackup, 0)
var diffOrigin *mantlev1.MantleBackup
for _, primaryBackup := range primaryBackups {
primaryBackup := primaryBackup
secondaryBackup, ok := secondaryBackupSet[primaryBackup.Name]
Expand All @@ -735,16 +745,12 @@ func searchForDiffOriginMantleBackup(
if *backup.Status.SnapID <= *primaryBackup.Status.SnapID {
continue
}
candidates = append(candidates, &primaryBackup)
}
if len(candidates) == 0 {
return nil
if diffOrigin == nil || *diffOrigin.Status.SnapID < *primaryBackup.Status.SnapID {
diffOrigin = &primaryBackup
}
}
// Sort by SnapID in descending order.
sort.Slice(candidates, func(i, j int) bool {
return *candidates[i].Status.SnapID > *candidates[j].Status.SnapID
})
return candidates[0]

return diffOrigin
}

func (r *MantleBackupReconciler) export(
Expand Down

0 comments on commit 6be7ae4

Please sign in to comment.