Skip to content

Commit

Permalink
session: ignore linearizability setting for auto-commit transactions (#…
Browse files Browse the repository at this point in the history
…22746)

Signed-off-by: Yilin Chen <[email protected]>
  • Loading branch information
sticnarf authored Feb 18, 2021
1 parent 4031706 commit 9cb9b69
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,13 @@ func (s *session) doCommit(ctx context.Context) error {
s.txn.SetOption(kv.Enable1PC, s.GetSessionVars().Enable1PC)
// priority of the sysvar is lower than `start transaction with causal consistency only`
if s.txn.GetOption(kv.GuaranteeLinearizability) == nil {
s.txn.SetOption(kv.GuaranteeLinearizability, s.GetSessionVars().GuaranteeLinearizability)
// We needn't ask the TiKV client to guarantee linearizability for auto-commit transactions
// because the property is naturally holds:
// We guarantee the commitTS of any transaction must not exceed the next timestamp from the TSO.
// An auto-commit transaction fetches its startTS from the TSO so its commitTS > its startTS > the commitTS
// of any previously committed transactions.
s.txn.SetOption(kv.GuaranteeLinearizability,
!s.GetSessionVars().IsAutocommit() && s.GetSessionVars().GuaranteeLinearizability)
}

return s.txn.Commit(tikvutil.SetSessionID(ctx, s.GetSessionVars().ConnectionID))
Expand Down
17 changes: 17 additions & 0 deletions session/session_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,20 @@ func (s *testSessionSerialSuite) TestClusterTableSendError(c *C) {
c.Assert(tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1))
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings()[0].Err, ErrorMatches, ".*TiDB server timeout, address is.*")
}

func (s *testSessionSerialSuite) TestAutoCommitNeedNotLinearizability(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t1;")
defer tk.MustExec("drop table if exists t1")
tk.MustExec(`create table t1 (c int)`)

c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/getMinCommitTSFromTSO", `panic`), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/store/tikv/getMinCommitTSFromTSO")

tk.Se.GetSessionVars().SetSystemVar("tidb_enable_async_commit", "1")
tk.Se.GetSessionVars().SetSystemVar("tidb_guarantee_linearizability", "1")
tk.MustExec("INSERT INTO t1 VALUES (1)")

tk.Se.GetSessionVars().SetSystemVar("tidb_enable_1pc", "1")
tk.MustExec("INSERT INTO t1 VALUES (2)")
}
1 change: 1 addition & 0 deletions store/tikv/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ func (c *twoPhaseCommitter) execute(ctx context.Context) (err error) {
// than the snapshot TS of all existent readers. So we get a new timestamp
// from PD as our MinCommitTS.
if commitTSMayBeCalculated && c.needLinearizability() {
failpoint.Inject("getMinCommitTSFromTSO", nil)
minCommitTS, err := c.store.oracle.GetTimestamp(ctx, &oracle.Option{TxnScope: oracle.GlobalTxnScope})
// If we fail to get a timestamp from PD, we just propagate the failure
// instead of falling back to the normal 2PC because a normal 2PC will
Expand Down

0 comments on commit 9cb9b69

Please sign in to comment.