Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 29 additions & 5 deletions pkg/ddl/split_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ import (

func splitPartitionTableRegion(ctx sessionctx.Context, store kv.SplittableStore, tbInfo *model.TableInfo, parts []model.PartitionDefinition, scatterScope string) {
// Max partition count is 8192, should we sample and just choose some partitions to split?
regionIDs := make([]uint64, 0, len(parts))
var regionIDs []uint64
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), ctx.GetSessionVars().GetSplitRegionTimeout())
defer cancel()
ctxWithTimeout = kv.WithInternalSourceType(ctxWithTimeout, kv.InternalTxnDDL)
if shardingBits(tbInfo) > 0 && tbInfo.PreSplitRegions > 0 {
regionIDs = make([]uint64, 0, len(parts)*(len(tbInfo.Indices)+1))
scatter, tableID := getScatterConfig(scatterScope, tbInfo.ID)
// Try to split global index region here.
regionIDs = append(regionIDs, splitIndexRegion(store, tbInfo, scatter, tableID)...)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it for the global index? Could we add a comment for it?

Besides, do we need to update the capacity for regionIDs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

for _, def := range parts {
regionIDs = append(regionIDs, preSplitPhysicalTableByShardRowID(ctxWithTimeout, store, tbInfo, def.ID, scatterScope)...)
}
} else {
regionIDs = make([]uint64, 0, len(parts))
for _, def := range parts {
regionIDs = append(regionIDs, SplitRecordRegion(ctxWithTimeout, store, def.ID, tbInfo.ID, scatterScope))
}
Expand Down Expand Up @@ -127,7 +132,7 @@ func preSplitPhysicalTableByShardRowID(ctx context.Context, store kv.SplittableS
logutil.DDLLogger().Warn("pre split some table regions failed",
zap.Stringer("table", tbInfo.Name), zap.Int("successful region count", len(regionIDs)), zap.Error(err))
}
regionIDs = append(regionIDs, splitIndexRegion(store, tbInfo, scatter, &tableID)...)
regionIDs = append(regionIDs, splitIndexRegion(store, tbInfo, scatter, physicalID)...)
return regionIDs
}

Expand All @@ -146,13 +151,32 @@ func SplitRecordRegion(ctx context.Context, store kv.SplittableStore, physicalTa
return 0
}

func splitIndexRegion(store kv.SplittableStore, tblInfo *model.TableInfo, scatter bool, tableID *int64) []uint64 {
func splitIndexRegion(store kv.SplittableStore, tblInfo *model.TableInfo, scatter bool, physicalTableID int64) []uint64 {
splitKeys := make([][]byte, 0, len(tblInfo.Indices))
for _, idx := range tblInfo.Indices {
indexPrefix := tablecodec.EncodeTableIndexPrefix(tblInfo.ID, idx.ID)
if tblInfo.GetPartitionInfo() != nil &&
((idx.Global && tblInfo.ID != physicalTableID) || (!idx.Global && tblInfo.ID == physicalTableID)) {
continue
}
id := idx.ID
// For normal index, split regions like
// [t_tid_, t_tid_i_idx1ID+1),
// [t_tid_i_idx1ID+1, t_tid_i_idx2ID+1),
// ...
// [t_tid_i_idxMaxID+1, t_tid_r_xxxx)
//
// For global index, split regions like
// [t_tid_i_idx1ID, t_tid_i_idx2ID),
// [t_tid_i_idx2ID, t_tid_i_idx3ID),
// ...
// [t_tid_i_idxMaxID, t_pid1_)
if !idx.Global {
id = id + 1
}
indexPrefix := tablecodec.EncodeTableIndexPrefix(physicalTableID, id)
splitKeys = append(splitKeys, indexPrefix)
}
regionIDs, err := store.SplitRegions(context.Background(), splitKeys, scatter, tableID)
regionIDs, err := store.SplitRegions(context.Background(), splitKeys, scatter, &physicalTableID)
if err != nil {
logutil.DDLLogger().Warn("pre split some table index regions failed",
zap.Stringer("table", tblInfo.Name), zap.Int("successful region count", len(regionIDs)), zap.Error(err))
Expand Down
4 changes: 2 additions & 2 deletions pkg/ddl/tests/partition/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,11 +1232,11 @@ func TestAlterTableTruncatePartitionPreSplitRegion(t *testing.T) {
PARTITION p3 VALUES LESS THAN (MAXVALUE))`)
re = tk.MustQuery("show table t2 regions")
rows = re.Rows()
require.Len(t, rows, 24)
require.Len(t, rows, 27)
tk.MustExec(`alter table t2 truncate partition p3`)
re = tk.MustQuery("show table t2 regions")
rows = re.Rows()
require.Len(t, rows, 24)
require.Len(t, rows, 27)
}

func TestCreateTableWithKeyPartition(t *testing.T) {
Expand Down
23 changes: 23 additions & 0 deletions pkg/executor/test/splittest/split_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,29 @@ func TestShowTableRegion(t *testing.T) {
}
require.Equal(t, infosync.PlacementScheduleStatePending.String(), rows[i][12])
}

tk.MustExec("drop table if exists sbtest0000")
tk.MustExec("CREATE TABLE sbtest0000(" +
" id bigint," +
" k bigint DEFAULT '0' NOT NULL," +
" c CHAR(120) DEFAULT '' NOT NULL," +
" pad CHAR(60) DEFAULT '' NOT NULL," +
" UNIQUE KEY uk(id) GLOBAL," +
" key idx0(k,c)," +
" key idx1(c))SHARD_ROW_ID_BITS=3 PRE_SPLIT_REGIONS=2 " +
"PARTITION BY RANGE(id)(PARTITION p VALUES LESS THAN (MAXVALUE))")
re = tk.MustQuery("show table sbtest0000 regions")
rows = re.Rows()
require.Len(t, rows, 7) // 3 index regions + 4 record regions

tbl = external.GetTableByName(t, tk, "test", "sbtest0000")
require.Equal(t, fmt.Sprintf("t_%d_i_1_", tbl.Meta().ID), rows[0][1]) // index `uk``
require.Equal(t, fmt.Sprintf("t_%d_i_4_", tbl.Meta().ID+1), rows[1][1])
require.Regexp(t, fmt.Sprintf("t_%d_r_.*", tbl.Meta().ID+1), rows[2][1])
require.Regexp(t, fmt.Sprintf("t_%d_r_.*", tbl.Meta().ID+1), rows[3][1])
require.Regexp(t, fmt.Sprintf("t_%d_r_.*", tbl.Meta().ID+1), rows[4][1])
require.Equal(t, fmt.Sprintf("t_%d_", tbl.Meta().ID+1), rows[5][1]) // index `idx0`
require.Equal(t, fmt.Sprintf("t_%d_i_3_", tbl.Meta().ID+1), rows[6][1]) // index `idx1`
Copy link
Contributor

@zimulala zimulala Feb 17, 2025

Choose a reason for hiding this comment

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

Should we consider adding a test case with both a global index and a normal index?

}

func BenchmarkLocateRegion(t *testing.B) {
Expand Down