Skip to content

Commit

Permalink
planner: provide a system view cluster_tidb_plan_cache to allow use…
Browse files Browse the repository at this point in the history
…rs to see the cluster's Plan Cache Info (#57781)

ref #54057
  • Loading branch information
qw4990 authored Nov 28, 2024
1 parent 924784a commit fb7e32f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2279,6 +2279,7 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) exec.Ex
strings.ToLower(infoschema.TableKeywords),
strings.ToLower(infoschema.TableTiDBIndexUsage),
strings.ToLower(infoschema.TableTiDBPlanCache),
strings.ToLower(infoschema.ClusterTableTiDBPlanCache),
strings.ToLower(infoschema.ClusterTableTiDBIndexUsage):
memTracker := memory.NewTracker(v.ID(), -1)
memTracker.AttachTo(b.ctx.GetSessionVars().StmtCtx.MemTracker)
Expand Down
12 changes: 10 additions & 2 deletions pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
case infoschema.ClusterTableTiDBIndexUsage:
err = e.setDataFromClusterIndexUsage(ctx, sctx)
case infoschema.TableTiDBPlanCache:
err = e.setDataFromPlanCache(ctx, sctx)
err = e.setDataFromPlanCache(ctx, sctx, false)
case infoschema.ClusterTableTiDBPlanCache:
err = e.setDataFromPlanCache(ctx, sctx, true)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -3916,7 +3918,7 @@ func (e *memtableRetriever) setDataFromClusterIndexUsage(ctx context.Context, sc
return nil
}

func (e *memtableRetriever) setDataFromPlanCache(_ context.Context, sctx sessionctx.Context) error {
func (e *memtableRetriever) setDataFromPlanCache(_ context.Context, sctx sessionctx.Context, cluster bool) (err error) {
values := domain.GetDomain(sctx).GetInstancePlanCache().All()
rows := make([][]types.Datum, 0, len(values))
for _, v := range values {
Expand Down Expand Up @@ -3946,6 +3948,12 @@ func (e *memtableRetriever) setDataFromPlanCache(_ context.Context, sctx session
rows = append(rows, row)
}

if cluster {
if rows, err = infoschema.AppendHostInfoToRows(sctx, rows); err != nil {
return err
}
}

e.rows = rows
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/infoschema/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const (
ClusterTableMemoryUsageOpsHistory = "CLUSTER_MEMORY_USAGE_OPS_HISTORY"
// ClusterTableTiDBIndexUsage is a table to show the usage stats of indexes across the whole cluster.
ClusterTableTiDBIndexUsage = "CLUSTER_TIDB_INDEX_USAGE"
// ClusterTableTiDBPlanCache is the plan cache status of tidb cluster.
ClusterTableTiDBPlanCache = "CLUSTER_TIDB_PLAN_CACHE"
)

// memTableToAllTiDBClusterTables means add memory table to cluster table that will send cop request to all TiDB nodes.
Expand All @@ -69,6 +71,7 @@ var memTableToAllTiDBClusterTables = map[string]string{
TableMemoryUsage: ClusterTableMemoryUsage,
TableMemoryUsageOpsHistory: ClusterTableMemoryUsageOpsHistory,
TableTiDBIndexUsage: ClusterTableTiDBIndexUsage,
TableTiDBPlanCache: ClusterTableTiDBPlanCache,
}

// memTableToDDLOwnerClusterTables means add memory table to cluster table that will send cop request to DDL owner node.
Expand Down
1 change: 1 addition & 0 deletions pkg/infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ var tableIDMap = map[string]int64{
ClusterTableTiDBIndexUsage: autoid.InformationSchemaDBID + 94,
TableTiFlashIndexes: autoid.InformationSchemaDBID + 95,
TableTiDBPlanCache: autoid.InformationSchemaDBID + 96,
ClusterTableTiDBPlanCache: autoid.InformationSchemaDBID + 97,
}

// columnInfo represents the basic column information of all kinds of INFORMATION_SCHEMA tables
Expand Down
51 changes: 51 additions & 0 deletions pkg/infoschema/test/clustertablestest/cluster_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1972,3 +1972,54 @@ func TestMDLViewIDConflict(t *testing.T) {
txnTK2.MustExec("COMMIT")
wg.Wait()
}

func TestPlanCacheView(t *testing.T) {
s := new(clusterTablesSuite)
s.store, s.dom = testkit.CreateMockStoreAndDomain(t)
s.rpcserver, s.listenAddr = s.setUpRPCService(t, "127.0.0.1:0", nil)
s.httpServer, s.mockAddr = s.setUpMockPDHTTPServer()
s.startTime = time.Now()
defer s.httpServer.Close()
defer s.rpcserver.Stop()

tk := s.newTestKitWithRoot(t)
tk.MustExec("use test")
tk.MustExec(`set global tidb_enable_instance_plan_cache=1`)
tk.MustExec(`create table t (a int)`)
tk.MustExec(`prepare st from 'select a from t where a<?'`)
tk.MustExec(`set @a=1`)
tk.MustExec(`execute st using @a`)
tk.MustExec(`set @a=2`)
tk.MustExec(`execute st using @a`)
tk.RefreshSession()

require.Eventually(t, func() bool {
result := tk.MustQuery(`select instance, sql_text, executions from information_schema.cluster_tidb_plan_cache`)
expectedResult := testkit.Rows(
":10080 select a from t where a<? 2")
if !result.Equal(expectedResult) {
logutil.BgLogger().Warn("result not equal", zap.Any("rows", result.Rows()))
return false
}
return true
}, time.Second*2, time.Millisecond*100)

tk.MustExec("use test")
tk.MustExec(`prepare st from 'select a from t where a in (?)'`)
tk.MustExec(`set @a=2`)
tk.MustExec(`execute st using @a`)
tk.MustExec(`execute st using @a`)
tk.MustExec(`execute st using @a`)
tk.RefreshSession()
require.Eventually(t, func() bool {
result := tk.MustQuery(`select instance, sql_text, executions from information_schema.cluster_tidb_plan_cache order by executions`)
expectedResult := testkit.Rows(
":10080 select a from t where a<? 2",
":10080 select a from t where a in (?) 3")
if !result.Equal(expectedResult) {
logutil.BgLogger().Warn("result not equal", zap.Any("rows", result.Rows()))
return false
}
return true
}, time.Second*2, time.Millisecond*100)
}

0 comments on commit fb7e32f

Please sign in to comment.