@@ -90,6 +90,18 @@ func (c *testCase) generateDDLOps() error {
90
90
if err := c .generateAlterIndexVisibility (defaultTime ); err != nil {
91
91
return errors .Trace (err )
92
92
}
93
+ if err := c .generateAlterPlacementPolicy (defaultTime ); err != nil {
94
+ return errors .Trace (err )
95
+ }
96
+ if err := c .generateCreatePlacementPolicy (defaultTime ); err != nil {
97
+ return errors .Trace (err )
98
+ }
99
+ if err := c .generateDropPlacementPolicy (defaultTime ); err != nil {
100
+ return errors .Trace (err )
101
+ }
102
+ if err := c .generateAddPlacementPolicyToTable (defaultTime ); err != nil {
103
+ return errors .Trace (err )
104
+ }
93
105
return nil
94
106
}
95
107
@@ -128,6 +140,11 @@ const (
128
140
129
141
ActionAlterIndexVisibility
130
142
143
+ ActionCreatePlacementPolicy
144
+ ActionDropPlacementPolicy
145
+ ActionAddPlacementPolicyToTable
146
+ ActionAlterPlacementPolicy
147
+
131
148
ddlKindNil
132
149
)
133
150
@@ -164,6 +181,11 @@ var mapOfDDLKind = map[string]DDLKind{
164
181
"drop primary key" : ActionDropPrimaryKey ,
165
182
166
183
"alter index visibility" : ActionAlterIndexVisibility ,
184
+
185
+ "create placement policy" : ActionCreatePlacementPolicy ,
186
+ "drop placement policy" : ActionDropPlacementPolicy ,
187
+ "add placement policy to table" : ActionAddPlacementPolicyToTable ,
188
+ "alter placement policy" : ActionAlterPlacementPolicy ,
167
189
}
168
190
169
191
var mapOfDDLKindToString = map [DDLKind ]string {
@@ -198,6 +220,11 @@ var mapOfDDLKindToString = map[DDLKind]string{
198
220
ActionDropPrimaryKey : "drop primary key" ,
199
221
200
222
ActionAlterIndexVisibility : "alter index visibility" ,
223
+
224
+ ActionCreatePlacementPolicy : "create placement policy" ,
225
+ ActionDropPlacementPolicy : "drop placement policy" ,
226
+ ActionAddPlacementPolicyToTable : "add placement policy to table" ,
227
+ ActionAlterPlacementPolicy : "alter placement policy" ,
201
228
}
202
229
203
230
// mapOfDDLKindProbability use to control every kind of ddl request execute probability.
@@ -232,6 +259,11 @@ var mapOfDDLKindProbability = map[DDLKind]float64{
232
259
ActionDropPrimaryKey : 0.10 ,
233
260
234
261
ActionAlterIndexVisibility : 0.20 ,
262
+
263
+ ActionCreatePlacementPolicy : 0.20 ,
264
+ ActionDropPlacementPolicy : 0.20 ,
265
+ ActionAddPlacementPolicyToTable : 0.20 ,
266
+ ActionAlterPlacementPolicy : 0.20 ,
235
267
}
236
268
237
269
type ddlJob struct {
@@ -247,14 +279,15 @@ type ddlJob struct {
247
279
type ddlJobArg unsafe.Pointer
248
280
249
281
type ddlJobTask struct {
250
- ddlID int
251
- k DDLKind
252
- tblInfo * ddlTestTable
253
- schemaInfo * ddlTestSchema
254
- viewInfo * ddlTestView
255
- sql string
256
- arg ddlJobArg
257
- err error // err is an error executed by the remote TiDB.
282
+ ddlID int
283
+ k DDLKind
284
+ tblInfo * ddlTestTable
285
+ schemaInfo * ddlTestSchema
286
+ viewInfo * ddlTestView
287
+ placementInfo * ddlTestPlacementPolicy
288
+ sql string
289
+ arg ddlJobArg
290
+ err error // err is an error executed by the remote TiDB.
258
291
}
259
292
260
293
// Maintain the tableInfo description in the memory, once schrddl fails, we can get more details from the memory schema copy.
@@ -284,6 +317,8 @@ func (c *testCase) updateTableInfo(task *ddlJobTask) error {
284
317
return c .dropTableJob (task )
285
318
case ddlCreateView :
286
319
return c .createViewJob (task )
320
+ case ddlDropView :
321
+ return c .dropViewJob (task )
287
322
case ddlAddIndex :
288
323
return c .addIndexJob (task )
289
324
case ddlRenameIndex :
@@ -306,6 +341,16 @@ func (c *testCase) updateTableInfo(task *ddlJobTask) error {
306
341
return c .setAddPrimaryKey (task )
307
342
case ActionDropPrimaryKey :
308
343
return c .setDropPrimaryKey (task )
344
+ case ActionAlterIndexVisibility :
345
+ return c .setAlterIndexVisibility (task )
346
+ case ActionCreatePlacementPolicy :
347
+ return c .setCreatePlacementPolicy (task )
348
+ case ActionDropPlacementPolicy :
349
+ return c .setDropPlacementPolicy (task )
350
+ case ActionAddPlacementPolicyToTable :
351
+ return c .setAddPlacementPolicyToTable (task )
352
+ case ActionAlterPlacementPolicy :
353
+ return c .setAlterPlacementPolicy (task )
309
354
}
310
355
return fmt .Errorf ("unknow ddl task , %v" , * task )
311
356
}
@@ -1064,8 +1109,9 @@ func (c *testCase) prepareAddPrimaryKey(_ interface{}, taskCh chan *ddlJobTask)
1064
1109
perm := rand .Perm (table .columns .Size ())
1065
1110
// build SQL
1066
1111
sql := fmt .Sprintf ("ALTER TABLE `%s` ADD PRIMARY KEY (" , table .name )
1112
+ var column * ddlTestColumn
1067
1113
for _ , i := range perm {
1068
- column : = getColumnFromArrayList (table .columns , i )
1114
+ column = getColumnFromArrayList (table .columns , i )
1069
1115
if column .canBePrimary () {
1070
1116
sql += fmt .Sprintf ("`%s`" , column .name )
1071
1117
break
@@ -1077,6 +1123,7 @@ func (c *testCase) prepareAddPrimaryKey(_ interface{}, taskCh chan *ddlJobTask)
1077
1123
k : ActionAddPrimaryKey ,
1078
1124
sql : sql ,
1079
1125
tblInfo : table ,
1126
+ arg : ddlJobArg (column ),
1080
1127
}
1081
1128
taskCh <- task
1082
1129
return nil
@@ -1085,6 +1132,8 @@ func (c *testCase) prepareAddPrimaryKey(_ interface{}, taskCh chan *ddlJobTask)
1085
1132
func (c * testCase ) setAddPrimaryKey (task * ddlJobTask ) error {
1086
1133
tblInfo := task .tblInfo
1087
1134
tblInfo .hasPK = true
1135
+ col := (* ddlTestColumn )(task .arg )
1136
+ col .isPrimaryKey = true
1088
1137
return nil
1089
1138
}
1090
1139
@@ -1117,6 +1166,13 @@ func (c *testCase) prepareDropPrimaryKey(_ interface{}, taskCh chan *ddlJobTask)
1117
1166
func (c * testCase ) setDropPrimaryKey (task * ddlJobTask ) error {
1118
1167
tblInfo := task .tblInfo
1119
1168
tblInfo .hasPK = false
1169
+ for ite := tblInfo .columns .Iterator (); ite .Next (); {
1170
+ col := ite .Value ().(* ddlTestColumn )
1171
+ if col .isPrimaryKey {
1172
+ col .isPrimaryKey = false
1173
+ break
1174
+ }
1175
+ }
1120
1176
return nil
1121
1177
}
1122
1178
@@ -1261,7 +1317,7 @@ func (c *testCase) prepareAlterIndexVisibility(_ interface{}, taskCh chan *ddlJo
1261
1317
return nil
1262
1318
}
1263
1319
1264
- func (c * testCase ) dropAlterIndexVisibilityJob (task * ddlJobTask ) error {
1320
+ func (c * testCase ) setAlterIndexVisibility (task * ddlJobTask ) error {
1265
1321
jobArg := (* ddlIndexJobArg )(task .arg )
1266
1322
tblInfo := task .tblInfo
1267
1323
@@ -1284,6 +1340,146 @@ func (c *testCase) dropAlterIndexVisibilityJob(task *ddlJobTask) error {
1284
1340
return nil
1285
1341
}
1286
1342
1343
+ func (c * testCase ) generateCreatePlacementPolicy (repeat int ) error {
1344
+ for i := 0 ; i < repeat ; i ++ {
1345
+ c .ddlOps = append (c .ddlOps , ddlTestOpExecutor {c .prepareCreatePlacementPolicy , nil , ActionCreatePlacementPolicy })
1346
+ }
1347
+ return nil
1348
+ }
1349
+
1350
+ func (c * testCase ) prepareCreatePlacementPolicy (_ interface {}, taskCh chan * ddlJobTask ) error {
1351
+ policy := & ddlTestPlacementPolicy {
1352
+ Name : uuid .NewV4 ().String (),
1353
+ Content : "us-east-1,us-west-1" ,
1354
+ }
1355
+
1356
+ sql := fmt .Sprintf ("CREATE PLACEMENT POLICY `%s` PRIMARY_REGION=\" us-east-1\" REGIONS=\" us-east-1,us-west-1\" " , policy .Name )
1357
+ taskCh <- & ddlJobTask {
1358
+ k : ActionCreatePlacementPolicy ,
1359
+ sql : sql ,
1360
+ placementInfo : policy ,
1361
+ }
1362
+ return nil
1363
+ }
1364
+
1365
+ func (c * testCase ) setCreatePlacementPolicy (task * ddlJobTask ) error {
1366
+ policy := task .placementInfo
1367
+ c .placementPolicies [policy .Name ] = policy
1368
+ return nil
1369
+ }
1370
+
1371
+ func (c * testCase ) generateDropPlacementPolicy (repeat int ) error {
1372
+ for i := 0 ; i < repeat ; i ++ {
1373
+ c .ddlOps = append (c .ddlOps , ddlTestOpExecutor {c .prepareDropPlacementPolicy , nil , ActionDropPlacementPolicy })
1374
+ }
1375
+ return nil
1376
+ }
1377
+
1378
+ func (c * testCase ) prepareDropPlacementPolicy (_ interface {}, taskCh chan * ddlJobTask ) error {
1379
+ var policy string
1380
+ for k := range c .placementPolicies {
1381
+ if rand .Intn (len (c .placementPolicies )) == 0 {
1382
+ policy = k
1383
+ break
1384
+ }
1385
+ }
1386
+
1387
+ if policy == "" {
1388
+ return nil
1389
+ }
1390
+
1391
+ sql := fmt .Sprintf ("DROP PLACEMENT POLICY `%s`" , policy )
1392
+ taskCh <- & ddlJobTask {
1393
+ k : ActionDropPlacementPolicy ,
1394
+ sql : sql ,
1395
+ placementInfo : c .placementPolicies [policy ],
1396
+ }
1397
+ return nil
1398
+ }
1399
+
1400
+ func (c * testCase ) setDropPlacementPolicy (task * ddlJobTask ) error {
1401
+ if task .placementInfo == nil {
1402
+ return nil
1403
+ }
1404
+ delete (c .placementPolicies , task .placementInfo .Name )
1405
+ return nil
1406
+ }
1407
+
1408
+ func (c * testCase ) generateAddPlacementPolicyToTable (repeat int ) error {
1409
+ for i := 0 ; i < repeat ; i ++ {
1410
+ c .ddlOps = append (c .ddlOps , ddlTestOpExecutor {c .prepareAddPlacementPolicyToTable , nil , ActionAddPlacementPolicyToTable })
1411
+ }
1412
+ return nil
1413
+ }
1414
+
1415
+ func (c * testCase ) prepareAddPlacementPolicyToTable (_ interface {}, taskCh chan * ddlJobTask ) error {
1416
+ var policy string
1417
+ for k := range c .placementPolicies {
1418
+ if rand .Intn (len (c .placementPolicies )) == 0 {
1419
+ policy = k
1420
+ break
1421
+ }
1422
+ }
1423
+
1424
+ if policy == "" {
1425
+ return nil
1426
+ }
1427
+
1428
+ table := c .pickupRandomTable ()
1429
+ if table == nil || table .policyName != "" {
1430
+ return nil
1431
+ }
1432
+
1433
+ sql := fmt .Sprintf ("ALTER TABLE `%s` PLACEMENT POLICY=`%s`" , table .name , policy )
1434
+ taskCh <- & ddlJobTask {
1435
+ k : ActionAddPlacementPolicyToTable ,
1436
+ sql : sql ,
1437
+ tblInfo : table ,
1438
+ placementInfo : c .placementPolicies [policy ],
1439
+ }
1440
+ return nil
1441
+ }
1442
+
1443
+ func (c * testCase ) setAddPlacementPolicyToTable (task * ddlJobTask ) error {
1444
+ table := task .tblInfo
1445
+ table .policyName = task .placementInfo .Name
1446
+ return nil
1447
+ }
1448
+
1449
+ func (c * testCase ) generateAlterPlacementPolicy (repeat int ) error {
1450
+ for i := 0 ; i < repeat ; i ++ {
1451
+ c .ddlOps = append (c .ddlOps , ddlTestOpExecutor {c .prepareAlterPlacementPolicy , nil , ActionAlterPlacementPolicy })
1452
+ }
1453
+ return nil
1454
+ }
1455
+
1456
+ func (c * testCase ) prepareAlterPlacementPolicy (_ interface {}, taskCh chan * ddlJobTask ) error {
1457
+ var policy string
1458
+ for k := range c .placementPolicies {
1459
+ if rand .Intn (len (c .placementPolicies )) == 0 {
1460
+ policy = k
1461
+ break
1462
+ }
1463
+ }
1464
+
1465
+ if policy == "" {
1466
+ return nil
1467
+ }
1468
+
1469
+ sql := fmt .Sprintf ("ALTER PLACEMENT POLICY `%s` REGIONS=\" us-west-1\" " , policy )
1470
+ taskCh <- & ddlJobTask {
1471
+ k : ActionAlterPlacementPolicy ,
1472
+ sql : sql ,
1473
+ placementInfo : c .placementPolicies [policy ],
1474
+ }
1475
+ return nil
1476
+ }
1477
+
1478
+ func (c * testCase ) setAlterPlacementPolicy (task * ddlJobTask ) error {
1479
+ c .placementPolicies [task .placementInfo .Name ].Content = "us-west-1"
1480
+ return nil
1481
+ }
1482
+
1287
1483
type ddlTestAddDropColumnStrategy = int
1288
1484
1289
1485
const (
@@ -1574,6 +1770,8 @@ func (c *testCase) prepareDropColumn(_ interface{}, taskCh chan *ddlJobTask) err
1574
1770
return nil
1575
1771
}
1576
1772
1773
+ table .lock .Lock ()
1774
+ defer table .lock .Unlock ()
1577
1775
columnsSnapshot := table .filterColumns (table .predicateAll )
1578
1776
if len (columnsSnapshot ) <= 1 {
1579
1777
return nil
@@ -1734,6 +1932,9 @@ func (c *testCase) generateModifySchemaCharsetAndCollate(repeat int) error {
1734
1932
1735
1933
func (c * testCase ) prepareModifySchemaCharsetAndCollate (_ interface {}, taskCh chan * ddlJobTask ) error {
1736
1934
schema := c .pickupRandomSchema ()
1935
+ if schema == nil {
1936
+ return nil
1937
+ }
1737
1938
charset , collation := c .pickupRandomCharsetAndCollate ()
1738
1939
taskCh <- & ddlJobTask {
1739
1940
k : ActionModifySchemaCharsetAndCollate ,
0 commit comments