Skip to content

Commit

Permalink
topsql: make topsql enable only be controlled by pub/sub sink (pingca…
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Dec 31, 2021
1 parent ec4f879 commit 974b578
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 23 deletions.
2 changes: 0 additions & 2 deletions domain/sysvar_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ func (do *Domain) checkEnableServerGlobalVar(name, sVal string) {
err = stmtsummary.StmtSummaryByDigestMap.SetMaxSQLLength(sVal, false)
case variable.TiDBCapturePlanBaseline:
variable.CapturePlanBaseline.Set(sVal, false)
case variable.TiDBEnableTopSQL:
topsqlstate.GlobalState.Enable.Store(variable.TiDBOptOn(sVal))
case variable.TiDBTopSQLPrecisionSeconds:
var val int64
val, err = strconv.ParseInt(sVal, 10, 64)
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8765,7 +8765,7 @@ func (s *testResourceTagSuite) TestResourceGroupTag(c *C) {
tbInfo := testGetTableByName(c, tk.Se, "test", "t")

// Enable Top SQL
topsqlstate.GlobalState.Enable.Store(true)
topsqlstate.EnableTopSQL()
config.UpdateGlobal(func(conf *config.Config) {
conf.TopSQL.ReceiverAddress = "mock-agent"
})
Expand Down
8 changes: 3 additions & 5 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1541,10 +1541,8 @@ func (s *testSerialSuite) TestSetTopSQLVariables(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@global.tidb_enable_top_sql='On';")
tk.MustQuery("select @@global.tidb_enable_top_sql;").Check(testkit.Rows("1"))
c.Assert(topsqlstate.GlobalState.Enable.Load(), IsTrue)
tk.MustExec("set @@global.tidb_enable_top_sql='off';")
tk.MustQuery("select @@global.tidb_enable_top_sql;").Check(testkit.Rows("0"))
c.Assert(topsqlstate.GlobalState.Enable.Load(), IsFalse)

tk.MustExec("set @@global.tidb_top_sql_precision_seconds=2;")
tk.MustQuery("select @@global.tidb_top_sql_precision_seconds;").Check(testkit.Rows("2"))
Expand Down Expand Up @@ -1602,7 +1600,7 @@ func (s *testSerialSuite) TestSetTopSQLVariables(c *C) {
tk.MustQuery("select @@global.tidb_top_sql_report_interval_seconds;").Check(testkit.Rows("120"))
c.Assert(topsqlstate.GlobalState.ReportIntervalSeconds.Load(), Equals, int64(120))

// Test for hide top sql variable in show variable.
tk.MustQuery("show variables like '%top_sql%'").Check(testkit.Rows())
tk.MustQuery("show global variables like '%top_sql%'").Check(testkit.Rows())
// Test for hide top sql variable except 'tidb_enable_top_sql' in show variable.
tk.MustQuery("show variables like '%top_sql%'").Check(testkit.Rows("tidb_enable_top_sql OFF"))
tk.MustQuery("show global variables like '%top_sql%'").Check(testkit.Rows("tidb_enable_top_sql OFF"))
}
4 changes: 1 addition & 3 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,6 @@ func TestTopSQLCPUProfile(t *testing.T) {
dbt.MustExec("create table t (a int auto_increment, b int, unique index idx(a));")
dbt.MustExec("create table t1 (a int auto_increment, b int, unique index idx(a));")
dbt.MustExec("create table t2 (a int auto_increment, b int, unique index idx(a));")
dbt.MustExec("set @@global.tidb_enable_top_sql='On';")
config.UpdateGlobal(func(conf *config.Config) {
conf.TopSQL.ReceiverAddress = "127.0.0.1:4001"
})
Expand Down Expand Up @@ -1544,11 +1543,10 @@ func TestTopSQLStatementStats(t *testing.T) {
dbt.MustExec("create table t3 (a int, b int, unique index idx(a));")

// Enable TopSQL
topsqlstate.GlobalState.Enable.Store(true)
topsqlstate.EnableTopSQL()
config.UpdateGlobal(func(conf *config.Config) {
conf.TopSQL.ReceiverAddress = "mock-agent"
})
dbt.MustExec("set @@global.tidb_enable_top_sql='On';")

const ExecCountPerSQL = 3

Expand Down
9 changes: 3 additions & 6 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1246,12 +1246,9 @@ var defaultSysVars = []*SysVar{
return nil
}},
// variable for top SQL feature.
{Scope: ScopeGlobal, Name: TiDBEnableTopSQL, Value: BoolToOnOff(topsqlstate.DefTiDBTopSQLEnable), Type: TypeBool, Hidden: true, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) {
return BoolToOnOff(topsqlstate.GlobalState.Enable.Load()), nil
}, SetGlobal: func(vars *SessionVars, s string) error {
topsqlstate.GlobalState.Enable.Store(TiDBOptOn(s))
return nil
}, GlobalConfigName: GlobalConfigEnableTopSQL},
// TopSQL enable only be controlled by TopSQL pub/sub sinker.
// This global variable only uses to update the global config which store in PD(ETCD).
{Scope: ScopeGlobal, Name: TiDBEnableTopSQL, Value: BoolToOnOff(topsqlstate.DefTiDBTopSQLEnable), Type: TypeBool, AllowEmpty: true, GlobalConfigName: GlobalConfigEnableTopSQL},
{Scope: ScopeGlobal, Name: TiDBTopSQLPrecisionSeconds, Value: strconv.Itoa(topsqlstate.DefTiDBTopSQLPrecisionSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: math.MaxInt64, GetGlobal: func(s *SessionVars) (string, error) {
return strconv.FormatInt(topsqlstate.GlobalState.PrecisionSeconds.Load(), 10), nil
}, SetGlobal: func(vars *SessionVars, s string) error {
Expand Down
12 changes: 6 additions & 6 deletions util/topsql/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (

// GlobalState is the global Top-SQL state.
var GlobalState = State{
Enable: atomic.NewBool(DefTiDBTopSQLEnable),
enable: atomic.NewBool(false),
PrecisionSeconds: atomic.NewInt64(DefTiDBTopSQLPrecisionSeconds),
MaxStatementCount: atomic.NewInt64(DefTiDBTopSQLMaxStatementCount),
MaxCollect: atomic.NewInt64(DefTiDBTopSQLMaxCollect),
Expand All @@ -36,8 +36,8 @@ var GlobalState = State{

// State is the state for control top sql feature.
type State struct {
// Enable top-sql or not.
Enable *atomic.Bool
// enable top-sql or not.
enable *atomic.Bool
// The refresh interval of top-sql.
PrecisionSeconds *atomic.Int64
// The maximum number of statements kept in memory.
Expand All @@ -50,15 +50,15 @@ type State struct {

// EnableTopSQL enables the top SQL feature.
func EnableTopSQL() {
GlobalState.Enable.Store(true)
GlobalState.enable.Store(true)
}

// DisableTopSQL disables the top SQL feature.
func DisableTopSQL() {
GlobalState.Enable.Store(false)
GlobalState.enable.Store(false)
}

// TopSQLEnabled uses to check whether enabled the top SQL feature.
func TopSQLEnabled() bool {
return GlobalState.Enable.Load()
return GlobalState.enable.Load()
}

0 comments on commit 974b578

Please sign in to comment.