@@ -1128,7 +1128,9 @@ func (e *executor) createTableWithInfoJob(
11281128 CDCWriteSource : ctx .GetSessionVars ().CDCWriteSource ,
11291129 InvolvingSchemaInfo : involvingSchemas ,
11301130 SQLMode : ctx .GetSessionVars ().SQLMode ,
1131+ SessionVars : make (map [string ]string ),
11311132 }
1133+ job .AddSessionVars (vardef .TiDBScatterRegion , getScatterScopeFromSessionctx (ctx ))
11321134 args := & model.CreateTableArgs {
11331135 TableInfo : tbInfo ,
11341136 OnExistReplace : cfg .OnExist == OnExistReplace ,
@@ -1160,13 +1162,14 @@ func (e *executor) createTableWithInfoPost(
11601162 ctx sessionctx.Context ,
11611163 tbInfo * model.TableInfo ,
11621164 schemaID int64 ,
1165+ scatterScope string ,
11631166) error {
11641167 var err error
11651168 var partitions []model.PartitionDefinition
11661169 if pi := tbInfo .GetPartitionInfo (); pi != nil {
11671170 partitions = pi .Definitions
11681171 }
1169- preSplitAndScatter (ctx , e .store , tbInfo , partitions )
1172+ preSplitAndScatter (ctx , e .store , tbInfo , partitions , scatterScope )
11701173 if tbInfo .AutoIncID > 1 {
11711174 // Default tableAutoIncID base is 0.
11721175 // If the first ID is expected to greater than 1, we need to do rebase.
@@ -1221,7 +1224,11 @@ func (e *executor) CreateTableWithInfo(
12211224 err = nil
12221225 }
12231226 } else {
1224- err = e .createTableWithInfoPost (ctx , tbInfo , jobW .SchemaID )
1227+ var scatterScope string
1228+ if val , ok := jobW .GetSessionVars (vardef .TiDBScatterRegion ); ok {
1229+ scatterScope = val
1230+ }
1231+ err = e .createTableWithInfoPost (ctx , tbInfo , jobW .SchemaID , scatterScope )
12251232 }
12261233
12271234 return errors .Trace (err )
@@ -1245,7 +1252,9 @@ func (e *executor) BatchCreateTableWithInfo(ctx sessionctx.Context,
12451252 BinlogInfo : & model.HistoryInfo {},
12461253 CDCWriteSource : ctx .GetSessionVars ().CDCWriteSource ,
12471254 SQLMode : ctx .GetSessionVars ().SQLMode ,
1255+ SessionVars : make (map [string ]string ),
12481256 }
1257+ job .AddSessionVars (vardef .TiDBScatterRegion , getScatterScopeFromSessionctx (ctx ))
12491258
12501259 var err error
12511260
@@ -1311,9 +1320,12 @@ func (e *executor) BatchCreateTableWithInfo(ctx sessionctx.Context,
13111320 }
13121321 return errors .Trace (err )
13131322 }
1314-
1323+ var scatterScope string
1324+ if val , ok := jobW .GetSessionVars (vardef .TiDBScatterRegion ); ok {
1325+ scatterScope = val
1326+ }
13151327 for _ , tblArgs := range args .Tables {
1316- if err = e .createTableWithInfoPost (ctx , tblArgs .TableInfo , jobW .SchemaID ); err != nil {
1328+ if err = e .createTableWithInfoPost (ctx , tblArgs .TableInfo , jobW .SchemaID , scatterScope ); err != nil {
13171329 return errors .Trace (err )
13181330 }
13191331 }
@@ -1376,24 +1388,16 @@ func (e *executor) CreatePlacementPolicyWithInfo(ctx sessionctx.Context, policy
13761388
13771389// preSplitAndScatter performs pre-split and scatter of the table's regions.
13781390// If `pi` is not nil, will only split region for `pi`, this is used when add partition.
1379- func preSplitAndScatter (ctx sessionctx.Context , store kv.Storage , tbInfo * model.TableInfo , parts []model.PartitionDefinition ) {
1391+ func preSplitAndScatter (ctx sessionctx.Context , store kv.Storage , tbInfo * model.TableInfo , parts []model.PartitionDefinition , scatterScope string ) {
1392+ failpoint .InjectCall ("preSplitAndScatter" , scatterScope )
13801393 if tbInfo .TempTableType != model .TempTableNone {
13811394 return
13821395 }
13831396 sp , ok := store .(kv.SplittableStore )
13841397 if ! ok || atomic .LoadUint32 (& EnableSplitTableRegion ) == 0 {
13851398 return
13861399 }
1387- var (
1388- preSplit func ()
1389- scatterScope string
1390- )
1391- val , ok := ctx .GetSessionVars ().GetSystemVar (vardef .TiDBScatterRegion )
1392- if ! ok {
1393- logutil .DDLLogger ().Warn ("get system variable met problem, won't scatter region" )
1394- } else {
1395- scatterScope = val
1396- }
1400+ var preSplit func ()
13971401 if len (parts ) > 0 {
13981402 preSplit = func () { splitPartitionTableRegion (ctx , sp , tbInfo , parts , scatterScope ) }
13991403 } else {
@@ -2286,7 +2290,9 @@ func (e *executor) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, s
22862290 BinlogInfo : & model.HistoryInfo {},
22872291 CDCWriteSource : ctx .GetSessionVars ().CDCWriteSource ,
22882292 SQLMode : ctx .GetSessionVars ().SQLMode ,
2293+ SessionVars : make (map [string ]string ),
22892294 }
2295+ job .AddSessionVars (vardef .TiDBScatterRegion , getScatterScopeFromSessionctx (ctx ))
22902296 args := & model.TablePartitionArgs {
22912297 PartInfo : partInfo ,
22922298 }
@@ -2797,7 +2803,9 @@ func (e *executor) TruncateTablePartition(ctx sessionctx.Context, ident ast.Iden
27972803 BinlogInfo : & model.HistoryInfo {},
27982804 CDCWriteSource : ctx .GetSessionVars ().CDCWriteSource ,
27992805 SQLMode : ctx .GetSessionVars ().SQLMode ,
2806+ SessionVars : make (map [string ]string ),
28002807 }
2808+ job .AddSessionVars (vardef .TiDBScatterRegion , getScatterScopeFromSessionctx (ctx ))
28012809 args := & model.TruncateTableArgs {
28022810 OldPartitionIDs : pids ,
28032811 // job submitter will fill new partition IDs.
@@ -4299,7 +4307,9 @@ func (e *executor) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error {
42994307 BinlogInfo : & model.HistoryInfo {},
43004308 CDCWriteSource : ctx .GetSessionVars ().CDCWriteSource ,
43014309 SQLMode : ctx .GetSessionVars ().SQLMode ,
4310+ SessionVars : make (map [string ]string ),
43024311 }
4312+ job .AddSessionVars (vardef .TiDBScatterRegion , getScatterScopeFromSessionctx (ctx ))
43034313 args := & model.TruncateTableArgs {
43044314 FKCheck : fkCheck ,
43054315 OldPartitionIDs : oldPartitionIDs ,
@@ -7080,3 +7090,14 @@ func (e *executor) RefreshMeta(sctx sessionctx.Context, args *model.RefreshMetaA
70807090 err := e .doDDLJob2 (sctx , job , args )
70817091 return errors .Trace (err )
70827092}
7093+
7094+ func getScatterScopeFromSessionctx (sctx sessionctx.Context ) string {
7095+ var scatterScope string
7096+ val , ok := sctx .GetSessionVars ().GetSystemVar (vardef .TiDBScatterRegion )
7097+ if ! ok {
7098+ logutil .DDLLogger ().Info ("won't scatter region since system variable didn't set" )
7099+ } else {
7100+ scatterScope = val
7101+ }
7102+ return scatterScope
7103+ }
0 commit comments