Skip to content

Commit

Permalink
Fix a bug with computing the lineage for a backup, which impacted pru…
Browse files Browse the repository at this point in the history
…ning
  • Loading branch information
erkie committed Jan 28, 2019
1 parent 65a5325 commit f1a49cc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@ func listAllBackups(hostname string, doSpaceName string, minioClient *minio.Clie

sort.Sort(byCreatedAt(backupItems))

return addLineagesToBackups(backupItems), nil
}

func addLineagesToBackups(backupItems []backupItem) []backupItem {
lineageID := int64(1)
for _, backupItem := range backupItems {
res := make([]backupItem, len(backupItems))
for index, backupItem := range backupItems {
if backupItem.BackupType == backupTypeFull {
backupItem.LineageID = lineageID
lineageID++
} else {
backupItem.LineageID = lineageID
}

res[index] = backupItem
}

return backupItems, nil
return res
}

func findRelevantBackupsUpTo(sinceTimestamp time.Time, allBackups []backupItem) []backupItem {
Expand Down
62 changes: 62 additions & 0 deletions cmd/retention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"fmt"
"testing"
"time"
)
Expand Down Expand Up @@ -110,6 +111,67 @@ func TestListingBackupsSince(t *testing.T) {
}
}

func TestComputingLineages(t *testing.T) {
allBackups := []backupItem{
buildBackup(0, "prod-test-db1/mysql-backup-201901281617.full.xbstream", 126),
buildBackup(0, "prod-test-db1/mysql-backup-201901232039.incremental.xbstream", 4),
buildBackup(0, "prod-test-db1/mysql-backup-201901231002.incremental.xbstream", 6),
buildBackup(0, "prod-test-db1/mysql-backup-201901220907.incremental.xbstream", 5),
buildBackup(0, "prod-test-db1/mysql-backup-201901211952.full.xbstream", 119),
}

results := []int64{
1,
2,
2,
2,
2,
}

allBackups = addLineagesToBackups(allBackups)

for index, backup := range allBackups {
if backup.LineageID != results[index] {
t.Errorf("Incorrect result for index #%d, expected %d got %d", index, results[index], backup.LineageID)
}
}
}

func TestFailingBackupScenario(t *testing.T) {
allBackups := []backupItem{
buildBackup(0, "prod-test-db1/mysql-backup-201901281617.full.xbstream", 126),
buildBackup(0, "prod-test-db1/mysql-backup-201901232039.incremental.xbstream", 4),
buildBackup(0, "prod-test-db1/mysql-backup-201901231002.incremental.xbstream", 6),
buildBackup(0, "prod-test-db1/mysql-backup-201901220907.incremental.xbstream", 5),
buildBackup(0, "prod-test-db1/mysql-backup-201901211952.full.xbstream", 119),
}

allBackups = addLineagesToBackups(allBackups)

fmt.Println("The failing one")
// Test in the middle of the history
sinceTimestamp, _ := parseBackupTimestamp("201901281617")
backups := findBackupsThatCanBeDeleted(allBackups, sinceTimestamp, &RetentionConfig{
RetentionInDays: 4,
})

if len(backups) != 4 {
t.Error("Found incorrect backups", len(backups))
}
if backups[0].Size != 4 {
t.Errorf("Wrong backup 0, found: %d", backups[0].Size)
}
if backups[1].Size != 6 {
t.Errorf("Wrong backup 1, found: %d", backups[1].Size)
}
if backups[2].Size != 5 {
t.Errorf("Wrong backup 2, found: %d", backups[2].Size)
}
if backups[3].Size != 119 {
t.Errorf("Wrong backup 3, found: %d", backups[3].Size)
}
}

func buildBackup(lineageID int64, name string, size int64) backupItem {
createdAt, backupType, _ := parseBackupName(name)
return backupItem{
Expand Down

0 comments on commit f1a49cc

Please sign in to comment.