Skip to content

Commit 6e1daf9

Browse files
Check RowsAffected when applying DML events to get more accurate statistics (#844)
* Check RowsAffected when applying DML events to get more accurate statistics Addresses #600. When applying a DML event, check the RowsAffected on the `Result` struct. Since all DML event queries are point queries, this will only ever be 0 or 1. The applier then takes this value and multiplies by the `rowsDelta` of the event, resulting in a properly-signed, accurate row delta to use in the statistics. If an error occurs here, log it, but do not surface this as an actual error .. simply assume the DML affected a row and move on. It will be inaccurate, but this is already the case. * Fix import * update wording to warning log message Co-authored-by: Tim Vaillancourt <[email protected]> Co-authored-by: Tim Vaillancourt <[email protected]>
1 parent d726b20 commit 6e1daf9

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

go/logic/applier.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 GitHub Inc.
2+
Copyright 2021 GitHub Inc.
33
See https://github.com/github/gh-ost/blob/master/LICENSE
44
*/
55

@@ -8,6 +8,7 @@ package logic
88
import (
99
gosql "database/sql"
1010
"fmt"
11+
"sync"
1112
"sync/atomic"
1213
"time"
1314

@@ -16,8 +17,8 @@ import (
1617
"github.com/github/gh-ost/go/mysql"
1718
"github.com/github/gh-ost/go/sql"
1819

20+
"github.com/openark/golib/log"
1921
"github.com/openark/golib/sqlutils"
20-
"sync"
2122
)
2223

2324
const (
@@ -1043,11 +1044,20 @@ func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent))
10431044
if buildResult.err != nil {
10441045
return rollback(buildResult.err)
10451046
}
1046-
if _, err := tx.Exec(buildResult.query, buildResult.args...); err != nil {
1047+
result, err := tx.Exec(buildResult.query, buildResult.args...)
1048+
if err != nil {
10471049
err = fmt.Errorf("%s; query=%s; args=%+v", err.Error(), buildResult.query, buildResult.args)
10481050
return rollback(err)
10491051
}
1050-
totalDelta += buildResult.rowsDelta
1052+
1053+
rowsAffected, err := result.RowsAffected()
1054+
if err != nil {
1055+
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)
1056+
rowsAffected = 1
1057+
}
1058+
// each DML is either a single insert (delta +1), update (delta +0) or delete (delta -1).
1059+
// multiplying by the rows actually affected (either 0 or 1) will give an accurate row delta for this DML event
1060+
totalDelta += buildResult.rowsDelta * rowsAffected
10511061
}
10521062
}
10531063
if err := tx.Commit(); err != nil {

0 commit comments

Comments
 (0)