提交 e9ac5125 编写于 作者: T terrymanu

Add CircuitBreakException

上级 de6a0bc1
......@@ -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) {
......
/*
* 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;
}
......@@ -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());
}
......
......@@ -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) {
......
......@@ -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) {
......
......@@ -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"));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册