Skip to content

Commit 919cfb0

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 f07d030 commit 919cfb0

File tree

10 files changed

+675
-199
lines changed

10 files changed

+675
-199
lines changed

pkg/ddl/delete_range.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,24 @@ func insertJobIntoDeleteRangeTable(ctx context.Context, wrapper DelRangeExecWrap
327327
// always delete the table range, even when it's a partitioned table where
328328
// it may contain global index regions.
329329
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, []int64{tableID}, ea, "truncate table: table ID"))
330-
case model.ActionDropTablePartition, model.ActionReorganizePartition,
331-
model.ActionRemovePartitioning, model.ActionAlterTablePartitioning:
330+
case model.ActionDropTablePartition:
332331
args, err := model.GetFinishedTablePartitionArgs(job)
333332
if err != nil {
334333
return errors.Trace(err)
335334
}
336-
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPhysicalTblIDs, ea, "reorganize/drop partition: physical table ID(s)"))
335+
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPhysicalTblIDs, ea, "drop partition: physical table ID(s)"))
336+
case model.ActionReorganizePartition, model.ActionRemovePartitioning, model.ActionAlterTablePartitioning:
337+
// Delete dropped partitions, as well as replaced global indexes.
338+
args, err := model.GetFinishedTablePartitionArgs(job)
339+
if err != nil {
340+
return errors.Trace(err)
341+
}
342+
for _, idx := range args.OldGlobalIndexes {
343+
if err := doBatchDeleteIndiceRange(ctx, wrapper, job.ID, idx.TableID, []int64{idx.IndexID}, ea, "reorganize partition, replaced global indexes"); err != nil {
344+
return errors.Trace(err)
345+
}
346+
}
347+
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPhysicalTblIDs, ea, "reorganize partition: physical table ID(s)"))
337348
case model.ActionTruncateTablePartition:
338349
args, err := model.GetTruncateTableArgs(job)
339350
if err != nil {

pkg/ddl/partition.go

Lines changed: 174 additions & 79 deletions
Large diffs are not rendered by default.

pkg/ddl/sanity_check.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (e *executor) checkDeleteRangeCnt(job *model.Job) {
4646
panic(err)
4747
}
4848
if actualCnt != expectedCnt {
49-
panic(fmt.Sprintf("expect delete range count %d, actual count %d", expectedCnt, actualCnt))
49+
panic(fmt.Sprintf("expect delete range count %d, actual count %d for job type '%s'", expectedCnt, actualCnt, job.Type.String()))
5050
}
5151
}
5252

@@ -110,7 +110,7 @@ func expectedDeleteRangeCnt(ctx delRangeCntCtx, job *model.Job) (int, error) {
110110
if err != nil {
111111
return 0, errors.Trace(err)
112112
}
113-
return len(args.OldPhysicalTblIDs), nil
113+
return len(args.OldPhysicalTblIDs) + len(args.OldGlobalIndexes), nil
114114
case model.ActionAddIndex, model.ActionAddPrimaryKey:
115115
args, err := model.GetFinishedModifyIndexArgs(job)
116116
if err != nil {

pkg/ddl/tests/partition/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ go_test(
3030
"//pkg/sessionctx",
3131
"//pkg/sessionctx/variable",
3232
"//pkg/sessiontxn",
33+
"//pkg/store/gcworker",
3334
"//pkg/store/mockstore",
3435
"//pkg/table",
3536
"//pkg/table/tables",

pkg/ddl/tests/partition/db_partition_test.go

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,14 +1336,32 @@ func TestGlobalIndexInsertInDropPartition(t *testing.T) {
13361336
partition p3 values less than (30)
13371337
);`)
13381338
tk.MustExec("alter table test_global add unique index idx_b (b) global")
1339-
tk.MustExec("insert into test_global values (1, 1, 1), (8, 8, 8), (11, 11, 11), (12, 12, 12);")
1339+
tk.MustExec("insert into test_global values (1, 1, 1), (2, 2, 2), (11, 11, 11), (12, 12, 12)")
13401340

1341+
doneMap := make(map[model.SchemaState]struct{})
13411342
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore", func(job *model.Job) {
13421343
assert.Equal(t, model.ActionDropTablePartition, job.Type)
1343-
if job.SchemaState == model.StateDeleteOnly {
1344-
tk2 := testkit.NewTestKit(t, store)
1345-
tk2.MustExec("use test")
1346-
tk2.MustExec("insert into test_global values (9, 9, 9)")
1344+
if _, ok := doneMap[job.SchemaState]; ok {
1345+
return
1346+
}
1347+
doneMap[job.SchemaState] = struct{}{}
1348+
tk2 := testkit.NewTestKit(t, store)
1349+
tk2.MustExec("use test")
1350+
switch job.SchemaState {
1351+
case model.StatePublic:
1352+
tk2.MustExec("insert into test_global values (3, 3, 3)")
1353+
tk2.MustExec("insert into test_global values (13, 13, 13)")
1354+
case model.StateWriteOnly:
1355+
tk2.MustContainErrMsg("insert into test_global values (4, 4, 4)", "[table:1526]Table has no partition for value matching a partition being dropped, 'p1'")
1356+
tk2.MustExec("insert into test_global values (14, 14, 14)")
1357+
case model.StateDeleteOnly:
1358+
tk2.MustExec("insert into test_global values (5, 5, 5)")
1359+
tk2.MustExec("insert into test_global values (15, 15, 15)")
1360+
case model.StateDeleteReorganization:
1361+
tk2.MustExec("insert into test_global values (6, 6, 6)")
1362+
tk2.MustExec("insert into test_global values (16, 16, 16)")
1363+
default:
1364+
require.Fail(t, "invalid schema state '%s'", job.SchemaState.String())
13471365
}
13481366
})
13491367

@@ -1352,7 +1370,7 @@ func TestGlobalIndexInsertInDropPartition(t *testing.T) {
13521370
tk1.MustExec("alter table test_global drop partition p1")
13531371

13541372
tk.MustExec("analyze table test_global")
1355-
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"))
1373+
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"))
13561374
}
13571375

13581376
func TestGlobalIndexUpdateInDropPartition(t *testing.T) {
@@ -3168,10 +3186,10 @@ func TestRemovePartitioningAutoIDs(t *testing.T) {
31683186
tk3.MustExec(`COMMIT`)
31693187
tk3.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
31703188
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
3171-
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "30 29 9"))
3189+
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "29 28 9"))
31723190
tk2.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
31733191
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
3174-
"19 18 4", "23 22 6", "27 26 8", "32 31 10"))
3192+
"19 18 4", "23 22 6", "27 26 8", "31 30 10"))
31753193

31763194
waitFor(4, "t", "write reorganization")
31773195
tk3.MustExec(`BEGIN`)
@@ -3181,30 +3199,66 @@ func TestRemovePartitioningAutoIDs(t *testing.T) {
31813199
tk3.MustExec(`insert into t values (null, 23)`)
31823200
tk2.MustExec(`COMMIT`)
31833201

3184-
/*
3185-
// Currently there is an duplicate entry issue, so it will rollback in WriteReorganization
3186-
// instead of continuing.
3187-
waitFor(4, "t", "delete reorganization")
3188-
tk2.MustExec(`BEGIN`)
3189-
tk2.MustExec(`insert into t values (null, 24)`)
3202+
waitFor(4, "t", "delete reorganization")
3203+
tk2.MustExec(`BEGIN`)
3204+
tk2.MustExec(`insert into t values (null, 24)`)
31903205

3191-
tk3.MustExec(`insert into t values (null, 25)`)
3192-
tk2.MustExec(`insert into t values (null, 26)`)
3193-
*/
3206+
tk3.MustExec(`insert into t values (null, 25)`)
3207+
tk2.MustExec(`insert into t values (null, 26)`)
31943208
tk3.MustExec(`COMMIT`)
3209+
tk2.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
3210+
"27 26 8",
3211+
"30012 12 12",
3212+
"30013 18 4",
3213+
"30014 24 7",
3214+
"30015 16 18",
3215+
"30016 22 6",
3216+
"30017 28 9",
3217+
"30018 11 11",
3218+
"30019 2 2",
3219+
"30020 20 5",
3220+
"31 30 10",
3221+
"35 34 22",
3222+
"39 38 24",
3223+
"43 42 26"))
31953224
tk3.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
3196-
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
3197-
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "27 26 8", "30 29 9",
3198-
"32 31 10", "35 34 21", "38 37 22", "41 40 23"))
3199-
3200-
//waitFor(4, "t", "public")
3201-
//tk2.MustExec(`commit`)
3202-
// TODO: Investigate and fix, but it is also related to https://github.com/pingcap/tidb/issues/46904
3203-
require.ErrorContains(t, <-alterChan, "[kv:1062]Duplicate entry '31' for key 't.PRIMARY'")
3225+
"27 26 8",
3226+
"30012 12 12",
3227+
"30013 18 4",
3228+
"30014 24 7",
3229+
"30015 16 18",
3230+
"30016 22 6",
3231+
"30017 28 9",
3232+
"30018 11 11",
3233+
"30019 2 2",
3234+
"30020 20 5",
3235+
"31 30 10",
3236+
"33 32 21",
3237+
"35 34 22",
3238+
"37 36 23",
3239+
"41 40 25"))
3240+
3241+
waitFor(4, "t", "public")
3242+
tk2.MustExec(`commit`)
32043243
tk3.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
3205-
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
3206-
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "27 26 8", "30 29 9",
3207-
"32 31 10", "35 34 21", "38 37 22", "41 40 23"))
3244+
"27 26 8",
3245+
"30012 12 12",
3246+
"30013 18 4",
3247+
"30014 24 7",
3248+
"30015 16 18",
3249+
"30016 22 6",
3250+
"30017 28 9",
3251+
"30018 11 11",
3252+
"30019 2 2",
3253+
"30020 20 5",
3254+
"31 30 10",
3255+
"33 32 21",
3256+
"35 34 22",
3257+
"37 36 23",
3258+
"39 38 24",
3259+
"41 40 25",
3260+
"43 42 26"))
3261+
require.NoError(t, <-alterChan)
32083262
}
32093263

32103264
func TestAlterLastIntervalPartition(t *testing.T) {

0 commit comments

Comments
 (0)