diff --git a/backup/backup.go b/backup/backup.go index df3fb047b..bb497a5cd 100644 --- a/backup/backup.go +++ b/backup/backup.go @@ -249,8 +249,15 @@ func (bm *BackupManager) ChoosePod(ctx context.Context, pods []*corev1.Pod) (int } } - lastIndex := lastBackup.SourceIndex - choosableIndexes := getIdxsWithUnchangedUUID(bm.uuidSet, lastBackup.UUIDSet) + var choosableIndexes []int + // for backward compatibility + // if lastBackup.UUIDSet is empty, use lastBackup.SourceUUID + if len(lastBackup.UUIDSet) == 0 { + s := map[string]string{strconv.Itoa(lastBackup.SourceIndex): lastBackup.SourceUUID} + choosableIndexes = getIdxsWithUnchangedUUID(bm.uuidSet, s) + } else { + choosableIndexes = getIdxsWithUnchangedUUID(bm.uuidSet, lastBackup.UUIDSet) + } if len(choosableIndexes) == 0 { bm.log.Info("the server_uuid of all pods has changed or some pods are not ready") @@ -271,6 +278,7 @@ func (bm *BackupManager) ChoosePod(ctx context.Context, pods []*corev1.Pod) (int } replicas := []int{} + lastIndex := lastBackup.SourceIndex for _, i := range choosableIndexes { if i == currentPrimaryIndex { continue diff --git a/backup/backup_test.go b/backup/backup_test.go index 5dff432d0..b85b29fb5 100644 --- a/backup/backup_test.go +++ b/backup/backup_test.go @@ -168,6 +168,12 @@ func TestGetIdxsWithUnchangedUUID(t *testing.T) { expectIdxs []int }{ + { + name: "last-empty", + current: map[string]string{"0": "uuid-0"}, + last: map[string]string{}, + expectIdxs: []int{}, + }, { name: "single-uuid-not-changed", current: map[string]string{"0": "uuid-0"}, @@ -250,11 +256,11 @@ func TestChoosePod(t *testing.T) { } } - makeBS := func(sourceIdx int, uuidSet map[string]string) mocov1beta2.BackupStatus { + makeBS := func(sourceIdx int, sourceUUID string, uuidSet map[string]string) mocov1beta2.BackupStatus { return mocov1beta2.BackupStatus{ Time: metav1.Now(), SourceIndex: sourceIdx, - SourceUUID: uuidSet[strconv.Itoa(sourceIdx)], + SourceUUID: sourceUUID, UUIDSet: uuidSet, } } @@ -326,11 +332,33 @@ func TestChoosePod(t *testing.T) { doBackupBinlog: false, warnings: 0, }, + { + name: "triple-last-uuid-set-is-empty", + replicas: 3, + current: 1, + bkup: makeBS(2, "uuid-2", map[string]string{}), + pods: makePod3(true, true, true), + err: nil, + expectIdx: 2, + doBackupBinlog: true, + warnings: 0, + }, + { + name: "triple-last-uuid-set-is-empty-source-uuid-changed", + replicas: 3, + current: 1, + bkup: makeBS(2, "uuid-b", map[string]string{}), + pods: makePod3(true, true, true), + err: nil, + expectIdx: 0, + doBackupBinlog: false, + warnings: 1, + }, { name: "single-2nd", replicas: 1, current: 0, - bkup: makeBS(0, map[string]string{"0": "uuid-0"}), + bkup: makeBS(0, "uuid-0", map[string]string{"0": "uuid-0"}), pods: makePod1(true), err: nil, expectIdx: 0, @@ -341,7 +369,7 @@ func TestChoosePod(t *testing.T) { name: "single-2nd-uuid-changed", replicas: 1, current: 0, - bkup: makeBS(0, map[string]string{"0": "uuid-a"}), + bkup: makeBS(0, "uuid-a", map[string]string{"0": "uuid-a"}), pods: makePod1(true), err: nil, expectIdx: 0, @@ -352,7 +380,7 @@ func TestChoosePod(t *testing.T) { name: "single-2nd-not-ready", replicas: 1, current: 0, - bkup: makeBS(0, map[string]string{"0": "uuid-0"}), + bkup: makeBS(0, "uuid-0", map[string]string{"0": "uuid-0"}), pods: makePod1(false), err: errors.New("no ready pod exists"), expectIdx: 0, @@ -363,7 +391,7 @@ func TestChoosePod(t *testing.T) { name: "triple-2nd", replicas: 3, current: 0, - bkup: makeBS(1, map[string]string{"0": "uuid-0", "1": "uuid-1", "2": "uuid-2"}), + bkup: makeBS(1, "uuid-1", map[string]string{"0": "uuid-0", "1": "uuid-1", "2": "uuid-2"}), pods: makePod3(true, true, true), err: nil, expectIdx: 1, @@ -374,7 +402,7 @@ func TestChoosePod(t *testing.T) { name: "triple-2nd-uuid-changed", replicas: 3, current: 0, - bkup: makeBS(1, map[string]string{"0": "uuid-0", "1": "uuid-b", "2": "uuid-2"}), + bkup: makeBS(1, "uuid-b", map[string]string{"0": "uuid-0", "1": "uuid-b", "2": "uuid-2"}), pods: makePod3(true, true, true), err: nil, expectIdx: 2, @@ -385,7 +413,7 @@ func TestChoosePod(t *testing.T) { name: "triple-2nd-last-index-not-ready", replicas: 3, current: 0, - bkup: makeBS(1, map[string]string{"0": "uuid-0", "1": "uuid-1", "2": "uuid-2"}), + bkup: makeBS(1, "uuid-1", map[string]string{"0": "uuid-0", "1": "uuid-1", "2": "uuid-2"}), pods: makePod3(true, false, true), err: nil, expectIdx: 2, @@ -396,7 +424,7 @@ func TestChoosePod(t *testing.T) { name: "triple-2nd-primary", replicas: 3, current: 1, - bkup: makeBS(1, map[string]string{"0": "uuid-0", "1": "uuid-1", "2": "uuid-2"}), + bkup: makeBS(1, "uuid-1", map[string]string{"0": "uuid-0", "1": "uuid-1", "2": "uuid-2"}), pods: makePod3(true, true, true), err: nil, expectIdx: 0, @@ -407,7 +435,7 @@ func TestChoosePod(t *testing.T) { name: "triple-2nd-all-not-ready", replicas: 3, current: 0, - bkup: makeBS(1, map[string]string{"0": "uuid-0", "1": "uuid-1", "2": "uuid-2"}), + bkup: makeBS(1, "uuid-1", map[string]string{"0": "uuid-0", "1": "uuid-1", "2": "uuid-2"}), pods: makePod3(false, false, false), err: errors.New("no ready pod exists"), expectIdx: 0, @@ -418,7 +446,7 @@ func TestChoosePod(t *testing.T) { name: "triple-2nd-replica-uuid-changed", replicas: 3, current: 0, - bkup: makeBS(1, map[string]string{"0": "uuid-0", "1": "uuid-a", "2": "uuid-b"}), + bkup: makeBS(1, "uuid-a", map[string]string{"0": "uuid-0", "1": "uuid-a", "2": "uuid-b"}), pods: makePod3(true, true, true), err: nil, expectIdx: 0, @@ -429,7 +457,7 @@ func TestChoosePod(t *testing.T) { name: "triple-2nd-some-not-ready-and-uuid-changed-1", replicas: 3, current: 0, - bkup: makeBS(1, map[string]string{"0": "uuid-a", "1": "uuid-1", "2": "uuid-b"}), + bkup: makeBS(1, "uuid-1", map[string]string{"0": "uuid-a", "1": "uuid-1", "2": "uuid-b"}), pods: makePod3(true, false, true), err: nil, expectIdx: 2, @@ -440,7 +468,7 @@ func TestChoosePod(t *testing.T) { name: "triple-2nd-some-not-ready-and-uuid-changed-2", replicas: 3, current: 0, - bkup: makeBS(0, map[string]string{"0": "uuid-a", "1": "uuid-1", "2": "uuid-b"}), + bkup: makeBS(0, "uuid-a", map[string]string{"0": "uuid-a", "1": "uuid-1", "2": "uuid-b"}), pods: makePod3(true, false, false), err: nil, expectIdx: 0,