Skip to content

Commit

Permalink
*: add telmetry support for resource control
Browse files Browse the repository at this point in the history
Signed-off-by: BornChanger <dawn_catcher@126.com>
  • Loading branch information
BornChanger committed Jan 18, 2023
1 parent 89de573 commit 039c11a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
16 changes: 14 additions & 2 deletions telemetry/data_feature_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type featureUsage struct {
EnableGlobalMemoryControl bool `json:"enableGlobalMemoryControl"`
AutoIDNoCache bool `json:"autoIDNoCache"`
IndexMergeUsageCounter *m.IndexMergeUsageCounter `json:"indexMergeUsageCounter"`
ResourceGroupUsage *resourceGroupUsage `json:"resourceGroup"`
}

type placementPolicyUsage struct {
Expand All @@ -69,6 +70,11 @@ type placementPolicyUsage struct {
NumPartitionWithExplicitPolicies uint64 `json:"numPartitionWithExplicitPolicies"`
}

type resourceGroupUsage struct {
Enabled bool `json:"resourceGroupEnabled"`
NumResourceGroups uint64 `json:"numResourceGroups"`
}

func getFeatureUsage(ctx context.Context, sctx sessionctx.Context) (*featureUsage, error) {
var usage featureUsage
var err error
Expand Down Expand Up @@ -114,11 +120,12 @@ func getFeatureUsage(ctx context.Context, sctx sessionctx.Context) (*featureUsag
return &usage, nil
}

// collectFeatureUsageFromInfoschema updates the usage for temporary table, cached table and placement policies.
// collectFeatureUsageFromInfoschema updates the usage for temporary table, cached table, placement policies and resource groups
func collectFeatureUsageFromInfoschema(ctx sessionctx.Context, usage *featureUsage) {
if usage.PlacementPolicyUsage == nil {
usage.PlacementPolicyUsage = &placementPolicyUsage{}
}

is := GetDomainInfoSchema(ctx)
for _, dbInfo := range is.AllSchemas() {
if dbInfo.PlacementPolicyRef != nil {
Expand Down Expand Up @@ -149,8 +156,13 @@ func collectFeatureUsageFromInfoschema(ctx sessionctx.Context, usage *featureUsa
}
}
}

usage.PlacementPolicyUsage.NumPlacementPolicies += uint64(len(is.AllPlacementPolicies()))

if usage.ResourceGroupUsage == nil {
usage.ResourceGroupUsage = &resourceGroupUsage{}
}
usage.ResourceGroupUsage.NumResourceGroups = uint64(len(is.AllResourceGroups()))
usage.ResourceGroupUsage.Enabled = variable.EnableResourceControl.Load()
}

// GetDomainInfoSchema is used by the telemetry package to get the latest schema information
Expand Down
39 changes: 39 additions & 0 deletions telemetry/data_feature_usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,45 @@ func TestPlacementPolicies(t *testing.T) {
require.Equal(t, uint64(1), usage.PlacementPolicyUsage.NumPartitionWithExplicitPolicies)
}

func TestResourceGroups(t *testing.T) {
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)

usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, uint64(0), usage.ResourceGroupUsage.NumResourceGroups)
require.Equal(t, false, usage.ResourceGroupUsage.Enabled)

tk.MustExec("set global tidb_enable_resource_control = 'ON'")
tk.MustExec("create resource group x rru_per_sec=100 wru_per_sec=200")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, true, usage.ResourceGroupUsage.Enabled)
require.Equal(t, uint64(1), usage.ResourceGroupUsage.NumResourceGroups)

tk.MustExec("create resource group y rru_per_sec=100 wru_per_sec=200")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, uint64(2), usage.ResourceGroupUsage.NumResourceGroups)

tk.MustExec("alter resource group y rru_per_sec=100 wru_per_sec=300")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, uint64(2), usage.ResourceGroupUsage.NumResourceGroups)

tk.MustExec("drop resource group y")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, uint64(1), usage.ResourceGroupUsage.NumResourceGroups)

tk.MustExec("set global tidb_enable_resource_control = 'OFF'")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, uint64(1), usage.ResourceGroupUsage.NumResourceGroups)
require.Equal(t, false, usage.ResourceGroupUsage.Enabled)
}

func TestAutoCapture(t *testing.T) {
store := testkit.CreateMockStore(t)

Expand Down

0 comments on commit 039c11a

Please sign in to comment.