@@ -19,12 +19,66 @@ import (
19
19
"testing"
20
20
21
21
pmodel "github.com/pingcap/tidb/pkg/parser/model"
22
+ "github.com/pingcap/tidb/pkg/sessionctx"
22
23
"github.com/pingcap/tidb/pkg/statistics"
23
24
"github.com/pingcap/tidb/pkg/statistics/handle/autoanalyze/refresher"
25
+ "github.com/pingcap/tidb/pkg/statistics/handle/util"
24
26
"github.com/pingcap/tidb/pkg/testkit"
25
27
"github.com/stretchr/testify/require"
26
28
)
27
29
30
+ func TestChangePruneMode (t * testing.T ) {
31
+ statistics .AutoAnalyzeMinCnt = 0
32
+ defer func () {
33
+ statistics .AutoAnalyzeMinCnt = 1000
34
+ }()
35
+
36
+ store , dom := testkit .CreateMockStoreAndDomain (t )
37
+ handle := dom .StatsHandle ()
38
+ tk := testkit .NewTestKit (t , store )
39
+ tk .MustExec ("use test" )
40
+ tk .MustExec ("create table t1 (a int, b int, index idx(a)) partition by range (a) (partition p0 values less than (10), partition p1 values less than (140))" )
41
+ tk .MustExec ("insert into t1 values (0, 0)" )
42
+ require .NoError (t , handle .DumpStatsDeltaToKV (true ))
43
+ require .NoError (t , handle .Update (context .Background (), dom .InfoSchema ()))
44
+ tk .MustExec ("analyze table t1" )
45
+ r := refresher .NewRefresher (handle , dom .SysProcTracker (), dom .DDLNotifier ())
46
+ defer r .Close ()
47
+
48
+ // Insert more data to each partition.
49
+ tk .MustExec ("insert into t1 values (1, 1), (11, 11)" )
50
+ require .NoError (t , handle .DumpStatsDeltaToKV (true ))
51
+ require .NoError (t , handle .Update (context .Background (), dom .InfoSchema ()))
52
+
53
+ // Two jobs are added because the prune mode is static.
54
+ tk .MustExec ("set global tidb_partition_prune_mode = 'static'" )
55
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
56
+ require .True (t , handle .HandleAutoAnalyze ())
57
+ return nil
58
+ }))
59
+ r .WaitAutoAnalyzeFinishedForTest ()
60
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
61
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
62
+ return nil
63
+ }))
64
+ r .WaitAutoAnalyzeFinishedForTest ()
65
+ require .Equal (t , 0 , r .Len ())
66
+
67
+ // Insert more data to each partition.
68
+ tk .MustExec ("insert into t1 values (2, 2), (3, 3), (4, 4), (12, 12), (13, 13), (14, 14)" )
69
+ require .NoError (t , handle .DumpStatsDeltaToKV (true ))
70
+ require .NoError (t , handle .Update (context .Background (), dom .InfoSchema ()))
71
+
72
+ // One job is added because the prune mode is dynamic.
73
+ tk .MustExec ("set global tidb_partition_prune_mode = 'dynamic'" )
74
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
75
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
76
+ return nil
77
+ }))
78
+ r .WaitAutoAnalyzeFinishedForTest ()
79
+ require .Equal (t , 0 , r .Len ())
80
+ }
81
+
28
82
func TestSkipAnalyzeTableWhenAutoAnalyzeRatioIsZero (t * testing.T ) {
29
83
statistics .AutoAnalyzeMinCnt = 0
30
84
defer func () {
@@ -67,12 +121,19 @@ func TestSkipAnalyzeTableWhenAutoAnalyzeRatioIsZero(t *testing.T) {
67
121
r := refresher .NewRefresher (handle , sysProcTracker , dom .DDLNotifier ())
68
122
defer r .Close ()
69
123
// No jobs are added.
70
- require .False (t , r .AnalyzeHighestPriorityTables ())
124
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
125
+ require .False (t , r .AnalyzeHighestPriorityTables (sctx ))
126
+ return nil
127
+ }))
71
128
require .Equal (t , 0 , r .Len ())
72
129
// Enable the auto analyze.
73
130
tk .MustExec ("set global tidb_auto_analyze_ratio = 0.2" )
74
131
// Jobs are added.
75
- require .True (t , r .AnalyzeHighestPriorityTables ())
132
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
133
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
134
+ return nil
135
+ }))
136
+ require .Equal (t , 0 , r .Len ())
76
137
}
77
138
78
139
func TestIgnoreNilOrPseudoStatsOfPartitionedTable (t * testing.T ) {
@@ -92,7 +153,10 @@ func TestIgnoreNilOrPseudoStatsOfPartitionedTable(t *testing.T) {
92
153
sysProcTracker := dom .SysProcTracker ()
93
154
r := refresher .NewRefresher (handle , sysProcTracker , dom .DDLNotifier ())
94
155
defer r .Close ()
95
- require .False (t , r .AnalyzeHighestPriorityTables ())
156
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
157
+ require .False (t , r .AnalyzeHighestPriorityTables (sctx ))
158
+ return nil
159
+ }))
96
160
require .Equal (t , 0 , r .Len (), "No jobs are added because table stats are nil" )
97
161
}
98
162
@@ -113,7 +177,10 @@ func TestIgnoreNilOrPseudoStatsOfNonPartitionedTable(t *testing.T) {
113
177
sysProcTracker := dom .SysProcTracker ()
114
178
r := refresher .NewRefresher (handle , sysProcTracker , dom .DDLNotifier ())
115
179
defer r .Close ()
116
- require .False (t , r .AnalyzeHighestPriorityTables ())
180
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
181
+ require .False (t , r .AnalyzeHighestPriorityTables (sctx ))
182
+ return nil
183
+ }))
117
184
require .Equal (t , 0 , r .Len (), "No jobs are added because table stats are nil" )
118
185
}
119
186
@@ -158,7 +225,10 @@ func TestIgnoreTinyTable(t *testing.T) {
158
225
sysProcTracker := dom .SysProcTracker ()
159
226
r := refresher .NewRefresher (handle , sysProcTracker , dom .DDLNotifier ())
160
227
defer r .Close ()
161
- require .True (t , r .AnalyzeHighestPriorityTables ())
228
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
229
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
230
+ return nil
231
+ }))
162
232
require .Equal (t , 0 , r .Len (), "Only t1 is added to the job queue, because t2 is a tiny table(not enough data)" )
163
233
}
164
234
@@ -194,7 +264,10 @@ func TestAnalyzeHighestPriorityTables(t *testing.T) {
194
264
r := refresher .NewRefresher (handle , sysProcTracker , dom .DDLNotifier ())
195
265
defer r .Close ()
196
266
// Analyze t1 first.
197
- require .True (t , r .AnalyzeHighestPriorityTables ())
267
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
268
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
269
+ return nil
270
+ }))
198
271
r .WaitAutoAnalyzeFinishedForTest ()
199
272
require .NoError (t , handle .DumpStatsDeltaToKV (true ))
200
273
require .NoError (t , handle .Update (context .Background (), dom .InfoSchema ()))
@@ -212,7 +285,10 @@ func TestAnalyzeHighestPriorityTables(t *testing.T) {
212
285
tblStats2 := handle .GetPartitionStats (tbl2 .Meta (), pid2 )
213
286
require .Equal (t , int64 (6 ), tblStats2 .ModifyCount )
214
287
// Do one more round.
215
- require .True (t , r .AnalyzeHighestPriorityTables ())
288
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
289
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
290
+ return nil
291
+ }))
216
292
r .WaitAutoAnalyzeFinishedForTest ()
217
293
// t2 is analyzed.
218
294
pid2 = tbl2 .Meta ().GetPartitionInfo ().Definitions [1 ].ID
@@ -257,7 +333,10 @@ func TestAnalyzeHighestPriorityTablesConcurrently(t *testing.T) {
257
333
r := refresher .NewRefresher (handle , sysProcTracker , dom .DDLNotifier ())
258
334
defer r .Close ()
259
335
// Analyze tables concurrently.
260
- require .True (t , r .AnalyzeHighestPriorityTables ())
336
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
337
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
338
+ return nil
339
+ }))
261
340
r .WaitAutoAnalyzeFinishedForTest ()
262
341
require .NoError (t , handle .DumpStatsDeltaToKV (true ))
263
342
require .NoError (t , handle .Update (context .Background (), dom .InfoSchema ()))
@@ -284,7 +363,10 @@ func TestAnalyzeHighestPriorityTablesConcurrently(t *testing.T) {
284
363
require .Equal (t , int64 (4 ), tblStats3 .ModifyCount )
285
364
286
365
// Do one more round to analyze t3.
287
- require .True (t , r .AnalyzeHighestPriorityTables ())
366
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
367
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
368
+ return nil
369
+ }))
288
370
r .WaitAutoAnalyzeFinishedForTest ()
289
371
require .NoError (t , handle .DumpStatsDeltaToKV (true ))
290
372
require .NoError (t , handle .Update (context .Background (), dom .InfoSchema ()))
@@ -322,7 +404,10 @@ func TestAnalyzeHighestPriorityTablesWithFailedAnalysis(t *testing.T) {
322
404
r := refresher .NewRefresher (handle , sysProcTracker , dom .DDLNotifier ())
323
405
defer r .Close ()
324
406
325
- r .AnalyzeHighestPriorityTables ()
407
+ require .NoError (t , util .CallWithSCtx (handle .SPool (), func (sctx sessionctx.Context ) error {
408
+ require .True (t , r .AnalyzeHighestPriorityTables (sctx ))
409
+ return nil
410
+ }))
326
411
r .WaitAutoAnalyzeFinishedForTest ()
327
412
328
413
is := dom .InfoSchema ()
0 commit comments