Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

topsql: make topsql enable only be controlled by pub/sub sink #31209

Merged
merged 15 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 0 additions & 2 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1539,10 +1539,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
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()
}