Skip to content

Commit

Permalink
issue-604: Added backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
shunki-fujita committed Jan 23, 2024
1 parent a7352fa commit 3318b8a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
12 changes: 10 additions & 2 deletions backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
54 changes: 41 additions & 13 deletions backup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 3318b8a

Please sign in to comment.