Skip to content

Commit

Permalink
session: add gofail test for (pingcap#8743) (pingcap#8771)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and ngaut committed Dec 25, 2018
1 parent 9d8feb5 commit accf34c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ func (s *schemaLeaseChecker) Check(txnTS uint64) error {
return domain.ErrInfoSchemaExpired
}

// mockCommitErrorOnce use to make sure gofail mockCommitError only mock commit error once.
var mockCommitErrorOnce = true

func (s *session) doCommit(ctx context.Context) error {
if !s.txn.Valid() {
return nil
Expand All @@ -290,6 +293,14 @@ func (s *session) doCommit(ctx context.Context) error {
s.txn.changeToInvalid()
s.sessionVars.SetStatusFlag(mysql.ServerStatusInTrans, false)
}()

// mockCommitError and mockGetTSErrorInRetry use to test PR #8743.
// gofail: var mockCommitError bool
//if mockCommitError && mockCommitErrorOnce {
// mockCommitErrorOnce = false
// return kv.ErrRetryable
//}

if s.sessionVars.BinlogClient != nil {
prewriteValue := binloginfo.GetPrewriteValue(s, false)
if prewriteValue != nil {
Expand Down
13 changes: 13 additions & 0 deletions session/session_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@ func (s *testSessionSuite) TestGetTSFailDirtyState(c *C) {
tk.MustExec("insert into t values (1)")
tk.MustQuery(`select * from t`).Check(testkit.Rows("1"))
}

func (s *testSessionSuite) TestGetTSFailDirtyStateInretry(c *C) {
defer gofail.Disable("github.com/pingcap/tidb/session/mockCommitError")
defer gofail.Disable("github.com/pingcap/tidb/session/mockGetTSErrorInRetry")

tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t (id int)")

gofail.Enable("github.com/pingcap/tidb/session/mockCommitError", `return(true)`)
gofail.Enable("github.com/pingcap/tidb/session/mockGetTSErrorInRetry", `return(true)`)
tk.MustExec("insert into t values (2)")
tk.MustQuery(`select * from t`).Check(testkit.Rows("2"))
}
11 changes: 11 additions & 0 deletions session/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,22 @@ type txnFuture struct {
mockFail bool
}

var mockGetTSErrorInRetryOnce = true

func (tf *txnFuture) wait() (kv.Transaction, error) {
if tf.mockFail {
return nil, errors.New("mock get timestamp fail")
}

// mockGetTSErrorInRetry should wait mockCommitErrorOnce first, then will run into retry() logic.
// Then mockGetTSErrorInRetry will return retryable error when first retry.
// Before PR #8743, we don't cleanup txn after meet error such as error like: PD server timeout[try again later]
// This may cause duplicate data to be written.
// gofail: var mockGetTSErrorInRetry bool
//if mockGetTSErrorInRetry && mockGetTSErrorInRetryOnce && !mockCommitErrorOnce {
// mockGetTSErrorInRetryOnce = false
// return nil, errors.Errorf("PD server timeout[try again later]")
//}
startTS, err := tf.future.Wait()
tf.span.Finish()
if err == nil {
Expand Down

0 comments on commit accf34c

Please sign in to comment.