Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#50075
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
nolouch authored and ti-chi-bot committed Feb 18, 2024
1 parent e456f72 commit 5227a25
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pkg/executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,41 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, err error, hasMoreResults boo
}
}

<<<<<<< HEAD
=======
func (a *ExecStmt) recordLastQueryInfo(err error) {
sessVars := a.Ctx.GetSessionVars()
// Record diagnostic information for DML statements
recordLastQuery := false
switch typ := a.StmtNode.(type) {
case *ast.ShowStmt:
recordLastQuery = typ.Tp != ast.ShowSessionStates
case *ast.ExecuteStmt, ast.DMLNode:
recordLastQuery = true
}
if recordLastQuery {
var lastRUConsumption float64
if ruDetailRaw := a.GoCtx.Value(util.RUDetailsCtxKey); ruDetailRaw != nil {
ruDetail := ruDetailRaw.(*util.RUDetails)
lastRUConsumption = ruDetail.RRU() + ruDetail.WRU()
}
failpoint.Inject("mockRUConsumption", func(_ failpoint.Value) {
lastRUConsumption = float64(len(sessVars.StmtCtx.OriginalSQL))
})
// Keep the previous queryInfo for `show session_states` because the statement needs to encode it.
sessVars.LastQueryInfo = sessionstates.QueryInfo{
TxnScope: sessVars.CheckAndGetTxnScope(),
StartTS: sessVars.TxnCtx.StartTS,
ForUpdateTS: sessVars.TxnCtx.GetForUpdateTS(),
RUConsumption: lastRUConsumption,
}
if err != nil {
sessVars.LastQueryInfo.ErrMsg = err.Error()
}
}
}

>>>>>>> 7a8d82eddb2 (*: remove duplicate prefix for last query info (#50075))
func (a *ExecStmt) checkPlanReplayerCapture(txnTS uint64) {
if kv.GetInternalSourceType(a.GoCtx) == kv.InternalTxnStats {
return
Expand Down
14 changes: 14 additions & 0 deletions pkg/session/test/variable/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ func TestIndexMergeRuntimeStats(t *testing.T) {

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
<<<<<<< HEAD
tk.MustExec("set @@tidb_enable_index_merge = 1")
tk.MustExec("create table t1(id int primary key, a int, b int, c int, d int)")
tk.MustExec("create index t1a on t1(a)")
Expand All @@ -385,4 +386,17 @@ func TestIndexMergeRuntimeStats(t *testing.T) {
require.Regexp(t, ".*time:.*loops:.*cop_task:.*", tableExplain)
tk.MustExec("set @@tidb_enable_collect_execution_info=0;")
tk.MustQuery("select /*+ use_index_merge(t1, primary, t1a) */ * from t1 where id < 2 or a > 4 order by a").Check(testkit.Rows("1 1 1 1 1", "5 5 5 5 5"))
=======
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx(a))")
tk.MustExec(`prepare stmt1 from 'select * from t'`)
tk.MustExec("execute stmt1")
checkMatch := func(actual []string, expected []interface{}) bool {
return strings.Contains(actual[0], expected[0].(string))
}
tk.MustQuery("select @@tidb_last_query_info;").CheckWithFunc(testkit.Rows(`"ru_consumption":15`), checkMatch)
tk.MustExec("select a from t where a = 1")
tk.MustQuery("select @@tidb_last_query_info;").CheckWithFunc(testkit.Rows(`"ru_consumption":27`), checkMatch)
tk.MustQuery("select @@tidb_last_query_info;").CheckWithFunc(testkit.Rows(`"ru_consumption":30`), checkMatch)
>>>>>>> 7a8d82eddb2 (*: remove duplicate prefix for last query info (#50075))
}
8 changes: 8 additions & 0 deletions pkg/sessionctx/sessionstates/session_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ type PreparedStmtInfo struct {

// QueryInfo represents the information of last executed query. It's used to expose information for test purpose.
type QueryInfo struct {
<<<<<<< HEAD
TxnScope string `json:"txn_scope"`
StartTS uint64 `json:"start_ts"`
ForUpdateTS uint64 `json:"for_update_ts"`
ErrMsg string `json:"error,omitempty"`
=======
TxnScope string `json:"txn_scope"`
StartTS uint64 `json:"start_ts"`
ForUpdateTS uint64 `json:"for_update_ts"`
RUConsumption float64 `json:"ru_consumption"`
ErrMsg string `json:"error,omitempty"`
>>>>>>> 7a8d82eddb2 (*: remove duplicate prefix for last query info (#50075))
}

// LastDDLInfo represents the information of last DDL. It's used to expose information for test purpose.
Expand Down

0 comments on commit 5227a25

Please sign in to comment.