From 039c11afb101d14c1fd40df2bb3c01e49a7f64c0 Mon Sep 17 00:00:00 2001 From: BornChanger Date: Wed, 18 Jan 2023 14:36:33 +0800 Subject: [PATCH] *: add telmetry support for resource control Signed-off-by: BornChanger --- telemetry/data_feature_usage.go | 16 ++++++++++-- telemetry/data_feature_usage_test.go | 39 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/telemetry/data_feature_usage.go b/telemetry/data_feature_usage.go index 6dec8edcb023a..6bbb3d8bf448d 100644 --- a/telemetry/data_feature_usage.go +++ b/telemetry/data_feature_usage.go @@ -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 { @@ -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 @@ -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 { @@ -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 diff --git a/telemetry/data_feature_usage_test.go b/telemetry/data_feature_usage_test.go index c303c53f3006b..c693b58c4132f 100644 --- a/telemetry/data_feature_usage_test.go +++ b/telemetry/data_feature_usage_test.go @@ -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)