Skip to content

Commit

Permalink
br: add a new CRD for compact backup (pingcap#5822)
Browse files Browse the repository at this point in the history
  • Loading branch information
RidRisR committed Jan 13, 2025
1 parent 026f726 commit fc5f9fc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 79 deletions.
16 changes: 4 additions & 12 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5601,9 +5601,7 @@ StorageProvider
<p>
(Members of <code>StorageProvider</code> are embedded into this type.)
</p>
<p>StorageProvider configures where and how backups should be stored.
*** Note: This field should generally not be left empty, unless you are certain the storage provider
*** can be obtained from another source, such as a schedule CR.</p>
<p>StorageProvider configures where and how backups should be stored.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -5681,9 +5679,7 @@ BRConfig
</em>
</td>
<td>
<p>BRConfig is the configs for BR
*** Note: This field should generally not be left empty, unless you are certain the BR config
*** can be obtained from another source, such as a schedule CR.</p>
<p>BRConfig is the configs for BR</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -5942,9 +5938,7 @@ StorageProvider
<p>
(Members of <code>StorageProvider</code> are embedded into this type.)
</p>
<p>StorageProvider configures where and how backups should be stored.
*** Note: This field should generally not be left empty, unless you are certain the storage provider
*** can be obtained from another source, such as a schedule CR.</p>
<p>StorageProvider configures where and how backups should be stored.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -6022,9 +6016,7 @@ BRConfig
</em>
</td>
<td>
<p>BRConfig is the configs for BR
*** Note: This field should generally not be left empty, unless you are certain the BR config
*** can be obtained from another source, such as a schedule CR.</p>
<p>BRConfig is the configs for BR</p>
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pingcap/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions pkg/apis/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3523,8 +3523,6 @@ type CompactSpec struct {
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
// StorageProvider configures where and how backups should be stored.
// *** Note: This field should generally not be left empty, unless you are certain the storage provider
// *** can be obtained from another source, such as a schedule CR.
StorageProvider `json:",inline"`
// StartTs is the start ts of the compact backup.
// Format supports TSO or datetime, e.g. '400036290571534337', '2018-05-11 01:42:23'.
Expand All @@ -3546,8 +3544,6 @@ type CompactSpec struct {
// +optional
ToolImage string `json:"toolImage,omitempty"`
// BRConfig is the configs for BR
// *** Note: This field should generally not be left empty, unless you are certain the BR config
// *** can be obtained from another source, such as a schedule CR.
BR *BRConfig `json:"br,omitempty"`
// ImagePullSecrets is an optional list of references to secrets in the same namespace to use for pulling any of the images.
// +optional
Expand Down
124 changes: 62 additions & 62 deletions pkg/backup/backupschedule/backup_schedule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,36 +292,36 @@ func (bm *backupScheduleManager) deleteLastcompactJob(bs *v1alpha1.BackupSchedul
// It returns a controller.RequeueError if the backup is still running,
// otherwise it updates the LastCompactTime or returns any encountered error.
func (bm *backupScheduleManager) handleLastCompact(ns, bsName, lastCompact string, bs *v1alpha1.BackupSchedule) error {
if lastCompact == "" {
return nil
}

compact, err := bm.deps.CompactBackupLister.CompactBackups(ns).Get(lastCompact)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("backup schedule %s/%s: get compact backup %s failed: %w", ns, bsName, lastCompact, err)
}

var timestamp string
switch compact.Status.State {
case string(v1alpha1.BackupComplete):
timestamp = compact.Spec.EndTs
case string(v1alpha1.BackupFailed):
timestamp = compact.Spec.StartTs
default:
return controller.RequeueErrorf("backup schedule %s/%s: compact backup %s is still running", ns, bsName, lastCompact)
}

t, err := config.ParseTSStringToGoTime(timestamp)
if err != nil {
return perrors.AddStack(err)
}

if err := bm.deps.CompactControl.DeleteCompactBackup(compact); err != nil {
return fmt.Errorf("backup schedule %s/%s: delete compact backup %s failed: %w", ns, bsName, lastCompact, err)
}
if lastCompact == "" {
return nil
}

compact, err := bm.deps.CompactBackupLister.CompactBackups(ns).Get(lastCompact)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("backup schedule %s/%s: get compact backup %s failed: %w", ns, bsName, lastCompact, err)
}

var timestamp string
switch compact.Status.State {
case string(v1alpha1.BackupComplete):
timestamp = compact.Spec.EndTs
case string(v1alpha1.BackupFailed):
timestamp = compact.Spec.StartTs
default:
return controller.RequeueErrorf("backup schedule %s/%s: compact backup %s is still running", ns, bsName, lastCompact)
}

t, err := config.ParseTSStringToGoTime(timestamp)
if err != nil {
return perrors.AddStack(err)
}

if err := bm.deps.CompactControl.DeleteCompactBackup(compact); err != nil {
return fmt.Errorf("backup schedule %s/%s: delete compact backup %s failed: %w", ns, bsName, lastCompact, err)
}

if !t.IsZero() && t.Before(bs.Status.LogBackupStartTs.Time) {
return fmt.Errorf("backupSchedule %s/%s last compact time can't rollback (from %v to %v)", bs.GetNamespace(), bs.GetName(), bs.Status.LogBackupStartTs, t)
Expand All @@ -330,46 +330,46 @@ func (bm *backupScheduleManager) handleLastCompact(ns, bsName, lastCompact strin
return fmt.Errorf("backupSchedule %s/%s last compact time can't rollback (from %v to %v)", bs.GetNamespace(), bs.GetName(), bs.Status.LastCompactTime, t)
}
bs.Status.LastCompactTime = &metav1.Time{Time: t}
return nil
return nil
}

func (bm *backupScheduleManager) canPerformNextCompact(bs *v1alpha1.BackupSchedule) error {
ns := bs.GetNamespace()
bsName := bs.GetName()
ns := bs.GetNamespace()
bsName := bs.GetName()

if bs.Status.LogBackupStartTs == nil {
return nil
}

// Determine if the backup schedule is part of a group
bsGroupName := bs.GetLabels()[label.BackupScheduleGroupLabelKey]
if bsGroupName == "" {
return bm.handleLastCompact(ns, bsName, bs.Status.LastCompact, bs)
}

backupScheduleGroupLabels := label.NewBackupScheduleGroup(bsGroupName)
selector, err := backupScheduleGroupLabels.Selector()
if err != nil {
return fmt.Errorf("generate backup schedule group %s label selector failed: %w", bsGroupName, err)
}

bss, err := bm.deps.BackupScheduleLister.BackupSchedules(ns).List(selector)
if err != nil {
return fmt.Errorf("backup schedule %s/%s: list backup schedules failed: %w", ns, bsName, err)
}

for _, bsMember := range bss {
if err := bm.handleLastCompact(ns, bsName, bsMember.Status.LastCompact, bsMember); err != nil {
// If it's a requeue error, propagate it
if controller.IsRequeueError(err) {
return err
}
// For other errors, return immediately
return fmt.Errorf("backup schedule %s/%s: processing compact failed: %w", ns, bsName, err)
}
}

return nil
// Determine if the backup schedule is part of a group
bsGroupName := bs.GetLabels()[label.BackupScheduleGroupLabelKey]
if bsGroupName == "" {
return bm.handleLastCompact(ns, bsName, bs.Status.LastCompact, bs)
}

backupScheduleGroupLabels := label.NewBackupScheduleGroup(bsGroupName)
selector, err := backupScheduleGroupLabels.Selector()
if err != nil {
return fmt.Errorf("generate backup schedule group %s label selector failed: %w", bsGroupName, err)
}

bss, err := bm.deps.BackupScheduleLister.BackupSchedules(ns).List(selector)
if err != nil {
return fmt.Errorf("backup schedule %s/%s: list backup schedules failed: %w", ns, bsName, err)
}

for _, bsMember := range bss {
if err := bm.handleLastCompact(ns, bsName, bsMember.Status.LastCompact, bsMember); err != nil {
// If it's a requeue error, propagate it
if controller.IsRequeueError(err) {
return err
}
// For other errors, return immediately
return fmt.Errorf("backup schedule %s/%s: processing compact failed: %w", ns, bsName, err)
}
}

return nil
}

func (bm *backupScheduleManager) performLogBackupIfNeeded(bs *v1alpha1.BackupSchedule) error {
Expand Down

0 comments on commit fc5f9fc

Please sign in to comment.