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/14 04:00:32 UTC

[shardingsphere] branch master updated: add check for create replica query rule (#9020)

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 1e47604  add check for create replica query rule (#9020)
1e47604 is described below

commit 1e476044ade7964f6b036305529f302a010fe680
Author: JingShang Lu <lu...@apache.org>
AuthorDate: Thu Jan 14 11:59:57 2021 +0800

    add check for create replica query rule (#9020)
---
 .../db/protocol/error/CommonErrorCode.java         |  2 ++
 .../ReplicaQueryRuleCreateExistsException.java     | 26 ++++------------------
 .../impl/CreateReplicaQueryRuleBackendHandler.java | 10 +++++++++
 .../frontend/mysql/MySQLErrPacketFactory.java      |  4 ++++
 4 files changed, 20 insertions(+), 22 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 f42e499..3cc460e 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
@@ -37,6 +37,8 @@ public enum CommonErrorCode implements SQLErrorCode {
     
     RESOURCE_NOT_EXIST(11004, "C11004", "Can not drop resources, resources %s do not exist."),
     
+    REPLICA_QUERY_RULE_EXIST(11008, "C11008", "Replica query rule already exists."),
+    
     UNSUPPORTED_COMMAND(19998, "C19998", "Unsupported command: [%s]"),
     
     UNKNOWN_EXCEPTION(19999, "C19999", "Unknown exception: [%s]");
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleCreateExistsException.java
similarity index 50%
copy from shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
copy to shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleCreateExistsException.java
index f42e499..8305561 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleCreateExistsException.java
@@ -15,35 +15,17 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.db.protocol.error;
+package org.apache.shardingsphere.proxy.backend.exception;
 
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 
 /**
- * Common error code.
+ * Replica query rule create exists exception.
  */
 @RequiredArgsConstructor
 @Getter
-public enum CommonErrorCode implements SQLErrorCode {
+public final class ReplicaQueryRuleCreateExistsException extends BackendException {
     
-    CIRCUIT_BREAK_MODE(10000, "C10000", "Circuit break mode is ON."),
-    
-    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]"),
-    
-    UNKNOWN_EXCEPTION(19999, "C19999", "Unknown exception: [%s]");
-    
-    private final int errorCode;
-    
-    private final String sqlState;
-    
-    private final String errorMessage;
+    private static final long serialVersionUID = -6902287715467426449L;
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReplicaQueryRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReplicaQueryRuleBackendHandler.java
index ff249ae..17e055d 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReplicaQueryRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReplicaQueryRuleBackendHandler.java
@@ -23,9 +23,12 @@ import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
 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.ReplicaQueryRuleCreateExistsException;
 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 org.apache.shardingsphere.replicaquery.api.config.ReplicaQueryRuleConfiguration;
 import org.apache.shardingsphere.replicaquery.yaml.config.YamlReplicaQueryRuleConfiguration;
 import org.apache.shardingsphere.replicaquery.yaml.converter.CreateReplicaQueryRuleStatementConverter;
 
@@ -43,12 +46,19 @@ public final class CreateReplicaQueryRuleBackendHandler extends SchemaRequiredBa
 
     @Override
     public ResponseHeader execute(final String schemaName, final CreateReplicaQueryRuleStatement sqlStatement) {
+        check(schemaName);
         YamlReplicaQueryRuleConfiguration config = CreateReplicaQueryRuleStatementConverter.convert(sqlStatement);
         Collection<RuleConfiguration> rules = new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(config));
         post(schemaName, rules);
         return new UpdateResponseHeader(sqlStatement);
     }
     
+    private void check(final String schemaName) {
+        if (ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream().filter(each -> each instanceof ReplicaQueryRuleConfiguration).findFirst().isPresent()) {
+            throw new ReplicaQueryRuleCreateExistsException();
+        }
+    }
+    
     private void post(final String schemaName, final Collection<RuleConfiguration> rules) {
         ShardingSphereEventBus.getInstance().post(new RuleConfigurationsPersistEvent(schemaName, rules));
     }
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 7a900c3..e668437 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,7 @@ 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.ReplicaQueryRuleCreateExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.ResourceInUsedException;
 import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
 import org.apache.shardingsphere.proxy.backend.exception.RuleNotExistsException;
@@ -118,6 +119,9 @@ public final class MySQLErrPacketFactory {
         if (cause instanceof ResourceInUsedException) {
             return new MySQLErrPacket(1, CommonErrorCode.RESOURCE_IN_USED, cause.getMessage());
         }
+        if (cause instanceof ReplicaQueryRuleCreateExistsException) {
+            return new MySQLErrPacket(1, CommonErrorCode.REPLICA_QUERY_RULE_EXIST);
+        }
         return new MySQLErrPacket(1, CommonErrorCode.UNKNOWN_EXCEPTION, cause.getMessage());
     }
 }