Skip to content

Commit

Permalink
infoschema: Add ALL_SQL_DIGESTS field to the TIDB_TRX table (#24863)
Browse files Browse the repository at this point in the history
  • Loading branch information
MyonKeminta committed May 27, 2021
1 parent e87d035 commit c3a27c9
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 102 deletions.
8 changes: 4 additions & 4 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8187,14 +8187,14 @@ func (s *testSerialSuite) TestDeadlockTable(c *C) {
TryLockTxn: 101,
SQLDigest: "aabbccdd",
Key: []byte("k1"),
AllSQLs: nil,
AllSQLDigests: nil,
TxnHoldingLock: 102,
},
{
TryLockTxn: 102,
SQLDigest: "ddccbbaa",
Key: []byte("k2"),
AllSQLs: []string{"sql1"},
AllSQLDigests: []string{"sql1"},
TxnHoldingLock: 101,
},
},
Expand All @@ -8208,12 +8208,12 @@ func (s *testSerialSuite) TestDeadlockTable(c *C) {
WaitChain: []deadlockhistory.WaitChainItem{
{
TryLockTxn: 201,
AllSQLs: []string{},
AllSQLDigests: []string{},
TxnHoldingLock: 202,
},
{
TryLockTxn: 202,
AllSQLs: []string{"sql1", "sql2, sql3"},
AllSQLDigests: []string{"sql1", "sql2, sql3"},
TxnHoldingLock: 203,
},
{
Expand Down
1 change: 1 addition & 0 deletions infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ func (*testSuite) TestInfoTables(c *C) {
"COLLATION_CHARACTER_SET_APPLICABILITY",
"PROCESSLIST",
"TIDB_TRX",
"DEADLOCKS",
}
for _, t := range infoTables {
tb, err1 := is.TableByName(util.InformationSchemaName, model.NewCIStr(t))
Expand Down
15 changes: 8 additions & 7 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,26 +1349,27 @@ var tableClientErrorsSummaryByHostCols = []columnInfo{

var tableTiDBTrxCols = []columnInfo{
{name: "ID", tp: mysql.TypeLonglong, size: 21, flag: mysql.PriKeyFlag | mysql.NotNullFlag | mysql.UnsignedFlag},
{name: "START_TIME", tp: mysql.TypeTimestamp, size: 26, comment: "Start time of the transaction"},
{name: "DIGEST", tp: mysql.TypeVarchar, size: 64, comment: "Digest of the sql the transaction are currently running"},
{name: "START_TIME", tp: mysql.TypeTimestamp, decimal: 6, size: 26, comment: "Start time of the transaction"},
{name: "CURRENT_SQL_DIGEST", tp: mysql.TypeVarchar, size: 64, comment: "Digest of the sql the transaction are currently running"},
{name: "STATE", tp: mysql.TypeEnum, enumElems: txninfo.TxnRunningStateStrs, comment: "Current running state of the transaction"},
{name: "WAITING_START_TIME", tp: mysql.TypeTimestamp, size: 26, comment: "Current lock waiting's start time"},
{name: "WAITING_START_TIME", tp: mysql.TypeTimestamp, decimal: 6, size: 26, comment: "Current lock waiting's start time"},
{name: "LEN", tp: mysql.TypeLonglong, size: 64, comment: "How many entries are in MemDB"},
{name: "SIZE", tp: mysql.TypeLonglong, size: 64, comment: "MemDB used memory"},
{name: "SESSION_ID", tp: mysql.TypeLonglong, size: 21, flag: mysql.UnsignedFlag, comment: "Which session this transaction belongs to"},
{name: "USER", tp: mysql.TypeVarchar, size: 16, comment: "The user who open this session"},
{name: "DB", tp: mysql.TypeVarchar, size: 64, comment: "The schema this transaction works on"},
{name: "ALL_SQL_DIGESTS", tp: mysql.TypeBlob, size: types.UnspecifiedLength, comment: "A list of the digests of SQL statements that the transaction has executed"},
}

var tableDeadlocksCols = []columnInfo{
{name: "DEADLOCK_ID", tp: mysql.TypeLonglong, size: 21, flag: mysql.NotNullFlag, comment: "The ID to dinstinguish different deadlock events"},
{name: "DEADLOCK_ID", tp: mysql.TypeLonglong, size: 21, flag: mysql.NotNullFlag, comment: "The ID to distinguish different deadlock events"},
{name: "OCCUR_TIME", tp: mysql.TypeTimestamp, decimal: 6, size: 26, comment: "The physical time when the deadlock occurs"},
{name: "RETRYABLE", tp: mysql.TypeTiny, size: 1, flag: mysql.NotNullFlag, comment: "Whether the deadlock is retryable. Retryable deadlocks are usually not reported to the client"},
{name: "TRY_LOCK_TRX_ID", tp: mysql.TypeLonglong, size: 21, flag: mysql.NotNullFlag, comment: "The transaction ID (start ts) of the transaction that's trying to acquire the lock"},
{name: "TRY_LOCK_TRX_ID", tp: mysql.TypeLonglong, size: 21, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "The transaction ID (start ts) of the transaction that's trying to acquire the lock"},
{name: "CURRENT_SQL_DIGEST", tp: mysql.TypeVarchar, size: 64, comment: "The digest of the SQL that's being blocked"},
{name: "KEY", tp: mysql.TypeBlob, size: types.UnspecifiedLength, comment: "The key on which a transaction is waiting for another"},
{name: "ALL_SQLS", tp: mysql.TypeBlob, size: types.UnspecifiedLength, comment: "A list of the digests of SQL statements that the transaction has executed"},
{name: "TRX_HOLDING_LOCK", tp: mysql.TypeLonglong, size: 21, flag: mysql.NotNullFlag, comment: "The transaction ID (start ts) of the transaction that's currently holding the lock"},
{name: "ALL_SQL_DIGESTS", tp: mysql.TypeBlob, size: types.UnspecifiedLength, comment: "A list of the digests of SQL statements that the transaction has executed"},
{name: "TRX_HOLDING_LOCK", tp: mysql.TypeLonglong, size: 21, flag: mysql.NotNullFlag | mysql.UnsignedFlag, comment: "The transaction ID (start ts) of the transaction that's currently holding the lock"},
}

var tableDataLockWaitsCols = []columnInfo{
Expand Down
20 changes: 16 additions & 4 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"runtime"
"strings"
"time"
"unsafe"

"github.com/gorilla/mux"
. "github.com/pingcap/check"
Expand Down Expand Up @@ -1524,7 +1525,7 @@ func (s *testTableSuite) TestInfoschemaClientErrors(c *C) {
func (s *testTableSuite) TestTrx(c *C) {
tk := s.newTestKitWithRoot(c)
_, digest := parser.NormalizeDigest("select * from trx for update;")
sm := &mockSessionManager{nil, make([]*txninfo.TxnInfo, 1)}
sm := &mockSessionManager{nil, make([]*txninfo.TxnInfo, 2)}
sm.txnInfo[0] = &txninfo.TxnInfo{
StartTS: 424768545227014155,
CurrentSQLDigest: digest.String(),
Expand All @@ -1536,10 +1537,21 @@ func (s *testTableSuite) TestTrx(c *C) {
Username: "root",
CurrentDB: "test",
}
blockTime2 := time.Date(2021, 05, 20, 13, 18, 30, 123456000, time.UTC)
sm.txnInfo[1] = &txninfo.TxnInfo{
StartTS: 425070846483628033,
CurrentSQLDigest: "",
AllSQLDigests: []string{"sql1", "sql2"},
State: txninfo.TxnLockWaiting,
BlockStartTime: unsafe.Pointer(&blockTime2),
ConnectionID: 10,
Username: "user1",
CurrentDB: "db1",
}
tk.Se.SetSessionManager(sm)
tk.MustQuery("select * from information_schema.TIDB_TRX;").Check(
testkit.Rows("424768545227014155 2021-05-07 12:56:48 " + digest.String() + " Normal <nil> 1 19 2 root test"),
)
tk.MustQuery("select * from information_schema.TIDB_TRX;").Check(testkit.Rows(
"424768545227014155 2021-05-07 04:56:48.001000 "+digest.String()+" Normal <nil> 1 19 2 root test []",
"425070846483628033 2021-05-20 13:16:35.778000 <nil> LockWaiting 2021-05-20 13:18:30.123456 0 0 10 user1 db1 [sql1, sql2]"))
}

func (s *testTableSuite) TestInfoschemaDeadlockPrivilege(c *C) {
Expand Down
13 changes: 10 additions & 3 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ func (s *session) TxnInfo() *txninfo.TxnInfo {
return nil
}
processInfo := s.ShowProcess()
txnInfo.CurrentSQLDigest = processInfo.Digest
txnInfo.ConnectionID = processInfo.ID
txnInfo.Username = processInfo.User
txnInfo.CurrentDB = processInfo.DB
Expand Down Expand Up @@ -1502,6 +1501,9 @@ func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex
// Uncorrelated subqueries will execute once when building plan, so we reset process info before building plan.
cmd32 := atomic.LoadUint32(&s.GetSessionVars().CommandValue)
s.SetProcessInfo(stmtNode.Text(), time.Now(), byte(cmd32), 0)
_, digest := s.sessionVars.StmtCtx.SQLDigest()
s.txn.onStmtStart(digest.String())
defer s.txn.onStmtEnd()

// Transform abstract syntax tree to a physical plan(stored in executor.ExecStmt).
compiler := executor.Compiler{Ctx: s}
Expand Down Expand Up @@ -1873,10 +1875,15 @@ func (s *session) ExecutePreparedStmt(ctx context.Context, stmtID uint32, args [
if err != nil {
return nil, err
}
s.txn.onStmtStart(preparedStmt.SQLDigest.String())
var rs sqlexec.RecordSet
if ok {
return s.cachedPlanExec(ctx, stmtID, preparedStmt, args)
rs, err = s.cachedPlanExec(ctx, stmtID, preparedStmt, args)
} else {
rs, err = s.preparedStmtExec(ctx, stmtID, preparedStmt, args)
}
return s.preparedStmtExec(ctx, stmtID, preparedStmt, args)
s.txn.onStmtEnd()
return rs, err
}

func (s *session) DropPreparedStmt(stmtID uint32) error {
Expand Down
Loading

0 comments on commit c3a27c9

Please sign in to comment.