Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(other): tbinlogdumper部署时增加全量同步的逻辑 #991

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
// Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://opensource.org/licenses/MIT
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

package tbinlogdumpercmd

import (
"fmt"

"github.com/spf13/cobra"

"dbm-services/bigdata/db-tools/dbactuator/pkg/util"
"dbm-services/common/go-pubpkg/logger"
"dbm-services/mysql/db-tools/dbactuator/internal/subcmd"
"dbm-services/mysql/db-tools/dbactuator/pkg/components/mysql/backupdemand"
)

type BackupDemandAct struct {
*subcmd.BaseOptions
Payload backupdemand.Component
}

func NewDumperBackupDemandCommand() *cobra.Command {
act := BackupDemandAct{BaseOptions: subcmd.GBaseOptions}
cmd := &cobra.Command{
Use: "backup-demand",
Short: "备份请求",
Example: fmt.Sprintf(
`dbactuator tbinlogdumper backup-demand %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
}

func (d *BackupDemandAct) Init() (err error) {
if err = d.BaseOptions.Validate(); err != nil { // @todo 应该在一开始就validate
return err
}
if err = d.Deserialize(&d.Payload.Params); err != nil {
logger.Error("DeserializeAndValidate err %s", err.Error())
return err
}
logger.Warn("params %+v", d.Payload.Params)

return
}

func (d *BackupDemandAct) Validate() error {
return nil
}

func (d *BackupDemandAct) Run() (err error) {
defer util.LoggerErrorStack(logger.Error, err)
steps := subcmd.Steps{
{
FunName: "初始化",
Func: d.Payload.Init,
},
{
FunName: "生成备份配置",
Func: d.Payload.GenerateBackupConfig,
},
{
FunName: "执行备份",
Func: d.Payload.DoBackup,
},
{
FunName: "返回报告",
Func: d.Payload.OutPutForTBinlogDumper,
},
}

if err = steps.Run(); err != nil {
return err
}

logger.Info("backup demand success")
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func NewTbinlogDumperCommand() *cobra.Command {
Commands: []*cobra.Command{
NewDeployTbinlogDumperCommand(),
NewUnInstallTbinlogDumperCommand(),
NewDumperBackupDemandCommand(),
NewDumpSchemaCommand(),
},
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tbinlogdumpercmd

import (
"fmt"

"dbm-services/common/go-pubpkg/logger"
"dbm-services/mysql/db-tools/dbactuator/internal/subcmd"
"dbm-services/mysql/db-tools/dbactuator/pkg/components/tbinlogdumper"
"dbm-services/mysql/db-tools/dbactuator/pkg/util"

"github.com/spf13/cobra"
)

// DumpSchemaAct TODO
type DumpSchemaAct struct {
*subcmd.BaseOptions
Service tbinlogdumper.DumpSchemaComp
}

// NewSenmanticDumpSchemaCommand godoc
//
// @Summary 备份表结构并导入
// @Description 备份表结构并导入
// @Tags tbinlogdumper
// @Accept json
// @Produce json
// @Param body body tbinlogdumper.DumpSchemaComp true "short description"
// @Router /tbinlogdumper/semantic-dumpschema [post]
func NewDumpSchemaCommand() *cobra.Command {
act := DumpSchemaAct{
BaseOptions: subcmd.GBaseOptions,
}
cmd := &cobra.Command{
Use: "dumpschema",
Short: "运行导出并导入表结构",
Example: fmt.Sprintf(
`dbactuator tbinlogdumper dumpschema %s %s`,
subcmd.CmdBaseExampleStr, subcmd.ToPrettyJson(act.Service.Example()),
),
Run: func(cmd *cobra.Command, args []string) {
util.CheckErr(act.Validate())
util.CheckErr(act.Init())
util.CheckErr(act.Run())
},
}
return cmd
}

// Validate TODO
func (d *DumpSchemaAct) Validate() (err error) {
return d.BaseOptions.Validate()
}

// Init TODO
func (d *DumpSchemaAct) Init() (err error) {
if err = d.Deserialize(&d.Service.Params); err != nil {
logger.Error("DeserializeAndValidate failed, %v", err)
return err
}
d.Service.GeneralParam = subcmd.GeneralRuntimeParam
return nil
}

// Run TODO
func (d *DumpSchemaAct) Run() (err error) {
steps := subcmd.Steps{
{
FunName: "init",
Func: d.Service.Init,
},
{
FunName: "precheck",
Func: d.Service.Precheck,
},
{
FunName: "运行导出表结构",
Func: d.Service.DumpSchema,
},
{
FunName: "修改表结构的存储引擎",
Func: d.Service.ModifyEngine,
},
{
FunName: "导入表结构到TBinlogdumper",
Func: d.Service.LoadSchema,
},
}
if err := steps.Run(); err != nil {
return err
}

logger.Info("同步表结构到TBinlogdumper实例成功")
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ type context struct {
randString string
resultReportPath string
statusReportPath string
backupPort []int // 当在 spider master备份时, 会有 [25000, 26000] 两个端口

backupPort []int // 当在 spider master备份时, 会有 [25000, 26000] 两个端口
backupDir string //只是兼容tbinlogdumper的备份日志输出,存储备份目录信息,没有任何处理逻辑
}

type Report struct {
Expand Down Expand Up @@ -152,6 +152,8 @@ func (c *Component) GenerateBackupConfig() error {
logger.Error("mkdir %s failed: %s", backupConfig.Public.BackupDir, err.Error())
return err
}
// 增加为tbinlogdumper做库表备份的日志输出,保存流程上下文
c.backupDir = backupConfig.Public.BackupDir
}

backupConfigFile := ini.Empty()
Expand Down Expand Up @@ -306,3 +308,22 @@ func (c *Component) Example() interface{} {
},
}
}

// OutPutForTBinlogDumper 增加为tbinlogdumper做库表备份的日志输出,保存流程上下文
func (c *Component) OutPutForTBinlogDumper() error {
ret := make(map[string]interface{})
report, err := c.generateReport()
if err != nil {
return err
}
ret["report_result"] = report.Result
ret["report_status"] = report.Status
ret["backup_dir"] = c.backupDir

err = components.PrintOutputCtx(ret)
if err != nil {
logger.Error("output backup report failed: %s", err.Error())
return err
}
return nil
}
Loading
Loading