Skip to content

Commit

Permalink
rename some fields
Browse files Browse the repository at this point in the history
  • Loading branch information
RidRisR committed Jan 22, 2025
1 parent 1cbd73b commit 16a6627
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 68 deletions.
17 changes: 8 additions & 9 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -651,13 +651,13 @@ string
</tr>
<tr>
<td>
<code>compactInterval</code></br>
<code>compactSpan</code></br>
<em>
string
</em>
</td>
<td>
<p>LogBackupInterval is the interval of log backup.</p>
<p>CompactSpan is to specify how long backups we want to compact.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -3999,13 +3999,13 @@ string
</tr>
<tr>
<td>
<code>compactInterval</code></br>
<code>compactSpan</code></br>
<em>
string
</em>
</td>
<td>
<p>LogBackupInterval is the interval of log backup.</p>
<p>CompactSpan is to specify how long backups we want to compact.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -4197,29 +4197,28 @@ Kubernetes meta/v1.Time
</tr>
<tr>
<td>
<code>lastCompactTime</code></br>
<code>lastCompactTs</code></br>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#time-v1-meta">
Kubernetes meta/v1.Time
</a>
</em>
</td>
<td>
<p>LastCompactTime represents the time when the last compact was compacted</p>
<p>LastCompactTs represents the endTs of the last compact</p>
</td>
</tr>
<tr>
<td>
<code>nextCompactTime</code></br>
<code>nextCompactEndTs</code></br>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.28/#time-v1-meta">
Kubernetes meta/v1.Time
</a>
</em>
</td>
<td>
<p>NextBackupTime represents the time when the next compact will be scheduled, it should
equal to a passed lastBackupTime</p>
<p>NextCompactEndTs represents the scheduled endTs of next compact</p>
</td>
</tr>
<tr>
Expand Down
6 changes: 3 additions & 3 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4491,7 +4491,7 @@ spec:
useKMS:
type: boolean
type: object
compactInterval:
compactSpan:
type: string
gcs:
properties:
Expand Down Expand Up @@ -7560,15 +7560,15 @@ spec:
type: string
lastCompact:
type: string
lastCompactTime:
lastCompactTs:
format: date-time
type: string
logBackup:
type: string
logBackupStartTs:
format: date-time
type: string
nextCompactTime:
nextCompactEndTs:
format: date-time
type: string
type: object
Expand Down
6 changes: 3 additions & 3 deletions manifests/crd/v1/pingcap.com_backupschedules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4491,7 +4491,7 @@ spec:
useKMS:
type: boolean
type: object
compactInterval:
compactSpan:
type: string
gcs:
properties:
Expand Down Expand Up @@ -7560,15 +7560,15 @@ spec:
type: string
lastCompact:
type: string
lastCompactTime:
lastCompactTs:
format: date-time
type: string
logBackup:
type: string
logBackupStartTs:
format: date-time
type: string
nextCompactTime:
nextCompactEndTs:
format: date-time
type: string
type: object
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pingcap/v1alpha1/openapi_generated.go

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

13 changes: 6 additions & 7 deletions pkg/apis/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2559,8 +2559,8 @@ type BackupScheduleSpec struct {
MaxBackups *int32 `json:"maxBackups,omitempty"`
// MaxReservedTime is to specify how long backups we want to keep.
MaxReservedTime *string `json:"maxReservedTime,omitempty"`
// LogBackupInterval is the interval of log backup.
CompactInterval *string `json:"compactInterval,omitempty"`
// CompactSpan is to specify how long backups we want to compact.
CompactSpan *string `json:"compactSpan,omitempty"`
// BackupTemplate is the specification of the backup structure to get scheduled.
BackupTemplate BackupSpec `json:"backupTemplate"`
// LogBackupTemplate is the specification of the log backup structure to get scheduled.
Expand Down Expand Up @@ -2598,11 +2598,10 @@ type BackupScheduleStatus struct {
LogBackupStartTs *metav1.Time `json:"logBackupStartTs,omitempty"`
// LastBackupTime represents the last time the backup was successfully created.
LastBackupTime *metav1.Time `json:"lastBackupTime,omitempty"`
// LastCompactTime represents the time when the last compact was compacted
LastCompactTime *metav1.Time `json:"lastCompactTime,omitempty"`
// NextBackupTime represents the time when the next compact will be scheduled, it should
// equal to a passed lastBackupTime
NextCompactTime *metav1.Time `json:"nextCompactTime,omitempty"`
// LastCompactTs represents the endTs of the last compact
LastCompactTs *metav1.Time `json:"lastCompactTs,omitempty"`
// NextCompactEndTs represents the scheduled endTs of next compact
NextCompactEndTs *metav1.Time `json:"nextCompactEndTs,omitempty"`
// AllBackupCleanTime represents the time when all backup entries are cleaned up
AllBackupCleanTime *metav1.Time `json:"allBackupCleanTime,omitempty"`
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go

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

70 changes: 32 additions & 38 deletions pkg/backup/backupschedule/backup_schedule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,69 +57,63 @@ func (bm *backupScheduleManager) doCompact(bs *v1alpha1.BackupSchedule, startTim
return err
}

func calEndTs(bs *v1alpha1.BackupSchedule, startTs time.Time, interval time.Duration, maxEndTs time.Time) time.Time {
fastCompactLimit := startTs.Add(3 * interval)
func calEndTs(bs *v1alpha1.BackupSchedule, startTs time.Time, span time.Duration, maxEndTs time.Time) time.Time {
compactLimit := startTs.Add(span)
var endTs time.Time

nextCompactTime := bs.Status.NextCompactTime
lastBackupTime := bs.Status.LastBackupTime

// Helper function to compare a candidate target with 'fastCompactLimit'.
checkTarget := func(target time.Time) (time.Time, *metav1.Time) {
if target.After(fastCompactLimit) {
return fastCompactLimit, &metav1.Time{Time: target}
if target.After(compactLimit) {
return compactLimit, &metav1.Time{Time: target}
} else if target.Before(compactLimit) {
return startTs, nil
}
return target, nil
}

if nextCompactTime != nil && nextCompactTime.After(startTs) {
endTs, bs.Status.NextCompactTime = checkTarget(nextCompactTime.Time)
} else if lastBackupTime != nil && lastBackupTime.After(startTs) {
endTs, bs.Status.NextCompactTime = checkTarget(lastBackupTime.Time)
} else {
endTs, bs.Status.NextCompactTime = checkTarget(maxEndTs)
var target time.Time
switch {
case bs.Status.NextCompactEndTs != nil && bs.Status.NextCompactEndTs.After(startTs):
target = bs.Status.NextCompactEndTs.Time
case bs.Status.LastBackupTime != nil && bs.Status.LastBackupTime.After(startTs):
target = bs.Status.LastBackupTime.Time
default:
target = maxEndTs
}

endTs, bs.Status.NextCompactEndTs = checkTarget(target)

return endTs
}

func (bm *backupScheduleManager) performCompact(bs *v1alpha1.BackupSchedule, maxEndTime time.Time, nowFn nowFn) error {
if bs.Spec.CompactInterval == nil || bs.Status.LogBackup == nil {
if bs.Spec.CompactSpan == nil || bs.Status.LogBackup == nil {
return nil
}

var startTs, endTs time.Time
switch {
case bs.Status.LogBackupStartTs == nil:
return fmt.Errorf("Compact failed: %s/%s, please start a log backup before compact it", bs.GetNamespace(), bs.GetName())
case bs.Status.LastCompactTime == nil:
case bs.Status.LastCompactTs == nil:
startTs = bs.Status.LogBackupStartTs.Time
default:
startTs = bs.Status.LastCompactTime.Time
startTs = bs.Status.LastCompactTs.Time
}
klog.Infof("backupSchedule %s/%s startTs is %v", bs.GetNamespace(), bs.GetName(), startTs)

interval, err := time.ParseDuration(*bs.Spec.CompactInterval)
span, err := time.ParseDuration(*bs.Spec.CompactSpan)
if err != nil {
return fmt.Errorf("failed to parse compact interval: %w", err)
}

endTs = calEndTs(bs, startTs, interval, maxEndTime)
klog.Infof("backupSchedule %s/%s endTs is %v", bs.GetNamespace(), bs.GetName(), endTs)

endTs = calEndTs(bs, startTs, span, maxEndTime)
if endTs.Equal(startTs) {
klog.Infof("backupSchedule %s/%s log backup no progress yet, skip", bs.GetNamespace(), bs.GetName())
return nil
}

now := nowFn()
expect := bs.Status.LastCompactTime.Time.Add(interval)
if now.Before(expect) {
klog.Infof("backupSchedule %s/%s compact time is not reached yet, expect: %v", bs.GetNamespace(), bs.GetName(), expect)
klog.Infof("backupSchedule %s/%s compact time is not reached yet, skip", bs.GetNamespace(), bs.GetName())
return nil
}

if err := bm.doCompact(bs, startTs, endTs, now); err != nil {
klog.Infof("backupSchedule %s/%s compact: from %v to %v", bs.GetNamespace(), bs.GetName(), startTs, endTs)
if err := bm.doCompact(bs, startTs, endTs, nowFn()); err != nil {
return err
}

Expand Down Expand Up @@ -261,7 +255,7 @@ func (bm *backupScheduleManager) canPerformNextBackup(bs *v1alpha1.BackupSchedul

// canPerformNextCompact handles the compact backup processing logic.
// It returns a controller.RequeueError if the backup is still running,
// otherwise it updates the LastCompactTime or returns any encountered error.
// otherwise it updates the LastCompactTs or returns any encountered error.
func (bm *backupScheduleManager) canPerformNextCompact(bs *v1alpha1.BackupSchedule) error {
if bs.Status.LastCompact == "" {
return nil
Expand Down Expand Up @@ -298,11 +292,11 @@ func (bm *backupScheduleManager) canPerformNextCompact(bs *v1alpha1.BackupSchedu
klog.Errorf("backupSchedule %s/%s last compact time can't rollback (from %v to %v)", bs.GetNamespace(), bs.GetName(), bs.Status.LogBackupStartTs, currentCompactTime)
return nil
}
if bs.Status.LastCompactTime != nil && currentCompactTime.Before(bs.Status.LastCompactTime.Time) {
klog.Errorf("backupSchedule %s/%s last compact time can't rollback (from %v to %v)", bs.GetNamespace(), bs.GetName(), bs.Status.LastCompactTime, currentCompactTime)
if bs.Status.LastCompactTs != nil && currentCompactTime.Before(bs.Status.LastCompactTs.Time) {
klog.Errorf("backupSchedule %s/%s last compact time can't rollback (from %v to %v)", bs.GetNamespace(), bs.GetName(), bs.Status.LastCompactTs, currentCompactTime)
return nil
}
bs.Status.LastCompactTime = &metav1.Time{Time: currentCompactTime}
bs.Status.LastCompactTs = &metav1.Time{Time: currentCompactTime}
return nil
}

Expand Down Expand Up @@ -629,10 +623,10 @@ func (bm *backupScheduleManager) backupGCByMaxReservedTime(bs *v1alpha1.BackupSc
var compactProgress uint64
if bs.Spec.CompactBackupTemplate == nil {
compactProgress = math.MaxUint64
} else if bs.Status.LastCompactTime == nil {
} else if bs.Status.LastCompactTs == nil {
compactProgress = 0
} else {
compactProgress = config.GoTimeToTS(bs.Status.LastCompactTime.Time)
compactProgress = config.GoTimeToTS(bs.Status.LastCompactTs.Time)
}

if truncateTSO > compactProgress {
Expand Down Expand Up @@ -944,10 +938,10 @@ func calLastCompactTs(bs *v1alpha1.BackupSchedule, compact *v1alpha1.CompactBack
if !compactTime.IsZero() && compactTime.Before(bs.Status.LogBackupStartTs.Time) {
klog.Errorf("backupSchedule %s/%s compact time can't rollback (from %v to %v)", bs.GetNamespace(), bs.GetName(), bs.Status.LogBackupStartTs, compactTime)
}
if bs.Status.LastCompactTime != nil && compactTime.Before(bs.Status.LastCompactTime.Time) {
if bs.Status.LastCompactTs != nil && compactTime.Before(bs.Status.LastCompactTs.Time) {
return
}
bs.Status.LastCompactTime = &metav1.Time{Time: compactTime}
bs.Status.LastCompactTs = &metav1.Time{Time: compactTime}
}

func checkTruncateTSOWithinLogBackupRange(logBackup *v1alpha1.Backup, truncateTSO uint64) (bool, error) {
Expand Down

0 comments on commit 16a6627

Please sign in to comment.