Skip to content

Commit

Permalink
session: remove the mockFail field from txnFuture (pingcap#12545)
Browse files Browse the repository at this point in the history
Signed-off-by: Lonng <[email protected]>
  • Loading branch information
lonng authored Oct 8, 2019
1 parent 52647f0 commit 253cd1f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
7 changes: 5 additions & 2 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3731,10 +3731,13 @@ func (s *testSuite3) TestTSOFail(c *C) {
tk.MustExec(`drop table if exists t`)
tk.MustExec(`create table t(a int)`)

ctx := context.Background()
ctx = context.WithValue(ctx, "mockGetTSFail", struct{}{})
c.Assert(failpoint.Enable("github.com/pingcap/tidb/session/mockGetTSFail", "return"), IsNil)
ctx := failpoint.WithHook(context.Background(), func(ctx context.Context, fpname string) bool {
return fpname == "github.com/pingcap/tidb/session/mockGetTSFail"
})
_, err := tk.Se.Execute(ctx, `select * from t`)
c.Assert(err, NotNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/session/mockGetTSFail"), IsNil)
}

func (s *testSuite3) TestSelectHashPartitionTable(c *C) {
Expand Down
10 changes: 7 additions & 3 deletions session/session_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@ func (s *testSessionSuite) TestGetTSFailDirtyState(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t (id int)")

ctx := context.Background()
ctx = context.WithValue(ctx, "mockGetTSFail", struct{}{})
tk.Se.Execute(ctx, "select * from t")
c.Assert(failpoint.Enable("github.com/pingcap/tidb/session/mockGetTSFail", "return"), IsNil)
ctx := failpoint.WithHook(context.Background(), func(ctx context.Context, fpname string) bool {
return fpname == "github.com/pingcap/tidb/session/mockGetTSFail"
})
_, err := tk.Se.Execute(ctx, "select * from t")
c.Assert(err, NotNil)

// Fix a bug that active txn fail set TxnState.fail to error, and then the following write
// affected by this fail flag.
tk.MustExec("insert into t values (1)")
tk.MustQuery(`select * from t`).Check(testkit.Rows("1"))
c.Assert(failpoint.Disable("github.com/pingcap/tidb/session/mockGetTSFail"), IsNil)
}

func (s *testSessionSuite) TestGetTSFailDirtyStateInretry(c *C) {
Expand Down
20 changes: 11 additions & 9 deletions session/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,24 @@ func mergeToDirtyDB(dirtyDB *executor.DirtyDB, op dirtyTableOperation) {
}
}

type txnFailFuture struct{}

func (txnFailFuture) Wait() (uint64, error) {
return 0, errors.New("mock get timestamp fail")
}

// txnFuture is a promise, which promises to return a txn in future.
type txnFuture struct {
future oracle.Future
store kv.Storage

mockFail bool
}

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

startTS, err := tf.future.Wait()
if err == nil {
return tf.store.BeginWithStartTS(startTS)
} else if _, ok := tf.future.(txnFailFuture); ok {
return nil, err
}

// It would retry get timestamp.
Expand All @@ -409,9 +411,9 @@ func (s *session) getTxnFuture(ctx context.Context) *txnFuture {
tsFuture = oracleStore.GetTimestampAsync(ctx)
}
ret := &txnFuture{future: tsFuture, store: s.store}
if x := ctx.Value("mockGetTSFail"); x != nil {
ret.mockFail = true
}
failpoint.InjectContext(ctx, "mockGetTSFail", func() {
ret.future = txnFailFuture{}
})
return ret
}

Expand Down

0 comments on commit 253cd1f

Please sign in to comment.