-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Use multiStatement to apply DML #1462
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7e4601
use multiStatements to apply DML
meiji163 90d6148
fix test
meiji163 2a3318c
conn.Raw not working
meiji163 1bd2b0b
Fix named value building.
arthurschreiber c1b6000
add arthurscreiber's review suggestions
meiji163 2e62f2a
set session outside of transaction
meiji163 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,13 @@ import ( | |
|
||
"github.com/github/gh-ost/go/base" | ||
"github.com/github/gh-ost/go/binlog" | ||
"github.com/github/gh-ost/go/mysql" | ||
"github.com/github/gh-ost/go/sql" | ||
|
||
"github.com/openark/golib/log" | ||
"context" | ||
"database/sql/driver" | ||
|
||
"github.com/github/gh-ost/go/mysql" | ||
drivermysql "github.com/go-sql-driver/mysql" | ||
"github.com/openark/golib/sqlutils" | ||
) | ||
|
||
|
@@ -1207,13 +1210,19 @@ func (this *Applier) buildDMLEventQuery(dmlEvent *binlog.BinlogDMLEvent) []*dmlB | |
// ApplyDMLEventQueries applies multiple DML queries onto the _ghost_ table | ||
func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent)) error { | ||
var totalDelta int64 | ||
ctx := context.TODO() | ||
|
||
err := func() error { | ||
tx, err := this.db.Begin() | ||
conn, err := this.db.Conn(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
tx, err := conn.BeginTx(ctx, nil) | ||
if err != nil { | ||
return err | ||
} | ||
rollback := func(err error) error { | ||
tx.Rollback() | ||
return err | ||
|
@@ -1225,26 +1234,70 @@ func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent)) | |
if _, err := tx.Exec(sessionQuery); err != nil { | ||
return rollback(err) | ||
} | ||
|
||
buildResults := make([]*dmlBuildResult, 0, len(dmlEvents)) | ||
for _, dmlEvent := range dmlEvents { | ||
for _, buildResult := range this.buildDMLEventQuery(dmlEvent) { | ||
if buildResult.err != nil { | ||
return rollback(buildResult.err) | ||
} | ||
result, err := tx.Exec(buildResult.query, buildResult.args...) | ||
if err != nil { | ||
err = fmt.Errorf("%w; query=%s; args=%+v", err, buildResult.query, buildResult.args) | ||
return rollback(err) | ||
} | ||
|
||
rowsAffected, err := result.RowsAffected() | ||
if err != nil { | ||
log.Warningf("error getting rows affected from DML event query: %s. i'm going to assume that the DML affected a single row, but this may result in inaccurate statistics", err) | ||
rowsAffected = 1 | ||
buildResults = append(buildResults, buildResult) | ||
} | ||
} | ||
|
||
execErr := conn.Raw(func(driverConn any) error { | ||
ex, ok := driverConn.(driver.ExecerContext) | ||
if !ok { | ||
return fmt.Errorf("could not cast driverConn to ExecerContext") | ||
} | ||
|
||
nvc, ok := driverConn.(driver.NamedValueChecker) | ||
if !ok { | ||
return fmt.Errorf("could not cast driverConn to NamedValueChecker") | ||
} | ||
meiji163 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var multiArgs []driver.NamedValue | ||
meiji163 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
multiQueryBuilder := strings.Builder{} | ||
var rowDeltas []int64 | ||
meiji163 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for _, buildResult := range buildResults { | ||
for _, arg := range buildResult.args { | ||
nv := driver.NamedValue{Value: driver.Value(arg)} | ||
nvc.CheckNamedValue(&nv) | ||
multiArgs = append(multiArgs, nv) | ||
} | ||
// each DML is either a single insert (delta +1), update (delta +0) or delete (delta -1). | ||
// multiplying by the rows actually affected (either 0 or 1) will give an accurate row delta for this DML event | ||
totalDelta += buildResult.rowsDelta * rowsAffected | ||
|
||
multiQueryBuilder.WriteString(buildResult.query) | ||
multiQueryBuilder.WriteString(";\n") | ||
|
||
rowDeltas = append(rowDeltas, buildResult.rowsDelta) | ||
} | ||
|
||
// this.migrationContext.Log.Infof("Executing query: %s, args: %+v", multiQueryBuilder.String(), multiArgs) | ||
res, err := ex.ExecContext(ctx, multiQueryBuilder.String(), multiArgs) | ||
if err != nil { | ||
err = fmt.Errorf("%w; query=%s; args=%+v", err, multiQueryBuilder.String(), multiArgs) | ||
this.migrationContext.Log.Errorf("Error exec: %+v", err) | ||
return err | ||
} | ||
|
||
mysqlRes, ok := res.(drivermysql.Result) | ||
if !ok { | ||
return fmt.Errorf("Could not cast %+v to mysql.Result", res) | ||
} | ||
meiji163 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// each DML is either a single insert (delta +1), update (delta +0) or delete (delta -1). | ||
// multiplying by the rows actually affected (either 0 or 1) will give an accurate row delta for this DML event | ||
for i, rowsAffected := range mysqlRes.AllRowsAffected() { | ||
totalDelta += rowDeltas[i] * rowsAffected | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we add |
||
} | ||
|
||
return nil | ||
}) | ||
|
||
if execErr != nil { | ||
return rollback(execErr) | ||
} | ||
if err := tx.Commit(); err != nil { | ||
return err | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have the
BEGIN
statement be the first in the list of the multistatement and avoid the network roundtrip for this.SET SESSION
can happen before theBEGIN
now. The reason why it was done inside of the transaction was to ensure that it happens on the same connection, but we're now explicitly checking out the connection out of the database pool.