Skip to content

Commit 0d4be6f

Browse files
committed
alter placement rule relate
1 parent 7754f7e commit 0d4be6f

File tree

5 files changed

+262
-38
lines changed

5 files changed

+262
-38
lines changed

ddl/ddl.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,15 @@ func NewDDLCase(cfg *DDLCaseConfig) *DDLCase {
153153
cases := make([]*testCase, cfg.Concurrency)
154154
for i := 0; i < cfg.Concurrency; i++ {
155155
cases[i] = &testCase{
156-
cfg: cfg,
157-
tables: make(map[string]*ddlTestTable),
158-
schemas: make(map[string]*ddlTestSchema),
159-
views: make(map[string]*ddlTestView),
160-
ddlOps: make([]ddlTestOpExecutor, 0),
161-
dmlOps: make([]dmlTestOpExecutor, 0),
162-
caseIndex: i,
163-
stop: 0,
156+
cfg: cfg,
157+
tables: make(map[string]*ddlTestTable),
158+
schemas: make(map[string]*ddlTestSchema),
159+
views: make(map[string]*ddlTestView),
160+
placementPolicies: map[string]*ddlTestPlacementPolicy{},
161+
ddlOps: make([]ddlTestOpExecutor, 0),
162+
dmlOps: make([]dmlTestOpExecutor, 0),
163+
caseIndex: i,
164+
stop: 0,
164165
}
165166
}
166167
b := &DDLCase{
@@ -409,8 +410,7 @@ func (c *testCase) execute(executeDDL ExecuteDDLFunc, exeDMLFunc ExecuteDMLFunc)
409410
log.Infof("[ddl] [instance %d] Executing post round operations...", c.caseIndex)
410411

411412
if !c.cfg.MySQLCompatible {
412-
err := c.executeAdminCheck()
413-
if err != nil {
413+
if err := c.executeAdminCheck(); err != nil {
414414
return errors.Trace(err)
415415
}
416416
}
@@ -593,6 +593,8 @@ func (c *testCase) executeAdminCheck() error {
593593
return nil
594594
}
595595

596+
c.tablesLock.Lock()
597+
defer c.tablesLock.Unlock()
596598
// build SQL
597599
sql := "ADMIN CHECK TABLE "
598600
i := 0
@@ -605,6 +607,9 @@ func (c *testCase) executeAdminCheck() error {
605607
for _, index := range table.indexes {
606608
checkIndexSQL := fmt.Sprintf("admin check index `%s` `%s`", table.name, index.name)
607609
_, err := c.pickupDB().Exec(checkIndexSQL)
610+
if ddlIgnoreError(err) {
611+
continue
612+
}
608613
if err != nil {
609614
return errors.Annotatef(err, "Error when executing SQL: %s", checkIndexSQL)
610615
}

ddl/ddl_ops.go

+211-10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ func (c *testCase) generateDDLOps() error {
9090
if err := c.generateAlterIndexVisibility(defaultTime); err != nil {
9191
return errors.Trace(err)
9292
}
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+
}
93105
return nil
94106
}
95107

@@ -128,6 +140,11 @@ const (
128140

129141
ActionAlterIndexVisibility
130142

143+
ActionCreatePlacementPolicy
144+
ActionDropPlacementPolicy
145+
ActionAddPlacementPolicyToTable
146+
ActionAlterPlacementPolicy
147+
131148
ddlKindNil
132149
)
133150

@@ -164,6 +181,11 @@ var mapOfDDLKind = map[string]DDLKind{
164181
"drop primary key": ActionDropPrimaryKey,
165182

166183
"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,
167189
}
168190

169191
var mapOfDDLKindToString = map[DDLKind]string{
@@ -198,6 +220,11 @@ var mapOfDDLKindToString = map[DDLKind]string{
198220
ActionDropPrimaryKey: "drop primary key",
199221

200222
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",
201228
}
202229

203230
// mapOfDDLKindProbability use to control every kind of ddl request execute probability.
@@ -232,6 +259,11 @@ var mapOfDDLKindProbability = map[DDLKind]float64{
232259
ActionDropPrimaryKey: 0.10,
233260

234261
ActionAlterIndexVisibility: 0.20,
262+
263+
ActionCreatePlacementPolicy: 0.20,
264+
ActionDropPlacementPolicy: 0.20,
265+
ActionAddPlacementPolicyToTable: 0.20,
266+
ActionAlterPlacementPolicy: 0.20,
235267
}
236268

237269
type ddlJob struct {
@@ -247,14 +279,15 @@ type ddlJob struct {
247279
type ddlJobArg unsafe.Pointer
248280

249281
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.
258291
}
259292

260293
// 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 {
284317
return c.dropTableJob(task)
285318
case ddlCreateView:
286319
return c.createViewJob(task)
320+
case ddlDropView:
321+
return c.dropViewJob(task)
287322
case ddlAddIndex:
288323
return c.addIndexJob(task)
289324
case ddlRenameIndex:
@@ -306,6 +341,16 @@ func (c *testCase) updateTableInfo(task *ddlJobTask) error {
306341
return c.setAddPrimaryKey(task)
307342
case ActionDropPrimaryKey:
308343
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)
309354
}
310355
return fmt.Errorf("unknow ddl task , %v", *task)
311356
}
@@ -1064,8 +1109,9 @@ func (c *testCase) prepareAddPrimaryKey(_ interface{}, taskCh chan *ddlJobTask)
10641109
perm := rand.Perm(table.columns.Size())
10651110
// build SQL
10661111
sql := fmt.Sprintf("ALTER TABLE `%s` ADD PRIMARY KEY (", table.name)
1112+
var column *ddlTestColumn
10671113
for _, i := range perm {
1068-
column := getColumnFromArrayList(table.columns, i)
1114+
column = getColumnFromArrayList(table.columns, i)
10691115
if column.canBePrimary() {
10701116
sql += fmt.Sprintf("`%s`", column.name)
10711117
break
@@ -1077,6 +1123,7 @@ func (c *testCase) prepareAddPrimaryKey(_ interface{}, taskCh chan *ddlJobTask)
10771123
k: ActionAddPrimaryKey,
10781124
sql: sql,
10791125
tblInfo: table,
1126+
arg: ddlJobArg(column),
10801127
}
10811128
taskCh <- task
10821129
return nil
@@ -1085,6 +1132,8 @@ func (c *testCase) prepareAddPrimaryKey(_ interface{}, taskCh chan *ddlJobTask)
10851132
func (c *testCase) setAddPrimaryKey(task *ddlJobTask) error {
10861133
tblInfo := task.tblInfo
10871134
tblInfo.hasPK = true
1135+
col := (*ddlTestColumn)(task.arg)
1136+
col.isPrimaryKey = true
10881137
return nil
10891138
}
10901139

@@ -1117,6 +1166,13 @@ func (c *testCase) prepareDropPrimaryKey(_ interface{}, taskCh chan *ddlJobTask)
11171166
func (c *testCase) setDropPrimaryKey(task *ddlJobTask) error {
11181167
tblInfo := task.tblInfo
11191168
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+
}
11201176
return nil
11211177
}
11221178

@@ -1261,7 +1317,7 @@ func (c *testCase) prepareAlterIndexVisibility(_ interface{}, taskCh chan *ddlJo
12611317
return nil
12621318
}
12631319

1264-
func (c *testCase) dropAlterIndexVisibilityJob(task *ddlJobTask) error {
1320+
func (c *testCase) setAlterIndexVisibility(task *ddlJobTask) error {
12651321
jobArg := (*ddlIndexJobArg)(task.arg)
12661322
tblInfo := task.tblInfo
12671323

@@ -1284,6 +1340,146 @@ func (c *testCase) dropAlterIndexVisibilityJob(task *ddlJobTask) error {
12841340
return nil
12851341
}
12861342

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+
12871483
type ddlTestAddDropColumnStrategy = int
12881484

12891485
const (
@@ -1574,6 +1770,8 @@ func (c *testCase) prepareDropColumn(_ interface{}, taskCh chan *ddlJobTask) err
15741770
return nil
15751771
}
15761772

1773+
table.lock.Lock()
1774+
defer table.lock.Unlock()
15771775
columnsSnapshot := table.filterColumns(table.predicateAll)
15781776
if len(columnsSnapshot) <= 1 {
15791777
return nil
@@ -1734,6 +1932,9 @@ func (c *testCase) generateModifySchemaCharsetAndCollate(repeat int) error {
17341932

17351933
func (c *testCase) prepareModifySchemaCharsetAndCollate(_ interface{}, taskCh chan *ddlJobTask) error {
17361934
schema := c.pickupRandomSchema()
1935+
if schema == nil {
1936+
return nil
1937+
}
17371938
charset, collation := c.pickupRandomCharsetAndCollate()
17381939
taskCh <- &ddlJobTask{
17391940
k: ActionModifySchemaCharsetAndCollate,

0 commit comments

Comments
 (0)