From ccb1b973d1c1b54362d80090580430ab21c7d591 Mon Sep 17 00:00:00 2001 From: yuanruji Date: Mon, 30 Sep 2024 14:48:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor(dbm-services):=20=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E6=89=A7=E8=A1=8Ccreate=20table=20like=20#7199?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/dbha/ha-module/test/agent_test.go | 2 +- .../app/syntax/spider_create_table_rule.go | 8 ++-- .../db-simulation/app/syntax/spider_rule.go | 1 - .../mysql/db-simulation/app/syntax/syntax.go | 42 ++++++++++++------- .../mysql/db-simulation/handler/handler.go | 8 ++-- .../mysql/db-simulation/spider_rule.yaml | 4 -- 6 files changed, 35 insertions(+), 30 deletions(-) diff --git a/dbm-services/common/dbha/ha-module/test/agent_test.go b/dbm-services/common/dbha/ha-module/test/agent_test.go index f182dbc997..5177955d33 100644 --- a/dbm-services/common/dbha/ha-module/test/agent_test.go +++ b/dbm-services/common/dbha/ha-module/test/agent_test.go @@ -68,7 +68,7 @@ func TestAgentNetTransfor(t *testing.T) { case 3: dbIns.App = "APP4444" } - err = agentIns.ReporterGM(d) + err = agentIns.RepairGM(&gmInfo) if err != nil { t.Errorf("reporter gmInfo failed.err:%s", err.Error()) return diff --git a/dbm-services/mysql/db-simulation/app/syntax/spider_create_table_rule.go b/dbm-services/mysql/db-simulation/app/syntax/spider_create_table_rule.go index 3eba48dd4a..b62c219cb0 100644 --- a/dbm-services/mysql/db-simulation/app/syntax/spider_create_table_rule.go +++ b/dbm-services/mysql/db-simulation/app/syntax/spider_create_table_rule.go @@ -33,16 +33,16 @@ func (c CreateTableResult) SpiderChecker(spiderVersion string) (r *CheckerResult return SpecialCharValidator(c.TableName) }) } - if c.IsCreateTableLike { - r.Trigger(SR.SpiderCreateTableRule.CreateTbLike, "") - } if c.IsCreateTableSelect { r.Trigger(SR.SpiderCreateTableRule.CreateWithSelect, "") } if c.ColCharsetNotEqTbCharset() { r.Trigger(SR.SpiderCreateTableRule.ColChasetNotEqTbChaset, "") } - c.shardKeyChecker(r) + // when sql is create table like, no check shard key + if !c.IsCreateTableLike { + c.shardKeyChecker(r) + } return r } diff --git a/dbm-services/mysql/db-simulation/app/syntax/spider_rule.go b/dbm-services/mysql/db-simulation/app/syntax/spider_rule.go index a7a52d502d..6671202e68 100644 --- a/dbm-services/mysql/db-simulation/app/syntax/spider_rule.go +++ b/dbm-services/mysql/db-simulation/app/syntax/spider_rule.go @@ -38,7 +38,6 @@ type SpiderRules struct { type SpiderCreateTableRule struct { ColChasetNotEqTbChaset *BoolRuleItem `yaml:"ColChasetNotEqTbChaset"` CreateWithSelect *BoolRuleItem `yaml:"CreateWithSelect"` - CreateTbLike *BoolRuleItem `yaml:"CreateTbLike"` ShardKeyNotPk *BoolRuleItem `yaml:"ShardKeyNotPk"` ShardKeyNotIndex *BoolRuleItem `yaml:"ShardKeyNotIndex"` IllegalComment *BoolRuleItem `yaml:"IllegalComment"` diff --git a/dbm-services/mysql/db-simulation/app/syntax/syntax.go b/dbm-services/mysql/db-simulation/app/syntax/syntax.go index 2f9c76645c..be229fef97 100644 --- a/dbm-services/mysql/db-simulation/app/syntax/syntax.go +++ b/dbm-services/mysql/db-simulation/app/syntax/syntax.go @@ -303,59 +303,69 @@ func getSQLParseResultFile(fileName, version string) string { return fmt.Sprintf("%s-%s.json", version, fileName) } -func (t *TmysqlParse) getCommand(filename, version string) (cmd string) { - var in, out string - in = path.Join(t.tmpWorkdir, filename) +// getCommand generates the command string for running TmysqlParse +// It takes the input filename and MySQL version as parameters +func (t *TmysqlParse) getCommand(filename, version string) string { + // Construct input and output file paths + inputPath := path.Join(t.tmpWorkdir, filename) outputFileName := getSQLParseResultFile(filename, version) - out = path.Join(t.tmpWorkdir, outputFileName) + outputPath := path.Join(t.tmpWorkdir, outputFileName) - cmd = fmt.Sprintf(`%s --sql-file=%s --output-path=%s --print-query-mode=2 --output-format='JSON_LINE_PER_OBJECT' --sql-mode='' `, - t.TmysqlParseBinPath, in, out) + // Build the base command with common options + cmd := fmt.Sprintf(`%s --sql-file=%s --output-path=%s `+ + `--print-query-mode=2 --output-format='JSON_LINE_PER_OBJECT' --sql-mode=''`, + t.TmysqlParseBinPath, inputPath, outputPath) + // Add MySQL version if provided if lo.IsNotEmpty(version) { - cmd += fmt.Sprintf(" --mysql-version=%s ", version) + cmd += fmt.Sprintf(" --mysql-version=%s", version) } return cmd } -// Execute 运行tmysqlpase -// -// @receiver tf -// @return err +// Execute runs the TmysqlParse command for each SQL file in parallel. +// It takes a channel to send the names of successfully executed files and the MySQL version as parameters. +// The function returns an error if any of the executions fail. func (tf *TmysqlParseFile) Execute(alreadExecutedSqlfileCh chan string, version string) (err error) { var wg sync.WaitGroup var errs []error - c := make(chan struct{}, 10) + c := make(chan struct{}, 10) // Semaphore to limit concurrent goroutines errChan := make(chan error, 5) + + // Iterate through all SQL files for _, fileName := range tf.Param.FileNames { wg.Add(1) - c <- struct{}{} + c <- struct{}{} // Acquire semaphore go func(sqlfile, ver string) { + defer wg.Done() + defer func() { <-c }() // Release semaphore + //nolint command := exec.Command("/bin/bash", "-c", tf.getCommand(sqlfile, ver)) logger.Info("command is %s", command) output, err := command.CombinedOutput() if err != nil { - errChan <- fmt.Errorf("tmysqlparse.sh command run failed. error info:" + err.Error() + "," + string(output)) + errChan <- fmt.Errorf("tmysqlparse.sh command run failed. error info: %v, %s", err, string(output)) } else { alreadExecutedSqlfileCh <- sqlfile } - <-c - wg.Done() }(fileName, version) } + // Wait for all goroutines to finish and close error channel go func() { wg.Wait() close(errChan) }() + // Collect all errors for err := range errChan { errs = append(errs, err) } + // Join all errors and return return errors.Join(errs...) } diff --git a/dbm-services/mysql/db-simulation/handler/handler.go b/dbm-services/mysql/db-simulation/handler/handler.go index 813102608a..869952e08f 100644 --- a/dbm-services/mysql/db-simulation/handler/handler.go +++ b/dbm-services/mysql/db-simulation/handler/handler.go @@ -36,8 +36,9 @@ type Response struct { // CreateClusterParam 创建临时的spider的集群参数 type CreateClusterParam struct { - Pwd string `json:"pwd"` - PodName string `json:"podname"` + Pwd string `json:"pwd"` + PodName string `json:"podname"` + SpiderVersion string `json:"spider_version"` } // CreateTmpSpiderPodCluster 创建临时的spider的集群,多用于测试,debug @@ -55,8 +56,7 @@ func CreateTmpSpiderPodCluster(r *gin.Context) { Charset: "utf8mb4", } ps.DbImage = config.GAppConfig.Image.Tendb57Img - ps.TdbCtlImage = config.GAppConfig.Image.TdbCtlImg - ps.SpiderImage = config.GAppConfig.Image.SpiderImg + ps.SpiderImage, ps.TdbCtlImage = getSpiderAndTdbctlImg(param.SpiderVersion, LatestVersion) if err := ps.CreateClusterPod(); err != nil { logger.Error(err.Error()) return diff --git a/dbm-services/mysql/db-simulation/spider_rule.yaml b/dbm-services/mysql/db-simulation/spider_rule.yaml index 568abf2fac..8b8573d06a 100644 --- a/dbm-services/mysql/db-simulation/spider_rule.yaml +++ b/dbm-services/mysql/db-simulation/spider_rule.yaml @@ -61,10 +61,6 @@ SpiderCreateTableRule: turnOn: true ban: true desc: "UNSUPPORT SQL CREATE TABLE WITH SELECT" - CreateTbLike: - turnOn: true - ban: true - desc: "UNSUPPORT SQL CREATE TABLE LIKE" ShardKeyNotPk: turnOn: true ban: true