Skip to content

Commit

Permalink
telemetry: rewrite cluster index usage info (#34296)
Browse files Browse the repository at this point in the history
close #34035
  • Loading branch information
ymkzpx authored May 7, 2022
1 parent cf5ad49 commit 7f3afc9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
60 changes: 26 additions & 34 deletions telemetry/data_feature_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ import (
"github.com/tikv/client-go/v2/metrics"
)

// emptyClusterIndexUsage is empty ClusterIndexUsage, deprecated.
var emptyClusterIndexUsage = ClusterIndexUsage{}

type featureUsage struct {
// transaction usage information
Txn *TxnUsage `json:"txn"`
// cluster index usage information
// key is the first 6 characters of sha2(TABLE_NAME, 256)
ClusterIndex *ClusterIndexUsage `json:"clusterIndex"`
NewClusterIndex *NewClusterIndexUsage `json:"newClusterIndex"`
TemporaryTable bool `json:"temporaryTable"`
CTE *m.CTEUsageCounter `json:"cte"`
CachedTable bool `json:"cachedTable"`
Expand All @@ -52,7 +56,7 @@ type placementPolicyUsage struct {
func getFeatureUsage(ctx sessionctx.Context) (*featureUsage, error) {
var usage featureUsage
var err error
usage.ClusterIndex, err = getClusterIndexUsageInfo(ctx)
usage.NewClusterIndex, usage.ClusterIndex, err = getClusterIndexUsageInfo(ctx)
if err != nil {
logutil.BgLogger().Info(err.Error())
return nil, err
Expand Down Expand Up @@ -109,7 +113,7 @@ func collectFeatureUsageFromInfoschema(ctx sessionctx.Context, usage *featureUsa
// while avoiding circle dependency with domain package.
var GetDomainInfoSchema func(sessionctx.Context) infoschema.InfoSchema

// ClusterIndexUsage records the usage info of all the tables, no more than 10k tables
// ClusterIndexUsage records the usage info of all the tables, no more than 10k tables, deprecated.
type ClusterIndexUsage map[string]TableClusteredInfo

// TableClusteredInfo records the usage info of clusterindex of each table
Expand All @@ -121,20 +125,26 @@ type TableClusteredInfo struct {
// NA means this field is no meaningful information
}

// NewClusterIndexUsage records the clustered index usage info of all the tables.
type NewClusterIndexUsage struct {
// The number of user's tables with clustered index enabled.
NumClusteredTables uint64 `json:"numClusteredTables"`
// The number of user's tables.
NumTotalTables uint64 `json:"numTotalTables"`
}

// getClusterIndexUsageInfo gets the ClusterIndex usage information. It's exported for future test.
func getClusterIndexUsageInfo(ctx sessionctx.Context) (cu *ClusterIndexUsage, err error) {
usage := make(ClusterIndexUsage)
func getClusterIndexUsageInfo(ctx sessionctx.Context) (ncu *NewClusterIndexUsage, cu *ClusterIndexUsage, err error) {
var newUsage NewClusterIndexUsage
exec := ctx.(sqlexec.RestrictedSQLExecutor)

// query INFORMATION_SCHEMA.tables to get the latest table information about ClusterIndex
rows, _, err := exec.ExecRestrictedSQL(context.TODO(), nil, `
SELECT left(sha2(TABLE_NAME, 256), 6) table_name_hash, TIDB_PK_TYPE, TABLE_SCHEMA, TABLE_NAME
SELECT TIDB_PK_TYPE
FROM information_schema.tables
WHERE table_schema not in ('INFORMATION_SCHEMA', 'METRICS_SCHEMA', 'PERFORMANCE_SCHEMA', 'mysql')
ORDER BY table_name_hash
limit 10000`)
WHERE table_schema not in ('INFORMATION_SCHEMA', 'METRICS_SCHEMA', 'PERFORMANCE_SCHEMA', 'mysql')`)
if err != nil {
return nil, err
return nil, nil, err
}

defer func() {
Expand All @@ -152,39 +162,21 @@ func getClusterIndexUsageInfo(ctx sessionctx.Context) (cu *ClusterIndexUsage, er

err = ctx.RefreshTxnCtx(context.TODO())
if err != nil {
return nil, err
return nil, nil, err
}
infoSchema := ctx.GetSessionVars().TxnCtx.InfoSchema.(infoschema.InfoSchema)

// check ClusterIndex information for each table
// row: 0 = table_name_hash, 1 = TIDB_PK_TYPE, 2 = TABLE_SCHEMA (db), 3 = TABLE_NAME

// row: 0 = TIDB_PK_TYPE
for _, row := range rows {
if row.Len() < 4 {
if row.Len() < 1 {
continue
}
tblClusteredInfo := TableClusteredInfo{false, "NA"}
if row.GetString(1) == "CLUSTERED" {
tblClusteredInfo.IsClustered = true
table, err := infoSchema.TableByName(model.NewCIStr(row.GetString(2)), model.NewCIStr(row.GetString(3)))
if err != nil {
continue
}
tableInfo := table.Meta()
if tableInfo.PKIsHandle {
tblClusteredInfo.ClusterPKType = "INT"
} else if tableInfo.IsCommonHandle {
tblClusteredInfo.ClusterPKType = "NON_INT"
} else {
// if both CLUSTERED IS TURE and CLUSTERPKTYPE IS NA met, this else is hit
// it means the status of INFORMATION_SCHEMA.tables if not consistent with session.Context
// WE SHOULD treat this issue SERIOUSLY
}
if row.GetString(0) == "CLUSTERED" {
newUsage.NumClusteredTables++
}
usage[row.GetString(0)] = tblClusteredInfo
}

return &usage, nil
newUsage.NumTotalTables = uint64(len(rows))
return &newUsage, &emptyClusterIndexUsage, nil
}

// TxnUsage records the usage info of transaction related features, including
Expand Down
16 changes: 16 additions & 0 deletions telemetry/data_feature_usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,19 @@ func TestAutoCapture(t *testing.T) {
require.NoError(t, err)
require.True(t, usage.AutoCapture)
}

func TestClusterIndexUsageInfo(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1(a int key clustered);")
tk.MustExec("create table t2(a int);")

usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.NotNil(t, usage.ClusterIndex)
require.Equal(t, uint64(1), usage.NewClusterIndex.NumClusteredTables)
require.Equal(t, uint64(2), usage.NewClusterIndex.NumTotalTables)
}

0 comments on commit 7f3afc9

Please sign in to comment.