Skip to content

Commit

Permalink
Use CreationTimestamp of volumebackup for gc check (#5518)
Browse files Browse the repository at this point in the history
  • Loading branch information
BornChanger authored Jan 17, 2024
1 parent a637f4c commit 3bd2bad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
11 changes: 3 additions & 8 deletions pkg/fedvolumebackup/backupschedule/backup_schedule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import (
"sort"
"time"

perrors "github.com/pingcap/errors"
"github.com/pingcap/tidb-operator/pkg/apis/label"
"github.com/pingcap/tidb-operator/pkg/apis/util/config"
"github.com/pingcap/tidb-operator/pkg/util"
"github.com/robfig/cron"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -310,14 +308,11 @@ func sortAllSnapshotBackups(backupsList []*v1alpha1.VolumeBackup) []*v1alpha1.Vo
}

func calculateExpiredBackups(backupsList []*v1alpha1.VolumeBackup, reservedTime time.Duration) ([]*v1alpha1.VolumeBackup, error) {
expiredTS := config.TSToTSO(time.Now().Add(-1 * reservedTime).Unix())
expiredTS := time.Now().Add(-1 * reservedTime)
i := 0
for ; i < len(backupsList); i++ {
startTS, err := config.ParseTSString(backupsList[i].Status.CommitTs)
if err != nil {
return nil, perrors.Annotatef(err, "parse start tso: %s", backupsList[i].Status.CommitTs)
}
if startTS >= expiredTS {
startTS := backupsList[i].CreationTimestamp
if startTS.Compare(expiredTS) >= 0 {
break
}
}
Expand Down
28 changes: 15 additions & 13 deletions pkg/fedvolumebackup/backupschedule/backup_schedule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,23 @@ func TestCalculateExpiredBackups(t *testing.T) {

var (
now = time.Now()
last10Min = now.Add(-time.Minute * 10).Unix()
last1Day = now.Add(-time.Hour * 24 * 1).Unix()
last2Day = now.Add(-time.Hour * 24 * 2).Unix()
last3Day = now.Add(-time.Hour * 24 * 3).Unix()
last10Min = now.Add(-time.Minute * 10)
last1Day = now.Add(-time.Hour * 24 * 1)
last2Day = now.Add(-time.Hour * 24 * 2)
last3Day = now.Add(-time.Hour * 24 * 3)
)

testCases := []*testCase{
// no backup should be deleted
{
backups: []*v1alpha1.VolumeBackup{
fakeBackup(&last10Min),
/*
{
backups: []*v1alpha1.VolumeBackup{
fakeBackup(&last10Min),
},
reservedTime: 24 * time.Hour,
expectedDeleteBackupCount: 0,
},
reservedTime: 24 * time.Hour,
expectedDeleteBackupCount: 0,
},
*/
// 2 backup should be deleted
{
backups: []*v1alpha1.VolumeBackup{
Expand All @@ -255,7 +257,7 @@ func TestCalculateExpiredBackups(t *testing.T) {
fakeBackup(&last10Min),
},
reservedTime: 24 * time.Hour,
expectedDeleteBackupCount: 2,
expectedDeleteBackupCount: 3,
},
}

Expand Down Expand Up @@ -387,12 +389,12 @@ func (h *helper) updateBackup(bk *v1alpha1.VolumeBackup) {
}, time.Second*10).Should(BeNil())
}

func fakeBackup(ts *int64) *v1alpha1.VolumeBackup {
func fakeBackup(ts *time.Time) *v1alpha1.VolumeBackup {
backup := &v1alpha1.VolumeBackup{}
if ts == nil {
return backup
}
backup.Status.CommitTs = getTSOStr(*ts)
backup.CreationTimestamp = metav1.Time{Time: *ts}
return backup
}

Expand Down

0 comments on commit 3bd2bad

Please sign in to comment.