From 3bd2bad93f7ca584756044526121e8bf8a673a54 Mon Sep 17 00:00:00 2001 From: BornChanger <97348524+BornChanger@users.noreply.github.com> Date: Wed, 17 Jan 2024 12:47:59 +0800 Subject: [PATCH] Use CreationTimestamp of volumebackup for gc check (#5518) --- .../backupschedule/backup_schedule_manager.go | 11 ++------ .../backup_schedule_manager_test.go | 28 ++++++++++--------- 2 files changed, 18 insertions(+), 21 deletions(-) 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 }