Skip to content

Commit 4371a1d

Browse files
authored
*: set default shard_row_id_bits and pre_split_regions for all tables (#55717)
close #55716
1 parent 027f01b commit 4371a1d

File tree

9 files changed

+97
-8
lines changed

9 files changed

+97
-8
lines changed

pkg/ddl/create_table.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,12 @@ func BuildTableInfoWithStmt(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCh
674674
return nil, errors.Trace(err)
675675
}
676676

677+
// set default shard row id bits and pre-split regions for table.
678+
if !tbInfo.HasClusteredIndex() && tbInfo.TempTableType == model.TempTableNone {
679+
tbInfo.ShardRowIDBits = ctx.GetSessionVars().ShardRowIDBits
680+
tbInfo.PreSplitRegions = ctx.GetSessionVars().PreSplitRegions
681+
}
682+
677683
if err = handleTableOptions(s.Options, tbInfo); err != nil {
678684
return nil, errors.Trace(err)
679685
}
@@ -799,8 +805,8 @@ func handleTableOptions(options []*ast.TableOption, tbInfo *model.TableInfo) err
799805
return dbterror.ErrUnsupportedShardRowIDBits
800806
}
801807
tbInfo.ShardRowIDBits = op.UintValue
802-
if tbInfo.ShardRowIDBits > shardRowIDBitsMax {
803-
tbInfo.ShardRowIDBits = shardRowIDBitsMax
808+
if tbInfo.ShardRowIDBits > variable.MaxShardRowIDBits {
809+
tbInfo.ShardRowIDBits = variable.MaxShardRowIDBits
804810
}
805811
tbInfo.MaxShardRowIDBits = tbInfo.ShardRowIDBits
806812
case ast.TableOptionPreSplitRegion:

pkg/ddl/ddl.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ const (
7777
addingDDLJobPrefix = "/tidb/ddl/add_ddl_job_"
7878
ddlPrompt = "ddl"
7979

80-
shardRowIDBitsMax = 15
81-
8280
batchAddingJobs = 100
8381

8482
reorgWorkerCnt = 10

pkg/ddl/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,8 +1848,8 @@ func (e *executor) AlterTable(ctx context.Context, sctx sessionctx.Context, stmt
18481848
for i, opt := range spec.Options {
18491849
switch opt.Tp {
18501850
case ast.TableOptionShardRowID:
1851-
if opt.UintValue > shardRowIDBitsMax {
1852-
opt.UintValue = shardRowIDBitsMax
1851+
if opt.UintValue > variable.MaxShardRowIDBits {
1852+
opt.UintValue = variable.MaxShardRowIDBits
18531853
}
18541854
err = e.ShardRowID(sctx, ident, opt.UintValue)
18551855
case ast.TableOptionAutoIncrement:

pkg/ddl/schematracker/checker.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"context"
2020
"fmt"
21+
"regexp"
2122
"strings"
2223
"sync/atomic"
2324

@@ -164,8 +165,24 @@ func (d *Checker) checkTableInfo(ctx sessionctx.Context, dbName, tableName pmode
164165
s1 := removeClusteredIndexComment(result.String())
165166
s2 := removeClusteredIndexComment(result2.String())
166167

168+
// Remove shard_row_id_bits and pre_split_regions comments.
169+
if ctx.GetSessionVars().ShardRowIDBits != 0 || ctx.GetSessionVars().PreSplitRegions != 0 {
170+
removeShardPreSplitComment := func(s string) string {
171+
pattern := ` \/\*T! SHARD_ROW_ID_BITS=.*?\*\/`
172+
re := regexp.MustCompile(pattern)
173+
ret := re.ReplaceAllString(s, "")
174+
pattern = ` \/\*T! PRE_SPLIT_REGIONS=.*?\*\/`
175+
re = regexp.MustCompile(pattern)
176+
ret = re.ReplaceAllString(ret, "")
177+
return ret
178+
}
179+
180+
s1 = removeShardPreSplitComment(s1)
181+
s2 = removeShardPreSplitComment(s2)
182+
}
183+
167184
if s1 != s2 {
168-
errStr := fmt.Sprintf("%s != %s", s1, s2)
185+
errStr := fmt.Sprintf("%s\n!=\n%s", s1, s2)
169186
panic(errStr)
170187
}
171188
}

pkg/executor/test/ddl/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ go_test(
88
"main_test.go",
99
],
1010
flaky = True,
11-
shard_count = 19,
11+
shard_count = 20,
1212
deps = [
1313
"//pkg/config",
1414
"//pkg/ddl/schematracker",

pkg/executor/test/ddl/ddl_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,3 +1106,41 @@ func TestRenameMultiTables(t *testing.T) {
11061106
tk.MustExec("drop database rename2")
11071107
tk.MustExec("drop database rename3")
11081108
}
1109+
1110+
func TestDefShardTables(t *testing.T) {
1111+
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())
1112+
1113+
tk := testkit.NewTestKit(t, store)
1114+
1115+
tk.MustExec("set @@session.tidb_enable_clustered_index = off")
1116+
tk.MustExec("set @@session.tidb_shard_row_id_bits = 4")
1117+
tk.MustExec("set @@session.tidb_pre_split_regions = 4")
1118+
tk.MustExec("use test")
1119+
tk.MustExec("create table t (i int primary key)")
1120+
result := tk.MustQuery("show create table t")
1121+
createSQL := result.Rows()[0][1]
1122+
expected := "CREATE TABLE `t` (\n `i` int(11) NOT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T! SHARD_ROW_ID_BITS=4 PRE_SPLIT_REGIONS=4 */"
1123+
require.Equal(t, expected, createSQL)
1124+
1125+
// test for manual setup shard_row_id_bits and pre_split_regions
1126+
tk.MustExec("create table t0 (i int primary key) /*T! SHARD_ROW_ID_BITS=2 PRE_SPLIT_REGIONS=2 */")
1127+
result = tk.MustQuery("show create table t0")
1128+
createSQL = result.Rows()[0][1]
1129+
expected = "CREATE TABLE `t0` (\n `i` int(11) NOT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T! SHARD_ROW_ID_BITS=2 PRE_SPLIT_REGIONS=2 */"
1130+
require.Equal(t, expected, createSQL)
1131+
1132+
// test for clustered index table
1133+
tk.MustExec("set @@session.tidb_enable_clustered_index = on")
1134+
tk.MustExec("create table t1 (i int primary key)")
1135+
result = tk.MustQuery("show create table t1")
1136+
createSQL = result.Rows()[0][1]
1137+
expected = "CREATE TABLE `t1` (\n `i` int(11) NOT NULL,\n PRIMARY KEY (`i`) /*T![clustered_index] CLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
1138+
require.Equal(t, expected, createSQL)
1139+
1140+
// test for global temporary table
1141+
tk.MustExec("create global temporary table tengine (id int) engine = 'innodb' on commit delete rows")
1142+
result = tk.MustQuery("show create table tengine")
1143+
createSQL = result.Rows()[0][1]
1144+
expected = "CREATE GLOBAL TEMPORARY TABLE `tengine` (\n `id` int(11) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS"
1145+
require.Equal(t, expected, createSQL)
1146+
}

pkg/sessionctx/variable/session.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,12 @@ type SessionVars struct {
837837
// status stands for the session status. e.g. in transaction or not, auto commit is on or off, and so on.
838838
status atomic.Uint32
839839

840+
// ShardRowIDBits is the number of shard bits for user table row ID.
841+
ShardRowIDBits uint64
842+
843+
// PreSplitRegions is the number of regions that should be pre-split for the table.
844+
PreSplitRegions uint64
845+
840846
// ClientCapability is client's capability.
841847
ClientCapability uint32
842848

pkg/sessionctx/variable/sysvar.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,14 @@ var defaultSysVars = []*SysVar{
20602060
s.AllowBatchCop = int(TidbOptInt64(val, DefTiDBAllowBatchCop))
20612061
return nil
20622062
}},
2063+
{Scope: ScopeGlobal | ScopeSession, Name: TiDBShardRowIDBits, Value: strconv.Itoa(DefShardRowIDBits), Type: TypeInt, MinValue: 0, MaxValue: MaxShardRowIDBits, SetSession: func(s *SessionVars, val string) error {
2064+
s.ShardRowIDBits = TidbOptUint64(val, DefShardRowIDBits)
2065+
return nil
2066+
}},
2067+
{Scope: ScopeGlobal | ScopeSession, Name: TiDBPreSplitRegions, Value: strconv.Itoa(DefPreSplitRegions), Type: TypeInt, MinValue: 0, MaxValue: MaxPreSplitRegions, SetSession: func(s *SessionVars, val string) error {
2068+
s.PreSplitRegions = TidbOptUint64(val, DefPreSplitRegions)
2069+
return nil
2070+
}},
20632071
{Scope: ScopeGlobal | ScopeSession, Name: TiDBInitChunkSize, Value: strconv.Itoa(DefInitChunkSize), Type: TypeUnsigned, MinValue: 1, MaxValue: initChunkSizeUpperBound, SetSession: func(s *SessionVars, val string) error {
20642072
s.InitChunkSize = tidbOptPositiveInt32(val, DefInitChunkSize)
20652073
return nil

pkg/sessionctx/variable/tidb_vars.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,14 @@ const (
387387
// The default value is 0
388388
TiDBAllowBatchCop = "tidb_allow_batch_cop"
389389

390+
// TiDBShardRowIDBits means all the tables created in the current session will be sharded.
391+
// The default value is 0
392+
TiDBShardRowIDBits = "tidb_shard_row_id_bits"
393+
394+
// TiDBPreSplitRegions means all the tables created in the current session will be pre-splited.
395+
// The default value is 0
396+
TiDBPreSplitRegions = "tidb_pre_split_regions"
397+
390398
// TiDBAllowMPPExecution means if we should use mpp way to execute query or not.
391399
// Default value is `true`, means to be determined by the optimizer.
392400
// Value set to `false` means never use mpp.
@@ -1207,6 +1215,12 @@ const (
12071215
// MaxConfigurableConcurrency is the maximum number of "threads" (goroutines) that can be specified
12081216
// for any type of configuration item that has concurrent workers.
12091217
MaxConfigurableConcurrency = 256
1218+
1219+
// MaxShardRowIDBits is the maximum number of bits that can be used for row-id sharding.
1220+
MaxShardRowIDBits = 15
1221+
1222+
// MaxPreSplitRegions is the maximum number of regions that can be pre-split.
1223+
MaxPreSplitRegions = 15
12101224
)
12111225

12121226
// Default TiDB system variable values.
@@ -1279,6 +1293,8 @@ const (
12791293
DefTiDBEnableOuterJoinReorder = true
12801294
DefTiDBEnableNAAJ = true
12811295
DefTiDBAllowBatchCop = 1
1296+
DefShardRowIDBits = 0
1297+
DefPreSplitRegions = 0
12821298
DefBlockEncryptionMode = "aes-128-ecb"
12831299
DefTiDBAllowMPPExecution = true
12841300
DefTiDBAllowTiFlashCop = false

0 commit comments

Comments
 (0)