You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/05/27 03:27:13 UTC

[shardingsphere] branch master updated: Refactor CreateReadwriteSplittingRuleBackendHandler (#10495)

This is an automated email from the ASF dual-hosted git repository.

wuweijie 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 d8bb88e  Refactor CreateReadwriteSplittingRuleBackendHandler (#10495)
d8bb88e is described below

commit d8bb88edc1c5d72dd0944441ca4452819f3f6dff
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Thu May 27 11:26:39 2021 +0800

    Refactor CreateReadwriteSplittingRuleBackendHandler (#10495)
    
    * Refactor CreateReadwriteSplittingRuleBackendHandler
    
    * Refactor CreateReadwriteSplittingRuleBackendHandler
    
    Co-authored-by: menghaoranss <me...@apache.org>
---
 .../db/protocol/error/CommonErrorCode.java         |  4 +-
 ...ption.java => DuplicateRuleNamesException.java} | 12 ++--
 .../exception/DuplicateTablesException.java        |  3 +
 ...CreateReadwriteSplittingRuleBackendHandler.java | 77 +++++++++++++++++-----
 ...teReadwriteSplittingRuleBackendHandlerTest.java | 45 +++++++++++--
 .../frontend/mysql/err/MySQLErrPacketFactory.java  |  9 +--
 6 files changed, 121 insertions(+), 29 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 9dc288a..b4c0487 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
@@ -40,8 +40,6 @@ public enum CommonErrorCode implements SQLErrorCode {
     READWRITE_SPLITTING_RULES_NOT_EXIST(1105, "C1105", "Readwrite splitting rules %s do not exist in schema %s."),
     
     ADD_REPLICA_QUERY_RULE_DATA_SOURCE_EXIST(1107, "C1107", "Data sources %s in replica query rule already exists."),
-
-    READWRITE_SPLITTING_RULE_EXIST(1108, "C1108", "Readwrite splitting rule already exists in schema %s."),
     
     INVALID_RESOURCE(1111, "C1111", "Can not add invalid resources %s."),
     
@@ -69,6 +67,8 @@ public enum CommonErrorCode implements SQLErrorCode {
 
     ENCRYPT_RULES_NOT_EXIST(1124, "C1124", "Encrypt rules %s do not exist in schema %s."),
 
+    DUPLICATE_RULE_NAMES(1125, "C1125", "Duplicate rule names %s in schema %s"),
+
     SCALING_JOB_NOT_EXIST(1201, "C1201", "Scaling job %s does not exist."),
     
     SCALING_OPERATE_FAILED(1209, "C1209", "Scaling Operate Failed: [%s]"),
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReadwriteSplittingRuleCreateExistsException.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateRuleNamesException.java
similarity index 79%
rename from shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReadwriteSplittingRuleCreateExistsException.java
rename to shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateRuleNamesException.java
index 5b03bd6..5782b89 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReadwriteSplittingRuleCreateExistsException.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateRuleNamesException.java
@@ -20,14 +20,18 @@ package org.apache.shardingsphere.proxy.backend.exception;
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 
+import java.util.Collection;
+
 /**
- * Readwrite-splitting rule create exists exception.
+ * Duplicate rule names exception.
  */
 @RequiredArgsConstructor
 @Getter
-public final class ReadwriteSplittingRuleCreateExistsException extends BackendException {
-    
-    private static final long serialVersionUID = -6902287715467426449L;
+public final class DuplicateRuleNamesException extends BackendException {
+
+    private static final long serialVersionUID = -1738699538105858939L;
 
     private final String schemaName;
+
+    private final Collection<String> ruleNames;
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateTablesException.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateTablesException.java
index 4890367..09fdb73 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateTablesException.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateTablesException.java
@@ -22,6 +22,9 @@ import lombok.RequiredArgsConstructor;
 
 import java.util.Collection;
 
+/**
+ * Duplicate tables exception.
+ */
 @RequiredArgsConstructor
 @Getter
 public final class DuplicateTablesException extends BackendException {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReadwriteSplittingRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReadwriteSplittingRuleBackendHandler.java
index 36a9ef4..093cd0e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReadwriteSplittingRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReadwriteSplittingRuleBackendHandler.java
@@ -18,22 +18,23 @@
 package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
 
 import com.google.common.base.Strings;
+import org.apache.shardingsphere.distsql.parser.segment.rdl.ReadwriteSplittingRuleSegment;
 import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateReadwriteSplittingRuleStatement;
 import org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationsAlteredEvent;
-import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.spi.typed.TypedSPIRegistry;
 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.DuplicateRuleNamesException;
 import org.apache.shardingsphere.proxy.backend.exception.InvalidLoadBalancersException;
-import org.apache.shardingsphere.proxy.backend.exception.ReadwriteSplittingRuleCreateExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
 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.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
 import org.apache.shardingsphere.readwritesplitting.common.yaml.config.YamlReadwriteSplittingRuleConfiguration;
 import org.apache.shardingsphere.readwritesplitting.common.yaml.converter.ReadwriteSplittingRuleStatementConverter;
 import org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
@@ -42,6 +43,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.stream.Collectors;
 
@@ -61,25 +63,37 @@ public final class CreateReadwriteSplittingRuleBackendHandler extends SchemaRequ
     @Override
     public ResponseHeader execute(final String schemaName, final CreateReadwriteSplittingRuleStatement sqlStatement) {
         check(schemaName, sqlStatement);
-        YamlReadwriteSplittingRuleConfiguration config = ReadwriteSplittingRuleStatementConverter.convert(sqlStatement);
-        Collection<RuleConfiguration> rules = new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(config));
-        post(schemaName, rules);
+        create(schemaName, sqlStatement);
+        post(schemaName);
         return new UpdateResponseHeader(sqlStatement);
     }
     
     private void check(final String schemaName, final CreateReadwriteSplittingRuleStatement sqlStatement) {
-        if (ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream().anyMatch(each -> each instanceof ReadwriteSplittingRuleConfiguration)) {
-            throw new ReadwriteSplittingRuleCreateExistsException(schemaName);
+        checkDuplicateRuleNames(schemaName, sqlStatement);
+        checkResources(schemaName, sqlStatement);
+        checkLoadBalancers(sqlStatement);
+    }
+
+    private void checkDuplicateRuleNames(final String schemaName, final CreateReadwriteSplittingRuleStatement sqlStatement) {
+        Optional<ReadwriteSplittingRuleConfiguration> optional = getReadwriteSplittingRuleConfiguration(schemaName);
+        if (optional.isPresent()) {
+            Collection<String> existRuleNames = getRuleNames(optional.get());
+            Collection<String> duplicateRuleNames = sqlStatement.getReadwriteSplittingRules()
+                    .stream().map(ReadwriteSplittingRuleSegment::getName).filter(existRuleNames::contains).collect(Collectors.toList());
+            if (!duplicateRuleNames.isEmpty()) {
+                throw new DuplicateRuleNamesException(schemaName, duplicateRuleNames);
+            }
         }
-        Collection<String> resources = new LinkedHashSet<>();
-        sqlStatement.getReadwriteSplittingRules().stream().filter(each -> Strings.isNullOrEmpty(each.getAutoAwareResource())).forEach(each -> {
-            resources.add(each.getWriteDataSource());
-            resources.addAll(each.getReadDataSources());
-        });
-        Collection<String> notExistResources = resources.stream().filter(each -> !this.isValidResource(schemaName, each)).collect(Collectors.toList());
+    }
+
+    private void checkResources(final String schemaName, final CreateReadwriteSplittingRuleStatement sqlStatement) {
+        Collection<String> notExistResources = getResources(sqlStatement).stream().filter(each -> !this.isValidResource(schemaName, each)).collect(Collectors.toList());
         if (!notExistResources.isEmpty()) {
             throw new ResourceNotExistedException(notExistResources);
         }
+    }
+
+    private void checkLoadBalancers(final CreateReadwriteSplittingRuleStatement sqlStatement) {
         Collection<String> invalidLoadBalances = sqlStatement.getReadwriteSplittingRules().stream().map(each -> each.getLoadBalancer()).distinct()
                 .filter(each -> !TypedSPIRegistry.findRegisteredService(ReplicaLoadBalanceAlgorithm.class, each, new Properties()).isPresent())
                 .collect(Collectors.toList());
@@ -88,12 +102,45 @@ public final class CreateReadwriteSplittingRuleBackendHandler extends SchemaRequ
         }
     }
 
+    private Collection<String> getResources(final CreateReadwriteSplittingRuleStatement sqlStatement) {
+        Collection<String> result = new LinkedHashSet<>();
+        sqlStatement.getReadwriteSplittingRules().stream().filter(each -> Strings.isNullOrEmpty(each.getAutoAwareResource())).forEach(each -> {
+            result.add(each.getWriteDataSource());
+            result.addAll(each.getReadDataSources());
+        });
+        return result;
+    }
+
     private boolean isValidResource(final String schemaName, final String resourceName) {
         return Objects.nonNull(ProxyContext.getInstance().getMetaData(schemaName).getResource())
                 && ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources().containsKey(resourceName);
     }
 
-    private void post(final String schemaName, final Collection<RuleConfiguration> rules) {
-        ShardingSphereEventBus.getInstance().post(new RuleConfigurationsAlteredEvent(schemaName, rules));
+    private Optional<ReadwriteSplittingRuleConfiguration> getReadwriteSplittingRuleConfiguration(final String schemaName) {
+        return ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
+                .filter(each -> each instanceof ReadwriteSplittingRuleConfiguration).map(each -> (ReadwriteSplittingRuleConfiguration) each).findAny();
+    }
+
+    private Collection<String> getRuleNames(final ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfiguration) {
+        return readwriteSplittingRuleConfiguration.getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toList());
+    }
+
+    private void create(final String schemaName, final CreateReadwriteSplittingRuleStatement sqlStatement) {
+        YamlReadwriteSplittingRuleConfiguration yamlReadwriteSplittingRuleConfiguration = ReadwriteSplittingRuleStatementConverter.convert(sqlStatement);
+        ReadwriteSplittingRuleConfiguration createdReadwriteSplittingRuleConfiguration = new YamlRuleConfigurationSwapperEngine()
+                .swapToRuleConfigurations(Collections.singleton(yamlReadwriteSplittingRuleConfiguration))
+                .stream().filter(each -> each instanceof ReadwriteSplittingRuleConfiguration).findAny().map(each -> (ReadwriteSplittingRuleConfiguration) each).get();
+        if (getReadwriteSplittingRuleConfiguration(schemaName).isPresent()) {
+            ReadwriteSplittingRuleConfiguration existReadwriteSplittingRuleConfiguration = getReadwriteSplittingRuleConfiguration(schemaName).get();
+            existReadwriteSplittingRuleConfiguration.getDataSources().addAll(createdReadwriteSplittingRuleConfiguration.getDataSources());
+            existReadwriteSplittingRuleConfiguration.getLoadBalancers().putAll(createdReadwriteSplittingRuleConfiguration.getLoadBalancers());
+        } else {
+            ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().add(createdReadwriteSplittingRuleConfiguration);
+        }
+    }
+
+    private void post(final String schemaName) {
+        ShardingSphereEventBus.getInstance().post(new RuleConfigurationsAlteredEvent(schemaName,
+                ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()));
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReadwriteSplittingRuleBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReadwriteSplittingRuleBackendHandlerTest.java
index 4275b5c..cdcb70b 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReadwriteSplittingRuleBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateReadwriteSplittingRuleBackendHandlerTest.java
@@ -27,12 +27,13 @@ import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 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.DuplicateRuleNamesException;
 import org.apache.shardingsphere.proxy.backend.exception.InvalidLoadBalancersException;
-import org.apache.shardingsphere.proxy.backend.exception.ReadwriteSplittingRuleCreateExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
 import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
 import org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
 import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import org.junit.Before;
@@ -43,7 +44,10 @@ import org.mockito.junit.MockitoJUnitRunner;
 
 import javax.sql.DataSource;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.Map;
 
 import static org.junit.Assert.assertNotNull;
@@ -105,11 +109,44 @@ public final class CreateReadwriteSplittingRuleBackendHandlerTest {
         assertTrue(responseHeader instanceof UpdateResponseHeader);
     }
     
-    @Test(expected = ReadwriteSplittingRuleCreateExistsException.class)
-    public void assertExecuteWithExistReadwriteSplittingRule() {
-        when(ruleMetaData.getConfigurations()).thenReturn(Collections.singletonList(new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Maps.newHashMap())));
+    @Test(expected = DuplicateRuleNamesException.class)
+    public void assertExecuteWithDuplicateRuleNames() {
+        ReadwriteSplittingDataSourceRuleConfiguration readwriteSplittingDataSourceRuleConfiguration
+                = new ReadwriteSplittingDataSourceRuleConfiguration("pr_ds", "",
+                "ds_write", Arrays.asList("ds_read_0", "ds_read_1"), "test");
+        when(ruleMetaData.getConfigurations()).thenReturn(Collections.singletonList(new ReadwriteSplittingRuleConfiguration(Collections
+                .singleton(readwriteSplittingDataSourceRuleConfiguration), Maps.newHashMap())));
+        ReadwriteSplittingRuleSegment readwriteSplittingRuleSegment = new ReadwriteSplittingRuleSegment();
+        readwriteSplittingRuleSegment.setName("pr_ds");
+        readwriteSplittingRuleSegment.setWriteDataSource("ds_write");
+        readwriteSplittingRuleSegment.setReadDataSources(Arrays.asList("ds_read_0", "ds_read_1"));
+        readwriteSplittingRuleSegment.setLoadBalancer("TEST");
+        when(sqlStatement.getReadwriteSplittingRules()).thenReturn(Collections.singletonList(readwriteSplittingRuleSegment));
         handler.execute("test", sqlStatement);
     }
+
+    @Test
+    public void assertExecuteWithExistReadwriteConfiguration() {
+        ReadwriteSplittingDataSourceRuleConfiguration readwriteSplittingDataSourceRuleConfiguration
+                = new ReadwriteSplittingDataSourceRuleConfiguration("pr_ds_1", "",
+                "ds_write", Arrays.asList("ds_read_0", "ds_read_1"), "test");
+        Collection<ReadwriteSplittingDataSourceRuleConfiguration> dataSources = new LinkedList<>();
+        dataSources.add(readwriteSplittingDataSourceRuleConfiguration);
+        when(ruleMetaData.getConfigurations()).thenReturn(Collections.singletonList(new ReadwriteSplittingRuleConfiguration(dataSources, new HashMap<>())));
+        ReadwriteSplittingRuleSegment readwriteSplittingRuleSegment = new ReadwriteSplittingRuleSegment();
+        readwriteSplittingRuleSegment.setName("pr_ds");
+        readwriteSplittingRuleSegment.setWriteDataSource("ds_write");
+        readwriteSplittingRuleSegment.setReadDataSources(Arrays.asList("ds_read_0", "ds_read_1"));
+        readwriteSplittingRuleSegment.setLoadBalancer("TEST");
+        when(sqlStatement.getReadwriteSplittingRules()).thenReturn(Collections.singletonList(readwriteSplittingRuleSegment));
+        when(shardingSphereMetaData.getResource()).thenReturn(shardingSphereResource);
+        Map<String, DataSource> dataSourceMap = mock(Map.class);
+        when(shardingSphereResource.getDataSources()).thenReturn(dataSourceMap);
+        when(dataSourceMap.containsKey(anyString())).thenReturn(true);
+        ResponseHeader responseHeader = handler.execute("test", sqlStatement);
+        assertNotNull(responseHeader);
+        assertTrue(responseHeader instanceof UpdateResponseHeader);
+    }
     
     @Test(expected = ResourceNotExistedException.class)
     public void assertExecuteWithNotExistResources() {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
index c2177af..c7140dd 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
@@ -30,6 +30,7 @@ import org.apache.shardingsphere.proxy.backend.exception.DBDropExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.DatabaseDiscoveryRuleExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.DatabaseDiscoveryRulesNotExistedException;
 import org.apache.shardingsphere.proxy.backend.exception.DuplicateResourceException;
+import org.apache.shardingsphere.proxy.backend.exception.DuplicateRuleNamesException;
 import org.apache.shardingsphere.proxy.backend.exception.DuplicateTablesException;
 import org.apache.shardingsphere.proxy.backend.exception.EncryptRuleExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.EncryptRulesNotExistedException;
@@ -38,7 +39,6 @@ import org.apache.shardingsphere.proxy.backend.exception.InvalidEncryptorsExcept
 import org.apache.shardingsphere.proxy.backend.exception.InvalidLoadBalancersException;
 import org.apache.shardingsphere.proxy.backend.exception.InvalidResourceException;
 import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
-import org.apache.shardingsphere.proxy.backend.exception.ReadwriteSplittingRuleCreateExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.ReadwriteSplittingRulesNotExistedException;
 import org.apache.shardingsphere.proxy.backend.exception.ResourceInUsedException;
 import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
@@ -169,9 +169,6 @@ public final class MySQLErrPacketFactory {
         if (cause instanceof AddReadwriteSplittingRuleDataSourcesExistedException) {
             return new MySQLErrPacket(1, CommonErrorCode.ADD_REPLICA_QUERY_RULE_DATA_SOURCE_EXIST, ((AddReadwriteSplittingRuleDataSourcesExistedException) cause).getRuleNames());
         }
-        if (cause instanceof ReadwriteSplittingRuleCreateExistsException) {
-            return new MySQLErrPacket(1, CommonErrorCode.READWRITE_SPLITTING_RULE_EXIST, ((ReadwriteSplittingRuleCreateExistsException) cause).getSchemaName());
-        }
         if (cause instanceof ScalingJobNotFoundException) {
             return new MySQLErrPacket(1, CommonErrorCode.SCALING_JOB_NOT_EXIST, ((ScalingJobNotFoundException) cause).getJobId());
         }
@@ -198,6 +195,10 @@ public final class MySQLErrPacketFactory {
             return new MySQLErrPacket(1, CommonErrorCode.ENCRYPT_RULES_NOT_EXIST, ((EncryptRulesNotExistedException) cause).getTables(),
                     ((EncryptRulesNotExistedException) cause).getSchemaName());
         }
+        if (cause instanceof DuplicateRuleNamesException) {
+            return new MySQLErrPacket(1, CommonErrorCode.DUPLICATE_RULE_NAMES, ((DuplicateRuleNamesException) cause).getRuleNames(),
+                    ((DuplicateRuleNamesException) cause).getSchemaName());
+        }
         return new MySQLErrPacket(1, CommonErrorCode.UNKNOWN_EXCEPTION, cause.getMessage());
     }
 }