Skip to content

Commit

Permalink
[feature](cloud) Support rename compute group sql
Browse files Browse the repository at this point in the history
  • Loading branch information
deardeng committed Dec 31, 2024
1 parent b959c66 commit 8f724c8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ supportedAlterStatement
dropRollupClause (COMMA dropRollupClause)* #alterTableDropRollup
| ALTER TABLE name=multipartIdentifier
SET LEFT_PAREN propertyItemList RIGHT_PAREN #alterTableProperties
| ALTER SYSTEM RENAME COMPUTE GROUP name=identifier RENAME newName=identifier #alterSystemRenameComputeGroup
;

supportedDropStatement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ public void dropFrontend(Frontend frontend) throws DdlException {

private String tryCreateComputeGroup(String clusterName, String computeGroupId) throws UserException {
if (Strings.isNullOrEmpty(((CloudEnv) Env.getCurrentEnv()).getCloudInstanceId())) {
throw new DdlException("unable to create compute group due to empty cluster_id");
throw new DdlException("unable to create compute group due to empty cloud_instance_id");
}

Cloud.ClusterPB clusterPB = Cloud.ClusterPB.newBuilder()
Expand Down Expand Up @@ -1158,4 +1158,43 @@ public String getInstanceId(String cloudUniqueId) throws IOException {
throw new IOException("Failed to get instance info");
}
}

public void renameComputeGroup(String originalName, String newGroupName) throws UserException {
String cloudInstanceId = ((CloudEnv) Env.getCurrentEnv()).getCloudInstanceId();
if (Strings.isNullOrEmpty(cloudInstanceId)) {
throw new DdlException("unable to rename compute group due to empty cloud_instance_id");
}
String originalComputeGroupId = ((CloudSystemInfoService) Env.getCurrentSystemInfo())
.getCloudClusterIdByName(originalName);
if (Strings.isNullOrEmpty(originalComputeGroupId)) {
throw new DdlException("unable to rename compute group, "
+ "cant get compute group id by compute name " + originalName);
}

Cloud.ClusterPB clusterPB = Cloud.ClusterPB.newBuilder()
.setClusterId(originalComputeGroupId)
.setClusterName(newGroupName)
.setType(Cloud.ClusterPB.Type.COMPUTE)
.build();

Cloud.AlterClusterRequest request = Cloud.AlterClusterRequest.newBuilder()
.setInstanceId(((CloudEnv) Env.getCurrentEnv()).getCloudInstanceId())
.setOp(Cloud.AlterClusterRequest.Operation.RENAME_CLUSTER)
.setCluster(clusterPB)
.build();


Cloud.AlterClusterResponse response;
try {
response = MetaServiceProxy.getInstance().alterCluster(request);
LOG.info("alter rename compute group, request: {}, response: {}", request, response);
if (response.getStatus().getCode() != Cloud.MetaServiceCode.OK) {
LOG.warn("alter rename compute group not ok, response: {}", response);
throw new UserException("failed to rename compute group errorCode: " + response.getStatus().getCode()
+ " msg: " + response.getStatus().getMsg());
}
} catch (RpcException e) {
throw new UserException("failed to alter rename compute group", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.apache.doris.nereids.DorisParser.AlterRoleContext;
import org.apache.doris.nereids.DorisParser.AlterSqlBlockRuleContext;
import org.apache.doris.nereids.DorisParser.AlterStorageVaultContext;
import org.apache.doris.nereids.DorisParser.AlterSystemRenameComputeGroupContext;
import org.apache.doris.nereids.DorisParser.AlterTableAddRollupContext;
import org.apache.doris.nereids.DorisParser.AlterTableClauseContext;
import org.apache.doris.nereids.DorisParser.AlterTableContext;
Expand Down Expand Up @@ -487,6 +488,7 @@
import org.apache.doris.nereids.trees.plans.commands.AlterRoleCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterStorageVaultCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterSystemRenameComputeGroupCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterTableCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterViewCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterWorkloadGroupCommand;
Expand Down Expand Up @@ -1284,6 +1286,11 @@ public LogicalPlan visitAlterStorageVault(AlterStorageVaultContext ctx) {
return new AlterStorageVaultCommand(vaultName, properties);
}

@Override
public LogicalPlan visitAlterSystemRenameComputeGroup(AlterSystemRenameComputeGroupContext ctx) {
return new AlterSystemRenameComputeGroupCommand(ctx.name.getText(), ctx.newName.getText());
}

@Override
public LogicalPlan visitShowConstraint(ShowConstraintContext ctx) {
List<String> parts = visitMultipartIdentifier(ctx.table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,6 @@ public enum PlanType {
SHOW_QUERY_PROFILE_COMMAND,
SWITCH_COMMAND,
HELP_COMMAND,
USE_COMMAND
USE_COMMAND,
ALTER_SYSTEM_RENAME_COMPUTE_GROUP
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.plans.commands;

import org.apache.doris.catalog.Env;
import org.apache.doris.cloud.system.CloudSystemInfoService;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.DdlException;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.StmtExecutor;

import com.google.common.base.Strings;

/**
* Alter Storage Vault command
*/
@SuppressWarnings("checkstyle:RegexpSingleline")
public class AlterSystemRenameComputeGroupCommand extends Command implements ForwardWithSync {
private final String originalName;
private final String newName;

public AlterSystemRenameComputeGroupCommand(String originalName, String newName) {
super(PlanType.ALTER_SYSTEM_RENAME_COMPUTE_GROUP);
this.originalName = originalName;
this.newName = newName;
}

private void validate() throws AnalysisException {
if (Strings.isNullOrEmpty(originalName) || Strings.isNullOrEmpty(newName)) {
throw new AnalysisException("rename group requires non-empty or non-empty name");
}
if (originalName.equals(newName)) {
throw new AnalysisException("rename compute group original name eq new name");
}
}

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
validate();
doRun(ctx);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitCommand(this, context);
}

private void doRun(ConnectContext ctx) throws Exception {
try {
// 1. send rename rpc to ms
((CloudSystemInfoService) Env.getCurrentSystemInfo()).renameComputeGroup(this.originalName, this.newName);
// 2. if 1 not throw exception, refresh cloud cluster
// if not do 2, will wait 10s to get new name
} catch (Exception e) {
throw new DdlException(e.getMessage());
}
}
}

0 comments on commit 8f724c8

Please sign in to comment.