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/06/08 06:49:18 UTC
[shardingsphere] branch master updated: Check sharding algorithm
for create & alter sharding table rule (#10711)
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 0e86265 Check sharding algorithm for create & alter sharding table rule (#10711)
0e86265 is described below
commit 0e86265f79074edcfd9f3eb4c393c32e119dccee
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Tue Jun 8 14:48:46 2021 +0800
Check sharding algorithm for create & alter sharding table rule (#10711)
* Check sharding algorithm for create & alter sharding table rule
* Check sharding algorithm for create & alter sharding table rule
* Check sharding algorithm for create & alter sharding table rule
* Check sharding algorithm for create & alter sharding table rule
* Check sharding algorithm for create & alter sharding table rule
* Check sharding algorithm for create & alter sharding table rule
---
.../db/protocol/error/CommonErrorCode.java | 4 ++-
.../InvalidShardingAlgorithmsException.java | 35 ++++++++++++++++++++++
.../AlterDatabaseDiscoveryRuleBackendHandler.java | 1 +
.../rdl/impl/AlterEncryptRuleBackendHandler.java | 1 +
.../AlterReadwriteSplittingRuleBackendHandler.java | 1 +
.../impl/AlterShardingTableRuleBackendHandler.java | 16 ++++++++++
.../CreateDatabaseDiscoveryRuleBackendHandler.java | 1 +
.../rdl/impl/CreateEncryptRuleBackendHandler.java | 1 +
...CreateReadwriteSplittingRuleBackendHandler.java | 1 +
.../CreateShardingTableRuleBackendHandler.java | 18 ++++++++++-
.../AlterShardingTableRuleBackendHandlerTest.java | 18 +++++++++--
.../CreateShardingTableRuleBackendHandlerTest.java | 20 +++++++++++++
.../frontend/mysql/err/MySQLErrPacketFactory.java | 4 +++
13 files changed, 117 insertions(+), 4 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 08879fc..19f7d50 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
@@ -62,7 +62,9 @@ public enum CommonErrorCode implements SQLErrorCode {
INVALID_ENCRYPTORS(1117, "C1117", "Invalid encryptors %s."),
ENCRYPT_RULES_NOT_EXIST(1118, "C1118", "Encrypt rules %s do not exist in schema %s."),
-
+
+ INVALID_SHARDING_ALGORITHMS(1119, "C1119", "Invalid sharding algorithms %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/InvalidShardingAlgorithmsException.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/InvalidShardingAlgorithmsException.java
new file mode 100644
index 0000000..7587fec
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/InvalidShardingAlgorithmsException.java
@@ -0,0 +1,35 @@
+/*
+ * 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.AllArgsConstructor;
+import lombok.Getter;
+
+import java.util.Collection;
+
+/**
+ * Invalid sharding algorithms exception.
+ */
+@AllArgsConstructor
+@Getter
+public final class InvalidShardingAlgorithmsException extends BackendException {
+
+ private static final long serialVersionUID = 4257890788163115250L;
+
+ private final Collection<String> algorithms;
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterDatabaseDiscoveryRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterDatabaseDiscoveryRuleBackendHandler.java
index ccd8880..8c963ae 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterDatabaseDiscoveryRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterDatabaseDiscoveryRuleBackendHandler.java
@@ -45,6 +45,7 @@ import java.util.stream.Collectors;
public final class AlterDatabaseDiscoveryRuleBackendHandler extends RDLBackendHandler<AlterDatabaseDiscoveryRuleStatement> {
static {
+ // TODO consider about register once only
ShardingSphereServiceLoader.register(DatabaseDiscoveryType.class);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterEncryptRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterEncryptRuleBackendHandler.java
index 06c7b65..e86c7c4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterEncryptRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterEncryptRuleBackendHandler.java
@@ -43,6 +43,7 @@ import java.util.stream.Collectors;
public final class AlterEncryptRuleBackendHandler extends RDLBackendHandler<AlterEncryptRuleStatement> {
static {
+ // TODO consider about register once only
ShardingSphereServiceLoader.register(EncryptAlgorithm.class);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterReadwriteSplittingRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterReadwriteSplittingRuleBackendHandler.java
index 6ea4e4f..e9da468 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterReadwriteSplittingRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterReadwriteSplittingRuleBackendHandler.java
@@ -46,6 +46,7 @@ import java.util.stream.Collectors;
public final class AlterReadwriteSplittingRuleBackendHandler extends RDLBackendHandler<AlterReadwriteSplittingRuleStatement> {
static {
+ // TODO consider about register once only
ShardingSphereServiceLoader.register(ReplicaLoadBalanceAlgorithm.class);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterShardingTableRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterShardingTableRuleBackendHandler.java
index 5a7d3e1..f2d67a2 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterShardingTableRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterShardingTableRuleBackendHandler.java
@@ -19,21 +19,26 @@ package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
import org.apache.shardingsphere.distsql.parser.segment.TableRuleSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterShardingTableRuleStatement;
+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.exception.DuplicateTablesException;
+import org.apache.shardingsphere.proxy.backend.exception.InvalidShardingAlgorithmsException;
import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.ShardingTableRuleNotExistedException;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.converter.ShardingRuleStatementConverter;
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
+import java.util.Properties;
import java.util.stream.Collectors;
/**
@@ -41,6 +46,11 @@ import java.util.stream.Collectors;
*/
public final class AlterShardingTableRuleBackendHandler extends RDLBackendHandler<AlterShardingTableRuleStatement> {
+ static {
+ // TODO consider about register once only
+ ShardingSphereServiceLoader.register(ShardingAlgorithm.class);
+ }
+
public AlterShardingTableRuleBackendHandler(final AlterShardingTableRuleStatement sqlStatement, final BackendConnection backendConnection) {
super(sqlStatement, backendConnection);
}
@@ -64,6 +74,12 @@ public final class AlterShardingTableRuleBackendHandler extends RDLBackendHandle
if (!notExistTables.isEmpty()) {
throw new ShardingTableRuleNotExistedException(schemaName, notExistTables);
}
+ Collection<String> invalidTableAlgorithms = sqlStatement.getTables().stream().map(each -> each.getTableStrategy().getAlgorithmName()).distinct()
+ .filter(each -> !TypedSPIRegistry.findRegisteredService(ShardingAlgorithm.class, each, new Properties()).isPresent())
+ .collect(Collectors.toList());
+ if (!invalidTableAlgorithms.isEmpty()) {
+ throw new InvalidShardingAlgorithmsException(invalidTableAlgorithms);
+ }
}
@Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateDatabaseDiscoveryRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateDatabaseDiscoveryRuleBackendHandler.java
index bf139fa..7154bc8 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateDatabaseDiscoveryRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateDatabaseDiscoveryRuleBackendHandler.java
@@ -47,6 +47,7 @@ import java.util.stream.Collectors;
public final class CreateDatabaseDiscoveryRuleBackendHandler extends RDLBackendHandler<CreateDatabaseDiscoveryRuleStatement> {
static {
+ // TODO consider about register once only
ShardingSphereServiceLoader.register(DatabaseDiscoveryType.class);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandler.java
index 0a9eaa2..ebb878f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandler.java
@@ -45,6 +45,7 @@ import java.util.stream.Collectors;
public final class CreateEncryptRuleBackendHandler extends RDLBackendHandler<CreateEncryptRuleStatement> {
static {
+ // TODO consider about register once only
ShardingSphereServiceLoader.register(EncryptAlgorithm.class);
}
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 c024225..f24be2a 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
@@ -47,6 +47,7 @@ import java.util.stream.Collectors;
public final class CreateReadwriteSplittingRuleBackendHandler extends RDLBackendHandler<CreateReadwriteSplittingRuleStatement> {
static {
+ // TODO consider about register once only
ShardingSphereServiceLoader.register(ReplicaLoadBalanceAlgorithm.class);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
index a786f53..7d121ea 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
@@ -19,19 +19,24 @@ package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
import org.apache.shardingsphere.distsql.parser.segment.TableRuleSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
+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.DuplicateTablesException;
+import org.apache.shardingsphere.proxy.backend.exception.InvalidShardingAlgorithmsException;
import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.converter.ShardingRuleStatementConverter;
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map.Entry;
import java.util.Optional;
+import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
@@ -40,6 +45,11 @@ import java.util.stream.Collectors;
*/
public final class CreateShardingTableRuleBackendHandler extends RDLBackendHandler<CreateShardingTableRuleStatement> {
+ static {
+ // TODO consider about register once only
+ ShardingSphereServiceLoader.register(ShardingAlgorithm.class);
+ }
+
public CreateShardingTableRuleBackendHandler(final CreateShardingTableRuleStatement sqlStatement, final BackendConnection backendConnection) {
super(sqlStatement, backendConnection);
}
@@ -57,8 +67,14 @@ public final class CreateShardingTableRuleBackendHandler extends RDLBackendHandl
if (!duplicateTableNames.isEmpty()) {
throw new DuplicateTablesException(duplicateTableNames);
}
+ Collection<String> invalidTableAlgorithms = sqlStatement.getTables().stream().map(each -> each.getTableStrategy().getAlgorithmName()).distinct()
+ .filter(each -> !TypedSPIRegistry.findRegisteredService(ShardingAlgorithm.class, each, new Properties()).isPresent())
+ .collect(Collectors.toList());
+ if (!invalidTableAlgorithms.isEmpty()) {
+ throw new InvalidShardingAlgorithmsException(invalidTableAlgorithms);
+ }
}
-
+
@Override
public void doExecute(final String schemaName, final CreateShardingTableRuleStatement sqlStatement) {
ShardingRuleConfiguration shardingRuleConfiguration = (ShardingRuleConfiguration) new YamlRuleConfigurationSwapperEngine()
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterShardingTableRuleBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterShardingTableRuleBackendHandlerTest.java
index d78de33..3e50a61 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterShardingTableRuleBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterShardingTableRuleBackendHandlerTest.java
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
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.DuplicateTablesException;
+import org.apache.shardingsphere.proxy.backend.exception.InvalidShardingAlgorithmsException;
import org.apache.shardingsphere.proxy.backend.exception.ShardingTableRuleNotExistedException;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
@@ -77,7 +78,7 @@ public final class AlterShardingTableRuleBackendHandlerTest {
@Mock
private ShardingSphereRuleMetaData ruleMetaData;
-
+
@Mock
private ShardingSphereResource shardingSphereResource;
@@ -95,7 +96,7 @@ public final class AlterShardingTableRuleBackendHandlerTest {
public void assertExecuteWithoutShardingRule() {
handler.execute("test", sqlStatement);
}
-
+
@Test
public void assertExecute() {
TableRuleSegment tableRuleSegment = new TableRuleSegment();
@@ -137,6 +138,19 @@ public final class AlterShardingTableRuleBackendHandlerTest {
handler.execute("test", sqlStatement);
}
+ @Test(expected = InvalidShardingAlgorithmsException.class)
+ public void assertExecuteWithInvalidAlgorithms() {
+ TableRuleSegment tableRuleSegment = new TableRuleSegment();
+ tableRuleSegment.setLogicTable("t_order_item");
+ tableRuleSegment.setDataSources(Collections.emptyList());
+ FunctionSegment shardingAlgorithm = new FunctionSegment();
+ shardingAlgorithm.setAlgorithmName("algorithm-not-exist");
+ tableRuleSegment.setTableStrategy(shardingAlgorithm);
+ when(sqlStatement.getTables()).thenReturn(Arrays.asList(tableRuleSegment));
+ when(ruleMetaData.getConfigurations()).thenReturn(buildShardingConfigurations());
+ handler.execute("test", sqlStatement);
+ }
+
private Collection<RuleConfiguration> buildShardingConfigurations() {
ShardingRuleConfiguration configuration = new ShardingRuleConfiguration();
configuration.getTables().add(new ShardingTableRuleConfiguration("t_order_item"));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandlerTest.java
index 7020121..cfcb382 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandlerTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
+import org.apache.shardingsphere.distsql.parser.segment.FunctionSegment;
import org.apache.shardingsphere.distsql.parser.segment.TableRuleSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
@@ -26,6 +27,7 @@ import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
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.DuplicateTablesException;
+import org.apache.shardingsphere.proxy.backend.exception.InvalidShardingAlgorithmsException;
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;
@@ -81,6 +83,12 @@ public final class CreateShardingTableRuleBackendHandlerTest {
@Test
public void assertExecute() {
+ TableRuleSegment tableRuleSegment = new TableRuleSegment();
+ tableRuleSegment.setLogicTable("t_order_item");
+ tableRuleSegment.setDataSources(Collections.emptyList());
+ FunctionSegment shardingAlgorithm = new FunctionSegment();
+ shardingAlgorithm.setAlgorithmName("hash_mod");
+ tableRuleSegment.setTableStrategy(shardingAlgorithm);
ResponseHeader responseHeader = handler.execute("test", sqlStatement);
assertNotNull(responseHeader);
assertTrue(responseHeader instanceof UpdateResponseHeader);
@@ -103,4 +111,16 @@ public final class CreateShardingTableRuleBackendHandlerTest {
when(sqlStatement.getTables()).thenReturn(Collections.singletonList(tableRuleSegment));
handler.execute("test", sqlStatement);
}
+
+ @Test(expected = InvalidShardingAlgorithmsException.class)
+ public void assertExecuteWithInvalidAlgorithms() {
+ TableRuleSegment tableRuleSegment = new TableRuleSegment();
+ tableRuleSegment.setLogicTable("t_order_item");
+ tableRuleSegment.setDataSources(Collections.emptyList());
+ FunctionSegment shardingAlgorithm = new FunctionSegment();
+ shardingAlgorithm.setAlgorithmName("algorithm-not-exist");
+ tableRuleSegment.setTableStrategy(shardingAlgorithm);
+ when(sqlStatement.getTables()).thenReturn(Arrays.asList(tableRuleSegment));
+ handler.execute("test", sqlStatement);
+ }
}
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 582b71c..c092b75 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
@@ -35,6 +35,7 @@ import org.apache.shardingsphere.proxy.backend.exception.InvalidDatabaseDiscover
import org.apache.shardingsphere.proxy.backend.exception.InvalidEncryptorsException;
import org.apache.shardingsphere.proxy.backend.exception.InvalidLoadBalancersException;
import org.apache.shardingsphere.proxy.backend.exception.InvalidResourceException;
+import org.apache.shardingsphere.proxy.backend.exception.InvalidShardingAlgorithmsException;
import org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
import org.apache.shardingsphere.proxy.backend.exception.ReadwriteSplittingRulesNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.ResourceInUsedException;
@@ -188,6 +189,9 @@ public final class MySQLErrPacketFactory {
return new MySQLErrPacket(1, CommonErrorCode.DUPLICATE_RULE_NAMES, ((DuplicateRuleNamesException) cause).getRuleNames(),
((DuplicateRuleNamesException) cause).getSchemaName());
}
+ if (cause instanceof InvalidShardingAlgorithmsException) {
+ return new MySQLErrPacket(1, CommonErrorCode.INVALID_SHARDING_ALGORITHMS, ((InvalidShardingAlgorithmsException) cause).getAlgorithms());
+ }
return new MySQLErrPacket(1, CommonErrorCode.UNKNOWN_EXCEPTION, cause.getMessage());
}
}