Skip to content

Commit 9c4c0b3

Browse files
authored
[Enhancement] (nereids)implement CreateWorkloadGroupCommand in nereids (#44970)
Issue Number: close #42589
1 parent 90cbfc5 commit 9c4c0b3

File tree

7 files changed

+130
-9
lines changed

7 files changed

+130
-9
lines changed

fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ supportedCreateStatement
183183
| CREATE (EXTERNAL)? TABLE (IF NOT EXISTS)? name=multipartIdentifier
184184
LIKE existedTable=multipartIdentifier
185185
(WITH ROLLUP (rollupNames=identifierList)?)? #createTableLike
186-
| CREATE ROLE (IF NOT EXISTS)? name=identifier (COMMENT STRING_LITERAL)? #createRole
186+
| CREATE ROLE (IF NOT EXISTS)? name=identifier (COMMENT STRING_LITERAL)? #createRole
187+
| CREATE WORKLOAD GROUP (IF NOT EXISTS)?
188+
name=identifierOrText properties=propertyClause? #createWorkloadGroup
187189
| CREATE ROW POLICY (IF NOT EXISTS)? name=identifier
188190
ON table=multipartIdentifier
189191
AS type=(RESTRICTIVE | PERMISSIVE)
@@ -764,8 +766,6 @@ unsupportedCreateStatement
764766
name=identifierOrText properties=propertyClause? #createResource
765767
| CREATE STORAGE VAULT (IF NOT EXISTS)?
766768
name=identifierOrText properties=propertyClause? #createStorageVault
767-
| CREATE WORKLOAD GROUP (IF NOT EXISTS)?
768-
name=identifierOrText properties=propertyClause? #createWorkloadGroup
769769
| CREATE WORKLOAD POLICY (IF NOT EXISTS)? name=identifierOrText
770770
(CONDITIONS LEFT_PAREN workloadPolicyConditions RIGHT_PAREN)?
771771
(ACTIONS LEFT_PAREN workloadPolicyActions RIGHT_PAREN)?

fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
import org.apache.doris.nereids.DorisParser.CreateTableContext;
105105
import org.apache.doris.nereids.DorisParser.CreateTableLikeContext;
106106
import org.apache.doris.nereids.DorisParser.CreateViewContext;
107+
import org.apache.doris.nereids.DorisParser.CreateWorkloadGroupContext;
107108
import org.apache.doris.nereids.DorisParser.CteContext;
108109
import org.apache.doris.nereids.DorisParser.DataTypeWithNullableContext;
109110
import org.apache.doris.nereids.DorisParser.DateCeilContext;
@@ -502,6 +503,7 @@
502503
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
503504
import org.apache.doris.nereids.trees.plans.commands.CreateTableLikeCommand;
504505
import org.apache.doris.nereids.trees.plans.commands.CreateViewCommand;
506+
import org.apache.doris.nereids.trees.plans.commands.CreateWorkloadGroupCommand;
505507
import org.apache.doris.nereids.trees.plans.commands.DeleteFromCommand;
506508
import org.apache.doris.nereids.trees.plans.commands.DeleteFromUsingCommand;
507509
import org.apache.doris.nereids.trees.plans.commands.DropCatalogRecycleBinCommand;
@@ -4862,6 +4864,15 @@ public LogicalPlan visitDropEncryptkey(DropEncryptkeyContext ctx) {
48624864
return new DropEncryptkeyCommand(new EncryptKeyName(nameParts), ctx.EXISTS() != null);
48634865
}
48644866

4867+
@Override
4868+
public LogicalPlan visitCreateWorkloadGroup(CreateWorkloadGroupContext ctx) {
4869+
String workloadGroupName = stripQuotes(ctx.name.getText());
4870+
boolean ifNotExists = ctx.EXISTS() != null;
4871+
Map<String, String> properties = ctx.propertyClause() != null
4872+
? Maps.newHashMap(visitPropertyClause(ctx.propertyClause())) : Maps.newHashMap();
4873+
return new CreateWorkloadGroupCommand(workloadGroupName, ifNotExists, properties);
4874+
}
4875+
48654876
@Override
48664877
public LogicalPlan visitDropFile(DropFileContext ctx) {
48674878
String dbName = null;

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ public enum PlanType {
247247
RECOVER_PARTITION_COMMAND,
248248
REPLAY_COMMAND,
249249
CREATE_ENCRYPTKEY_COMMAND,
250+
CREATE_WORKLOAD_GROUP_COMMAND,
250251
CREATE_FILE_COMMAND,
251252
CREATE_ROUTINE_LOAD_COMMAND
252253
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.commands;
19+
20+
import org.apache.doris.analysis.StmtType;
21+
import org.apache.doris.catalog.Env;
22+
import org.apache.doris.common.AnalysisException;
23+
import org.apache.doris.common.ErrorCode;
24+
import org.apache.doris.common.ErrorReport;
25+
import org.apache.doris.common.FeNameFormat;
26+
import org.apache.doris.mysql.privilege.PrivPredicate;
27+
import org.apache.doris.nereids.trees.plans.PlanType;
28+
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
29+
import org.apache.doris.qe.ConnectContext;
30+
import org.apache.doris.qe.StmtExecutor;
31+
import org.apache.doris.resource.workloadgroup.WorkloadGroup;
32+
import org.apache.doris.resource.workloadgroup.WorkloadGroupMgr;
33+
34+
import org.apache.commons.lang3.StringUtils;
35+
36+
import java.util.Map;
37+
38+
/**
39+
* Create workload group command
40+
*/
41+
public class CreateWorkloadGroupCommand extends Command implements ForwardWithSync {
42+
private final boolean ifNotExists;
43+
private final String workloadGroupName;
44+
private final Map<String, String> properties;
45+
46+
/**
47+
* Constructor for CreateWorkloadGroupCommand
48+
*/
49+
public CreateWorkloadGroupCommand(String workloadGroupName, boolean ifNotExists, Map<String, String> properties) {
50+
super(PlanType.CREATE_WORKLOAD_GROUP_COMMAND);
51+
this.workloadGroupName = workloadGroupName;
52+
this.ifNotExists = ifNotExists;
53+
this.properties = properties;
54+
}
55+
56+
private void validate(ConnectContext ctx) throws AnalysisException {
57+
// check auth
58+
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.ADMIN)) {
59+
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "ADMIN");
60+
}
61+
62+
// check name
63+
FeNameFormat.checkWorkloadGroupName(workloadGroupName);
64+
65+
if (properties == null || properties.isEmpty()) {
66+
throw new AnalysisException("Workload Group properties can't be empty");
67+
}
68+
69+
if (properties.containsKey(WorkloadGroup.INTERNAL_TYPE)) {
70+
throw new AnalysisException(WorkloadGroup.INTERNAL_TYPE + " can not be create or modified ");
71+
}
72+
73+
String tagStr = properties.get(WorkloadGroup.TAG);
74+
if (!StringUtils.isEmpty(tagStr) && (WorkloadGroupMgr.DEFAULT_GROUP_NAME.equals(workloadGroupName)
75+
|| WorkloadGroupMgr.INTERNAL_GROUP_NAME.equals(workloadGroupName))) {
76+
throw new AnalysisException(
77+
WorkloadGroupMgr.INTERNAL_GROUP_NAME + " and " + WorkloadGroupMgr.DEFAULT_GROUP_NAME
78+
+ " group can not set tag");
79+
}
80+
}
81+
82+
@Override
83+
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
84+
validate(ctx);
85+
// Create workload group
86+
WorkloadGroup workloadGroup = WorkloadGroup.create(workloadGroupName, properties);
87+
WorkloadGroupMgr workloadGroupMgr = Env.getCurrentEnv().getWorkloadGroupMgr();
88+
workloadGroupMgr.createWorkloadGroup(workloadGroup, ifNotExists);
89+
}
90+
91+
@Override
92+
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
93+
return visitor.visitCreateWorkloadGroupCommand(this, context);
94+
}
95+
96+
@Override
97+
public StmtType stmtType() {
98+
return StmtType.CREATE;
99+
}
100+
}

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/visitor/CommandVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.doris.nereids.trees.plans.commands.CreateTableCommand;
4848
import org.apache.doris.nereids.trees.plans.commands.CreateTableLikeCommand;
4949
import org.apache.doris.nereids.trees.plans.commands.CreateViewCommand;
50+
import org.apache.doris.nereids.trees.plans.commands.CreateWorkloadGroupCommand;
5051
import org.apache.doris.nereids.trees.plans.commands.DeleteFromCommand;
5152
import org.apache.doris.nereids.trees.plans.commands.DeleteFromUsingCommand;
5253
import org.apache.doris.nereids.trees.plans.commands.DropCatalogRecycleBinCommand;
@@ -562,6 +563,10 @@ default R visitShowTableIdCommand(ShowTableIdCommand showTableIdCommand, C conte
562563
return visitCommand(showTableIdCommand, context);
563564
}
564565

566+
default R visitCreateWorkloadGroupCommand(CreateWorkloadGroupCommand createWorkloadGroupCommand, C context) {
567+
return visitCommand(createWorkloadGroupCommand, context);
568+
}
569+
565570
default R visitShowEncryptKeysCommand(ShowEncryptKeysCommand showEncryptKeysCommand, C context) {
566571
return visitCommand(showEncryptKeysCommand, context);
567572
}

fe/fe-core/src/main/java/org/apache/doris/resource/workloadgroup/WorkloadGroupMgr.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,12 @@ private String getWorkloadGroupNameAndCheckPriv(ConnectContext context) throws A
357357
return groupName;
358358
}
359359

360-
public void createWorkloadGroup(CreateWorkloadGroupStmt stmt) throws DdlException {
361-
WorkloadGroup workloadGroup = WorkloadGroup.create(stmt.getWorkloadGroupName(), stmt.getProperties());
360+
public void createWorkloadGroup(WorkloadGroup workloadGroup, boolean isIfNotExists) throws DdlException {
362361
String workloadGroupName = workloadGroup.getName();
363362
writeLock();
364363
try {
365364
if (nameToWorkloadGroup.containsKey(workloadGroupName)) {
366-
if (stmt.isIfNotExists()) {
365+
if (isIfNotExists) {
367366
return;
368367
}
369368
throw new DdlException("workload group " + workloadGroupName + " already exist");
@@ -382,6 +381,11 @@ public void createWorkloadGroup(CreateWorkloadGroupStmt stmt) throws DdlExceptio
382381
LOG.info("Create workload group success: {}", workloadGroup);
383382
}
384383

384+
public void createWorkloadGroup(CreateWorkloadGroupStmt stmt) throws DdlException {
385+
WorkloadGroup workloadGroup = WorkloadGroup.create(stmt.getWorkloadGroupName(), stmt.getProperties());
386+
createWorkloadGroup(workloadGroup, stmt.isIfNotExists());
387+
}
388+
385389
public void createInternalWorkloadGroup() {
386390
Map<String, String> properties = Maps.newHashMap();
387391
// 100 is cgroup v2 default cpu_share value

regression-test/suites/workload_manager_p0/test_nereids_workload_test.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
suite("test_nereids_workload_test") {
1919
sql "drop workload group if exists test_nereids_wg1;"
2020
sql "drop workload group if exists test_nereids_wg2;"
21-
sql "create workload group test_nereids_wg1 properties('cpu_share'='1024');"
22-
sql "create workload group test_nereids_wg2 properties('cpu_share'='1024');"
21+
checkNereidsExecute("create workload group test_nereids_wg1 properties('cpu_share'='1024');")
22+
checkNereidsExecute("create workload group test_nereids_wg2 properties('cpu_share'='1024');")
2323
qt_check_workload_check1("select NAME from information_schema.workload_groups where NAME='test_nereids_wg1';")
2424
checkNereidsExecute("drop workload group test_nereids_wg1;")
2525
qt_check_workload_check2("select NAME from information_schema.workload_groups where NAME='test_nereids_wg1';")
2626
checkNereidsExecute("drop workload group if exists test_nereids_wg2;")
27-
}
27+
}

0 commit comments

Comments
 (0)