Skip to content

Commit

Permalink
br: a more straight forward operator interface (pingcap#5710) (pingca…
Browse files Browse the repository at this point in the history
…p#5760)

Signed-off-by: ti-chi-bot <[email protected]>
Co-authored-by: ris <[email protected]>
Co-authored-by: Xuecheng Zhang <[email protected]>
  • Loading branch information
3 people committed Oct 18, 2024
1 parent 07eca4b commit fe53476
Show file tree
Hide file tree
Showing 19 changed files with 503 additions and 77 deletions.
28 changes: 28 additions & 0 deletions cmd/backup-manager/app/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ func (bo *Options) doStartLogBackup(ctx context.Context, backup *v1alpha1.Backup
return bo.brCommandRun(ctx, fullArgs)
}

// doResumeLogBackup generates br args about log backup resume and runs br binary to do the real backup work.
func (bo *Options) doResumeLogBackup(ctx context.Context, backup *v1alpha1.Backup) error {
specificArgs := []string{
"log",
"resume",
fmt.Sprintf("--task-name=%s", backup.Name),
}
fullArgs, err := bo.backupCommandTemplate(backup, specificArgs, false)
if err != nil {
return err
}
return bo.brCommandRun(ctx, fullArgs)
}

// doStoplogBackup generates br args about log backup stop and runs br binary to do the real backup work.
func (bo *Options) doStopLogBackup(ctx context.Context, backup *v1alpha1.Backup) error {
specificArgs := []string{
Expand All @@ -177,6 +191,20 @@ func (bo *Options) doStopLogBackup(ctx context.Context, backup *v1alpha1.Backup)
return bo.brCommandRun(ctx, fullArgs)
}

// doPauselogBackup generates br args about log backup pause and runs br binary to do the real backup work.
func (bo *Options) doPauseLogBackup(ctx context.Context, backup *v1alpha1.Backup) error {
specificArgs := []string{
"log",
"pause",
fmt.Sprintf("--task-name=%s", backup.Name),
}
fullArgs, err := bo.backupCommandTemplate(backup, specificArgs, false)
if err != nil {
return err
}
return bo.brCommandRun(ctx, fullArgs)
}

// doTruncateLogBackup generates br args about log backup truncate and runs br binary to do the real backup work.
func (bo *Options) doTruncateLogBackup(ctx context.Context, backup *v1alpha1.Backup) error {
specificArgs := []string{
Expand Down
65 changes: 65 additions & 0 deletions cmd/backup-manager/app/backup/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ func (bm *Manager) performLogBackup(ctx context.Context, backup *v1alpha1.Backup
resultStatus, reason, err = bm.stopLogBackup(ctx, backup)
case string(v1alpha1.LogTruncateCommand):
resultStatus, reason, err = bm.truncateLogBackup(ctx, backup)
case string(v1alpha1.LogResumeCommand):
resultStatus, reason, err = bm.resumeLogBackup(ctx, backup)
case string(v1alpha1.LogPauseCommand):
resultStatus, reason, err = bm.pauseLogBackup(ctx, backup)
default:
return fmt.Errorf("log backup %s unknown log subcommand %s", bm, bm.SubCommand)
}
Expand Down Expand Up @@ -551,6 +555,36 @@ func (bm *Manager) startLogBackup(ctx context.Context, backup *v1alpha1.Backup)
return updateStatus, "", nil
}

// resumeLogBackup resume log backup.
func (bm *Manager) resumeLogBackup(ctx context.Context, backup *v1alpha1.Backup) (*controller.BackupUpdateStatus, string, error) {
started := time.Now()

// change Prepare to Running before real backup process start
if err := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Command: v1alpha1.LogResumeCommand,
Type: v1alpha1.BackupRunning,
Status: corev1.ConditionTrue,
}, nil); err != nil {
return nil, "UpdateStatusFailed", err
}

// run br binary to do the real job
backupErr := bm.doResumeLogBackup(ctx, backup)

if backupErr != nil {
klog.Errorf("Resume log backup of cluster %s failed, err: %s", bm, backupErr)
return nil, "ResumeLogBackuFailed", backupErr
}
klog.Infof("Resume log backup of cluster %s success", bm)

finish := time.Now()
updateStatus := &controller.BackupUpdateStatus{
TimeStarted: &metav1.Time{Time: started},
TimeCompleted: &metav1.Time{Time: finish},
}
return updateStatus, "", nil
}

// stopLogBackup stops log backup.
func (bm *Manager) stopLogBackup(ctx context.Context, backup *v1alpha1.Backup) (*controller.BackupUpdateStatus, string, error) {
started := time.Now()
Expand Down Expand Up @@ -582,6 +616,37 @@ func (bm *Manager) stopLogBackup(ctx context.Context, backup *v1alpha1.Backup) (
return updateStatus, "", nil
}

// pauseLogBackup pauses log backup.
func (bm *Manager) pauseLogBackup(ctx context.Context, backup *v1alpha1.Backup) (*controller.BackupUpdateStatus, string, error) {
started := time.Now()

// change Prepare to Running before real backup process start
if err := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Command: v1alpha1.LogPauseCommand,
Type: v1alpha1.BackupRunning,
Status: corev1.ConditionTrue,
}, nil); err != nil {
return nil, "UpdateStatusFailed", err
}

// run br binary to do the real job
backupErr := bm.doPauseLogBackup(ctx, backup)

if backupErr != nil {
klog.Errorf("Pause log backup of cluster %s failed, err: %s", bm, backupErr)
return nil, "PauseLogBackupFailed", backupErr
}
klog.Infof("Pause log backup of cluster %s success", bm)

finish := time.Now()

updateStatus := &controller.BackupUpdateStatus{
TimeStarted: &metav1.Time{Time: started},
TimeCompleted: &metav1.Time{Time: finish},
}
return updateStatus, "", nil
}

// truncateLogBackup truncates log backup.
func (bm *Manager) truncateLogBackup(ctx context.Context, backup *v1alpha1.Backup) (*controller.BackupUpdateStatus, string, error) {
started := time.Now()
Expand Down
29 changes: 29 additions & 0 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ Default is current timestamp.</p>
</tr>
<tr>
<td>
<code>logSubcommand</code></br>
<em>
<a href="#logsubcommandtype">
LogSubCommandType
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Subcommand is the subcommand for BR, such as start, stop, pause etc.</p>
</td>
</tr>
<tr>
<td>
<code>logTruncateUntil</code></br>
<em>
string
Expand Down Expand Up @@ -4195,6 +4209,20 @@ Default is current timestamp.</p>
</tr>
<tr>
<td>
<code>logSubcommand</code></br>
<em>
<a href="#logsubcommandtype">
LogSubCommandType
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Subcommand is the subcommand for BR, such as start, stop, pause etc.</p>
</td>
</tr>
<tr>
<td>
<code>logTruncateUntil</code></br>
<em>
string
Expand Down Expand Up @@ -8735,6 +8763,7 @@ BackupConditionType
<p>
(<em>Appears on:</em>
<a href="#backupcondition">BackupCondition</a>,
<a href="#backupspec">BackupSpec</a>,
<a href="#logsubcommandstatus">LogSubCommandStatus</a>)
</p>
<p>
Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/ec2 v1.53.0
github.com/aws/smithy-go v1.12.1
github.com/docker/docker v17.12.0-ce-rc1.0.20200916142827-bd33bbf0497b+incompatible
github.com/dustin/go-humanize v1.0.0
github.com/dustin/go-humanize v1.0.1
github.com/emicklei/go-restful v2.16.0+incompatible
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-sql-driver/mysql v1.5.0
Expand All @@ -38,7 +38,7 @@ require (
github.com/pingcap/tidb-operator/pkg/apis v1.5.4
github.com/pingcap/tidb-operator/pkg/client v1.5.4
github.com/prometheus/client_golang v1.7.1
github.com/prometheus/client_model v0.2.0
github.com/prometheus/client_model v0.3.0
github.com/prometheus/common v0.26.0
github.com/prometheus/prom2json v1.3.0
github.com/r3labs/diff/v2 v2.15.1
Expand All @@ -47,12 +47,12 @@ require (
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.3
github.com/tikv/pd v2.1.17+incompatible
github.com/yisaer/crd-validation v0.0.3
go.etcd.io/etcd v0.5.0-alpha.5.0.20200910180754-dd1b699fc489
gocloud.dev v0.18.0
golang.org/x/sync v0.1.0
golang.org/x/sync v0.3.0
golang.org/x/time v0.3.0
gomodules.xyz/jsonpatch/v2 v2.1.0
google.golang.org/grpc v1.59.0
Expand All @@ -79,6 +79,8 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.2 // indirect
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
Expand Down Expand Up @@ -193,7 +195,7 @@ require (
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.128.0 // indirect
google.golang.org/api v0.139.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
Loading

0 comments on commit fe53476

Please sign in to comment.