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

sink(ticdc): use separate ctx for each DML #7981

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions cdc/sink/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,6 @@ func (s *mysqlSink) execDMLWithMaxRetries(pctx context.Context, dmls *preparedDM
return retry.Do(pctx, func() error {
writeTimeout, _ := time.ParseDuration(s.params.writeTimeout)
writeTimeout += networkDriftDuration
ctx, cancelFunc := context.WithTimeout(pctx, writeTimeout)
defer cancelFunc()

failpoint.Inject("MySQLSinkTxnRandomError", func() {
failpoint.Return(
Expand All @@ -661,7 +659,7 @@ func (s *mysqlSink) execDMLWithMaxRetries(pctx context.Context, dmls *preparedDM
time.Sleep(time.Hour)
})
err := s.statistics.RecordBatchExecution(func() (int, error) {
tx, err := s.db.BeginTx(ctx, nil)
tx, err := s.db.BeginTx(pctx, nil)
if err != nil {
return 0, logDMLTxnErr(
cerror.WrapError(cerror.ErrMySQLTxnError, err),
Expand All @@ -671,6 +669,7 @@ func (s *mysqlSink) execDMLWithMaxRetries(pctx context.Context, dmls *preparedDM
for i, query := range dmls.sqls {
args := dmls.values[i]
log.Debug("exec row", zap.String("sql", query), zap.Any("args", args))
ctx, cancelFunc := context.WithTimeout(pctx, writeTimeout)
if _, err := tx.ExecContext(ctx, query, args...); err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
if errors.Cause(rbErr) != context.Canceled {
Expand All @@ -680,10 +679,12 @@ func (s *mysqlSink) execDMLWithMaxRetries(pctx context.Context, dmls *preparedDM
start, s.params.changefeedID, query, dmls.rowCount, dmls.startTs)
}
}
cancelFunc()
return 0, logDMLTxnErr(
cerror.WrapError(cerror.ErrMySQLTxnError, err),
start, s.params.changefeedID, query, dmls.rowCount, dmls.startTs)
}
cancelFunc()
}

if err = tx.Commit(); err != nil {
Expand Down
19 changes: 6 additions & 13 deletions cdc/sinkv2/eventsink/txn/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,28 +557,18 @@ func (s *mysqlBackend) execDMLWithMaxRetries(pctx context.Context, dmls *prepare
return retry.Do(pctx, func() error {
writeTimeout, _ := time.ParseDuration(s.cfg.WriteTimeout)
writeTimeout += networkDriftDuration
ctx, cancelFunc := context.WithTimeout(pctx, writeTimeout)
defer cancelFunc()

failpoint.Inject("MySQLSinkTxnRandomError", func() {
fmt.Printf("start to random error")
err := logDMLTxnErr(errors.Trace(driver.ErrBadConn), start, s.changefeed, "failpoint", 0, nil)
failpoint.Return(err)
})
failpoint.Inject("MySQLSinkHangLongTime", func() {
timer := time.NewTimer(time.Hour)
select {
case <-timer.C:
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
failpoint.Return(context.Canceled)
}
time.Sleep(time.Hour)
})

err := s.statistics.RecordBatchExecution(func() (int, error) {
tx, err := s.db.BeginTx(ctx, nil)
tx, err := s.db.BeginTx(pctx, nil)
if err != nil {
return 0, logDMLTxnErr(
cerror.WrapError(cerror.ErrMySQLTxnError, err),
Expand All @@ -589,6 +579,7 @@ func (s *mysqlBackend) execDMLWithMaxRetries(pctx context.Context, dmls *prepare
args := dmls.values[i]
log.Debug("exec row", zap.Int("workerID", s.workerID),
zap.String("sql", query), zap.Any("args", args))
ctx, cancelFunc := context.WithTimeout(pctx, writeTimeout)
if _, err := tx.ExecContext(ctx, query, args...); err != nil {
err := logDMLTxnErr(
cerror.WrapError(cerror.ErrMySQLTxnError, err),
Expand All @@ -598,13 +589,15 @@ func (s *mysqlBackend) execDMLWithMaxRetries(pctx context.Context, dmls *prepare
log.Warn("failed to rollback txn", zap.Error(rbErr))
}
}
cancelFunc()
return 0, err
}
cancelFunc()
}

// we set write source for each txn,
// so we can use it to trace the data source
if err = s.setWriteSource(ctx, tx); err != nil {
if err = s.setWriteSource(pctx, tx); err != nil {
err := logDMLTxnErr(
cerror.WrapError(cerror.ErrMySQLTxnError, err),
start, s.changefeed,
Expand Down
12 changes: 0 additions & 12 deletions cdc/sinkv2/eventsink/txn/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/DATA-DOG/go-sqlmock"
dmysql "github.com/go-sql-driver/mysql"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser/mysql"
Expand Down Expand Up @@ -1447,17 +1446,6 @@ func TestPrepareBatchDMLs(t *testing.T) {
}
}

func TestNetworkPartition(t *testing.T) {
ctx := context.Background()
ms := newMySQLBackendWithoutDB(ctx)
ms.cfg.WriteTimeout = "1s"
_ = failpoint.Enable("github.com/pingcap/tiflow/cdc/sinkv2/eventsink/txn/mysql/MySQLSinkHangLongTime", "return")
defer failpoint.Disable("github.com/pingcap/tiflow/cdc/sinkv2/eventsink/txn/mysql/MySQLSinkHangLongTime")

err := ms.execDMLWithMaxRetries(ctx, &preparedDMLs{})
require.Equal(t, context.Canceled, err)
}

func TestGroupRowsByType(t *testing.T) {
ctx := context.Background()
ms := newMySQLBackendWithoutDB(ctx)
Expand Down