Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: add telemetry support for add index acceleration #37849

Merged
Merged
6 changes: 6 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const (
MaxCommentLength = 1024
)

var (
telemetryAddIndexLightningUsage = metrics.TelemetryAddIndexLightningCnt
)

func buildIndexColumns(ctx sessionctx.Context, columns []*model.ColumnInfo, indexPartSpecifications []*ast.IndexPartSpecification) ([]*model.IndexColumn, error) {
// Build offsets.
idxParts := make([]*model.IndexColumn, 0, len(indexPartSpecifications))
Expand Down Expand Up @@ -600,6 +604,8 @@ func (w *worker) onCreateIndex(d *ddlCtx, t *meta.Meta, job *model.Job, isPK boo
// none -> delete only
reorgTp := pickBackfillType(job)
if reorgTp.NeedMergeProcess() {
// Increase telemetryAddIndexLightningUsage
telemetryAddIndexLightningUsage.Inc()
indexInfo.BackfillState = model.BackfillStateRunning
}
indexInfo.State = model.StateDeleteOnly
Expand Down
26 changes: 26 additions & 0 deletions metrics/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ var (
Name: "table_partition_drop_interval_partition_usage",
Help: "Counter of partitions added by ALTER TABLE FIRST PARTITION statements",
})
TelemetryAddIndexLightningCnt = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "telemetry",
Name: "add_index_lightning_usage",
Help: "Counter of usage of add index lightning",
})
)

// readCounter reads the value of a prometheus.Counter.
Expand Down Expand Up @@ -284,3 +291,22 @@ func GetSavepointStmtCounter() int64 {
func GetLazyPessimisticUniqueCheckSetCounter() int64 {
return readCounter(LazyPessimisticUniqueCheckSetCount)
}

// AddIndexLightning records the usages of Add Index with Lightning solution.
type AddIndexLightningUsageCounter struct {
AddIndexLightningUsed int64 `json:"add_index_lightning_used"`
Benjamin2037 marked this conversation as resolved.
Show resolved Hide resolved
}

// Sub returns the difference of two counters.
func (a AddIndexLightningUsageCounter) Sub(rhs AddIndexLightningUsageCounter) AddIndexLightningUsageCounter {
return AddIndexLightningUsageCounter{
AddIndexLightningUsed: a.AddIndexLightningUsed - rhs.AddIndexLightningUsed,
}
}

// GetAddIndexLightningCounter gets the add index lightning counts.
func GetAddIndexLightningCounter() AddIndexLightningUsageCounter {
return AddIndexLightningUsageCounter{
AddIndexLightningUsed: readCounter(TelemetryAddIndexLightningCnt),
}
}
13 changes: 13 additions & 0 deletions telemetry/data_feature_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type featureUsage struct {
LogBackup bool `json:"logBackup"`
EnablePaging bool `json:"enablePaging"`
EnableCostModelVer2 bool `json:"enableCostModelVer2"`
AddIndexLightning *m.AddIndexLightningUsageCounter `json:"AddIndexLightning"`
}

type placementPolicyUsage struct {
Expand Down Expand Up @@ -97,6 +98,8 @@ func getFeatureUsage(ctx context.Context, sctx sessionctx.Context) (*featureUsag

usage.EnableCostModelVer2 = getCostModelVer2UsageInfo(sctx)

usage.AddIndexLightning = getAddIndexLightningUsageInfo()

return &usage, nil
}

Expand Down Expand Up @@ -228,6 +231,7 @@ var initialMultiSchemaChangeCounter m.MultiSchemaChangeUsageCounter
var initialTablePartitionCounter m.TablePartitionUsageCounter
var initialSavepointStmtCounter int64
var initialLazyPessimisticUniqueCheckSetCount int64
var initialAddIndexLightningCounter m.AddIndexLightningUsageCounter

// getTxnUsageInfo gets the usage info of transaction related features. It's exported for tests.
func getTxnUsageInfo(ctx sessionctx.Context) *TxnUsage {
Expand Down Expand Up @@ -316,6 +320,10 @@ func postReportTablePartitionUsage() {
initialTablePartitionCounter = m.ResetTablePartitionCounter(initialTablePartitionCounter)
}

func postReportAddIndexLightingUsage() {
initialAddIndexLightningCounter = m.GetAddIndexLightningCounter()
}

func getTablePartitionUsageInfo() *m.TablePartitionUsageCounter {
curr := m.GetTablePartitionCounter()
diff := curr.Cal(initialTablePartitionCounter)
Expand Down Expand Up @@ -358,3 +366,8 @@ func getCostModelVer2UsageInfo(ctx sessionctx.Context) bool {
func getPagingUsageInfo(ctx sessionctx.Context) bool {
return ctx.GetSessionVars().EnablePaging
}
func getAddIndexLightningUsageInfo() *m.AddIndexLightningUsageCounter {
curr := m.GetAddIndexLightningCounter()
diff := curr.Sub(initialAddIndexLightningCounter)
return &diff
}
31 changes: 31 additions & 0 deletions telemetry/data_feature_usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/telemetry"
"github.com/pingcap/tidb/testkit"
Expand Down Expand Up @@ -419,3 +420,33 @@ func TestLazyPessimisticUniqueCheck(t *testing.T) {
usage = telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(2), usage.LazyUniqueCheckSetCounter)
}

func TestAddIndexLightning(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.Equal(t, int64(0), usage.AddIndexLightning.AddIndexLightningUsed)

allow := ddl.IsEnableFastReorg()
require.Equal(t, false, allow)
tk.MustExec("use test")
tk.MustExec("drop table if exists tele_t")
tk.MustExec("create table tele_t(id int, b int)")
tk.MustExec("insert into tele_t values(1,1),(2,2);")
tk.MustExec("alter table tele_t add index idx_org(b)")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(0), usage.AddIndexLightning.AddIndexLightningUsed)

tk.MustExec("set @@global.tidb_ddl_enable_fast_reorg = on")
allow = ddl.IsEnableFastReorg()
require.Equal(t, true, allow)
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
// Because we have check once ddl.IsEnableFastReorg() so we get two as result.
Benjamin2037 marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, int64(0), usage.AddIndexLightning.AddIndexLightningUsed)
tk.MustExec("alter table tele_t add index idx_new(b)")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(1), usage.AddIndexLightning.AddIndexLightningUsed)
}