Skip to content

Commit

Permalink
executor: bootstrap SQLs are not treated as internal queries in state…
Browse files Browse the repository at this point in the history
…ment summary (#17024) (#17062)
  • Loading branch information
sre-bot authored May 9, 2020
1 parent 75b3d09 commit df4efa3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
11 changes: 6 additions & 5 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,13 @@ func getPlanDigest(sctx sessionctx.Context, p plannercore.Plan) (normalized, pla
// SummaryStmt collects statements for information_schema.statements_summary
func (a *ExecStmt) SummaryStmt(succ bool) {
sessVars := a.Ctx.GetSessionVars()
var userString string
if sessVars.User != nil {
userString = sessVars.User.Username
}

// Internal SQLs must also be recorded to keep the consistency of `PrevStmt` and `PrevStmtDigest`.
if !stmtsummary.StmtSummaryByDigestMap.Enabled() || (sessVars.InRestrictedSQL && !stmtsummary.StmtSummaryByDigestMap.EnabledInternal()) {
if !stmtsummary.StmtSummaryByDigestMap.Enabled() || ((sessVars.InRestrictedSQL || len(userString) == 0) && !stmtsummary.StmtSummaryByDigestMap.EnabledInternal()) {
sessVars.SetPrevStmtDigest("")
return
}
Expand Down Expand Up @@ -924,10 +929,6 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
execDetail := stmtCtx.GetExecDetails()
copTaskInfo := stmtCtx.CopTasksDetails()
memMax := stmtCtx.MemTracker.MaxConsumed()
var userString string
if sessVars.User != nil {
userString = sessVars.User.Username
}

stmtsummary.StmtSummaryByDigestMap.AddStatement(&stmtsummary.StmtExecInfo{
SchemaName: strings.ToLower(sessVars.CurrentDB),
Expand Down
31 changes: 18 additions & 13 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func (s *testTableSuiteBase) TearDownSuite(c *C) {
testleak.AfterTest(c)()
}

func (s *testTableSuiteBase) newTestKitWithRoot(c *C) *testkit.TestKit {
tk := testkit.NewTestKitWithInit(c, s.store)
c.Assert(tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil), IsTrue)
return tk
}

type testClusterTableSuite struct {
*testTableSuiteBase
rpcserver *grpc.Server
Expand Down Expand Up @@ -745,7 +751,7 @@ func (s *testTableSuite) checkSystemSchemaTableID(c *C, dbName string, dbID, sta
}

func (s *testClusterTableSuite) TestSelectClusterTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk := s.newTestKitWithRoot(c)
slowLogFileName := "tidb-slow.log"
prepareSlowLogfile(c, slowLogFileName)
defer os.Remove(slowLogFileName)
Expand Down Expand Up @@ -855,7 +861,7 @@ func (s *testTableSuite) TestSelectHiddenColumn(c *C) {

// Test statements_summary.
func (s *testTableSuite) TestStmtSummaryTable(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk := s.newTestKitWithRoot(c)

tk.MustQuery("select column_comment from information_schema.columns " +
"where table_name='STATEMENTS_SUMMARY' and column_name='STMT_TYPE'",
Expand All @@ -878,7 +884,7 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {
tk.MustQuery("select @@global.tidb_stmt_summary_refresh_interval").Check(testkit.Rows("999999999"))

// Create a new session to test.
tk = testkit.NewTestKitWithInit(c, s.store)
tk = s.newTestKitWithRoot(c)

// Test INSERT
tk.MustExec("insert into t values(1, 'a')")
Expand Down Expand Up @@ -961,7 +967,7 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {
tk.MustQuery("select @@global.tidb_enable_stmt_summary").Check(testkit.Rows("0"))

// Create a new session to test
tk = testkit.NewTestKitWithInit(c, s.store)
tk = s.newTestKitWithRoot(c)

// This statement shouldn't be summarized.
tk.MustQuery("select * from t where a=2")
Expand Down Expand Up @@ -1006,7 +1012,7 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {
tk.MustExec("set global tidb_enable_stmt_summary = false")

// Create a new session to test.
tk = testkit.NewTestKitWithInit(c, s.store)
tk = s.newTestKitWithRoot(c)

tk.MustQuery("select * from t where a=2")

Expand All @@ -1032,7 +1038,7 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {
).Check(testkit.Rows())

// Create a new session to test
tk = testkit.NewTestKitWithInit(c, s.store)
tk = s.newTestKitWithRoot(c)

tk.MustExec("set global tidb_enable_stmt_summary = on")
tk.MustExec("set global tidb_stmt_summary_history_size = 24")
Expand Down Expand Up @@ -1098,8 +1104,7 @@ func (s *testTableSuite) TestStmtSummaryTable(c *C) {

// Test statements_summary_history.
func (s *testTableSuite) TestStmtSummaryHistoryTable(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)

tk := s.newTestKitWithRoot(c)
tk.MustExec("drop table if exists test_summary")
tk.MustExec("create table test_summary(a int, b varchar(10), key k(a))")

Expand All @@ -1113,7 +1118,7 @@ func (s *testTableSuite) TestStmtSummaryHistoryTable(c *C) {
tk.MustQuery("select @@global.tidb_stmt_summary_refresh_interval").Check(testkit.Rows("999999999"))

// Create a new session to test.
tk = testkit.NewTestKitWithInit(c, s.store)
tk = s.newTestKitWithRoot(c)

// Test INSERT
tk.MustExec("insert into test_summary values(1, 'a')")
Expand All @@ -1137,7 +1142,7 @@ func (s *testTableSuite) TestStmtSummaryHistoryTable(c *C) {

// Test statements_summary_history.
func (s *testTableSuite) TestStmtSummaryInternalQuery(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk := s.newTestKitWithRoot(c)

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b varchar(10), key k(a))")
Expand All @@ -1157,7 +1162,7 @@ func (s *testTableSuite) TestStmtSummaryInternalQuery(c *C) {
// Test Internal

// Create a new session to test.
tk = testkit.NewTestKitWithInit(c, s.store)
tk = s.newTestKitWithRoot(c)

tk.MustExec("select * from t where t.a = 1")
tk.MustQuery(`select exec_count, digest_text
Expand All @@ -1169,7 +1174,7 @@ func (s *testTableSuite) TestStmtSummaryInternalQuery(c *C) {
defer tk.MustExec("set global tidb_stmt_summary_internal_query = false")

// Create a new session to test.
tk = testkit.NewTestKitWithInit(c, s.store)
tk = s.newTestKitWithRoot(c)

tk.MustExec("admin flush bindings")
tk.MustExec("admin evolve bindings")
Expand All @@ -1183,7 +1188,7 @@ func (s *testTableSuite) TestStmtSummaryInternalQuery(c *C) {

// Test error count and warning count.
func (s *testTableSuite) TestStmtSummaryErrorCount(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk := s.newTestKitWithRoot(c)

// Clear summaries.
tk.MustExec("set global tidb_enable_stmt_summary = 0")
Expand Down

0 comments on commit df4efa3

Please sign in to comment.