diff --git a/pkg/fedvolumebackup/backupschedule/backup_schedule_manager.go b/pkg/fedvolumebackup/backupschedule/backup_schedule_manager.go index e3818e6eb3..6f83555d23 100644 --- a/pkg/fedvolumebackup/backupschedule/backup_schedule_manager.go +++ b/pkg/fedvolumebackup/backupschedule/backup_schedule_manager.go @@ -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" @@ -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 } } diff --git a/pkg/fedvolumebackup/backupschedule/backup_schedule_manager_test.go b/pkg/fedvolumebackup/backupschedule/backup_schedule_manager_test.go index 2a6e7246fe..c4ec239d88 100644 --- a/pkg/fedvolumebackup/backupschedule/backup_schedule_manager_test.go +++ b/pkg/fedvolumebackup/backupschedule/backup_schedule_manager_test.go @@ -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{ @@ -255,7 +257,7 @@ func TestCalculateExpiredBackups(t *testing.T) { fakeBackup(&last10Min), }, reservedTime: 24 * time.Hour, - expectedDeleteBackupCount: 2, + expectedDeleteBackupCount: 3, }, } @@ -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 }