|
| 1 | +// Copyright 2025 PingCAP, Inc. Licensed under Apache-2.0. |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/pingcap/errors" |
| 7 | + "github.com/pingcap/log" |
| 8 | + "github.com/pingcap/tidb/br/pkg/task" |
| 9 | + "github.com/pingcap/tidb/br/pkg/trace" |
| 10 | + "github.com/pingcap/tidb/br/pkg/version/build" |
| 11 | + "github.com/pingcap/tidb/pkg/session" |
| 12 | + "github.com/pingcap/tidb/pkg/util/logutil" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "go.uber.org/zap" |
| 15 | + "sourcegraph.com/sourcegraph/appdash" |
| 16 | +) |
| 17 | + |
| 18 | +// NewAbortCommand returns an abort subcommand |
| 19 | +func NewAbortCommand() *cobra.Command { |
| 20 | + command := &cobra.Command{ |
| 21 | + Use: "abort", |
| 22 | + Short: "abort restore tasks", |
| 23 | + SilenceUsage: true, |
| 24 | + PersistentPreRunE: func(c *cobra.Command, args []string) error { |
| 25 | + if err := Init(c); err != nil { |
| 26 | + return errors.Trace(err) |
| 27 | + } |
| 28 | + build.LogInfo(build.BR) |
| 29 | + logutil.LogEnvVariables() |
| 30 | + task.LogArguments(c) |
| 31 | + // disable stats otherwise takes too much memory |
| 32 | + session.DisableStats4Test() |
| 33 | + |
| 34 | + return nil |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + command.AddCommand( |
| 39 | + newAbortRestoreCommand(), |
| 40 | + // future: newAbortBackupCommand(), |
| 41 | + ) |
| 42 | + task.DefineRestoreFlags(command.PersistentFlags()) |
| 43 | + |
| 44 | + return command |
| 45 | +} |
| 46 | + |
| 47 | +// newAbortRestoreCommand returns an abort restore subcommand |
| 48 | +func newAbortRestoreCommand() *cobra.Command { |
| 49 | + command := &cobra.Command{ |
| 50 | + Use: "restore", |
| 51 | + Short: "abort restore tasks", |
| 52 | + SilenceUsage: true, |
| 53 | + } |
| 54 | + |
| 55 | + command.AddCommand( |
| 56 | + newAbortRestoreFullCommand(), |
| 57 | + newAbortRestoreDBCommand(), |
| 58 | + newAbortRestoreTableCommand(), |
| 59 | + newAbortRestorePointCommand(), |
| 60 | + ) |
| 61 | + |
| 62 | + return command |
| 63 | +} |
| 64 | + |
| 65 | +func newAbortRestoreFullCommand() *cobra.Command { |
| 66 | + command := &cobra.Command{ |
| 67 | + Use: "full", |
| 68 | + Short: "abort a full restore task", |
| 69 | + Args: cobra.NoArgs, |
| 70 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 71 | + return runAbortRestoreCommand(cmd, task.FullRestoreCmd) |
| 72 | + }, |
| 73 | + } |
| 74 | + // define flags specific to full restore |
| 75 | + task.DefineFilterFlags(command, filterOutSysAndMemKeepAuthAndBind, false) |
| 76 | + task.DefineRestoreSnapshotFlags(command) |
| 77 | + return command |
| 78 | +} |
| 79 | + |
| 80 | +func newAbortRestoreDBCommand() *cobra.Command { |
| 81 | + command := &cobra.Command{ |
| 82 | + Use: "db", |
| 83 | + Short: "abort a database restore task", |
| 84 | + Args: cobra.NoArgs, |
| 85 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 86 | + return runAbortRestoreCommand(cmd, task.DBRestoreCmd) |
| 87 | + }, |
| 88 | + } |
| 89 | + task.DefineDatabaseFlags(command) |
| 90 | + return command |
| 91 | +} |
| 92 | + |
| 93 | +func newAbortRestoreTableCommand() *cobra.Command { |
| 94 | + command := &cobra.Command{ |
| 95 | + Use: "table", |
| 96 | + Short: "abort a table restore task", |
| 97 | + Args: cobra.NoArgs, |
| 98 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 99 | + return runAbortRestoreCommand(cmd, task.TableRestoreCmd) |
| 100 | + }, |
| 101 | + } |
| 102 | + task.DefineTableFlags(command) |
| 103 | + return command |
| 104 | +} |
| 105 | + |
| 106 | +func newAbortRestorePointCommand() *cobra.Command { |
| 107 | + command := &cobra.Command{ |
| 108 | + Use: "point", |
| 109 | + Short: "abort a point-in-time restore task", |
| 110 | + Args: cobra.NoArgs, |
| 111 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 112 | + return runAbortRestoreCommand(cmd, task.PointRestoreCmd) |
| 113 | + }, |
| 114 | + } |
| 115 | + task.DefineFilterFlags(command, filterOutSysAndMemKeepAuthAndBind, true) |
| 116 | + task.DefineStreamRestoreFlags(command) |
| 117 | + return command |
| 118 | +} |
| 119 | + |
| 120 | +func runAbortRestoreCommand(command *cobra.Command, cmdName string) error { |
| 121 | + cfg := task.RestoreConfig{Config: task.Config{LogProgress: HasLogFile()}} |
| 122 | + if err := cfg.ParseFromFlags(command.Flags(), false); err != nil { |
| 123 | + command.SilenceUsage = false |
| 124 | + return errors.Trace(err) |
| 125 | + } |
| 126 | + |
| 127 | + if task.IsStreamRestore(cmdName) { |
| 128 | + if err := cfg.ParseStreamRestoreFlags(command.Flags()); err != nil { |
| 129 | + return errors.Trace(err) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + ctx := GetDefaultContext() |
| 134 | + if cfg.EnableOpenTracing { |
| 135 | + var store *appdash.MemoryStore |
| 136 | + ctx, store = trace.TracerStartSpan(ctx) |
| 137 | + defer trace.TracerFinishSpan(ctx, store) |
| 138 | + } |
| 139 | + |
| 140 | + if err := task.RunRestoreAbort(ctx, tidbGlue, cmdName, &cfg); err != nil { |
| 141 | + log.Error("failed to abort restore task", zap.Error(err)) |
| 142 | + return errors.Trace(err) |
| 143 | + } |
| 144 | + return nil |
| 145 | +} |
0 commit comments