Skip to content

Commit 8e8da6d

Browse files
mjonssti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#57114
Signed-off-by: ti-chi-bot <[email protected]>
1 parent b298e21 commit 8e8da6d

File tree

10 files changed

+4681
-67
lines changed

10 files changed

+4681
-67
lines changed

pkg/ddl/delete_range.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,36 @@ func insertJobIntoDeleteRangeTable(ctx context.Context, sctx sessionctx.Context,
332332
return errors.Trace(err)
333333
}
334334
}
335+
<<<<<<< HEAD
336+
=======
337+
// always delete the table range, even when it's a partitioned table where
338+
// it may contain global index regions.
339+
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, []int64{tableID}, ea, "truncate table: table ID"))
340+
case model.ActionDropTablePartition:
341+
args, err := model.GetFinishedTablePartitionArgs(job)
342+
if err != nil {
343+
return errors.Trace(err)
344+
}
345+
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPhysicalTblIDs, ea, "drop partition: physical table ID(s)"))
346+
case model.ActionReorganizePartition, model.ActionRemovePartitioning, model.ActionAlterTablePartitioning:
347+
// Delete dropped partitions, as well as replaced global indexes.
348+
args, err := model.GetFinishedTablePartitionArgs(job)
349+
if err != nil {
350+
return errors.Trace(err)
351+
}
352+
for _, idx := range args.OldGlobalIndexes {
353+
if err := doBatchDeleteIndiceRange(ctx, wrapper, job.ID, idx.TableID, []int64{idx.IndexID}, ea, "reorganize partition, replaced global indexes"); err != nil {
354+
return errors.Trace(err)
355+
}
356+
}
357+
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPhysicalTblIDs, ea, "reorganize partition: physical table ID(s)"))
358+
case model.ActionTruncateTablePartition:
359+
args, err := model.GetTruncateTableArgs(job)
360+
if err != nil {
361+
return errors.Trace(err)
362+
}
363+
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPartitionIDs, ea, "truncate partition: physical table ID(s)"))
364+
>>>>>>> b6025b97877 (*: Reorg partition fix delete ranges and handling non-clustered tables with concurrent DML (#57114))
335365
// ActionAddIndex, ActionAddPrimaryKey needs do it, because it needs to be rolled back when it's canceled.
336366
case model.ActionAddIndex, model.ActionAddPrimaryKey:
337367
allIndexIDs := make([]int64, 1)

pkg/ddl/partition.go

Lines changed: 344 additions & 42 deletions
Large diffs are not rendered by default.

pkg/ddl/sanity_check.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (d *ddl) checkDeleteRangeCnt(job *model.Job) {
4848
panic(err)
4949
}
5050
if actualCnt != expectedCnt {
51-
panic(fmt.Sprintf("expect delete range count %d, actual count %d", expectedCnt, actualCnt))
51+
panic(fmt.Sprintf("expect delete range count %d, actual count %d for job type '%s'", expectedCnt, actualCnt, job.Type.String()))
5252
}
5353
}
5454

@@ -106,7 +106,21 @@ func expectedDeleteRangeCnt(ctx delRangeCntCtx, job *model.Job) (int, error) {
106106
if err := job.DecodeArgs(&physicalTableIDs); err != nil {
107107
return 0, errors.Trace(err)
108108
}
109+
<<<<<<< HEAD
109110
return len(physicalTableIDs), nil
111+
=======
112+
if job.Type == model.ActionTruncateTable {
113+
return len(args.OldPartitionIDs) + 1, nil
114+
}
115+
return len(args.OldPartitionIDs), nil
116+
case model.ActionDropTablePartition, model.ActionReorganizePartition,
117+
model.ActionRemovePartitioning, model.ActionAlterTablePartitioning:
118+
args, err := model.GetFinishedTablePartitionArgs(job)
119+
if err != nil {
120+
return 0, errors.Trace(err)
121+
}
122+
return len(args.OldPhysicalTblIDs) + len(args.OldGlobalIndexes), nil
123+
>>>>>>> b6025b97877 (*: Reorg partition fix delete ranges and handling non-clustered tables with concurrent DML (#57114))
110124
case model.ActionAddIndex, model.ActionAddPrimaryKey:
111125
indexID := make([]int64, 1)
112126
ifExists := make([]bool, 1)

pkg/ddl/tests/partition/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ go_test(
2525
"//pkg/sessionctx",
2626
"//pkg/sessionctx/variable",
2727
"//pkg/sessiontxn",
28+
"//pkg/store/gcworker",
2829
"//pkg/store/mockstore",
2930
"//pkg/table",
3031
"//pkg/table/tables",

pkg/ddl/tests/partition/db_partition_test.go

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,16 +1401,41 @@ func TestGlobalIndexInsertInDropPartition(t *testing.T) {
14011401
partition p2 values less than (20),
14021402
partition p3 values less than (30)
14031403
);`)
1404+
<<<<<<< HEAD
14041405
tk.MustExec("alter table test_global add unique index idx_b (b);")
14051406
tk.MustExec("insert into test_global values (1, 1, 1), (8, 8, 8), (11, 11, 11), (12, 12, 12);")
14061407

14071408
hook := &callback.TestDDLCallback{Do: dom}
14081409
hook.OnJobRunBeforeExported = func(job *model.Job) {
1410+
=======
1411+
tk.MustExec("alter table test_global add unique index idx_b (b) global")
1412+
tk.MustExec("insert into test_global values (1, 1, 1), (2, 2, 2), (11, 11, 11), (12, 12, 12)")
1413+
1414+
doneMap := make(map[model.SchemaState]struct{})
1415+
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore", func(job *model.Job) {
1416+
>>>>>>> b6025b97877 (*: Reorg partition fix delete ranges and handling non-clustered tables with concurrent DML (#57114))
14091417
assert.Equal(t, model.ActionDropTablePartition, job.Type)
1410-
if job.SchemaState == model.StateDeleteOnly {
1411-
tk2 := testkit.NewTestKit(t, store)
1412-
tk2.MustExec("use test")
1413-
tk2.MustExec("insert into test_global values (9, 9, 9)")
1418+
if _, ok := doneMap[job.SchemaState]; ok {
1419+
return
1420+
}
1421+
doneMap[job.SchemaState] = struct{}{}
1422+
tk2 := testkit.NewTestKit(t, store)
1423+
tk2.MustExec("use test")
1424+
switch job.SchemaState {
1425+
case model.StatePublic:
1426+
tk2.MustExec("insert into test_global values (3, 3, 3)")
1427+
tk2.MustExec("insert into test_global values (13, 13, 13)")
1428+
case model.StateWriteOnly:
1429+
tk2.MustContainErrMsg("insert into test_global values (4, 4, 4)", "[table:1526]Table has no partition for value matching a partition being dropped, 'p1'")
1430+
tk2.MustExec("insert into test_global values (14, 14, 14)")
1431+
case model.StateDeleteOnly:
1432+
tk2.MustExec("insert into test_global values (5, 5, 5)")
1433+
tk2.MustExec("insert into test_global values (15, 15, 15)")
1434+
case model.StateDeleteReorganization:
1435+
tk2.MustExec("insert into test_global values (6, 6, 6)")
1436+
tk2.MustExec("insert into test_global values (16, 16, 16)")
1437+
default:
1438+
require.Fail(t, "invalid schema state '%s'", job.SchemaState.String())
14141439
}
14151440
}
14161441
dom.DDL().SetHook(hook)
@@ -1420,7 +1445,7 @@ func TestGlobalIndexInsertInDropPartition(t *testing.T) {
14201445
tk1.MustExec("alter table test_global drop partition p1")
14211446

14221447
tk.MustExec("analyze table test_global")
1423-
tk.MustQuery("select * from test_global use index(idx_b) order by a").Check(testkit.Rows("9 9 9", "11 11 11", "12 12 12"))
1448+
tk.MustQuery("select * from test_global use index(idx_b) order by a").Check(testkit.Rows("5 5 5", "6 6 6", "11 11 11", "12 12 12", "13 13 13", "14 14 14", "15 15 15", "16 16 16"))
14241449
}
14251450

14261451
func TestUpdateGlobalIndex(t *testing.T) {
@@ -3608,10 +3633,10 @@ func TestRemovePartitioningAutoIDs(t *testing.T) {
36083633
tk3.MustExec(`COMMIT`)
36093634
tk3.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
36103635
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
3611-
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "30 29 9"))
3636+
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "29 28 9"))
36123637
tk2.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
36133638
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
3614-
"19 18 4", "23 22 6", "27 26 8", "32 31 10"))
3639+
"19 18 4", "23 22 6", "27 26 8", "31 30 10"))
36153640

36163641
waitFor(4, "t", "write reorganization")
36173642
tk3.MustExec(`BEGIN`)
@@ -3621,28 +3646,73 @@ func TestRemovePartitioningAutoIDs(t *testing.T) {
36213646
tk3.MustExec(`insert into t values (null, 23)`)
36223647
tk2.MustExec(`COMMIT`)
36233648

3649+
<<<<<<< HEAD
36243650
/*
36253651
waitFor(4, "t", "delete reorganization")
36263652
tk2.MustExec(`BEGIN`)
36273653
tk2.MustExec(`insert into t values (null, 24)`)
3654+
=======
3655+
waitFor(4, "t", "delete reorganization")
3656+
tk2.MustExec(`BEGIN`)
3657+
tk2.MustExec(`insert into t values (null, 24)`)
3658+
>>>>>>> b6025b97877 (*: Reorg partition fix delete ranges and handling non-clustered tables with concurrent DML (#57114))
36283659

3629-
tk3.MustExec(`insert into t values (null, 25)`)
3630-
tk2.MustExec(`insert into t values (null, 26)`)
3631-
*/
3660+
tk3.MustExec(`insert into t values (null, 25)`)
3661+
tk2.MustExec(`insert into t values (null, 26)`)
36323662
tk3.MustExec(`COMMIT`)
3663+
tk2.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
3664+
"27 26 8",
3665+
"30012 12 12",
3666+
"30013 18 4",
3667+
"30014 24 7",
3668+
"30015 16 18",
3669+
"30016 22 6",
3670+
"30017 28 9",
3671+
"30018 11 11",
3672+
"30019 2 2",
3673+
"30020 20 5",
3674+
"31 30 10",
3675+
"35 34 22",
3676+
"39 38 24",
3677+
"43 42 26"))
36333678
tk3.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
3634-
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
3635-
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "27 26 8", "30 29 9",
3636-
"32 31 10", "35 34 21", "38 37 22", "41 40 23"))
3637-
3638-
//waitFor(4, "t", "public")
3639-
//tk2.MustExec(`commit`)
3640-
// TODO: Investigate and fix, but it is also related to https://github.com/pingcap/tidb/issues/46904
3641-
require.ErrorContains(t, <-alterChan, "[kv:1062]Duplicate entry '31' for key 't.PRIMARY'")
3679+
"27 26 8",
3680+
"30012 12 12",
3681+
"30013 18 4",
3682+
"30014 24 7",
3683+
"30015 16 18",
3684+
"30016 22 6",
3685+
"30017 28 9",
3686+
"30018 11 11",
3687+
"30019 2 2",
3688+
"30020 20 5",
3689+
"31 30 10",
3690+
"33 32 21",
3691+
"35 34 22",
3692+
"37 36 23",
3693+
"41 40 25"))
3694+
3695+
waitFor(4, "t", "public")
3696+
tk2.MustExec(`commit`)
36423697
tk3.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
3643-
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
3644-
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "27 26 8", "30 29 9",
3645-
"32 31 10", "35 34 21", "38 37 22", "41 40 23"))
3698+
"27 26 8",
3699+
"30012 12 12",
3700+
"30013 18 4",
3701+
"30014 24 7",
3702+
"30015 16 18",
3703+
"30016 22 6",
3704+
"30017 28 9",
3705+
"30018 11 11",
3706+
"30019 2 2",
3707+
"30020 20 5",
3708+
"31 30 10",
3709+
"33 32 21",
3710+
"35 34 22",
3711+
"37 36 23",
3712+
"39 38 24",
3713+
"41 40 25",
3714+
"43 42 26"))
3715+
require.NoError(t, <-alterChan)
36463716
}
36473717

36483718
func TestAlterLastIntervalPartition(t *testing.T) {

0 commit comments

Comments
 (0)