Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Reorg partition fix delete ranges and handling non-clustered tables with concurrent DML #57114

Merged
merged 63 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
6df378f
Better index management during rollback of partitioning DDLs
mjonss Oct 22, 2024
27978e0
Linting
mjonss Oct 22, 2024
8046319
Merge remote-tracking branch 'pingcap/master' into reorg-part-fix-56634
mjonss Oct 22, 2024
10f5f61
Reworked index handling in Reorganize Partition.
mjonss Oct 23, 2024
cc4f5a8
Fixed assertion issues.
mjonss Oct 23, 2024
ee82920
reverted non-needed changes.
mjonss Oct 23, 2024
c45e3e9
Reverted non-needed changes.
mjonss Oct 23, 2024
16d1628
Simplified index management during partitioned table initialization.
mjonss Oct 24, 2024
a45bba6
Improved test and fixed more issues.
mjonss Oct 24, 2024
f68c29d
minor simplification and fixed failpoint leading to panic
mjonss Oct 24, 2024
ff3d980
Added test for verifying cleanup.
mjonss Oct 24, 2024
91fb5f9
Disabled test due to cleanup not fixed.
mjonss Oct 24, 2024
bf5defc
Simplified new partitions index states.
mjonss Oct 24, 2024
6103faf
Skipping global index entries changes.
mjonss Oct 24, 2024
f803d7b
Merge remote-tracking branch 'pingcap/master' into reorg-part-fix-56634
mjonss Oct 28, 2024
526e781
Added extra state in REORGANIZE PARTITION
mjonss Oct 28, 2024
1d92581
Simplified shouldAssert to a skipAssert flag instead.
mjonss Oct 28, 2024
e2df898
Fixed schema version diffs
mjonss Oct 29, 2024
8224d5d
Fixed test failures and updated tests.
mjonss Oct 29, 2024
08fab0b
Merge remote-tracking branch 'pingcap/master' into reorg-part-fix-56634
mjonss Oct 29, 2024
5112320
Merge branch 'reorg-part-fix-56634' into reorg-partition-one-extra-state
mjonss Oct 29, 2024
677ac4a
Linting
mjonss Oct 30, 2024
e3125e2
Merge remote-tracking branch 'pingcap/master' into reorg-part-fix-56634
mjonss Oct 30, 2024
e8b54eb
Merge branch 'reorg-part-fix-56634' into reorg-partition-one-extra-state
mjonss Oct 30, 2024
28f21e4
Merge remote-tracking branch 'pingcap/master' into reorg-part-fix-56634
mjonss Oct 30, 2024
b49c525
Merge branch 'reorg-part-fix-56634' into reorg-partition-one-extra-state
mjonss Oct 30, 2024
d4347d9
Merge remote-tracking branch 'pingcap/master' into reorg-part-fix-56634
mjonss Nov 1, 2024
f21b86e
Removed limitation of global index cannot have all partitioning columns
mjonss Nov 1, 2024
07df994
Merge branch 'reorg-part-fix-56634' into reorg-partition-one-extra-state
mjonss Nov 1, 2024
e6bf5b8
Added DeleteRange of replaced indexes for Reorganize Partition code.
mjonss Nov 2, 2024
03d049e
review comments addressed, simplified if statement.
mjonss Nov 4, 2024
d90bf46
WIP issues with non-clustered table and REORG PARTITION.
mjonss Nov 5, 2024
9a062d2
Fixed line garbage
mjonss Nov 5, 2024
2d7acf0
Working prototype.
mjonss Nov 5, 2024
a90fc2b
Updated test
mjonss Nov 5, 2024
826f3ea
bazel_prepare
mjonss Nov 5, 2024
1e4437b
Updated test comment
mjonss Nov 5, 2024
51ee5e3
Removed debug logs
mjonss Nov 5, 2024
9dad6b6
Cleanup of comments etc.
mjonss Nov 5, 2024
50830ee
Updated integration test.
mjonss Nov 5, 2024
4029e5a
Linting
mjonss Nov 5, 2024
4f1c046
Merge remote-tracking branch 'pingcap/master' into reorg-part-fix-56634
mjonss Nov 5, 2024
83355db
Merge branch 'reorg-part-fix-56634' into reorg-partition-one-extra-state
mjonss Nov 5, 2024
05b13fb
Updated comments and removed dead code.
mjonss Nov 5, 2024
e27a5e4
Merge branch 'reorg-partition-one-extra-state' into reorg-partition-f…
mjonss Nov 5, 2024
dc668fb
Cleanup and comments fixing
mjonss Nov 6, 2024
ff9d10f
TablePartitionArgs was missing the new OldIndexes variable in DDLv1
mjonss Nov 6, 2024
6599fe2
Merge remote-tracking branch 'pingcap/master' into reorg-partition-on…
mjonss Nov 15, 2024
e065bea
Merge branch 'reorg-partition-one-extra-state' into reorg-partition-f…
mjonss Nov 15, 2024
332f235
Fixed test case
mjonss Nov 15, 2024
78c38ec
Merge branch 'reorg-partition-one-extra-state' into reorg-partition-f…
mjonss Nov 15, 2024
3c1d49e
Updated test
mjonss Nov 19, 2024
ff8e6d7
Merge remote-tracking branch 'pingcap/master' into reorg-partition-on…
mjonss Nov 19, 2024
4d23ce7
Merge branch 'reorg-partition-one-extra-state' into reorg-partition-f…
mjonss Nov 19, 2024
707c46b
Updated test
mjonss Nov 19, 2024
f486ac5
Enabled test, which this PR fixes
mjonss Nov 20, 2024
91a9f6a
Merge branch 'reorg-partition-one-extra-state' into reorg-partition-f…
mjonss Nov 20, 2024
e4f718f
Removed line that should have been for failpoint
mjonss Nov 20, 2024
d9ca928
Merge remote-tracking branch 'pingcap/master' into reorg-partition-on…
mjonss Nov 20, 2024
4a88a6d
Merge branch 'reorg-partition-one-extra-state' into reorg-partition-f…
mjonss Nov 21, 2024
9bfc05e
Merge remote-tracking branch 'pingcap/master' into reorg-partition-fi…
mjonss Nov 26, 2024
1e4e433
Review comments
mjonss Nov 27, 2024
7556105
Simplified the expected gc ranges.
mjonss Nov 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pkg/ddl/delete_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,25 @@ func insertJobIntoDeleteRangeTable(ctx context.Context, wrapper DelRangeExecWrap
// always delete the table range, even when it's a partitioned table where
// it may contain global index regions.
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, []int64{tableID}, ea, "truncate table: table ID"))
case model.ActionDropTablePartition, model.ActionReorganizePartition,
case model.ActionDropTablePartition:
args, err := model.GetFinishedTablePartitionArgs(job)
if err != nil {
return errors.Trace(err)
}
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPhysicalTblIDs, ea, "drop partition: physical table ID(s)"))
case model.ActionReorganizePartition,
model.ActionRemovePartitioning, model.ActionAlterTablePartitioning:
mjonss marked this conversation as resolved.
Show resolved Hide resolved
// Delete dropped partitions, as well as replaced global indexes.
args, err := model.GetFinishedTablePartitionArgs(job)
if err != nil {
return errors.Trace(err)
}
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPhysicalTblIDs, ea, "reorganize/drop partition: physical table ID(s)"))
for _, idx := range args.OldIndexes {
if err := doBatchDeleteIndiceRange(ctx, wrapper, job.ID, idx.TableID, []int64{idx.IndexID}, ea, "reorganize partition, replaced global indexes"); err != nil {
return errors.Trace(err)
}
}
return errors.Trace(doBatchDeleteTablesRange(ctx, wrapper, job.ID, args.OldPhysicalTblIDs, ea, "reorganize partition: physical table ID(s)"))
case model.ActionTruncateTablePartition:
args, err := model.GetTruncateTableArgs(job)
if err != nil {
Expand Down
253 changes: 174 additions & 79 deletions pkg/ddl/partition.go

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion pkg/ddl/sanity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ func (e *executor) checkDeleteRangeCnt(job *model.Job) {
panic(err)
}
if actualCnt != expectedCnt {
panic(fmt.Sprintf("expect delete range count %d, actual count %d", expectedCnt, actualCnt))
switch job.Type {
case model.ActionReorganizePartition, model.ActionRemovePartitioning, model.ActionAlterTablePartitioning:
if actualCnt < expectedCnt {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about actualCnt > expectedCnt? Maybe we could improve expectedDeleteRangeCnt function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but since I added tests to verify that no old partitions or indexes contain data after GC, I don't think it is needed to replicate the full logic of cleaning up global indexes etc. Checking that non of the previous indexes and partitions exists in KV store after the DDL should be OK.

Copy link
Contributor

@Defined2014 Defined2014 Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think modify expectedDeleteRangeCnt is better. This looks more integrated. /cc @tangenta

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think modify expectedDeleteRangeCnt is better.

Agree. This is more like a test, it is better to change the test itself instead of the assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will update the logic in expectedDeleteRangeCnt().

panic(fmt.Sprintf("expect delete range count %d, actual count %d for job type '%s'", expectedCnt, actualCnt, job.Type.String()))
}
default:
panic(fmt.Sprintf("expect delete range count %d, actual count %d for job type '%s'", expectedCnt, actualCnt, job.Type.String()))
}
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/ddl/tests/partition/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go_test(
"//pkg/sessionctx",
"//pkg/sessionctx/variable",
"//pkg/sessiontxn",
"//pkg/store/gcworker",
"//pkg/store/mockstore",
"//pkg/table",
"//pkg/table/tables",
Expand Down
110 changes: 82 additions & 28 deletions pkg/ddl/tests/partition/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1336,14 +1336,32 @@ func TestGlobalIndexInsertInDropPartition(t *testing.T) {
partition p3 values less than (30)
);`)
tk.MustExec("alter table test_global add unique index idx_b (b) global")
tk.MustExec("insert into test_global values (1, 1, 1), (8, 8, 8), (11, 11, 11), (12, 12, 12);")
tk.MustExec("insert into test_global values (1, 1, 1), (2, 2, 2), (11, 11, 11), (12, 12, 12)")

doneMap := make(map[model.SchemaState]struct{})
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore", func(job *model.Job) {
assert.Equal(t, model.ActionDropTablePartition, job.Type)
if job.SchemaState == model.StateDeleteOnly {
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("use test")
tk2.MustExec("insert into test_global values (9, 9, 9)")
if _, ok := doneMap[job.SchemaState]; ok {
return
}
doneMap[job.SchemaState] = struct{}{}
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("use test")
switch job.SchemaState {
case model.StatePublic:
tk2.MustExec("insert into test_global values (3, 3, 3)")
tk2.MustExec("insert into test_global values (13, 13, 13)")
case model.StateWriteOnly:
tk2.MustContainErrMsg("insert into test_global values (4, 4, 4)", "[table:1526]Table has no partition for value matching a partition being dropped, 'p1'")
tk2.MustExec("insert into test_global values (14, 14, 14)")
case model.StateDeleteOnly:
tk2.MustExec("insert into test_global values (5, 5, 5)")
tk2.MustExec("insert into test_global values (15, 15, 15)")
case model.StateDeleteReorganization:
tk2.MustExec("insert into test_global values (6, 6, 6)")
tk2.MustExec("insert into test_global values (16, 16, 16)")
default:
require.Fail(t, "invalid schema state '%s'", job.SchemaState.String())
}
})

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

tk.MustExec("analyze table test_global")
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"))
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"))
}

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

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

/*
// Currently there is an duplicate entry issue, so it will rollback in WriteReorganization
// instead of continuing.
waitFor(4, "t", "delete reorganization")
tk2.MustExec(`BEGIN`)
tk2.MustExec(`insert into t values (null, 24)`)
waitFor(4, "t", "delete reorganization")
tk2.MustExec(`BEGIN`)
tk2.MustExec(`insert into t values (null, 24)`)

tk3.MustExec(`insert into t values (null, 25)`)
tk2.MustExec(`insert into t values (null, 26)`)
*/
tk3.MustExec(`insert into t values (null, 25)`)
tk2.MustExec(`insert into t values (null, 26)`)
tk3.MustExec(`COMMIT`)
tk2.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
"27 26 8",
"30012 12 12",
"30013 18 4",
"30014 24 7",
"30015 16 18",
"30016 22 6",
"30017 28 9",
"30018 11 11",
"30019 2 2",
"30020 20 5",
"31 30 10",
"35 34 22",
"39 38 24",
"43 42 26"))
tk3.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "27 26 8", "30 29 9",
"32 31 10", "35 34 21", "38 37 22", "41 40 23"))

//waitFor(4, "t", "public")
//tk2.MustExec(`commit`)
// TODO: Investigate and fix, but it is also related to https://github.com/pingcap/tidb/issues/46904
require.ErrorContains(t, <-alterChan, "[kv:1062]Duplicate entry '31' for key 't.PRIMARY'")
"27 26 8",
"30012 12 12",
"30013 18 4",
"30014 24 7",
"30015 16 18",
"30016 22 6",
"30017 28 9",
"30018 11 11",
"30019 2 2",
"30020 20 5",
"31 30 10",
"33 32 21",
"35 34 22",
"37 36 23",
"41 40 25"))

waitFor(4, "t", "public")
tk2.MustExec(`commit`)
tk3.MustQuery(`select _tidb_rowid, a, b from t`).Sort().Check(testkit.Rows(
"13 11 11", "14 2 2", "15 12 12", "17 16 18",
"19 18 4", "21 20 5", "23 22 6", "25 24 7", "27 26 8", "30 29 9",
"32 31 10", "35 34 21", "38 37 22", "41 40 23"))
"27 26 8",
"30012 12 12",
"30013 18 4",
"30014 24 7",
"30015 16 18",
"30016 22 6",
"30017 28 9",
"30018 11 11",
"30019 2 2",
"30020 20 5",
"31 30 10",
"33 32 21",
"35 34 22",
"37 36 23",
"39 38 24",
"41 40 25",
"43 42 26"))
require.NoError(t, <-alterChan)
}

func TestAlterLastIntervalPartition(t *testing.T) {
Expand Down
Loading