Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#49769
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 Apr 18, 2024
1 parent 4c3389e commit 19055f2
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 22 deletions.
79 changes: 78 additions & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
<<<<<<< HEAD:executor/adapter.go
"github.com/pingcap/tidb/bindinfo"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl/placement"
Expand Down Expand Up @@ -69,6 +70,50 @@ import (
"github.com/pingcap/tidb/util/topsql"
topsqlstate "github.com/pingcap/tidb/util/topsql/state"
"github.com/pingcap/tidb/util/tracing"
=======
"github.com/pingcap/tidb/pkg/bindinfo"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/ddl/placement"
"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/executor/internal/exec"
executor_metrics "github.com/pingcap/tidb/pkg/executor/metrics"
"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/infoschema"
"github.com/pingcap/tidb/pkg/keyspace"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/parser/terror"
"github.com/pingcap/tidb/pkg/planner"
plannercore "github.com/pingcap/tidb/pkg/planner/core"
"github.com/pingcap/tidb/pkg/plugin"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/sessionctx/sessionstates"
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/sessiontxn"
"github.com/pingcap/tidb/pkg/sessiontxn/staleread"
"github.com/pingcap/tidb/pkg/types"
util2 "github.com/pingcap/tidb/pkg/util"
"github.com/pingcap/tidb/pkg/util/breakpoint"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/dbterror/exeerrors"
"github.com/pingcap/tidb/pkg/util/execdetails"
"github.com/pingcap/tidb/pkg/util/hint"
"github.com/pingcap/tidb/pkg/util/logutil"
"github.com/pingcap/tidb/pkg/util/plancodec"
"github.com/pingcap/tidb/pkg/util/replayer"
"github.com/pingcap/tidb/pkg/util/sqlexec"
"github.com/pingcap/tidb/pkg/util/stmtsummary"
stmtsummaryv2 "github.com/pingcap/tidb/pkg/util/stmtsummary/v2"
"github.com/pingcap/tidb/pkg/util/stringutil"
"github.com/pingcap/tidb/pkg/util/topsql"
topsqlstate "github.com/pingcap/tidb/pkg/util/topsql/state"
"github.com/pingcap/tidb/pkg/util/tracing"
>>>>>>> 33480e8c8d8 (*: add last ru consumption for tidb_last_query_info (#49769)):pkg/executor/adapter.go
"github.com/prometheus/client_golang/prometheus"
tikverr "github.com/tikv/client-go/v2/error"
"github.com/tikv/client-go/v2/oracle"
Expand Down Expand Up @@ -1361,7 +1406,7 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, err error, hasMoreResults boo
}
}
sessVars.PrevStmt = FormatSQL(a.GetTextToLog(false))

a.recordLastQueryInfo(err)
a.observePhaseDurations(sessVars.InRestrictedSQL, execDetail.CommitDetail)
executeDuration := time.Since(sessVars.StartTime) - sessVars.DurationCompile
if sessVars.InRestrictedSQL {
Expand Down Expand Up @@ -1405,6 +1450,38 @@ func (a *ExecStmt) FinishExecuteStmt(txnTS uint64, err error, hasMoreResults boo
}
}

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(),
LastRUConsumption: lastRUConsumption,
}
if err != nil {
sessVars.LastQueryInfo.ErrMsg = err.Error()
}
}
}

func (a *ExecStmt) checkPlanReplayerCapture(txnTS uint64) {
if kv.GetInternalSourceType(a.GoCtx) == kv.InternalTxnStats {
return
Expand Down
28 changes: 28 additions & 0 deletions pkg/session/test/variable/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "variable_test",
timeout = "short",
srcs = [
"main_test.go",
"variable_test.go",
],
flaky = True,
shard_count = 10,
deps = [
"//pkg/config",
"//pkg/kv",
"//pkg/session",
"//pkg/sessionctx/variable",
"//pkg/store/copr",
"//pkg/testkit",
"//pkg/testkit/testmain",
"//pkg/testkit/testsetup",
"//pkg/util/dbterror/exeerrors",
"//pkg/util/memory",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//tikv",
"@org_uber_go_goleak//:goleak",
],
)
Loading

0 comments on commit 19055f2

Please sign in to comment.