Skip to content

Commit

Permalink
telemetry, session : add telemetry for cache table feature (#29963)
Browse files Browse the repository at this point in the history
  • Loading branch information
JayLZhou authored Nov 22, 2021
1 parent c835349 commit 2d2fba5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
15 changes: 14 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3015,7 +3015,7 @@ func (s *session) GetTxnWriteThroughputSLI() *sli.TxnWriteThroughputSLI {
return &s.txn.writeSLI
}

var _ telemetry.TemporaryTableFeatureChecker = &session{}
var _ telemetry.TemporaryOrCacheTableFeatureChecker = &session{}

// TemporaryTableExists is used by the telemetry package to avoid circle dependency.
func (s *session) TemporaryTableExists() bool {
Expand All @@ -3030,6 +3030,19 @@ func (s *session) TemporaryTableExists() bool {
return false
}

// CachedTableExists is used by the telemetry package to avoid circle dependency.
func (s *session) CachedTableExists() bool {
is := domain.GetDomain(s).InfoSchema()
for _, dbInfo := range is.AllSchemas() {
for _, tbInfo := range is.SchemaTables(dbInfo.Name) {
if tbInfo.Meta().TableCacheStatusType != model.TableCacheStatusDisable {
return true
}
}
}
return false
}

// GetInfoSchema returns snapshotInfoSchema if snapshot schema is set.
// Transaction infoschema is returned if inside an explicit txn.
// Otherwise the latest infoschema is returned.
Expand Down
12 changes: 8 additions & 4 deletions telemetry/data_feature_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type featureUsage struct {
ClusterIndex *ClusterIndexUsage `json:"clusterIndex"`
TemporaryTable bool `json:"temporaryTable"`
CTE *m.CTEUsageCounter `json:"cte"`
CachedTable bool `json:"cachedTable"`
}

func getFeatureUsage(ctx sessionctx.Context) (*featureUsage, error) {
Expand All @@ -49,11 +50,13 @@ func getFeatureUsage(ctx sessionctx.Context) (*featureUsage, error) {
txnUsage := getTxnUsageInfo(ctx)

// Avoid the circle dependency.
temporaryTable := ctx.(TemporaryTableFeatureChecker).TemporaryTableExists()
temporaryTable := ctx.(TemporaryOrCacheTableFeatureChecker).TemporaryTableExists()

cteUsage := getCTEUsageInfo()

return &featureUsage{txnUsage, clusterIdxUsage, temporaryTable, cteUsage}, nil
cachedTable := ctx.(TemporaryOrCacheTableFeatureChecker).CachedTableExists()

return &featureUsage{txnUsage, clusterIdxUsage, temporaryTable, cteUsage, cachedTable}, nil
}

// ClusterIndexUsage records the usage info of all the tables, no more than 10k tables
Expand Down Expand Up @@ -138,10 +141,11 @@ func getClusterIndexUsageInfo(ctx sessionctx.Context) (cu *ClusterIndexUsage, er
return &usage, nil
}

// TemporaryTableFeatureChecker is defined to avoid package circle dependency.
// TemporaryOrCacheTableFeatureChecker is defined to avoid package circle dependency.
// The session struct implements this interface.
type TemporaryTableFeatureChecker interface {
type TemporaryOrCacheTableFeatureChecker interface {
TemporaryTableExists() bool
CachedTableExists() bool
}

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

func TestCachedTable(t *testing.T) {
t.Parallel()

store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.False(t, usage.CachedTable)
tk.MustExec("drop table if exists tele_cache_t")
tk.MustExec("create table tele_cache_t (id int)")
tk.MustExec("alter table tele_cache_t cache")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.True(t, usage.CachedTable)
tk.MustExec("alter table tele_cache_t nocache")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.False(t, usage.CachedTable)
}
1 change: 1 addition & 0 deletions telemetry/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestMain(m *testing.M) {
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
goleak.IgnoreTopFunction("github.com/pingcap/tidb/table/tables.mockRemoteService"),
}

goleak.VerifyTestMain(m, opts...)
Expand Down

0 comments on commit 2d2fba5

Please sign in to comment.