Skip to content

Commit 15fe8f1

Browse files
authored
telemetry: add telemetry related code back (#61753)
close #61766
1 parent 9f1396b commit 15fe8f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3337
-30
lines changed

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ type Config struct {
229229
Experimental Experimental `toml:"experimental" json:"experimental"`
230230
// SkipRegisterToDashboard tells TiDB don't register itself to the dashboard.
231231
SkipRegisterToDashboard bool `toml:"skip-register-to-dashboard" json:"skip-register-to-dashboard"`
232-
// EnableTelemetry enables the usage data report to PingCAP. Deprecated: Telemetry has been removed.
232+
// EnableTelemetry enables the usage data print to log.
233233
EnableTelemetry bool `toml:"enable-telemetry" json:"enable-telemetry"`
234234
// Labels indicates the labels set for the tidb server. The labels describe some specific properties for the tidb
235235
// server like `zone`/`rack`/`host`. Currently, labels won't affect the tidb server except for some special

pkg/config/config.toml.example

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ new_collations_enabled_on_first_bootstrap = true
9494
# *If you want to start a TiDB service, NEVER enable this.*
9595
skip-register-to-dashboard = false
9696

97-
# When enabled, usage data (for example, instance versions) will be reported to PingCAP periodically for user experience analytics.
98-
# If this config is set to `false` on all TiDB servers, telemetry will be always disabled regardless of the value of the global variable `tidb_enable_telemetry`.
99-
# See PingCAP privacy policy for details: https://pingcap.com/en/privacy-policy/
97+
# When enabled, usage data (for example, instance versions) will be reported to log periodically for user experience analytics.
98+
# If this config is set to `false`, telemetry will be always disabled regardless of the value of the global variable `tidb_enable_telemetry`.
10099
enable-telemetry = false
101100

102101
# deprecate-integer-display-length is used to be compatible with MySQL 8.0 in which the integer declared with display length will be returned with

pkg/ddl/index.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ const (
9494
MaxCommentLength = 1024
9595
)
9696

97+
var telemetryAddIndexIngestUsage = metrics.TelemetryAddIndexIngestCnt
98+
9799
func buildIndexColumns(ctx *metabuild.Context, columns []*model.ColumnInfo, indexPartSpecifications []*ast.IndexPartSpecification, columnarIndexType model.ColumnarIndexType) ([]*model.IndexColumn, bool, error) {
98100
// Build offsets.
99101
idxParts := make([]*model.IndexColumn, 0, len(indexPartSpecifications))
@@ -1099,6 +1101,8 @@ SwitchIndexState:
10991101
}
11001102
loadCloudStorageURI(w, job)
11011103
if reorgTp.NeedMergeProcess() {
1104+
// Increase telemetryAddIndexIngestUsage
1105+
telemetryAddIndexIngestUsage.Inc()
11021106
for _, indexInfo := range allIndexInfos {
11031107
indexInfo.BackfillState = model.BackfillStateRunning
11041108
}

pkg/distsql/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ go_library(
2727
"//pkg/sessionctx/vardef",
2828
"//pkg/store/copr",
2929
"//pkg/tablecodec",
30+
"//pkg/telemetry",
3031
"//pkg/types",
3132
"//pkg/util/chunk",
3233
"//pkg/util/codec",

pkg/distsql/select_result.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/pingcap/tidb/pkg/parser/terror"
3535
"github.com/pingcap/tidb/pkg/planner/util"
3636
"github.com/pingcap/tidb/pkg/store/copr"
37+
"github.com/pingcap/tidb/pkg/telemetry"
3738
"github.com/pingcap/tidb/pkg/types"
3839
"github.com/pingcap/tidb/pkg/util/chunk"
3940
"github.com/pingcap/tidb/pkg/util/codec"
@@ -52,6 +53,12 @@ var (
5253
errQueryInterrupted = dbterror.ClassExecutor.NewStd(errno.ErrQueryInterrupted)
5354
)
5455

56+
var (
57+
telemetryBatchedQueryTaskCnt = metrics.TelemetryBatchedQueryTaskCnt
58+
telemetryStoreBatchedCnt = metrics.TelemetryStoreBatchedCnt
59+
telemetryStoreBatchedFallbackCnt = metrics.TelemetryStoreBatchedFallbackCnt
60+
)
61+
5562
var (
5663
_ SelectResult = (*selectResult)(nil)
5764
_ SelectResult = (*serialSelectResults)(nil)
@@ -311,6 +318,35 @@ type selectResult struct {
311318
}
312319

313320
func (r *selectResult) fetchResp(ctx context.Context) error {
321+
defer func() {
322+
if r.stats != nil {
323+
// Ignore internal sql.
324+
if !r.ctx.InRestrictedSQL && r.stats.copRespTime.Size() > 0 {
325+
ratio := r.stats.calcCacheHit()
326+
if ratio >= 1 {
327+
telemetry.CurrentCoprCacheHitRatioGTE100Count.Inc()
328+
}
329+
if ratio >= 0.8 {
330+
telemetry.CurrentCoprCacheHitRatioGTE80Count.Inc()
331+
}
332+
if ratio >= 0.4 {
333+
telemetry.CurrentCoprCacheHitRatioGTE40Count.Inc()
334+
}
335+
if ratio >= 0.2 {
336+
telemetry.CurrentCoprCacheHitRatioGTE20Count.Inc()
337+
}
338+
if ratio >= 0.1 {
339+
telemetry.CurrentCoprCacheHitRatioGTE10Count.Inc()
340+
}
341+
if ratio >= 0.01 {
342+
telemetry.CurrentCoprCacheHitRatioGTE1Count.Inc()
343+
}
344+
if ratio >= 0 {
345+
telemetry.CurrentCoprCacheHitRatioGTE0Count.Inc()
346+
}
347+
}
348+
}
349+
}()
314350
for {
315351
r.respChkIdx = 0
316352
startTime := time.Now()
@@ -620,6 +656,9 @@ func (r *selectResult) Close() error {
620656
batched, fallback := ci.GetStoreBatchInfo()
621657
if batched != 0 || fallback != 0 {
622658
r.stats.storeBatchedNum, r.stats.storeBatchedFallbackNum = batched, fallback
659+
telemetryStoreBatchedCnt.Add(float64(r.stats.storeBatchedNum))
660+
telemetryStoreBatchedFallbackCnt.Add(float64(r.stats.storeBatchedFallbackNum))
661+
telemetryBatchedQueryTaskCnt.Add(float64(r.stats.copRespTime.Size()))
623662
}
624663
}
625664
r.stats.fetchRspDuration = r.fetchDuration

pkg/domain/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ go_library(
7676
"//pkg/statistics/handle/util",
7777
"//pkg/statistics/util",
7878
"//pkg/store",
79+
"//pkg/telemetry",
7980
"//pkg/ttl/ttlworker",
8081
"//pkg/types",
8182
"//pkg/util",

pkg/domain/domain.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import (
8686
statslogutil "github.com/pingcap/tidb/pkg/statistics/handle/logutil"
8787
handleutil "github.com/pingcap/tidb/pkg/statistics/handle/util"
8888
kvstore "github.com/pingcap/tidb/pkg/store"
89+
"github.com/pingcap/tidb/pkg/telemetry"
8990
"github.com/pingcap/tidb/pkg/ttl/ttlworker"
9091
"github.com/pingcap/tidb/pkg/types"
9192
"github.com/pingcap/tidb/pkg/util"
@@ -1578,6 +1579,40 @@ func (do *Domain) globalBindHandleWorkerLoop(owner owner.Manager) {
15781579
}, "globalBindHandleWorkerLoop")
15791580
}
15801581

1582+
// TelemetryLoop create a goroutine that reports usage data in a loop, it should be called only once
1583+
// in BootstrapSession.
1584+
func (do *Domain) TelemetryLoop(ctx sessionctx.Context) {
1585+
ctx.GetSessionVars().InRestrictedSQL = true
1586+
err := telemetry.InitialRun(ctx)
1587+
if err != nil {
1588+
logutil.BgLogger().Warn("Initial telemetry run failed", zap.Error(err))
1589+
}
1590+
1591+
reportTicker := time.NewTicker(telemetry.ReportInterval)
1592+
subWindowTicker := time.NewTicker(telemetry.SubWindowSize)
1593+
1594+
do.wg.Run(func() {
1595+
defer func() {
1596+
logutil.BgLogger().Info("TelemetryReportLoop exited.")
1597+
}()
1598+
defer util.Recover(metrics.LabelDomain, "TelemetryReportLoop", nil, false)
1599+
1600+
for {
1601+
select {
1602+
case <-do.exit:
1603+
return
1604+
case <-reportTicker.C:
1605+
err := telemetry.ReportUsageData(ctx)
1606+
if err != nil {
1607+
logutil.BgLogger().Warn("TelemetryLoop retports usaged data failed", zap.Error(err))
1608+
}
1609+
case <-subWindowTicker.C:
1610+
telemetry.RotateSubWindow()
1611+
}
1612+
}
1613+
}, "TelemetryLoop")
1614+
}
1615+
15811616
// SetupPlanReplayerHandle setup plan replayer handle
15821617
func (do *Domain) SetupPlanReplayerHandle(collectorSctx sessionctx.Context, workersSctxs []sessionctx.Context) {
15831618
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnStats)
@@ -2865,6 +2900,9 @@ func (do *Domain) readTableCostWorker(wbLearningHandle *workloadlearning.Handle,
28652900

28662901
func init() {
28672902
initByLDFlagsForGlobalKill()
2903+
telemetry.GetDomainInfoSchema = func(ctx sessionctx.Context) infoschema.InfoSchema {
2904+
return GetDomain(ctx).InfoSchema()
2905+
}
28682906
}
28692907

28702908
var (

pkg/executor/adapter.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,48 @@ func (a *recordSet) GetExecutor4Test() any {
251251
return a.executor
252252
}
253253

254+
// TelemetryInfo records some telemetry information during execution.
255+
type TelemetryInfo struct {
256+
UseNonRecursive bool
257+
UseRecursive bool
258+
UseMultiSchemaChange bool
259+
UseExchangePartition bool
260+
UseFlashbackToCluster bool
261+
PartitionTelemetry *PartitionTelemetryInfo
262+
AccountLockTelemetry *AccountLockTelemetryInfo
263+
UseIndexMerge bool
264+
UseTableLookUp atomic.Bool
265+
}
266+
267+
// PartitionTelemetryInfo records table partition telemetry information during execution.
268+
type PartitionTelemetryInfo struct {
269+
UseTablePartition bool
270+
UseTablePartitionList bool
271+
UseTablePartitionRange bool
272+
UseTablePartitionHash bool
273+
UseTablePartitionRangeColumns bool
274+
UseTablePartitionRangeColumnsGt1 bool
275+
UseTablePartitionRangeColumnsGt2 bool
276+
UseTablePartitionRangeColumnsGt3 bool
277+
UseTablePartitionListColumns bool
278+
TablePartitionMaxPartitionsNum uint64
279+
UseCreateIntervalPartition bool
280+
UseAddIntervalPartition bool
281+
UseDropIntervalPartition bool
282+
UseCompactTablePartition bool
283+
UseReorganizePartition bool
284+
}
285+
286+
// AccountLockTelemetryInfo records account lock/unlock information during execution
287+
type AccountLockTelemetryInfo struct {
288+
// The number of CREATE/ALTER USER statements that lock the user
289+
LockUser int64
290+
// The number of CREATE/ALTER USER statements that unlock the user
291+
UnlockUser int64
292+
// The number of CREATE/ALTER USER statements
293+
CreateOrAlterUser int64
294+
}
295+
254296
// ExecStmt implements the sqlexec.Statement interface, it builds a planner.Plan to an sqlexec.Statement.
255297
type ExecStmt struct {
256298
// GoCtx stores parent go context.Context for a stmt.
@@ -285,6 +327,7 @@ type ExecStmt struct {
285327
// OutputNames will be set if using cached plan
286328
OutputNames []*types.FieldName
287329
PsStmt *plannercore.PlanCacheStmt
330+
Ti *TelemetryInfo
288331
}
289332

290333
// GetStmtNode returns the stmtNode inside Statement
@@ -339,7 +382,7 @@ func (a *ExecStmt) PointGet(ctx context.Context) (*recordSet, error) {
339382
}
340383

341384
if executor == nil {
342-
b := newExecutorBuilder(a.Ctx, a.InfoSchema)
385+
b := newExecutorBuilder(a.Ctx, a.InfoSchema, a.Ti)
343386
executor = b.build(a.Plan)
344387
if b.err != nil {
345388
return nil, b.err
@@ -1236,7 +1279,7 @@ func (a *ExecStmt) buildExecutor() (exec.Executor, error) {
12361279
ctx.GetSessionVars().StmtCtx.Priority = kv.PriorityLow
12371280
}
12381281

1239-
b := newExecutorBuilder(ctx, a.InfoSchema)
1282+
b := newExecutorBuilder(ctx, a.InfoSchema, a.Ti)
12401283
e := b.build(a.Plan)
12411284
if b.err != nil {
12421285
return nil, errors.Trace(b.err)

pkg/executor/benchmark_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func buildHashAggExecutor(ctx sessionctx.Context, src exec.Executor, schema *exp
6868
plan.SetSchema(schema)
6969
plan.Init(ctx.GetPlanCtx(), nil, 0)
7070
plan.SetChildren(nil)
71-
b := newExecutorBuilder(ctx, nil)
71+
b := newExecutorBuilder(ctx, nil, nil)
7272
exec := b.build(plan)
7373
hashAgg := exec.(*aggregate.HashAggExec)
7474
hashAgg.SetChildren(0, src)
@@ -120,7 +120,7 @@ func buildStreamAggExecutor(ctx sessionctx.Context, srcExec exec.Executor, schem
120120
plan = sg
121121
}
122122

123-
b := newExecutorBuilder(ctx, nil)
123+
b := newExecutorBuilder(ctx, nil, nil)
124124
return b.build(plan)
125125
}
126126

@@ -353,7 +353,7 @@ func buildWindowExecutor(ctx sessionctx.Context, windowFunc string, funcs int, f
353353
plan = win
354354
}
355355

356-
b := newExecutorBuilder(ctx, nil)
356+
b := newExecutorBuilder(ctx, nil, nil)
357357
exec := b.build(plan)
358358
return exec
359359
}
@@ -1254,7 +1254,7 @@ func prepare4IndexInnerHashJoin(tc *IndexJoinTestCase, outerDS *testutil.MockDat
12541254
keyOff2IdxOff[i] = i
12551255
}
12561256

1257-
readerBuilder, err := newExecutorBuilder(tc.Ctx, nil).
1257+
readerBuilder, err := newExecutorBuilder(tc.Ctx, nil, nil).
12581258
newDataReaderBuilder(&mockPhysicalIndexReader{e: innerDS})
12591259
if err != nil {
12601260
return nil, err
@@ -1328,7 +1328,7 @@ func prepare4IndexMergeJoin(tc *IndexJoinTestCase, outerDS *testutil.MockDataSou
13281328
outerCompareFuncs = append(outerCompareFuncs, expression.GetCmpFunction(nil, outerJoinKeys[i], outerJoinKeys[i]))
13291329
}
13301330

1331-
readerBuilder, err := newExecutorBuilder(tc.Ctx, nil).
1331+
readerBuilder, err := newExecutorBuilder(tc.Ctx, nil, nil).
13321332
newDataReaderBuilder(&mockPhysicalIndexReader{e: innerDS})
13331333
if err != nil {
13341334
return nil, err

pkg/executor/brie_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestBRIEBuilderOptions(t *testing.T) {
152152
sctx.GetSessionVars().User = &auth.UserIdentity{Username: "test"}
153153
is := infoschema.MockInfoSchema([]*model.TableInfo{core.MockSignedTable(), core.MockUnsignedTable()})
154154
ResetGlobalBRIEQueueForTest()
155-
builder := NewMockExecutorBuilderForTest(sctx, is)
155+
builder := NewMockExecutorBuilderForTest(sctx, is, nil)
156156
ctx := context.Background()
157157
p := parser.New()
158158
p.SetParserConfig(parser.ParserConfig{EnableWindowFunction: true, EnableStrictDoubleTypeCheck: true})

0 commit comments

Comments
 (0)