Skip to content

Commit

Permalink
refactor(dbm-services): 允许执行create table like TencentBlueKing#7199
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Sep 30, 2024
1 parent fce8454 commit ccb1b97
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dbm-services/common/dbha/ha-module/test/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 0 additions & 1 deletion dbm-services/mysql/db-simulation/app/syntax/spider_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
42 changes: 26 additions & 16 deletions dbm-services/mysql/db-simulation/app/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}

Expand Down
8 changes: 4 additions & 4 deletions dbm-services/mysql/db-simulation/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions dbm-services/mysql/db-simulation/spider_rule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ccb1b97

Please sign in to comment.