Skip to content

Commit

Permalink
feat(mysql): flashback支持记录闪回 TencentBlueKing#7331
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlook authored and zhangzhw8 committed Oct 14, 2024
1 parent 64d8154 commit 116bf5a
Show file tree
Hide file tree
Showing 21 changed files with 1,780 additions and 100 deletions.
23 changes: 23 additions & 0 deletions dbm-services/common/go-pubpkg/mysqlcomm/binlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mysqlcomm

import (
"fmt"
"strings"

"github.com/spf13/cast"
)

// GetSequenceFromFilename get number suffix from binlog file name
func GetSequenceFromFilename(binlogFileName string) int {
file0Arr := strings.Split(binlogFileName, ".")
return cast.ToInt(strings.TrimLeft(file0Arr[1], "0"))
}

// ConstructBinlogFilename build new binlog filename with number sufffix
func ConstructBinlogFilename(fileNameTmpl string, sequenceSuffix int) string {
file0Arr := strings.Split(fileNameTmpl, ".")
file0Arr1Len := cast.ToString(len(file0Arr[1]))
fileNameFmt := "%s." + "%0" + file0Arr1Len + "d"
newFileName := fmt.Sprintf(fileNameFmt, file0Arr[0], sequenceSuffix)
return newFileName
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package mysqlcmd

import (
"fmt"

"dbm-services/common/go-pubpkg/logger"
"dbm-services/mysql/db-tools/dbactuator/internal/subcmd"
"dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql/rollback"
"dbm-services/mysql/db-tools/dbactuator/pkg/util"
_ "dbm-services/mysql/db-tools/dbactuator/pkg/util/mysqlutil" // mysqlutil TODO

"github.com/spf13/cobra"
)

// GoFlashbackBinlogAct TODO
type GoFlashbackBinlogAct struct {
*subcmd.BaseOptions
Payload rollback.GoFlashbackComp
}

// GoFlashbackBinlogCommand godoc
//
// @Summary 导入 binlog
// @Description 通过 `mysqlbinlog --flashback xxx | mysql` 导入 binlog
// @Tags mysql
// @Accept json
// @Param body body rollback.GoFlashbackComp true "short description"
// @Router /mysql/flashback-binlog [post]
func GoFlashbackBinlogCommand() *cobra.Command {
act := GoFlashbackBinlogAct{
BaseOptions: subcmd.GBaseOptions,
}
cmd := &cobra.Command{
Use: "goflashback-binlog",
Short: "导入binlog",
Example: fmt.Sprintf(
"dbactuator mysql goflashback-binlog %s %s",
subcmd.CmdBaseExampleStr,
subcmd.ToPrettyJson(act.Payload.Example()),
),
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(act.Validate())
util.CheckErr(act.Init())
util.CheckErr(act.Run())
},
}
return cmd
}

// Init TODO
func (d *GoFlashbackBinlogAct) Init() (err error) {
if err = d.BaseOptions.Validate(); err != nil {
return err
}
if err = d.Deserialize(&d.Payload.Params); err != nil {
logger.Error("Deserialize err %s", err.Error())
return err
}
d.Payload.GeneralParam = subcmd.GeneralRuntimeParam
return
}

// Validate TODO
func (d *GoFlashbackBinlogAct) Validate() error {
return nil
}

// Run TODO
func (d *GoFlashbackBinlogAct) Run() (err error) {
defer util.LoggerErrorStack(logger.Error, err)
steps := subcmd.Steps{
{
FunName: "初始化",
Func: d.Payload.Params.Init,
},
{
FunName: "预检查",
Func: d.Payload.Params.PreCheck,
},
{
FunName: "开始 flashback binlog",
Func: d.Payload.Params.Start,
},
}
if err = steps.Run(); err != nil {
return err
}
logger.Info("import binlog successfully")
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func NewMysqlCommand() *cobra.Command {
NewPushMySQLRotateBinlogConfigCommand(),
NewPushMySQLCrondConfigCommand(),
ChangeServerIdCommand(),
GoFlashbackBinlogCommand(),
},
},
{
Expand Down
Loading

0 comments on commit 116bf5a

Please sign in to comment.