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 eedc687  Check if resource name is duplicate for add resource (#10068)
eedc687 is described below

commit eedc6874ba322bab7134680d761fdca9ef165e24
Author: Haoran Meng <[email protected]>
AuthorDate: Wed Apr 14 16:58:08 2021 +0800

    Check if resource name is duplicate for add resource (#10068)
    
    * Add check if data source name is duplicate for add resource
    
    * Add check if data source name is duplicate for add resource
---
 .../db/protocol/error/CommonErrorCode.java         |  2 ++
 .../exception/DuplicateResourceException.java      | 32 ++++++++++++++++++++++
 .../rdl/impl/AddResourceBackendHandler.java        | 23 ++++++++++++++++
 .../rdl/impl/AddResourceBackendHandlerTest.java    | 25 +++++++++++++++++
 .../frontend/mysql/err/MySQLErrPacketFactory.java  |  4 +++
 5 files changed, 86 insertions(+)

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 8908a03..7a9a5e4 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
@@ -51,6 +51,8 @@ public enum CommonErrorCode implements SQLErrorCode {
     
     INVALID_RESOURCE(1111, "C1111", "Can not add invalid resources %s."),
     
+    DUPLICATE_RESOURCE(1112, "C1112", "Duplicate resource name %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/DuplicateResourceException.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateResourceException.java
new file mode 100644
index 0000000..c7832e9
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/DuplicateResourceException.java
@@ -0,0 +1,32 @@
+/*
+ * 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.exception;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.Collection;
+
+@RequiredArgsConstructor
+@Getter
+public final class DuplicateResourceException extends BackendException {
+    
+    private static final long serialVersionUID = 2103793827572264148L;
+    
+    private final Collection<String> resourceNames;
+}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandler.java
index 0283f00..894edd6 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandler.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
 
+import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
 import 
org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceAddedEvent;
 import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
@@ -24,6 +25,8 @@ import 
org.apache.shardingsphere.infra.config.datasource.DataSourceValidator;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 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.DuplicateResourceException;
 import 
org.apache.shardingsphere.proxy.backend.exception.InvalidResourceException;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
@@ -33,8 +36,11 @@ import 
org.apache.shardingsphere.proxy.converter.AddResourcesStatementConverter;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Set;
 
 /**
  * Add resource backend handler.
@@ -53,6 +59,7 @@ public final class AddResourceBackendHandler extends 
SchemaRequiredBackendHandle
     
     @Override
     public ResponseHeader execute(final String schemaName, final 
AddResourceStatement sqlStatement) {
+        check(schemaName, sqlStatement);
         Map<String, DataSourceConfiguration> dataSources = 
DataSourceParameterConverter.getDataSourceConfigurationMap(
                 
DataSourceParameterConverter.getDataSourceParameterMapFromYamlConfiguration(AddResourcesStatementConverter.convert(databaseType,
 sqlStatement)));
         Collection<String> invalidDataSourceNames = new ArrayList<>();
@@ -68,6 +75,22 @@ public final class AddResourceBackendHandler extends 
SchemaRequiredBackendHandle
         return new UpdateResponseHeader(sqlStatement);
     }
     
+    private void check(final String schemaName, final AddResourceStatement 
sqlStatement) {
+        List<String> dataSourceNames = new 
ArrayList<>(sqlStatement.getDataSources().size());
+        Set<String> duplicateDataSourceNames = new HashSet<>();
+        for (DataSourceSegment dataSourceSegment : 
sqlStatement.getDataSources()) {
+            if (dataSourceNames.contains(dataSourceSegment.getName()) 
+                    || ProxyContext.getInstance().getMetaData(schemaName)
+                    
.getResource().getDataSources().containsKey(dataSourceSegment.getName())) {
+                duplicateDataSourceNames.add(dataSourceSegment.getName());
+            }
+            dataSourceNames.add(dataSourceSegment.getName());
+        }
+        if (!duplicateDataSourceNames.isEmpty()) {
+            throw new DuplicateResourceException(duplicateDataSourceNames);
+        }
+    }
+    
     private void post(final String schemaName, final Map<String, 
DataSourceConfiguration> dataSources) {
         // TODO Need to get the executed feedback from registry center for 
returning.
         ShardingSphereEventBus.getInstance().post(new 
DataSourceAddedEvent(schemaName, dataSources));
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandlerTest.java
index 62763e0..0a3bf52 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AddResourceBackendHandlerTest.java
@@ -21,10 +21,15 @@ import 
org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
 import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceValidator;
+import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -33,10 +38,13 @@ import org.mockito.junit.MockitoJUnitRunner;
 
 import java.lang.reflect.Field;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.HashMap;
 
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -51,6 +59,18 @@ public final class AddResourceBackendHandlerTest {
     @Mock
     private BackendConnection backendConnection;
     
+    @Mock
+    private MetaDataContexts metaDataContexts;
+    
+    @Mock
+    private TransactionContexts transactionContexts;
+    
+    @Mock
+    private ShardingSphereMetaData shardingSphereMetaData;
+    
+    @Mock
+    private ShardingSphereResource shardingSphereResource;
+    
     private AddResourceBackendHandler addResourceBackendHandler;
     
     @Before
@@ -63,6 +83,11 @@ public final class AddResourceBackendHandlerTest {
     
     @Test
     public void assertExecute() {
+        ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
+        
when(metaDataContexts.getAllSchemaNames()).thenReturn(Arrays.asList("test"));
+        
when(metaDataContexts.getMetaData(eq("test"))).thenReturn(shardingSphereMetaData);
+        
when(shardingSphereMetaData.getResource()).thenReturn(shardingSphereResource);
+        when(shardingSphereResource.getDataSources()).thenReturn(new 
HashMap<>());
         
when(dataSourceValidator.validate(any(DataSourceConfiguration.class))).thenReturn(true);
         ResponseHeader responseHeader = 
addResourceBackendHandler.execute("test", buildAddResourceStatement());
         assertTrue(responseHeader instanceof UpdateResponseHeader);
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 f3cbcd9..88f05c9 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
@@ -27,6 +27,7 @@ import 
org.apache.shardingsphere.proxy.backend.exception.AddReadWriteSplittingRu
 import org.apache.shardingsphere.proxy.backend.exception.CircuitBreakException;
 import 
org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.DBDropExistsException;
+import 
org.apache.shardingsphere.proxy.backend.exception.DuplicateResourceException;
 import 
org.apache.shardingsphere.proxy.backend.exception.InvalidResourceException;
 import 
org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.ReadWriteSplittingRuleCreateExistsException;
@@ -137,6 +138,9 @@ public final class MySQLErrPacketFactory {
         if (cause instanceof InvalidResourceException) {
             return new MySQLErrPacket(1, CommonErrorCode.INVALID_RESOURCE, 
((InvalidResourceException) cause).getResourceNames());
         }
+        if (cause instanceof DuplicateResourceException) {
+            return new MySQLErrPacket(1, CommonErrorCode.DUPLICATE_RESOURCE, 
((DuplicateResourceException) cause).getResourceNames());
+        }
         if (cause instanceof ShardingRuleNotExistedException) {
             return new MySQLErrPacket(1, 
CommonErrorCode.SHARDING_RULE_NOT_EXIST);
         }

Reply via email to