@@ -19,6 +19,7 @@ import (
19
19
"fmt"
20
20
"strconv"
21
21
"strings"
22
+ "sync/atomic"
22
23
"testing"
23
24
"time"
24
25
@@ -27,15 +28,19 @@ import (
27
28
"github.com/pingcap/failpoint"
28
29
"github.com/pingcap/tidb/br/pkg/storage"
29
30
"github.com/pingcap/tidb/pkg/config"
31
+ "github.com/pingcap/tidb/pkg/ddl"
30
32
"github.com/pingcap/tidb/pkg/ddl/util/callback"
33
+ "github.com/pingcap/tidb/pkg/disttask/framework/proto"
31
34
"github.com/pingcap/tidb/pkg/disttask/framework/scheduler"
35
+ "github.com/pingcap/tidb/pkg/disttask/framework/taskexecutor"
32
36
"github.com/pingcap/tidb/pkg/kv"
33
37
"github.com/pingcap/tidb/pkg/lightning/backend/external"
34
38
"github.com/pingcap/tidb/pkg/parser/model"
35
39
"github.com/pingcap/tidb/pkg/sessionctx/variable"
36
40
"github.com/pingcap/tidb/pkg/store/helper"
37
41
"github.com/pingcap/tidb/pkg/tablecodec"
38
42
"github.com/pingcap/tidb/pkg/testkit"
43
+ "github.com/pingcap/tidb/pkg/testkit/testfailpoint"
39
44
"github.com/pingcap/tidb/pkg/types"
40
45
"github.com/pingcap/tidb/tests/realtikvtest"
41
46
"github.com/stretchr/testify/require"
@@ -261,16 +266,104 @@ func TestGlobalSortDuplicateErrMsg(t *testing.T) {
261
266
tk .MustExec (`set @@global.tidb_ddl_enable_fast_reorg = 1;` )
262
267
tk .MustExec ("set @@global.tidb_enable_dist_task = 1;" )
263
268
tk .MustExec (fmt .Sprintf (`set @@global.tidb_cloud_storage_uri = "%s"` , cloudStorageURI ))
264
- defer func () {
269
+ atomic .StoreUint32 (& ddl .EnableSplitTableRegion , 1 )
270
+ tk .MustExec ("set @@global.tidb_scatter_region = on" )
271
+ t .Cleanup (func () {
265
272
tk .MustExec ("set @@global.tidb_enable_dist_task = 0;" )
266
273
variable .CloudStorageURI .Store ("" )
267
- }()
274
+ atomic .StoreUint32 (& ddl .EnableSplitTableRegion , 0 )
275
+ tk .MustExec ("set @@global.tidb_scatter_region = default" )
276
+ })
277
+ testfailpoint .Enable (t , "github.com/pingcap/tidb/pkg/disttask/framework/taskexecutor/collectTaskError" , "return(true)" )
278
+ testfailpoint .Enable (t , "github.com/pingcap/tidb/pkg/ddl/mockRegionBatch" , `return(1)` )
279
+
280
+ testcases := []struct {
281
+ caseName string
282
+ createTableSQL string
283
+ splitTableSQL string
284
+ initDataSQL string
285
+ addUniqueKeySQL string
286
+ errMsg string
287
+ }{
288
+ {
289
+ "int index" ,
290
+ "create table t (a int, b int, c int);" ,
291
+ "" ,
292
+ "insert into t values (1, 1, 1), (2, 1, 2);" ,
293
+ "alter table t add unique index idx(b);" ,
294
+ "Duplicate entry '1' for key 't.idx" ,
295
+ },
296
+ {
297
+ "int index on multi regions" ,
298
+ "create table t (a int primary key, b int);" ,
299
+ "split table t between (0) and (4000) regions 4;" ,
300
+ "insert into t values (1, 1), (1001, 1), (2001, 2001), (4001, 1);" ,
301
+ "alter table t add unique index idx(b);" ,
302
+ "[kv:1062]Duplicate entry '1' for key 't.idx'" ,
303
+ },
304
+ {
305
+ "varchar index" ,
306
+ "create table t (id int, data varchar(255));" ,
307
+ "" ,
308
+ "insert into t values (1, '1'), (2, '1');" ,
309
+ "alter table t add unique index i(data);" ,
310
+ "[kv:1062]Duplicate entry '1' for key 't.i'" ,
311
+ },
312
+ {
313
+ "combined index" ,
314
+ "create table t (id int, data varchar(255));" ,
315
+ "" ,
316
+ "insert into t values (1, '1'), (1, '1');" ,
317
+ "alter table t add unique index i(id, data);" ,
318
+ "[kv:1062]Duplicate entry '1-1' for key 't.i'" ,
319
+ },
320
+ {
321
+ "multi value index" ,
322
+ "create table t (id int, data json);" ,
323
+ "" ,
324
+ `insert into t values (1, '{"code":[1,1]}'), (2, '{"code":[1,1]}');` ,
325
+ "alter table t add unique index zips( (CAST(data->'$.code' AS UNSIGNED ARRAY)));" ,
326
+ "Duplicate entry '1' for key 't.zips" ,
327
+ },
328
+ {
329
+ "global index" ,
330
+ "create table t (k int, c int) partition by list (k) (partition odd values in (1,3,5,7,9), partition even values in (2,4,6,8,10));" ,
331
+ "" ,
332
+ "insert into t values (1, 1), (2, 1)" ,
333
+ "alter table t add unique index i(c) global" ,
334
+ "[kv:1062]Duplicate entry '1' for key 't.i'" ,
335
+ },
336
+ }
268
337
269
- tk .MustExec ("create table t (a int, b int, c int);" )
270
- tk .MustExec ("insert into t values (1, 1, 1);" )
271
- tk .MustExec ("insert into t values (2, 1, 2);" )
338
+ checkSubtaskErr := func (t * testing.T ) {
339
+ errorSubtask := taskexecutor .GetErrorSubtask4Test .Swap (nil )
340
+ require .NotEmpty (t , errorSubtask )
341
+ require .Equal (t , proto .BackfillStepWriteAndIngest , errorSubtask .Step )
342
+ }
343
+
344
+ for _ , tc := range testcases {
345
+ t .Run (tc .caseName , func (tt * testing.T ) {
346
+ // init
347
+ taskexecutor .GetErrorSubtask4Test .Store (nil )
348
+ tk .MustExec (tc .createTableSQL )
349
+ tk .MustExec (tc .initDataSQL )
350
+ tt .Cleanup (func () {
351
+ tk .MustExec ("drop table if exists t" )
352
+ })
353
+
354
+ // pre-check
355
+ if len (tc .splitTableSQL ) > 0 {
356
+ tk .MustQuery (tc .splitTableSQL ).Check (testkit .Rows ("3 1" ))
357
+ }
358
+ if strings .Contains (tc .createTableSQL , "partition" ) {
359
+ rs := tk .MustQuery ("show table t regions" )
360
+ require .Len (tt , rs .Rows (), 2 )
361
+ }
272
362
273
- tk .MustGetErrMsg ("alter table t add unique index idx(b);" , "[kv:1062]Duplicate entry '1' for key 't.idx'" )
363
+ tk .MustContainErrMsg (tc .addUniqueKeySQL , tc .errMsg )
364
+ checkSubtaskErr (tt )
365
+ })
366
+ }
274
367
}
275
368
276
369
func TestIngestUseGivenTS (t * testing.T ) {
0 commit comments