forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_feature_usage_test.go
417 lines (343 loc) · 15.9 KB
/
data_feature_usage_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package telemetry_test
import (
"fmt"
"testing"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/telemetry"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)
func TestTxnUsageInfo(t *testing.T) {
store := testkit.CreateMockStore(t)
t.Run("Used", func(t *testing.T) {
tk := testkit.NewTestKit(t, store)
tk.MustExec(fmt.Sprintf("set global %s = 0", variable.TiDBEnableAsyncCommit))
tk.MustExec(fmt.Sprintf("set global %s = 0", variable.TiDBEnable1PC))
txnUsage := telemetry.GetTxnUsageInfo(tk.Session())
require.False(t, txnUsage.AsyncCommitUsed)
require.False(t, txnUsage.OnePCUsed)
tk.MustExec(fmt.Sprintf("set global %s = 1", variable.TiDBEnableAsyncCommit))
tk.MustExec(fmt.Sprintf("set global %s = 1", variable.TiDBEnable1PC))
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.True(t, txnUsage.AsyncCommitUsed)
require.True(t, txnUsage.OnePCUsed)
tk.MustExec(fmt.Sprintf("set global %s = 0", variable.TiDBEnableMutationChecker))
tk.MustExec(fmt.Sprintf("set global %s = off", variable.TiDBTxnAssertionLevel))
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.False(t, txnUsage.MutationCheckerUsed)
require.Equal(t, "OFF", txnUsage.AssertionLevel)
tk.MustExec(fmt.Sprintf("set global %s = 1", variable.TiDBEnableMutationChecker))
tk.MustExec(fmt.Sprintf("set global %s = strict", variable.TiDBTxnAssertionLevel))
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.True(t, txnUsage.MutationCheckerUsed)
require.Equal(t, "STRICT", txnUsage.AssertionLevel)
tk.MustExec(fmt.Sprintf("set global %s = fast", variable.TiDBTxnAssertionLevel))
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, "FAST", txnUsage.AssertionLevel)
tk.MustExec(fmt.Sprintf("set global %s = 1", variable.TiDBRCReadCheckTS))
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.True(t, txnUsage.RcCheckTS)
})
t.Run("Count", func(t *testing.T) {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists txn_usage_info")
tk.MustExec("create table txn_usage_info (a int)")
tk.MustExec(fmt.Sprintf("set %s = 1", variable.TiDBEnableAsyncCommit))
tk.MustExec(fmt.Sprintf("set %s = 1", variable.TiDBEnable1PC))
tk.MustExec("insert into txn_usage_info values (1)")
tk.MustExec(fmt.Sprintf("set %s = 0", variable.TiDBEnable1PC))
tk.MustExec("insert into txn_usage_info values (2)")
tk.MustExec(fmt.Sprintf("set %s = 0", variable.TiDBEnableAsyncCommit))
tk.MustExec("insert into txn_usage_info values (3)")
txnUsage := telemetry.GetTxnUsageInfo(tk.Session())
require.True(t, txnUsage.AsyncCommitUsed)
require.True(t, txnUsage.OnePCUsed)
require.Greater(t, txnUsage.TxnCommitCounter.AsyncCommit, int64(0))
require.Greater(t, txnUsage.TxnCommitCounter.OnePC, int64(0))
require.Greater(t, txnUsage.TxnCommitCounter.TwoPC, int64(0))
})
}
func TestTemporaryTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.False(t, usage.TemporaryTable)
tk.MustExec("create global temporary table t (id int) on commit delete rows")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.True(t, usage.TemporaryTable)
}
func TestCachedTable(t *testing.T) {
store := testkit.CreateMockStore(t)
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)
}
func TestAccountLock(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(0), usage.AccountLock.LockUser)
require.Equal(t, int64(0), usage.AccountLock.UnlockUser)
require.Equal(t, int64(0), usage.AccountLock.CreateOrAlterUser)
tk.MustExec("drop user if exists testUser")
tk.MustExec("create user testUser account lock")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(1), usage.AccountLock.LockUser)
require.Equal(t, int64(0), usage.AccountLock.UnlockUser)
require.Equal(t, int64(1), usage.AccountLock.CreateOrAlterUser)
tk.MustExec("alter user testUser account unlock")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(1), usage.AccountLock.LockUser)
require.Equal(t, int64(1), usage.AccountLock.UnlockUser)
require.Equal(t, int64(2), usage.AccountLock.CreateOrAlterUser)
}
func TestMultiSchemaChange(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(0), usage.MultiSchemaChange.MultiSchemaChangeUsed)
tk.MustExec("drop table if exists tele_multi_t")
tk.MustExec("create table tele_multi_t(id int)")
tk.MustExec("alter table tele_multi_t add column b int")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(0), usage.MultiSchemaChange.MultiSchemaChangeUsed)
tk.MustExec("alter table tele_multi_t add column c int, drop column b")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(1), usage.MultiSchemaChange.MultiSchemaChangeUsed)
tk.MustExec("alter table tele_multi_t add column b int, drop column c")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(2), usage.MultiSchemaChange.MultiSchemaChangeUsed)
tk.MustExec("alter table tele_multi_t drop column b")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(2), usage.MultiSchemaChange.MultiSchemaChangeUsed)
}
func TestTablePartition(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionListCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionMaxPartitionsCnt)
tk.MustExec("drop table if exists pt")
tk.MustExec("create table pt (a int,b int) partition by hash(a) partitions 4")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(1), usage.TablePartition.TablePartitionCnt)
require.Equal(t, int64(1), usage.TablePartition.TablePartitionHashCnt)
require.Equal(t, int64(4), usage.TablePartition.TablePartitionMaxPartitionsCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionListCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionRangeCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionRangeColumnsCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionListColumnsCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionCreateIntervalPartitionsCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionAddIntervalPartitionsCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionDropIntervalPartitionsCnt)
telemetry.PostReportTelemetryDataForTest()
tk.MustExec("drop table if exists pt1")
tk.MustExec("create table pt1 (a int,b int) partition by range(a) (" +
"partition p0 values less than (3)," +
"partition p1 values less than (6), " +
"partition p2 values less than (9)," +
"partition p3 values less than (12)," +
"partition p4 values less than (15))")
tk.MustExec("alter table pt1 first partition less than (9)")
tk.MustExec("alter table pt1 last partition less than (21)")
tk.MustExec("drop table if exists pt1")
tk.MustExec("create table pt1 (d datetime primary key, v varchar(255)) partition by range columns(d)" +
" interval (1 day) first partition less than ('2022-01-01') last partition less than ('2022-02-22')")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(2), usage.TablePartition.TablePartitionCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionHashCnt)
require.Equal(t, int64(5), usage.TablePartition.TablePartitionMaxPartitionsCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionListCnt)
require.Equal(t, int64(1), usage.TablePartition.TablePartitionRangeCnt)
require.Equal(t, int64(1), usage.TablePartition.TablePartitionRangeColumnsCnt)
require.Equal(t, int64(0), usage.TablePartition.TablePartitionListColumnsCnt)
require.Equal(t, int64(1), usage.TablePartition.TablePartitionCreateIntervalPartitionsCnt)
require.Equal(t, int64(1), usage.TablePartition.TablePartitionAddIntervalPartitionsCnt)
require.Equal(t, int64(1), usage.TablePartition.TablePartitionDropIntervalPartitionsCnt)
}
func TestPlacementPolicies(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, uint64(0), usage.PlacementPolicyUsage.NumPlacementPolicies)
require.Equal(t, uint64(0), usage.PlacementPolicyUsage.NumDBWithPolicies)
require.Equal(t, uint64(0), usage.PlacementPolicyUsage.NumTableWithPolicies)
require.Equal(t, uint64(0), usage.PlacementPolicyUsage.NumPartitionWithExplicitPolicies)
tk.MustExec("create placement policy p1 followers=4;")
tk.MustExec(`create placement policy p2 primary_region="cn-east-1" regions="cn-east-1,cn-east"`)
tk.MustExec(`create placement policy p3 followers=3`)
tk.MustExec("alter database test placement policy=p1;")
tk.MustExec("create table t1(a int);")
tk.MustExec("create table t2(a int) placement policy=p2;")
tk.MustExec("create table t3(id int) PARTITION BY RANGE (id) (" +
"PARTITION p0 VALUES LESS THAN (100) placement policy p3," +
"PARTITION p1 VALUES LESS THAN (1000))")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, uint64(3), usage.PlacementPolicyUsage.NumPlacementPolicies)
require.Equal(t, uint64(1), usage.PlacementPolicyUsage.NumDBWithPolicies)
require.Equal(t, uint64(3), usage.PlacementPolicyUsage.NumTableWithPolicies)
require.Equal(t, uint64(1), usage.PlacementPolicyUsage.NumPartitionWithExplicitPolicies)
tk.MustExec("drop table t2;")
tk.MustExec("drop placement policy p2;")
tk.MustExec("alter table t3 placement policy=default")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, uint64(2), usage.PlacementPolicyUsage.NumPlacementPolicies)
require.Equal(t, uint64(1), usage.PlacementPolicyUsage.NumDBWithPolicies)
require.Equal(t, uint64(1), usage.PlacementPolicyUsage.NumTableWithPolicies)
require.Equal(t, uint64(1), usage.PlacementPolicyUsage.NumPartitionWithExplicitPolicies)
}
func TestAutoCapture(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.False(t, usage.AutoCapture)
tk.MustExec("SET GLOBAL tidb_capture_plan_baselines = on")
defer func() {
tk.MustExec("SET GLOBAL tidb_capture_plan_baselines = off")
}()
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.True(t, usage.AutoCapture)
}
func TestClusterIndexUsageInfo(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1(a int key clustered);")
tk.MustExec("create table t2(a int);")
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.NotNil(t, usage.ClusterIndex)
require.Equal(t, uint64(1), usage.NewClusterIndex.NumClusteredTables)
require.Equal(t, uint64(2), usage.NewClusterIndex.NumTotalTables)
}
func TestNonTransactionalUsage(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(0), usage.NonTransactionalUsage.DeleteCount)
tk.MustExec("create table t(a int);")
tk.MustExec("batch limit 1 delete from t")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(1), usage.NonTransactionalUsage.DeleteCount)
}
func TestGlobalKillUsageInfo(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.True(t, usage.GlobalKill)
originCfg := config.GetGlobalConfig()
newCfg := *originCfg
newCfg.EnableGlobalKill = false
config.StoreGlobalConfig(&newCfg)
defer func() {
config.StoreGlobalConfig(originCfg)
}()
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.False(t, usage.GlobalKill)
}
func TestPagingUsageInfo(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.True(t, usage.EnablePaging == variable.DefTiDBEnablePaging)
tk.Session().GetSessionVars().EnablePaging = false
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.False(t, usage.EnablePaging)
}
func TestCostModelVer2UsageInfo(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.False(t, usage.EnableCostModelVer2)
tk.Session().GetSessionVars().CostModelVersion = 2
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.True(t, usage.EnableCostModelVer2)
}
func TestTxnSavepointUsageInfo(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("savepoint sp1")
tk.MustExec("savepoint sp2")
txnUsage := telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(2), txnUsage.SavepointCounter)
tk.MustExec("savepoint sp3")
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(3), txnUsage.SavepointCounter)
telemetry.PostSavepointCount()
tk.MustExec("savepoint sp1")
txnUsage = telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(1), txnUsage.SavepointCounter)
}
func TestLazyPessimisticUniqueCheck(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)
tk.MustExec("use test")
usage := telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(0), usage.LazyUniqueCheckSetCounter)
tk2.MustExec("set @@tidb_constraint_check_in_place_pessimistic = 0")
tk2.MustExec("set @@tidb_constraint_check_in_place_pessimistic = 0")
usage = telemetry.GetTxnUsageInfo(tk.Session())
require.Equal(t, int64(2), usage.LazyUniqueCheckSetCounter)
}