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: introduce pub/sub mode #29020

Closed
wants to merge 60 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
b464ff4
register pubusb
zhongzc Oct 20, 2021
ab68337
run fine
zhongzc Oct 20, 2021
8d6b03e
globally enabled not to affect instance level enabled directly
zhongzc Oct 21, 2021
38fccf9
tiny fix
zhongzc Oct 21, 2021
6d4a115
add license
zhongzc Oct 21, 2021
91d7143
add tests & add comment
zhongzc Oct 25, 2021
8f2eae6
fix tests
zhongzc Oct 25, 2021
b75478e
fix tests
zhongzc Oct 25, 2021
30201ec
fix
zhongzc Oct 25, 2021
9c3f723
update tipb
zhongzc Oct 26, 2021
ba167c3
Merge remote-tracking branch 'origin/master' into pubsub
zhongzc Oct 26, 2021
a6aa4f8
fix test
zhongzc Oct 26, 2021
27b47f7
Merge branch 'master' into pubsub
zhongzc Oct 26, 2021
f6537e2
add metrics
zhongzc Oct 26, 2021
24fb6ca
remove topsql var case
zhongzc Oct 27, 2021
c3784e7
Merge remote-tracking branch 'origin/master' into pubsub
zhongzc Oct 27, 2021
5abcea5
add profile metrics
zhongzc Oct 27, 2021
373197d
add profile metrics
zhongzc Oct 27, 2021
cb2fce3
fix unsubscribe
zhongzc Oct 27, 2021
1b076d9
fix timeout
zhongzc Nov 16, 2021
2437176
address comment
zhongzc Dec 1, 2021
2fdec8d
Merge remote-tracking branch 'origin/master' into pubsub
zhongzc Dec 1, 2021
433c1f3
tiny refactor
zhongzc Dec 6, 2021
b2afed8
address comment
zhongzc Dec 6, 2021
6610be4
Merge remote-tracking branch 'origin/master' into pubsub
zhongzc Dec 6, 2021
4874b81
address comment
zhongzc Dec 6, 2021
09a0f33
fix global var test
zhongzc Dec 6, 2021
cee41fa
address comments
zhongzc Dec 6, 2021
b1bb877
add keepalive
zhongzc Dec 6, 2021
13890a5
add more grpc configs
zhongzc Dec 9, 2021
1c58afd
client -> datasink
zhongzc Dec 9, 2021
2ed9c6e
share tipb report data construction
zhongzc Dec 9, 2021
2de6779
Merge branch 'master' into pubsub
zhongzc Dec 9, 2021
c3e92da
polish
zhongzc Dec 9, 2021
0d50283
remove the global service & timeout -> deadline
zhongzc Dec 9, 2021
3eb4cfd
Merge branch 'master' into pubsub
zhongzc Dec 9, 2021
bca0e17
remove session var
zhongzc Dec 9, 2021
5f09f4b
fix
zhongzc Dec 9, 2021
2909a57
fix
zhongzc Dec 9, 2021
c53a043
comment DataSink
zhongzc Dec 10, 2021
a3cf4d6
not to close receiver
zhongzc Dec 10, 2021
26c5812
split handleDataSinkRegistration
zhongzc Dec 10, 2021
a7474d7
fix leak goroutine in test
zhongzc Dec 10, 2021
2a64d59
Merge branch 'master' into pubsub
zhongzc Dec 10, 2021
9decb9d
Merge branch 'master' into pubsub
zhongzc Dec 10, 2021
3628b3e
fix imports
zhongzc Dec 10, 2021
991e309
Merge branch 'master' into pubsub
zhongzc Dec 10, 2021
1ecea77
enabled
zhongzc Dec 10, 2021
3b72c97
fix unbuffered
zhongzc Dec 10, 2021
df42ffe
fix InstanceEnabled
zhongzc Dec 10, 2021
7b813ff
Merge branch 'master' into pubsub
zhongzc Dec 10, 2021
2a7111f
fix init NewRemoteTopSQLReporter
zhongzc Dec 10, 2021
127c72e
use `globalTopSQLReport` to judge topsql enabled
zhongzc Dec 10, 2021
95c7d27
Merge branch 'master' into pubsub
zhongzc Dec 10, 2021
947ae62
Merge branch 'master' into pubsub
zhongzc Dec 11, 2021
04e6a5a
remove redundant `EnablePProfSQLCPU`
zhongzc Dec 13, 2021
0ce9795
Update util/topsql/reporter/reporter.go
zhongzc Dec 13, 2021
cc4cef3
refactor
zhongzc Dec 13, 2021
c8a6ea5
move tracecpu out of topsql
zhongzc Dec 13, 2021
54c34e0
polish
zhongzc Dec 13, 2021
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: 1 addition & 1 deletion ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func newMetaWithQueueTp(txn kv.Transaction, tp workerType) *meta.Meta {
}

func (w *worker) setDDLLabelForTopSQL(job *model.Job) {
if !variable.TopSQLEnabled() || job == nil {
if !variable.TopSQLInstanceEnabled() || job == nil {
return
}

Expand Down
2 changes: 1 addition & 1 deletion distsql/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (builder *RequestBuilder) SetFromInfoSchema(pis interface{}) *RequestBuilde

// SetResourceGroupTag sets the request resource group tag.
func (builder *RequestBuilder) SetResourceGroupTag(sc *stmtctx.StatementContext) *RequestBuilder {
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
builder.Request.ResourceGroupTag = sc.GetResourceGroupTag()
}
return builder
Expand Down
2 changes: 1 addition & 1 deletion domain/sysvar_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func checkEnableServerGlobalVar(name, sVal string) {
case variable.TiDBCapturePlanBaseline:
variable.CapturePlanBaseline.Set(sVal, false)
case variable.TiDBEnableTopSQL:
variable.TopSQLVariable.Enable.Store(variable.TiDBOptOn(sVal))
// TODO: whether the topsql global variable is enabled or not doesn't affect instance enabled
zhongzc marked this conversation as resolved.
Show resolved Hide resolved
case variable.TiDBTopSQLPrecisionSeconds:
var val int64
val, err = strconv.ParseInt(sVal, 10, 64)
Expand Down
2 changes: 1 addition & 1 deletion executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (a *ExecStmt) RebuildPlan(ctx context.Context) (int64, error) {
}

func (a *ExecStmt) setPlanLabelForTopSQL(ctx context.Context) context.Context {
if a.Plan == nil || !variable.TopSQLEnabled() {
if a.Plan == nil || !variable.TopSQLInstanceEnabled() {
return ctx
}
vars := a.Ctx.GetSessionVars()
Expand Down
6 changes: 3 additions & 3 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func (e *SelectLockExec) Next(ctx context.Context, req *chunk.Chunk) error {
func newLockCtx(seVars *variable.SessionVars, lockWaitTime int64) *tikvstore.LockCtx {
var planDigest *parser.Digest
_, sqlDigest := seVars.StmtCtx.SQLDigest()
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
_, planDigest = seVars.StmtCtx.GetPlanDigest()
}
lockCtx := tikvstore.NewLockCtx(seVars.TxnCtx.GetForUpdateTS(), lockWaitTime, seVars.StmtCtx.GetLockWaitStartTime())
Expand Down Expand Up @@ -1715,7 +1715,7 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
goCtx = pprof.WithLabels(goCtx, pprof.Labels("sql", util.QueryStrForLog(prepareStmt.NormalizedSQL)))
pprof.SetGoroutineLabels(goCtx)
}
if variable.TopSQLEnabled() && prepareStmt.SQLDigest != nil {
if variable.TopSQLInstanceEnabled() && prepareStmt.SQLDigest != nil {
topsql.AttachSQLInfo(goCtx, prepareStmt.NormalizedSQL, prepareStmt.SQLDigest, "", nil, vars.InRestrictedSQL)
}
}
Expand Down Expand Up @@ -1895,7 +1895,7 @@ func FillVirtualColumnValue(virtualRetTypes []*types.FieldType, virtualColumnInd
}

func setResourceGroupTagForTxn(sc *stmtctx.StatementContext, snapshot kv.Snapshot) {
if snapshot != nil && variable.TopSQLEnabled() {
if snapshot != nil && variable.TopSQLInstanceEnabled() {
snapshot.SetOption(kv.ResourceGroupTag, sc.GetResourceGroupTag())
}
}
7 changes: 2 additions & 5 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8653,11 +8653,8 @@ func (s *testResourceTagSuite) TestResourceGroupTag(c *C) {
tk.MustExec("create table t(a int, b int, unique index idx(a));")
tbInfo := testGetTableByName(c, tk.Se, "test", "t")

// Enable Top SQL
variable.TopSQLVariable.Enable.Store(true)
config.UpdateGlobal(func(conf *config.Config) {
conf.TopSQL.ReceiverAddress = "mock-agent"
})
variable.TopSQLVariable.InstanceEnable.Store(true)
defer variable.TopSQLVariable.InstanceEnable.Store(false)

c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/unistoreRPCClientSendHook", `return(true)`), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/store/mockstore/unistore/unistoreRPCClientSendHook")
Expand Down
2 changes: 1 addition & 1 deletion executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (e *PrepareExec) Next(ctx context.Context, req *chunk.Chunk) error {
SchemaVersion: ret.InfoSchema.SchemaMetaVersion(),
}
normalizedSQL, digest := parser.NormalizeDigest(prepared.Stmt.Text())
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
ctx = topsql.AttachSQLInfo(ctx, normalizedSQL, digest, "", nil, vars.InRestrictedSQL)
}

Expand Down
6 changes: 0 additions & 6 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1441,12 +1441,6 @@ 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(variable.TopSQLVariable.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(variable.TopSQLVariable.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
2 changes: 1 addition & 1 deletion executor/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (e *UpdateExec) updateRows(ctx context.Context) (int, error) {
txn.GetSnapshot().SetOption(kv.CollectRuntimeStats, e.stats.SnapshotRuntimeStats)
}
}
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
txn, err := e.ctx.Txn(true)
if err == nil {
txn.SetOption(kv.ResourceGroupTag, e.ctx.GetSessionVars().StmtCtx.GetResourceGroupTag())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
github.com/pingcap/sysutil v0.0.0-20210730114356-fcd8a63f68c5
github.com/pingcap/tidb-tools v5.2.2-0.20211019062242-37a8bef2fa17+incompatible
github.com/pingcap/tidb/parser v0.0.0-20211011031125-9b13dc409c5e
github.com/pingcap/tipb v0.0.0-20211008080435-3fd327dfce0e
github.com/pingcap/tipb v0.0.0-20211026032243-186050f116ed
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@ github.com/pingcap/tidb-dashboard v0.0.0-20210312062513-eef5d6404638/go.mod h1:O
github.com/pingcap/tidb-dashboard v0.0.0-20210716172320-2226872e3296/go.mod h1:OCXbZTBTIMRcIt0jFsuCakZP+goYRv6IjawKbwLS2TQ=
github.com/pingcap/tidb-tools v5.2.2-0.20211019062242-37a8bef2fa17+incompatible h1:c7+izmker91NkjkZ6FgTlmD4k1A5FLOAq+li6Ki2/GY=
github.com/pingcap/tidb-tools v5.2.2-0.20211019062242-37a8bef2fa17+incompatible/go.mod h1:XGdcy9+yqlDSEMTpOXnwf3hiTeqrV6MN/u1se9N8yIM=
github.com/pingcap/tipb v0.0.0-20211008080435-3fd327dfce0e h1:fZY5T65QWiPc9noQJ1UkdwejZyBZjNfxzSyTcBjKrEU=
github.com/pingcap/tipb v0.0.0-20211008080435-3fd327dfce0e/go.mod h1:A7mrd7WHBl1o63LE2bIBGEJMTNWXqhgmYiOvMLxozfs=
github.com/pingcap/tipb v0.0.0-20211026032243-186050f116ed h1:46eHok6tYe87/+RQCWRzbCnQFlL65K/Nv2Z98LhMl5Y=
github.com/pingcap/tipb v0.0.0-20211026032243-186050f116ed/go.mod h1:A7mrd7WHBl1o63LE2bIBGEJMTNWXqhgmYiOvMLxozfs=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
2 changes: 1 addition & 1 deletion server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ func (cc *clientConn) dispatch(ctx context.Context, data []byte) error {
cc.lastPacket = data
cmd := data[0]
data = data[1:]
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
defer pprof.SetGoroutineLabels(ctx)
}
if variable.EnablePProfSQLCPU.Load() {
Expand Down
4 changes: 2 additions & 2 deletions server/conn_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (cc *clientConn) handleStmtExecute(ctx context.Context, data []byte) (err e
stmtID := binary.LittleEndian.Uint32(data[0:4])
pos += 4

if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
preparedStmt, _ := cc.preparedStmtID2CachePreparedStmt(stmtID)
if preparedStmt != nil && preparedStmt.SQLDigest != nil {
ctx = topsql.AttachSQLInfo(ctx, preparedStmt.NormalizedSQL, preparedStmt.SQLDigest, "", nil, false)
Expand Down Expand Up @@ -273,7 +273,7 @@ func (cc *clientConn) handleStmtFetch(ctx context.Context, data []byte) (err err
return errors.Annotate(mysql.NewErr(mysql.ErrUnknownStmtHandler,
strconv.FormatUint(uint64(stmtID), 10), "stmt_fetch"), cc.preparedStmt2String(stmtID))
}
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
prepareObj, _ := cc.preparedStmtID2CachePreparedStmt(stmtID)
if prepareObj != nil && prepareObj.SQLDigest != nil {
ctx = topsql.AttachSQLInfo(ctx, prepareObj.NormalizedSQL, prepareObj.SQLDigest, "", nil, false)
Expand Down
2 changes: 2 additions & 0 deletions server/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/topsql"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/peer"
Expand All @@ -54,6 +55,7 @@ func NewRPCServer(config *config.Config, dom *domain.Domain, sm util.SessionMana
}
diagnosticspb.RegisterDiagnosticsServer(s, rpcSrv)
tikvpb.RegisterTikvServer(s, rpcSrv)
topsql.RegisterPubSubServer(s)
return s
}

Expand Down
54 changes: 27 additions & 27 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,8 @@ func (ts *tidbTestTopSQLSuite) TestTopSQLCPUProfile(c *C) {
c.Assert(err, IsNil)
}()

variable.TopSQLVariable.InstanceEnable.Store(true)
defer variable.TopSQLVariable.InstanceEnable.Store(false)
collector := mockTopSQLTraceCPU.NewTopSQLCollector()
tracecpu.GlobalSQLCPUProfiler.SetCollector(&collectorWrapper{collector})

Expand All @@ -1641,7 +1643,6 @@ func (ts *tidbTestTopSQLSuite) TestTopSQLCPUProfile(c *C) {
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 @@ -1837,18 +1838,18 @@ func (ts *tidbTestTopSQLSuite) TestTopSQLCPUProfile(c *C) {
checkFn("commit", "")
}

func (ts *tidbTestTopSQLSuite) TestTopSQLAgent(c *C) {
func (ts *tidbTestTopSQLSuite) TestTopSQLReceiver(c *C) {
c.Skip("unstable, skip it and fix it before 20210702")
db, err := sql.Open("mysql", ts.getDSN())
c.Assert(err, IsNil, Commentf("Error connecting"))
defer func() {
err := db.Close()
c.Assert(err, IsNil)
}()
agentServer, err := mockTopSQLReporter.StartMockAgentServer()
receiverServer, err := mockTopSQLReporter.StartMockReceiverServer()
c.Assert(err, IsNil)
defer func() {
agentServer.Stop()
receiverServer.Stop()
}()

c.Assert(failpoint.Enable("github.com/pingcap/tidb/util/topsql/reporter/resetTimeoutForTest", `return(true)`), IsNil)
Expand Down Expand Up @@ -1878,27 +1879,26 @@ func (ts *tidbTestTopSQLSuite) TestTopSQLAgent(c *C) {
conf.TopSQL.ReceiverAddress = addr
})
}
dbt.mustExec("set @@global.tidb_enable_top_sql='On';")
setTopSQLReceiverAddress("")
dbt.mustExec("set @@global.tidb_top_sql_precision_seconds=1;")
dbt.mustExec("set @@global.tidb_top_sql_report_interval_seconds=2;")
dbt.mustExec("set @@global.tidb_top_sql_max_statement_count=5;")

r := reporter.NewRemoteTopSQLReporter(reporter.NewGRPCReportClient(plancodec.DecodeNormalizedPlan))
r := reporter.NewRemoteTopSQLReporter(reporter.NewReportClientRegistry(), reporter.NewGRPCReportClient(plancodec.DecodeNormalizedPlan))
tracecpu.GlobalSQLCPUProfiler.SetCollector(&collectorWrapper{r})

// TODO: change to ensure that the right sql statements are reported, not just counts
checkFn := func(n int) {
records := agentServer.GetLatestRecords()
records := receiverServer.GetLatestRecords()
c.Assert(len(records), Equals, n)
for _, r := range records {
sqlMeta, exist := agentServer.GetSQLMetaByDigestBlocking(r.SqlDigest, time.Second)
sqlMeta, exist := receiverServer.GetSQLMetaByDigestBlocking(r.SqlDigest, time.Second)
c.Assert(exist, IsTrue)
c.Check(sqlMeta.NormalizedSql, Matches, "select.*from.*join.*")
if len(r.PlanDigest) == 0 {
continue
}
plan, exist := agentServer.GetPlanMetaByDigestBlocking(r.PlanDigest, time.Second)
plan, exist := receiverServer.GetPlanMetaByDigestBlocking(r.PlanDigest, time.Second)
c.Assert(exist, IsTrue)
plan = strings.Replace(plan, "\n", " ", -1)
plan = strings.Replace(plan, "\t", " ", -1)
Expand All @@ -1923,22 +1923,22 @@ func (ts *tidbTestTopSQLSuite) TestTopSQLAgent(c *C) {
cancel := runWorkload(0, 10)
// Test with null agent address, the agent server can't receive any record.
setTopSQLReceiverAddress("")
agentServer.WaitCollectCnt(1, time.Second*4)
receiverServer.WaitCollectCnt(1, time.Second*4)
checkFn(0)
// Test after set agent address and the evict take effect.
dbt.mustExec("set @@global.tidb_top_sql_max_statement_count=5;")
setTopSQLReceiverAddress(agentServer.Address())
agentServer.WaitCollectCnt(1, time.Second*4)
setTopSQLReceiverAddress(receiverServer.Address())
receiverServer.WaitCollectCnt(1, time.Second*4)
checkFn(5)
// Test with wrong agent address, the agent server can't receive any record.
dbt.mustExec("set @@global.tidb_top_sql_max_statement_count=8;")
setTopSQLReceiverAddress("127.0.0.1:65530")

agentServer.WaitCollectCnt(1, time.Second*4)
receiverServer.WaitCollectCnt(1, time.Second*4)
checkFn(0)
// Test after set agent address and the evict take effect.
setTopSQLReceiverAddress(agentServer.Address())
agentServer.WaitCollectCnt(1, time.Second*4)
setTopSQLReceiverAddress(receiverServer.Address())
receiverServer.WaitCollectCnt(1, time.Second*4)
checkFn(8)
cancel() // cancel case 1

Expand All @@ -1947,44 +1947,44 @@ func (ts *tidbTestTopSQLSuite) TestTopSQLAgent(c *C) {
// empty agent address, should not collect records
dbt.mustExec("set @@global.tidb_top_sql_max_statement_count=5;")
setTopSQLReceiverAddress("")
agentServer.WaitCollectCnt(1, time.Second*4)
receiverServer.WaitCollectCnt(1, time.Second*4)
checkFn(0)
// set correct address, should collect records
setTopSQLReceiverAddress(agentServer.Address())
agentServer.WaitCollectCnt(1, time.Second*4)
setTopSQLReceiverAddress(receiverServer.Address())
receiverServer.WaitCollectCnt(1, time.Second*4)
checkFn(5)
// agent server hangs for a while
agentServer.HangFromNow(time.Second * 6)
receiverServer.HangFromNow(time.Second * 6)
// run another set of SQL queries
cancel2()

cancel3 := runWorkload(11, 20)
agentServer.WaitCollectCnt(1, time.Second*8)
receiverServer.WaitCollectCnt(1, time.Second*8)
checkFn(5)
cancel3()

// case 3: agent restart
cancel4 := runWorkload(0, 10)
// empty agent address, should not collect records
setTopSQLReceiverAddress("")
agentServer.WaitCollectCnt(1, time.Second*4)
receiverServer.WaitCollectCnt(1, time.Second*4)
checkFn(0)
// set correct address, should collect records
setTopSQLReceiverAddress(agentServer.Address())
agentServer.WaitCollectCnt(1, time.Second*8)
setTopSQLReceiverAddress(receiverServer.Address())
receiverServer.WaitCollectCnt(1, time.Second*8)
checkFn(5)
// run another set of SQL queries
cancel4()

cancel5 := runWorkload(11, 20)
// agent server shutdown
agentServer.Stop()
receiverServer.Stop()
// agent server restart
agentServer, err = mockTopSQLReporter.StartMockAgentServer()
receiverServer, err = mockTopSQLReporter.StartMockReceiverServer()
c.Assert(err, IsNil)
setTopSQLReceiverAddress(agentServer.Address())
setTopSQLReceiverAddress(receiverServer.Address())
// check result
agentServer.WaitCollectCnt(2, time.Second*8)
receiverServer.WaitCollectCnt(2, time.Second*8)
checkFn(5)
cancel5()
}
Expand Down
8 changes: 4 additions & 4 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ func (s *session) ExecuteInternal(ctx context.Context, sql string, args ...inter
s.sessionVars.InRestrictedSQL = true
defer func() {
s.sessionVars.InRestrictedSQL = origin
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
// Restore the goroutine label by using the original ctx after execution is finished.
pprof.SetGoroutineLabels(ctx)
}
Expand Down Expand Up @@ -1408,7 +1408,7 @@ func (s *session) ParseWithParams(ctx context.Context, sql string, args ...inter
for _, warn := range warns {
s.sessionVars.StmtCtx.AppendWarning(util.SyntaxWarn(warn))
}
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
normalized, digest := parser.NormalizeDigest(sql)
if digest != nil {
// Reset the goroutine label when internal sql execute finish.
Expand All @@ -1422,7 +1422,7 @@ func (s *session) ParseWithParams(ctx context.Context, sql string, args ...inter
// ExecRestrictedStmt implements RestrictedSQLExecutor interface.
func (s *session) ExecRestrictedStmt(ctx context.Context, stmtNode ast.StmtNode, opts ...sqlexec.OptionFuncAlias) (
[]chunk.Row, []*ast.ResultField, error) {
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
defer pprof.SetGoroutineLabels(ctx)
}
var execOption sqlexec.ExecOption
Expand Down Expand Up @@ -1530,7 +1530,7 @@ func (s *session) ExecuteStmt(ctx context.Context, stmtNode ast.StmtNode) (sqlex
return nil, err
}
normalizedSQL, digest := s.sessionVars.StmtCtx.SQLDigest()
if variable.TopSQLEnabled() {
if variable.TopSQLInstanceEnabled() {
ctx = topsql.AttachSQLInfo(ctx, normalizedSQL, digest, "", nil, s.sessionVars.InRestrictedSQL)
}

Expand Down
4 changes: 2 additions & 2 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1736,9 +1736,9 @@ var defaultSysVars = []*SysVar{
}},
// variable for top SQL feature.
{Scope: ScopeGlobal, Name: TiDBEnableTopSQL, Value: BoolToOnOff(DefTiDBTopSQLEnable), Type: TypeBool, Hidden: true, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) {
return BoolToOnOff(TopSQLVariable.Enable.Load()), nil
return Off, nil
}, SetGlobal: func(vars *SessionVars, s string) error {
TopSQLVariable.Enable.Store(TiDBOptOn(s))
// TODO: call pd to enable topsql globally. Not to affect the current instance directly.
zhongzc marked this conversation as resolved.
Show resolved Hide resolved
return nil
}},
{Scope: ScopeGlobal, Name: TiDBTopSQLPrecisionSeconds, Value: strconv.Itoa(DefTiDBTopSQLPrecisionSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: math.MaxInt64, GetGlobal: func(s *SessionVars) (string, error) {
Expand Down
Loading