You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/01/07 14:16:01 UTC
[shardingsphere] branch master updated: add support drop resource
(#8894)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 89b3678 add support drop resource (#8894)
89b3678 is described below
commit 89b3678f3bb3206c3b82c9d1685e496a39eaceb1
Author: JingShang Lu <lu...@apache.org>
AuthorDate: Thu Jan 7 22:15:39 2021 +0800
add support drop resource (#8894)
* add support drop resource
* add check
* fix
* Update CommonErrorCode.java
* Update DistSQLVisitor.java
* fix
* fix
* fix
* Update CommonErrorCode.java
* Update ConfigCenter.java
---
.../db/protocol/error/CommonErrorCode.java | 4 +
.../src/main/antlr4/imports/RDLStatement.g4 | 4 +
.../distsql/parser/autogen/DistSQLStatement.g4 | 1 +
.../distsql/parser/core/DistSQLVisitor.java | 11 ++
.../rdl/drop/impl/DropResourceStatement.java | 16 ++-
.../governance/core/config/ConfigCenter.java | 10 ++
.../ReplicaQueryRuleNotExistedException.java | 2 +-
...Exception.java => ResourceInUsedException.java} | 8 +-
...ption.java => ResourceNotExistedException.java} | 8 +-
.../text/distsql/rdl/RDLBackendHandlerFactory.java | 5 +
.../rdl/impl/DropResourceBackendHandler.java | 120 +++++++++++++++++++++
.../text/distsql/RDLBackendHandlerFactoryTest.java | 69 ++++++++++++
.../frontend/mysql/MySQLErrPacketFactory.java | 8 ++
13 files changed, 248 insertions(+), 18 deletions(-)
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
index 0f49283..f42e499 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
@@ -32,6 +32,10 @@ public enum CommonErrorCode implements SQLErrorCode {
SHARDING_TABLE_RULES_NOT_EXISTED(11001, "C11001", "Sharding table rule %s is not exist."),
TABLES_IN_USED(11002, "C11002", "Can not drop rule, tables %s in the rule are still in used."),
+
+ RESOURCE_IN_USED(11003, "C11003", "Can not drop resources, resources %s in the rule are still in used."),
+
+ RESOURCE_NOT_EXIST(11004, "C11004", "Can not drop resources, resources %s do not exist."),
UNSUPPORTED_COMMAND(19998, "C19998", "Unsupported command: [%s]"),
diff --git a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4 b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
index 88a69d7..138933d 100644
--- a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
+++ b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
@@ -23,6 +23,10 @@ addResource
: ADD RESOURCE LP dataSource (COMMA dataSource)* RP
;
+dropResource
+ : DROP RESOURCE LP IDENTIFIER (COMMA IDENTIFIER)* RP
+ ;
+
dataSource
: dataSourceName EQ dataSourceDefinition
;
diff --git a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4 b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
index 4636690..b1d92dd 100644
--- a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
+++ b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
@@ -21,6 +21,7 @@ import Symbol, RDLStatement, RQLStatement;
execute
: (addResource
+ | dropResource
| createShardingRule
| dropShardingRule
| createReplicaQueryRule
diff --git a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
index 6e27832..dfdb630 100644
--- a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
+++ b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.C
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DataSourceContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DataSourceDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropReplicaQueryRuleContext;
+import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropResourceContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropShardingRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ReplicaQueryRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.SchemaNameContext;
@@ -44,6 +45,7 @@ import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AlterR
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateReplicaQueryRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
@@ -79,6 +81,15 @@ public final class DistSQLVisitor extends DistSQLStatementBaseVisitor<ASTNode> {
}
@Override
+ public ASTNode visitDropResource(final DropResourceContext ctx) {
+ DropResourceStatement result = new DropResourceStatement();
+ for (TerminalNode each : ctx.IDENTIFIER()) {
+ result.getResourceNames().add(each.getText());
+ }
+ return result;
+ }
+
+ @Override
public ASTNode visitDataSourceDefinition(final DataSourceDefinitionContext ctx) {
DataSourceSegment result = new DataSourceSegment();
result.setHostName(ctx.hostName().getText());
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropResourceStatement.java
similarity index 70%
copy from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
copy to shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropResourceStatement.java
index 9ee7f04..9f79cf3 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
+++ b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropResourceStatement.java
@@ -15,21 +15,19 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.exception;
+package org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropRDLStatement;
import java.util.Collection;
+import java.util.LinkedList;
/**
- * replica query rule not existed exception.
+ * Drop resource statement.
*/
-@RequiredArgsConstructor
@Getter
-public final class ReplicaQueryRuleNotExistedException extends BackendException {
-
- private static final long serialVersionUID = -5119217255419990719L;
-
- private final Collection<String> ruleNames;
+public final class DropResourceStatement extends DropRDLStatement {
+
+ private final Collection<String> resourceNames = new LinkedList<>();
}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
index 68ab38a..fc5750f 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
@@ -23,6 +23,7 @@ import com.google.common.base.Strings;
import com.google.common.eventbus.Subscribe;
import org.apache.shardingsphere.governance.core.config.checker.RuleConfigurationChecker;
import org.apache.shardingsphere.governance.core.config.checker.RuleConfigurationCheckerFactory;
+import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourcePersistEvent;
import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsAlteredEvent;
import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
@@ -113,6 +114,15 @@ public final class ConfigCenter {
public synchronized void renew(final DataSourcePersistEvent event) {
addDataSourceConfigurations(event.getSchemaName(), event.getDataSourceConfigurations());
}
+
+ /**
+ * Change data source configurations.
+ * @param event Data source event.
+ */
+ @Subscribe
+ public synchronized void renew(final DataSourceChangedEvent event) {
+ persistDataSourceConfigurations(event.getSchemaName(), event.getDataSourceConfigurations());
+ }
/**
* Persist rule configurations.
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
index 9ee7f04..bcc955b 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
@@ -23,7 +23,7 @@ import lombok.RequiredArgsConstructor;
import java.util.Collection;
/**
- * replica query rule not existed exception.
+ * Replica query rule not existed exception.
*/
@RequiredArgsConstructor
@Getter
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ResourceInUsedException.java
similarity index 80%
copy from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
copy to shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ResourceInUsedException.java
index 9ee7f04..7609f53 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ResourceInUsedException.java
@@ -23,13 +23,13 @@ import lombok.RequiredArgsConstructor;
import java.util.Collection;
/**
- * replica query rule not existed exception.
+ * Resource in used exception.
*/
@RequiredArgsConstructor
@Getter
-public final class ReplicaQueryRuleNotExistedException extends BackendException {
+public final class ResourceInUsedException extends BackendException {
- private static final long serialVersionUID = -5119217255419990719L;
+ private static final long serialVersionUID = -3427324685070457375L;
- private final Collection<String> ruleNames;
+ private final Collection<String> resourceNames;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ResourceNotExistedException.java
similarity index 80%
copy from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
copy to shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ResourceNotExistedException.java
index 9ee7f04..3a6d7f1 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ResourceNotExistedException.java
@@ -23,13 +23,13 @@ import lombok.RequiredArgsConstructor;
import java.util.Collection;
/**
- * replica query rule not existed exception.
+ * Resource not existed exception.
*/
@RequiredArgsConstructor
@Getter
-public final class ReplicaQueryRuleNotExistedException extends BackendException {
+public final class ResourceNotExistedException extends BackendException {
- private static final long serialVersionUID = -5119217255419990719L;
+ private static final long serialVersionUID = 1704331180489268L;
- private final Collection<String> ruleNames;
+ private final Collection<String> resourceNames;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
index 57bc594..b04a7c6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
@@ -24,6 +24,7 @@ import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AlterR
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateReplicaQueryRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropShardingRuleStatement;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -37,6 +38,7 @@ import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.CreateRepli
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.CreateShardingRuleBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropDatabaseBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropReplicaQueryRuleBackendHandler;
+import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropResourceBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.DropShardingRuleBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
@@ -78,6 +80,9 @@ public final class RDLBackendHandlerFactory {
if (sqlStatement instanceof AddResourceStatement) {
return Optional.of(new AddResourceBackendHandler(databaseType, (AddResourceStatement) sqlStatement, backendConnection));
}
+ if (sqlStatement instanceof DropResourceStatement) {
+ return Optional.of(new DropResourceBackendHandler((DropResourceStatement) sqlStatement, backendConnection));
+ }
if (sqlStatement instanceof CreateDatabaseStatement) {
return Optional.of(new CreateDatabaseBackendHandler((CreateDatabaseStatement) sqlStatement));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropResourceBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropResourceBackendHandler.java
new file mode 100644
index 0000000..be37219
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropResourceBackendHandler.java
@@ -0,0 +1,120 @@
+/*
+ * 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.shardingsphere.proxy.backend.text.distsql.rdl.impl;
+
+import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
+import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangedEvent;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
+import org.apache.shardingsphere.infra.rule.type.DataSourceContainedRule;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
+import org.apache.shardingsphere.proxy.backend.exception.ResourceInUsedException;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import org.apache.shardingsphere.proxy.backend.text.SchemaRequiredBackendHandler;
+
+import javax.sql.DataSource;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Drop resource backend handler.
+ */
+public final class DropResourceBackendHandler extends SchemaRequiredBackendHandler<DropResourceStatement> {
+
+ public DropResourceBackendHandler(final DropResourceStatement sqlStatement, final BackendConnection backendConnection) {
+ super(sqlStatement, backendConnection);
+ }
+
+ @Override
+ public ResponseHeader execute(final String schemaName, final DropResourceStatement sqlStatement) {
+ Collection<String> resourceNames = sqlStatement.getResourceNames();
+ check(schemaName, resourceNames);
+ Map<String, DataSource> resourceMap = drop(schemaName, resourceNames);
+ post(schemaName, resourceMap);
+ return new UpdateResponseHeader(sqlStatement);
+ }
+
+ private void check(final String schemaName, final Collection<String> resourceNames) {
+ Map<String, DataSource> resourceMap = ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources();
+ if (null == resourceMap || resourceMap.isEmpty()) {
+ throw new ResourceNotExistedException(resourceNames);
+ }
+ Collection<String> notExistedResourceNames = resourceNames.stream().filter(each -> !resourceMap.containsKey(each)).collect(Collectors.toList());
+ if (!notExistedResourceNames.isEmpty()) {
+ throw new ResourceNotExistedException(notExistedResourceNames);
+ }
+ Collection<ShardingSphereRule> ruleConfig = ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getRules();
+ Set<String> useResources = new HashSet<>();
+ for (ShardingSphereRule each : ruleConfig) {
+ if (each instanceof DataSourceContainedRule) {
+ useResources = getResouces((DataSourceContainedRule) each);
+ } else if (each instanceof DataNodeContainedRule) {
+ useResources = getResouces((DataNodeContainedRule) each);
+ }
+ }
+ Collection<String> conflictResources = new LinkedList<>();
+ for (String each : useResources) {
+ if (useResources.contains(each)) {
+ conflictResources.add(each);
+ }
+ }
+ if (!conflictResources.isEmpty()) {
+ throw new ResourceInUsedException(notExistedResourceNames);
+ }
+ }
+
+ private Set<String> getResouces(final DataSourceContainedRule rule) {
+ Set<String> result = new HashSet<>();
+ for (Collection<String> each : rule.getDataSourceMapper().values()) {
+ result.addAll(each);
+ }
+ return result;
+ }
+
+ private Set<String> getResouces(final DataNodeContainedRule rule) {
+ Set<String> result = new HashSet<>();
+ for (Collection<DataNode> each : rule.getAllDataNodes().values()) {
+ result.addAll(each.stream().map(DataNode::getDataSourceName).collect(Collectors.toList()));
+ }
+ return result;
+ }
+
+ private Map<String, DataSource> drop(final String schemaName, final Collection<String> resourceNames) {
+ Map<String, DataSource> result = ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources();
+ for (String each : resourceNames) {
+ result.remove(each);
+ }
+ return result;
+ }
+
+ private void post(final String schemaName, final Map<String, DataSource> resourceMap) {
+ Map<String, DataSourceConfiguration> datasourceMap = DataSourceConverter.getDataSourceConfigurationMap(resourceMap);
+ ShardingSphereEventBus.getInstance().post(new DataSourceChangedEvent(schemaName, datasourceMap));
+ }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
index 462b7cb..ba90c10 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
@@ -19,7 +19,11 @@ package org.apache.shardingsphere.proxy.backend.text.distsql;
import lombok.SneakyThrows;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AlterReplicaQueryRuleStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateReplicaQueryRuleStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropReplicaQueryRuleStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropResourceStatement;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
@@ -30,6 +34,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
+import org.apache.shardingsphere.proxy.backend.exception.ReplicaQueryRuleNotExistedException;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
@@ -186,6 +191,70 @@ public final class RDLBackendHandlerFactoryTest {
ResponseHeader response = rdlBackendHandler.get().execute();
assertThat(response, instanceOf(UpdateResponseHeader.class));
}
+
+ @Test(expected = ClassCastException.class)
+ public void assertExecuteDropResourceContext() throws SQLException {
+ BackendConnection connection = mock(BackendConnection.class);
+ when(connection.getSchemaName()).thenReturn("schema");
+ try {
+ RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(DropResourceStatement.class), connection);
+ } catch (final SQLException ex) {
+ assertThat(ex.getMessage(), is("No Registry center to execute `DropResourceStatement` SQL"));
+ }
+ setGovernanceMetaDataContexts(true);
+ Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(DropResourceStatement.class), connection);
+ assertTrue(rdlBackendHandler.isPresent());
+ ResponseHeader response = rdlBackendHandler.get().execute();
+ assertThat(response, instanceOf(UpdateResponseHeader.class));
+ }
+
+ @Test(expected = ReplicaQueryRuleNotExistedException.class)
+ public void assertExecuteDropReplicaQueryRuleContext() throws SQLException {
+ BackendConnection connection = mock(BackendConnection.class);
+ when(connection.getSchemaName()).thenReturn("schema");
+ try {
+ RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(DropReplicaQueryRuleStatement.class), connection);
+ } catch (final SQLException ex) {
+ assertThat(ex.getMessage(), is("No Registry center to execute `DropReplicaQueryRuleStatement` SQL"));
+ }
+ setGovernanceMetaDataContexts(true);
+ Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(DropReplicaQueryRuleStatement.class), connection);
+ assertTrue(rdlBackendHandler.isPresent());
+ ResponseHeader response = rdlBackendHandler.get().execute();
+ assertThat(response, instanceOf(UpdateResponseHeader.class));
+ }
+
+ @Test
+ public void assertExecuteCreateReplicaQueryRuleContext() throws SQLException {
+ BackendConnection connection = mock(BackendConnection.class);
+ when(connection.getSchemaName()).thenReturn("schema");
+ try {
+ RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(CreateReplicaQueryRuleStatement.class), connection);
+ } catch (final SQLException ex) {
+ assertThat(ex.getMessage(), is("No Registry center to execute `CreateReplicaQueryRuleStatement` SQL"));
+ }
+ setGovernanceMetaDataContexts(true);
+ Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(CreateReplicaQueryRuleStatement.class), connection);
+ assertTrue(rdlBackendHandler.isPresent());
+ ResponseHeader response = rdlBackendHandler.get().execute();
+ assertThat(response, instanceOf(UpdateResponseHeader.class));
+ }
+
+ @Test(expected = ReplicaQueryRuleNotExistedException.class)
+ public void assertExecuteAlterReplicaQueryRuleContext() throws SQLException {
+ BackendConnection connection = mock(BackendConnection.class);
+ when(connection.getSchemaName()).thenReturn("schema");
+ try {
+ RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(AlterReplicaQueryRuleStatement.class), connection);
+ } catch (final SQLException ex) {
+ assertThat(ex.getMessage(), is("No Registry center to execute `AlterReplicaQueryRuleStatement` SQL"));
+ }
+ setGovernanceMetaDataContexts(true);
+ Optional<TextProtocolBackendHandler> rdlBackendHandler = RDLBackendHandlerFactory.newInstance(new MySQLDatabaseType(), mock(AlterReplicaQueryRuleStatement.class), connection);
+ assertTrue(rdlBackendHandler.isPresent());
+ ResponseHeader response = rdlBackendHandler.get().execute();
+ assertThat(response, instanceOf(UpdateResponseHeader.class));
+ }
@SneakyThrows(ReflectiveOperationException.class)
private void setGovernanceMetaDataContexts(final boolean isGovernance) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
index 363a3ec..7a900c3 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
@@ -28,6 +28,8 @@ import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException
import org.apache.shardingsphere.proxy.backend.exception.DBDropExistsException;
import org.apache.shardingsphere.proxy.backend.exception.LockWaitTimeoutException;
import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
+import org.apache.shardingsphere.proxy.backend.exception.ResourceInUsedException;
+import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistsException;
import org.apache.shardingsphere.proxy.backend.exception.ShardingTableRuleNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.TableModifyInTransactionException;
@@ -110,6 +112,12 @@ public final class MySQLErrPacketFactory {
if (cause instanceof LockWaitTimeoutException) {
return new MySQLErrPacket(1, MySQLServerErrorCode.ER_LOCKING_SERVICE_TIMEOUT, ((LockWaitTimeoutException) cause).getTimeoutMilliseconds());
}
+ if (cause instanceof ResourceNotExistedException) {
+ return new MySQLErrPacket(1, CommonErrorCode.RESOURCE_NOT_EXIST, cause.getMessage());
+ }
+ if (cause instanceof ResourceInUsedException) {
+ return new MySQLErrPacket(1, CommonErrorCode.RESOURCE_IN_USED, cause.getMessage());
+ }
return new MySQLErrPacket(1, CommonErrorCode.UNKNOWN_EXCEPTION, cause.getMessage());
}
}