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

mysqlctl: Use DBA connection for schema operations #13178

Merged
merged 1 commit into from
Jun 17, 2023
Merged
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
29 changes: 19 additions & 10 deletions go/vt/mysqlctl/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,24 @@ import (

var autoIncr = regexp.MustCompile(` AUTO_INCREMENT=\d+`)

// executeSchemaCommands executes some SQL commands, using the mysql
// command line tool. It uses the dba connection parameters, with credentials.
func (mysqld *Mysqld) executeSchemaCommands(sql string) error {
params, err := mysqld.dbcfgs.DbaConnector().MysqlParams()
// executeSchemaCommands executes some SQL commands. It uses the dba connection parameters, with credentials.
func (mysqld *Mysqld) executeSchemaCommands(ctx context.Context, sql string) error {
conn, err := getPoolReconnect(ctx, mysqld.dbaPool)
if err != nil {
return err
}

return mysqld.executeMysqlScript(params, strings.NewReader(sql))
defer conn.Recycle()
_, more, err := conn.ExecuteFetchMulti(sql, 0, false)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rohit-nayak-ps @mattlord It was something 🤦 here. The problem is that executeSchemaCommands gets multiple queries separated with ; as input and we didn't properly read off all results which means they are not properly processed immediately and we get the weird behavior we saw.

I changed this to call ExecuteFetchMulti and with draining all results and things are now properly passing and there's no weird behavior anymore with missing tables.

if err != nil {
return err
}
for more {
_, more, _, err = conn.ReadQueryResult(0, false)
if err != nil {
return err
}
}
return nil
}

func encodeEntityName(name string) string {
Expand Down Expand Up @@ -427,7 +436,7 @@ func (mysqld *Mysqld) PreflightSchemaChange(ctx context.Context, dbName string,
initialCopySQL += s + ";\n"
}
}
if err = mysqld.executeSchemaCommands(initialCopySQL); err != nil {
if err = mysqld.executeSchemaCommands(ctx, initialCopySQL); err != nil {
return nil, err
}

Expand All @@ -443,7 +452,7 @@ func (mysqld *Mysqld) PreflightSchemaChange(ctx context.Context, dbName string,
sql := "SET sql_log_bin = 0;\n"
sql += "USE _vt_preflight;\n"
sql += change
if err = mysqld.executeSchemaCommands(sql); err != nil {
if err = mysqld.executeSchemaCommands(ctx, sql); err != nil {
return nil, err
}

Expand All @@ -459,7 +468,7 @@ func (mysqld *Mysqld) PreflightSchemaChange(ctx context.Context, dbName string,
// and clean up the extra database
dropSQL := "SET sql_log_bin = 0;\n"
dropSQL += "DROP DATABASE _vt_preflight;\n"
if err = mysqld.executeSchemaCommands(dropSQL); err != nil {
if err = mysqld.executeSchemaCommands(ctx, dropSQL); err != nil {
return nil, err
}

Expand Down Expand Up @@ -519,7 +528,7 @@ func (mysqld *Mysqld) ApplySchemaChange(ctx context.Context, dbName string, chan

// execute the schema change using an external mysql process
// (to benefit from the extra commands in mysql cli)
if err = mysqld.executeSchemaCommands(sql); err != nil {
if err = mysqld.executeSchemaCommands(ctx, sql); err != nil {
return nil, err
}

Expand Down