Skip to content

Commit b8d4ebb

Browse files
author
ffffwh
committed
retry tx when there is a deadlock
1 parent a65ec4d commit b8d4ebb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

driver/mysql/applier_incr.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
gomysql "github.com/go-mysql-org/go-mysql/mysql"
1919
"github.com/pkg/errors"
2020
uuid "github.com/satori/go.uuid"
21+
mysqldriver "github.com/go-sql-driver/mysql"
2122
)
2223

2324
const (
@@ -233,11 +234,21 @@ func (a *ApplierIncr) MtsWorker(workerIndex int) {
233234
case entryContext := <-a.applyBinlogMtsTxQueue:
234235
hasEntry = true
235236
logger.Debug("a binlogEntry MTS dequeue", "gno", entryContext.Entry.Coordinates.GetGNO())
236-
if err := a.ApplyBinlogEvent(workerIndex, entryContext); err != nil {
237-
a.OnError(common.TaskStateDead, err) // TODO coordinate with other goroutine
238-
keepLoop = false
239-
} else {
240-
// do nothing
237+
const deadlockTryLimit = 3
238+
for iTry := 0; ; iTry++ {
239+
err := a.ApplyBinlogEvent(workerIndex, entryContext)
240+
if err != nil {
241+
if merr, isME := err.(*mysqldriver.MySQLError); isME {
242+
if merr.Number == sql.ErrLockDeadlock && iTry < deadlockTryLimit {
243+
logger.Info("found deadlock. will retry tx", "gno", entryContext.Entry.Coordinates.GetGNO(),
244+
"iTry", iTry)
245+
continue
246+
}
247+
}
248+
a.OnError(common.TaskStateDead, err) // TODO coordinate with other goroutine
249+
keepLoop = false
250+
}
251+
break;
241252
}
242253
logger.Debug("after ApplyBinlogEvent.", "gno", entryContext.Entry.Coordinates.GetGNO())
243254
case <-t.C:

0 commit comments

Comments
 (0)