Skip to content

Commit

Permalink
refactor(dbm-services): 优化模拟执行解析表的返回 #9053
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Jan 17, 2025
1 parent 151ca49 commit 7054ecc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 23 deletions.
7 changes: 0 additions & 7 deletions dbm-services/common/db-resource/internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ func init() {
logger.Info("tb_rp_operation_info columns %v", TbRpOperationInfoColumns)
}

// func migration() {
// err := DB.Self.AutoMigrate(&TbRpDailySnapShot{})
// if err != nil {
// logger.Error("auto migrate failed %v", err)
// }
// }

func createSysDb() {
user := config.AppConfig.Db.UserName
pwd := config.AppConfig.Db.PassWord
Expand Down
2 changes: 2 additions & 0 deletions dbm-services/mysql/db-simulation/app/service/kubernets.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ func (k *DbPodSets) getLoadSchemaSQLCmd(bkpath, file string) (cmd string) {
// 从中控dump的schema文件,默认是添加了tc_admin=0,需要删除
// 因为模拟执行是需要将中控进行sql转发
commands = append(commands, fmt.Sprintf("sed -i '/50720 SET tc_admin=0/d' %s", file))
// del definer
commands = append(commands, fmt.Sprintf("sed -i 's/\\sDEFINER=`[^`]*`@`[^`]*`//g' %s", file))
commands = append(commands, fmt.Sprintf("mysql -uroot -p%s --default-character-set=%s -vvv < %s", k.BaseInfo.RootPwd,
k.BaseInfo.Charset, file))
return strings.Join(commands, " && ")
Expand Down
22 changes: 18 additions & 4 deletions dbm-services/mysql/db-simulation/app/syntax/parse_relation_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,19 @@ func (t *TmysqlParse) doParseInchan(alreadExecutedSqlfileCh chan string,
wg.Add(1)
c <- struct{}{}
go func(fileName string) {
defer wg.Done()
cdbs, dbs, commands, dumpAllDbs, err := t.analyzeRelationDbs(fileName, mysqlVersion)
logger.Info("createDbs:%v,dbs:%v,dumpAllDbs:%v,err:%v", cdbs, dbs, dumpAllDbs, err)
if err != nil {
logger.Error("analyzeRelationDbs failed %s", err.Error())
errChan <- err
wg.Done()
return
}
// 如果有dumpall 则直接返回退出,不在继续分析
if dumpAllDbs {
dumpAll = true
<-c
wg.Done()
stopChan <- struct{}{}
}
t.mu.Lock()
Expand All @@ -110,6 +111,7 @@ func (t *TmysqlParse) doParseInchan(alreadExecutedSqlfileCh chan string,
allCommands = append(allCommands, commands...)
t.mu.Unlock()
<-c
wg.Done()
}(sqlfile)
}

Expand Down Expand Up @@ -170,12 +172,24 @@ func (t *TmysqlParse) analyzeRelationDbs(inputfileName, mysqlVersion string) (
return nil, nil, nil, false, fmt.Errorf("%s", res.ErrorMsg)
}
if lo.IsNotEmpty(res.Command) {
allCommandType = append(allCommandType, res.Command)
if res.Command == SQLTypeCreateTable {
var c CreateTableResult
if err = json.Unmarshal(line, &c); err != nil {
logger.Error("json unmasrshal line:%s failed %s", string(line), err.Error())
return nil, nil, nil, false, err
}
// 需要排除create table like
if !c.IsCreateTableLike && !c.IsCreateTableSelect {
allCommandType = append(allCommandType, res.Command)
}
} else {
allCommandType = append(allCommandType, res.Command)
}
}
if slices.Contains([]string{SQLTypeCreateProcedure, SQLTypeCreateFunction, SQLTypeCreateView, SQLTypeCreateTrigger,
SQLTypeInsertSelect, SQLTypeRelaceSelect},
res.Command) {
return nil, nil, nil, true, nil
return nil, nil, allCommandType, true, nil
}
if lo.IsEmpty(res.DbName) {
continue
Expand Down Expand Up @@ -207,7 +221,7 @@ func (tf *TmysqlParseFile) ParseSpecialTbls(mysqlVersion string) (relationTbls [
for k, v := range m {
relationTbls = append(relationTbls, RelationTbl{
DbName: k,
Tbls: v,
Tbls: lo.Uniq(v),
})
}
return relationTbls, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const (
// SQLTypeCreateView is create view sql
SQLTypeCreateView = "create_view"

// SQLTypeInsert is insert sql
SQLTypeInsert = "insert"
// SQLTypeReplace is replace sql
SQLTypeReplace = "replace"
// SQLTypeAlterTable is alter table sql
SQLTypeAlterTable = "alter_table"
// SQLTypeDelete is delete sql
Expand Down Expand Up @@ -276,14 +280,12 @@ type CreateTrigger struct {
type CreateFunction struct {
ParseBase
Definer UserHost `json:"definer,omitempty"`
// TODO
}

// CreateEvent tmysqlparse create event result
type CreateEvent struct {
ParseBase
Definer UserHost `json:"definer,omitempty"`
// TODO
}

// CreateIndex tmysqlparse create index result
Expand Down
41 changes: 32 additions & 9 deletions dbm-services/mysql/db-simulation/handler/syntax_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,29 @@ func (s SyntaxHandler) ParseSQLFileRelationDb(r *gin.Context) {
s.SendResponse(r, err, nil)
return
}
defer p.DelTempDir()
// 如果所有的命令都是alter table, dump指定库表
logger.Debug("debug: %v,%d", allCommands, len(allCommands))
if isAllOperateTable(allCommands) || isAllCreateTable(allCommands) {
if isAllOperateTable(allCommands) {
relationTbls, err := p.ParseSpecialTbls("")
if err != nil {
s.SendResponse(r, err, nil)
return
}
tblCount := 0
for _, tbl := range relationTbls {
tblCount += len(lo.Uniq(tbl.Tbls))
}
// sql语句的变更表数量大于5000,防止mysqldump 拼接参数过长导致执行失败
if tblCount > 5000 || len(relationTbls) > 100 {
s.SendResponse(r, nil, gin.H{
"create_dbs": createDbs,
"dbs": dbs,
"dump_all": dumpall,
"timestamp": time.Now().Unix(),
})
return
}
s.SendResponse(r, nil, gin.H{
"create_dbs": createDbs,
"dbs": dbs,
Expand All @@ -285,12 +300,20 @@ func (s SyntaxHandler) ParseSQLFileRelationDb(r *gin.Context) {
}

func isAllOperateTable(allCommands []string) bool {
return lo.Every([]string{syntax.SQLTypeAlterTable, syntax.SQLTypeUseDb,
syntax.SQLTypeCreateIndex, syntax.SQLTypeDropTable}, allCommands)
}

func isAllCreateTable(allCommands []string) bool {
return lo.Every([]string{syntax.SQLTypeCreateTable, syntax.SQLTypeUseDb}, allCommands)
if len(allCommands) == 0 {
return false
}
// 不允许只用use db
if len(allCommands) == 1 && allCommands[0] == syntax.SQLTypeUseDb {
return false
}
return lo.Every([]string{
syntax.SQLTypeAlterTable, syntax.SQLTypeUseDb,
syntax.SQLTypeCreateIndex, syntax.SQLTypeDropTable,
syntax.SQLTypeInsert, syntax.SQLTypeUpdate,
syntax.SQLTypeDelete, syntax.SQLTypeCreateTable,
syntax.SQLTypeReplace,
}, allCommands)
}

// ParseSQLRelationDb 语法检查入参SQL string
Expand Down Expand Up @@ -333,8 +356,8 @@ func (s *SyntaxHandler) ParseSQLRelationDb(r *gin.Context) {
return
}
// 如果所有的命令都是alter table, dump指定库表
logger.Info("make debug: %v,%d", allCommands, len(allCommands))
if isAllOperateTable(allCommands) || isAllCreateTable(allCommands) {
logger.Info("all command types: %v,%d", allCommands, len(allCommands))
if isAllOperateTable(allCommands) {
relationTbls, err := p.ParseSpecialTbls("")
if err != nil {
s.SendResponse(r, err, nil)
Expand Down
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/db-simulation/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 0.0.1-alpha.91
appVersion: 0.0.1-alpha.92
description: A Helm chart for Kubernetes
name: db-simulation
type: application
Expand Down

0 comments on commit 7054ecc

Please sign in to comment.