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 e9ac512 Add CircuitBreakException
new 97f8f24 Merge pull request #7484 from terrymanu/dev
e9ac512 is described below
commit e9ac5125fd2441c9916bfa640bfb83fab898da77
Author: terrymanu <[email protected]>
AuthorDate: Wed Sep 16 17:28:32 2020 +0800
Add CircuitBreakException
---
.../shardingsphere/sharding/rule/ShardingRule.java | 56 +++++++++++-----------
.../backend/exception/CircuitBreakException.java | 26 ++++++++++
.../frontend/mysql/MySQLErrPacketFactory.java | 4 ++
.../execute/MySQLComStmtExecuteExecutor.java | 5 +-
.../text/query/MySQLComQueryPacketExecutor.java | 5 +-
.../frontend/mysql/MySQLErrPacketFactoryTest.java | 6 +--
6 files changed, 65 insertions(+), 37 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index 5bd30b1..1dc3754 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -77,36 +77,36 @@ public final class ShardingRule implements
DataNodeRoutedRule {
private final KeyGenerateAlgorithm defaultKeyGenerateAlgorithm;
- public ShardingRule(final ShardingRuleConfiguration configuration, final
Collection<String> dataSourceNames) {
- Preconditions.checkArgument(null != configuration, "ShardingRuleConfig
cannot be null.");
+ public ShardingRule(final ShardingRuleConfiguration config, final
Collection<String> dataSourceNames) {
+ Preconditions.checkArgument(null != config, "ShardingRuleConfig cannot
be null.");
Preconditions.checkArgument(null != dataSourceNames &&
!dataSourceNames.isEmpty(), "Data sources cannot be empty.");
- this.dataSourceNames = getDataSourceNames(configuration.getTables(),
dataSourceNames);
- configuration.getShardingAlgorithms().forEach((key, value) ->
shardingAlgorithms.put(key,
ShardingSphereAlgorithmFactory.createAlgorithm(value,
ShardingAlgorithm.class)));
- configuration.getKeyGenerators().forEach((key, value) ->
keyGenerators.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value,
KeyGenerateAlgorithm.class)));
- tableRules = new
LinkedList<>(createTableRules(configuration.getTables(),
configuration.getDefaultKeyGenerateStrategy()));
- tableRules.addAll(createAutoTableRules(configuration.getAutoTables(),
configuration.getDefaultKeyGenerateStrategy()));
- broadcastTables = configuration.getBroadcastTables();
- bindingTableRules =
createBindingTableRules(configuration.getBindingTableGroups());
- defaultDatabaseShardingStrategy =
createDefaultShardingStrategy(configuration.getDefaultDatabaseShardingStrategy());
- defaultTableShardingStrategy =
createDefaultShardingStrategy(configuration.getDefaultTableShardingStrategy());
- defaultKeyGenerateAlgorithm = null ==
configuration.getDefaultKeyGenerateStrategy()
- ?
TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class) :
keyGenerators.get(configuration.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
- }
-
- public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration
configuration, final Collection<String> dataSourceNames) {
- Preconditions.checkArgument(null != configuration, "ShardingRuleConfig
cannot be null.");
+ this.dataSourceNames = getDataSourceNames(config.getTables(),
dataSourceNames);
+ config.getShardingAlgorithms().forEach((key, value) ->
shardingAlgorithms.put(key,
ShardingSphereAlgorithmFactory.createAlgorithm(value,
ShardingAlgorithm.class)));
+ config.getKeyGenerators().forEach((key, value) ->
keyGenerators.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value,
KeyGenerateAlgorithm.class)));
+ tableRules = new LinkedList<>(createTableRules(config.getTables(),
config.getDefaultKeyGenerateStrategy()));
+ tableRules.addAll(createAutoTableRules(config.getAutoTables(),
config.getDefaultKeyGenerateStrategy()));
+ broadcastTables = config.getBroadcastTables();
+ bindingTableRules =
createBindingTableRules(config.getBindingTableGroups());
+ defaultDatabaseShardingStrategy =
createDefaultShardingStrategy(config.getDefaultDatabaseShardingStrategy());
+ defaultTableShardingStrategy =
createDefaultShardingStrategy(config.getDefaultTableShardingStrategy());
+ defaultKeyGenerateAlgorithm = null ==
config.getDefaultKeyGenerateStrategy()
+ ?
TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class) :
keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
+ }
+
+ public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration
config, final Collection<String> dataSourceNames) {
+ Preconditions.checkArgument(null != config, "ShardingRuleConfig cannot
be null.");
Preconditions.checkArgument(null != dataSourceNames &&
!dataSourceNames.isEmpty(), "Data sources cannot be empty.");
- this.dataSourceNames = getDataSourceNames(configuration.getTables(),
dataSourceNames);
- shardingAlgorithms.putAll(configuration.getShardingAlgorithms());
- keyGenerators.putAll(configuration.getKeyGenerators());
- tableRules = new
LinkedList<>(createTableRules(configuration.getTables(),
configuration.getDefaultKeyGenerateStrategy()));
- tableRules.addAll(createAutoTableRules(configuration.getAutoTables(),
configuration.getDefaultKeyGenerateStrategy()));
- broadcastTables = configuration.getBroadcastTables();
- bindingTableRules =
createBindingTableRules(configuration.getBindingTableGroups());
- defaultDatabaseShardingStrategy =
createDefaultShardingStrategy(configuration.getDefaultDatabaseShardingStrategy());
- defaultTableShardingStrategy =
createDefaultShardingStrategy(configuration.getDefaultTableShardingStrategy());
- defaultKeyGenerateAlgorithm = null ==
configuration.getDefaultKeyGenerateStrategy()
- ?
TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class) :
keyGenerators.get(configuration.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
+ this.dataSourceNames = getDataSourceNames(config.getTables(),
dataSourceNames);
+ shardingAlgorithms.putAll(config.getShardingAlgorithms());
+ keyGenerators.putAll(config.getKeyGenerators());
+ tableRules = new LinkedList<>(createTableRules(config.getTables(),
config.getDefaultKeyGenerateStrategy()));
+ tableRules.addAll(createAutoTableRules(config.getAutoTables(),
config.getDefaultKeyGenerateStrategy()));
+ broadcastTables = config.getBroadcastTables();
+ bindingTableRules =
createBindingTableRules(config.getBindingTableGroups());
+ defaultDatabaseShardingStrategy =
createDefaultShardingStrategy(config.getDefaultDatabaseShardingStrategy());
+ defaultTableShardingStrategy =
createDefaultShardingStrategy(config.getDefaultTableShardingStrategy());
+ defaultKeyGenerateAlgorithm = null ==
config.getDefaultKeyGenerateStrategy()
+ ?
TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class) :
keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
}
private Collection<String> getDataSourceNames(final
Collection<ShardingTableRuleConfiguration> tableRuleConfigs, final
Collection<String> dataSourceNames) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/CircuitBreakException.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/CircuitBreakException.java
new file mode 100644
index 0000000..8735fae
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/CircuitBreakException.java
@@ -0,0 +1,26 @@
+/*
+ * 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;
+
+/**
+ * Circuit break exception.
+ */
+public final class CircuitBreakException extends BackendException {
+
+ private static final long serialVersionUID = 6339672680026286798L;
+}
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 f7437f4..12e5ccb 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
@@ -23,6 +23,7 @@ import
org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
import
org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+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.NoDatabaseSelectedException;
@@ -76,6 +77,9 @@ public final class MySQLErrPacketFactory {
if (cause instanceof TableExistsException) {
return new MySQLErrPacket(sequenceId,
MySQLServerErrorCode.ER_TABLE_EXISTS_ERROR, ((TableExistsException)
cause).getTableName());
}
+ if (cause instanceof CircuitBreakException) {
+ return new MySQLErrPacket(sequenceId,
CommonErrorCode.CIRCUIT_BREAK_MODE);
+ }
if (cause instanceof ShardingSphereConfigurationException || cause
instanceof SQLParsingException) {
return new MySQLErrPacket(sequenceId,
MySQLServerErrorCode.ER_NOT_SUPPORTED_YET, cause.getMessage());
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
index 4100b70..b2f75a5 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutor.java
@@ -18,7 +18,6 @@
package
org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute;
import lombok.Getter;
-import org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLColumnType;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLColumnDefinition41Packet;
@@ -26,7 +25,6 @@ import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLFie
import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute.MySQLBinaryResultSetRowPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.binary.execute.MySQLComStmtExecutePacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLEofPacket;
-import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import
org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.QueryHeader;
@@ -34,6 +32,7 @@ import
org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicati
import
org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngineFactory;
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.CircuitBreakException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
@@ -70,7 +69,7 @@ public final class MySQLComStmtExecuteExecutor implements
QueryCommandExecutor {
@Override
public Collection<DatabasePacket<?>> execute() throws SQLException {
if (ProxyContext.getInstance().getSchemaContexts().isCircuitBreak()) {
- return Collections.singletonList(new MySQLErrPacket(1,
CommonErrorCode.CIRCUIT_BREAK_MODE));
+ throw new CircuitBreakException();
}
BackendResponse backendResponse =
databaseCommunicationEngine.execute();
if (backendResponse instanceof QueryResponse) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutor.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutor.java
index efd7336..be8452a 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutor.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutor.java
@@ -18,7 +18,6 @@
package
org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query;
import lombok.Getter;
-import org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLColumnType;
import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLColumnDefinition41Packet;
@@ -27,13 +26,13 @@ import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.MySQLFie
import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.MySQLTextResultSetRowPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.query.MySQLComQueryPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLEofPacket;
-import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
import
org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.QueryHeader;
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.CircuitBreakException;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
@@ -67,7 +66,7 @@ public final class MySQLComQueryPacketExecutor implements
QueryCommandExecutor {
@Override
public Collection<DatabasePacket<?>> execute() throws SQLException {
if (ProxyContext.getInstance().getSchemaContexts().isCircuitBreak()) {
- return Collections.singletonList(new MySQLErrPacket(1,
CommonErrorCode.CIRCUIT_BREAK_MODE));
+ throw new CircuitBreakException();
}
BackendResponse backendResponse = textProtocolBackendHandler.execute();
if (backendResponse instanceof QueryResponse) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
index e3806fa..ac6a1f7 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
@@ -36,7 +36,7 @@ public final class MySQLErrPacketFactoryTest {
@Test
public void assertNewInstanceWithSQLException() {
- MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(1, new
SQLException("No reason", "XXX", 9999, new RuntimeException()));
+ MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(1, new
SQLException("No reason", "XXX", 9999, new RuntimeException("")));
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getErrorCode(), is(9999));
assertThat(actual.getSqlState(), is("XXX"));
@@ -44,7 +44,7 @@ public final class MySQLErrPacketFactoryTest {
}
@Test
- public void assertNewInstanceWithSQLExceptionOfNullSqlState() {
+ public void assertNewInstanceWithSQLExceptionOfNullSQLState() {
MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(1, new
SQLException(new RuntimeException("No reason")));
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getErrorCode(), is(1815));
@@ -54,7 +54,7 @@ public final class MySQLErrPacketFactoryTest {
@Test
public void assertNewInstanceWithSQLExceptionOfNullParam() {
- MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(1, new
SQLException());
+ MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(1, new
SQLException(""));
assertThat(actual.getSequenceId(), is(1));
assertThat(actual.getErrorCode(), is(1815));
assertThat(actual.getSqlState(), is("HY000"));