Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CreationTimestamp of volumebackup for gc check #5518

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading