From 4e85d15ca99625ed808f2767b005e1122d0006e7 Mon Sep 17 00:00:00 2001 From: crazycs Date: Wed, 23 Jun 2021 15:31:22 +0800 Subject: [PATCH 1/2] cherry pick #25616 to release-5.1 Signed-off-by: ti-srebot --- server/tidb_test.go | 12 ++++++++++++ session/session.go | 22 +++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/server/tidb_test.go b/server/tidb_test.go index a28bce7a52bbd..5605c9ff42bbd 100644 --- a/server/tidb_test.go +++ b/server/tidb_test.go @@ -1357,6 +1357,15 @@ func (ts *tidbTestTopSQLSuite) TestTopSQLCPUProfile(c *C) { } } + // Test case 4: transaction commit + ctx4, cancel4 := context.WithCancel(context.Background()) + defer cancel4() + go ts.loopExec(ctx4, c, func(db *sql.DB) { + db.Exec("begin") + db.Exec("insert into t () values (),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),()") + db.Exec("commit") + }) + // Check result of test case 1. for _, ca := range cases1 { checkFn(ca.sql, ca.planRegexp) @@ -1374,6 +1383,9 @@ func (ts *tidbTestTopSQLSuite) TestTopSQLCPUProfile(c *C) { checkFn(ca.prepare, ca.planRegexp) ca.cancel() } + + // Check result of test case 4. + checkFn("commit", "") } func (ts *tidbTestTopSQLSuite) TestTopSQLAgent(c *C) { diff --git a/session/session.go b/session/session.go index 880fa13a6671d..31852bb8fd20c 100644 --- a/session/session.go +++ b/session/session.go @@ -504,10 +504,11 @@ func (s *session) doCommit(ctx context.Context) error { } } + sessVars := s.GetSessionVars() // Get the related table or partition IDs. - relatedPhysicalTables := s.GetSessionVars().TxnCtx.TableDeltaMap + relatedPhysicalTables := sessVars.TxnCtx.TableDeltaMap // Get accessed global temporary tables in the transaction. - temporaryTables := s.GetSessionVars().TxnCtx.GlobalTemporaryTables + temporaryTables := sessVars.TxnCtx.GlobalTemporaryTables physicalTableIDs := make([]int64, 0, len(relatedPhysicalTables)) for id := range relatedPhysicalTables { // Schema change on global temporary tables doesn't affect transactions. @@ -520,11 +521,12 @@ func (s *session) doCommit(ctx context.Context) error { s.txn.SetOption(kv.SchemaChecker, domain.NewSchemaChecker(domain.GetDomain(s), s.GetInfoSchema().SchemaMetaVersion(), physicalTableIDs)) s.txn.SetOption(kv.InfoSchema, s.sessionVars.TxnCtx.InfoSchema) s.txn.SetOption(kv.CommitHook, func(info string, _ error) { s.sessionVars.LastTxnInfo = info }) - if s.GetSessionVars().EnableAmendPessimisticTxn { + if sessVars.EnableAmendPessimisticTxn { s.txn.SetOption(kv.SchemaAmender, NewSchemaAmenderForTikvTxn(s)) } - s.txn.SetOption(kv.EnableAsyncCommit, s.GetSessionVars().EnableAsyncCommit) - s.txn.SetOption(kv.Enable1PC, s.GetSessionVars().Enable1PC) + s.txn.SetOption(kv.EnableAsyncCommit, sessVars.EnableAsyncCommit) + s.txn.SetOption(kv.Enable1PC, sessVars.Enable1PC) + s.txn.SetOption(kv.ResourceGroupTag, sessVars.StmtCtx.GetResourceGroupTag()) // priority of the sysvar is lower than `start transaction with causal consistency only` if val := s.txn.GetOption(kv.GuaranteeLinearizability); val == nil || val.(bool) { // We needn't ask the TiKV client to guarantee linearizability for auto-commit transactions @@ -533,10 +535,16 @@ func (s *session) doCommit(ctx context.Context) error { // 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().TxnCtx.IsExplicit && s.GetSessionVars().GuaranteeLinearizability) + sessVars.TxnCtx.IsExplicit && sessVars.GuaranteeLinearizability) + } +<<<<<<< HEAD +======= + if tables := sessVars.TxnCtx.GlobalTemporaryTables; len(tables) > 0 { + s.txn.SetOption(kv.KVFilter, temporaryTableKVFilter(tables)) } +>>>>>>> ddfc0bd8c... *: fix wrong resource tag of transaction commit statement (#25616) - return s.txn.Commit(tikvutil.SetSessionID(ctx, s.GetSessionVars().ConnectionID)) + return s.txn.Commit(tikvutil.SetSessionID(ctx, sessVars.ConnectionID)) } // removeTempTableFromBuffer filters out the temporary table key-values. From 899ccfc4e8518782fd25100f755bb1b6ea1e6a8a Mon Sep 17 00:00:00 2001 From: crazycs Date: Tue, 20 Jul 2021 17:10:12 +0800 Subject: [PATCH 2/2] resolve conflict Signed-off-by: crazycs --- session/session.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/session/session.go b/session/session.go index 31852bb8fd20c..f10e71f5eba27 100644 --- a/session/session.go +++ b/session/session.go @@ -537,12 +537,6 @@ func (s *session) doCommit(ctx context.Context) error { s.txn.SetOption(kv.GuaranteeLinearizability, sessVars.TxnCtx.IsExplicit && sessVars.GuaranteeLinearizability) } -<<<<<<< HEAD -======= - if tables := sessVars.TxnCtx.GlobalTemporaryTables; len(tables) > 0 { - s.txn.SetOption(kv.KVFilter, temporaryTableKVFilter(tables)) - } ->>>>>>> ddfc0bd8c... *: fix wrong resource tag of transaction commit statement (#25616) return s.txn.Commit(tikvutil.SetSessionID(ctx, sessVars.ConnectionID)) }