From 9d3f7e4146ac7cd6610e5b5f22023cc7210acb6b Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Mon, 2 Nov 2020 18:35:21 +0800 Subject: [PATCH] Use SQLRouteExecutor to refactor SQL route engine (#8009) * For code format * Use SQLRouteExecutor to refactor SQL route engine --- .../EncryptSQLRewriterParameterizedTest.java | 2 +- ...rdingTableAddressingMetaDataDecorator.java | 7 +- .../MixSQLRewriterParameterizedTest.java | 2 +- .../ShardingSQLRewriterParameterizedTest.java | 2 +- .../type/standard/AbstractSQLRouteTest.java | 2 +- .../logic/LogicSchemaMetaDataLoader.java | 4 +- .../infra/context/kernel/KernelProcessor.java | 2 +- .../infra/route/engine/SQLRouteEngine.java | 47 ++++--------- .../infra/route/engine/SQLRouteExecutor.java | 37 +++++++++++ .../impl/AllSQLRouteExecutor.java} | 40 ++++++----- .../engine/impl/PartialSQLRouteExecutor.java | 66 +++++++++++++++++++ .../route/engine/SQLRouteEngineTest.java | 4 +- 12 files changed, 150 insertions(+), 65 deletions(-) create mode 100644 shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java rename shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/{UnconfiguredSchemaSQLRouter.java => engine/impl/AllSQLRouteExecutor.java} (50%) create mode 100644 shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java index 3542febfc4a28..05f17c707231f 100644 --- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java +++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java @@ -82,7 +82,7 @@ protected Collection createSQLRewriteUnits() throws IOException getTestParameters().getInputParameters(), sqlStatementParserEngine.parse(getTestParameters().getInputSQL(), false)); LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters()); ShardingSphereSchema schema = new ShardingSphereSchema("sharding_db", Collections.emptyList(), rules, Collections.emptyMap(), metaData); - RouteContext routeContext = new SQLRouteEngine(props, rules).route(logicSQL, schema); + RouteContext routeContext = new SQLRouteEngine(rules, props).route(logicSQL, schema); SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getSchemaMetaData().getConfiguredSchemaMetaData(), props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext); return sqlRewriteResult instanceof GenericSQLRewriteResult diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java index aa80389c1f3b8..9a8fc45b41267 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java @@ -38,10 +38,13 @@ public void decorate(final ShardingRule rule, final TableAddressingMetaData meta } private void decorate(final TableRule tableRule, final TableAddressingMetaData metaData) { + boolean found = false; for (String each : tableRule.getActualDataNodes().stream().map(DataNode::getTableName).collect(Collectors.toSet())) { - metaData.getTableDataSourceNamesMapper().remove(each); + found = null != metaData.getTableDataSourceNamesMapper().remove(each) || found; + } + if (found) { + metaData.getTableDataSourceNamesMapper().put(tableRule.getLogicTable(), new LinkedList<>(tableRule.getActualDatasourceNames())); } - metaData.getTableDataSourceNamesMapper().put(tableRule.getLogicTable(), new LinkedList<>(tableRule.getActualDatasourceNames())); } @Override diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java index 2e09c66cb59cd..53dc92216c05e 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java @@ -89,7 +89,7 @@ protected Collection createSQLRewriteUnits() throws IOException getTestParameters().getInputParameters(), sqlStatementParserEngine.parse(getTestParameters().getInputSQL(), false)); LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters()); ShardingSphereSchema schema = new ShardingSphereSchema("sharding_db", Collections.emptyList(), rules, Collections.emptyMap(), metaData); - RouteContext routeContext = new SQLRouteEngine(props, rules).route(logicSQL, schema); + RouteContext routeContext = new SQLRouteEngine(rules, props).route(logicSQL, schema); SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getSchemaMetaData().getConfiguredSchemaMetaData(), props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext); return sqlRewriteResult instanceof GenericSQLRewriteResult diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java index a75b8d2d31a0e..f3505a8cef8d3 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java @@ -89,7 +89,7 @@ protected Collection createSQLRewriteUnits() throws IOException getTestParameters().getInputParameters(), sqlStatementParserEngine.parse(getTestParameters().getInputSQL(), false)); LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters()); ShardingSphereSchema schema = new ShardingSphereSchema("sharding_db", Collections.emptyList(), rules, Collections.emptyMap(), metaData); - RouteContext routeContext = new SQLRouteEngine(props, rules).route(logicSQL, schema); + RouteContext routeContext = new SQLRouteEngine(rules, props).route(logicSQL, schema); SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getSchemaMetaData().getConfiguredSchemaMetaData(), props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext); return sqlRewriteResult instanceof GenericSQLRewriteResult diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java index 5a6076c617093..84990e4627776 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java @@ -62,7 +62,7 @@ protected final RouteContext assertRoute(final String sql, final List pa metaData.getSchemaMetaData().getConfiguredSchemaMetaData(), parameters, sqlStatementParserEngine.parse(sql, false)); LogicSQL logicSQL = new LogicSQL(sqlStatementContext, sql, parameters); ShardingSphereSchema schema = new ShardingSphereSchema("sharding_db", Collections.emptyList(), Collections.singleton(shardingRule), Collections.emptyMap(), metaData); - RouteContext result = new SQLRouteEngine(props, Collections.singletonList(shardingRule)).route(logicSQL, schema); + RouteContext result = new SQLRouteEngine(Collections.singletonList(shardingRule), props).route(logicSQL, schema); assertThat(result.getRouteUnits().size(), is(1)); return result; } diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java index 1600e15f0fde8..858ff63969219 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java @@ -136,8 +136,8 @@ public Optional load(final DatabaseType databaseType, fin return load(databaseType, dataSourceMap, tableName, props); } - private Map> loadUnConfiguredSchemaMetaData(final DatabaseType databaseType, final Map dataSourceMap, - final Collection excludedTableNames) throws SQLException { + private Map> loadUnConfiguredSchemaMetaData(final DatabaseType databaseType, + final Map dataSourceMap, final Collection excludedTableNames) throws SQLException { Map> result = new HashMap<>(dataSourceMap.size(), 1); for (Entry entry : dataSourceMap.entrySet()) { Collection tableNames = PhysicalSchemaMetaDataLoader.loadTableNames(entry.getValue(), databaseType, excludedTableNames); diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java index 96c81ff8a3ca8..ddcc9107aa16d 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java +++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java @@ -47,7 +47,7 @@ public final class KernelProcessor { */ public ExecutionContext generateExecutionContext(final LogicSQL logicSQL, final ShardingSphereSchema schema, final ConfigurationProperties props) { Collection rules = schema.getRules(); - SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(props, rules); + SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(rules, props); SQLStatementContext sqlStatementContext = logicSQL.getSqlStatementContext(); RouteContext routeContext = sqlRouteEngine.route(logicSQL, schema); SQLRewriteEntry rewriteEntry = new SQLRewriteEntry(schema.getMetaData().getSchemaMetaData().getConfiguredSchemaMetaData(), props, rules); diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java index 4d9e8e353c8a4..a5a489cd5b698 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java @@ -17,42 +17,31 @@ package org.apache.shardingsphere.infra.route.engine; +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.infra.binder.LogicSQL; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; -import org.apache.shardingsphere.infra.route.SQLRouter; -import org.apache.shardingsphere.infra.route.UnconfiguredSchemaSQLRouter; import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.route.engine.impl.AllSQLRouteExecutor; +import org.apache.shardingsphere.infra.route.engine.impl.PartialSQLRouteExecutor; import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.schema.ShardingSphereSchema; -import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; -import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry; -import org.apache.shardingsphere.infra.binder.LogicSQL; +import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; +import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement; import java.util.Collection; -import java.util.Map; -import java.util.Map.Entry; /** * SQL route engine. */ +@RequiredArgsConstructor public final class SQLRouteEngine { - static { - ShardingSphereServiceLoader.register(SQLRouter.class); - } + private final Collection rules; private final ConfigurationProperties props; - @SuppressWarnings("rawtypes") - private final Map routers; - - private final SPIRoutingHook routingHook; - - public SQLRouteEngine(final ConfigurationProperties props, final Collection rules) { - this.props = props; - routers = OrderedSPIRegistry.getRegisteredServices(rules, SQLRouter.class); - routingHook = new SPIRoutingHook(); - } + private final SPIRoutingHook routingHook = new SPIRoutingHook(); /** * Route SQL. @@ -64,7 +53,8 @@ public SQLRouteEngine(final ConfigurationProperties props, final Collection entry : routers.entrySet()) { - if (result.getRouteUnits().isEmpty()) { - result = entry.getValue().createRouteContext(logicSQL, schema, entry.getKey(), props); - } else { - entry.getValue().decorateRouteContext(result, logicSQL, schema, entry.getKey(), props); - } - } - new UnconfiguredSchemaSQLRouter().decorate(result, logicSQL, schema); - return result; + // TODO use dynamic config to judge UnconfiguredSchema + private boolean isNeedAllSchemas(final SQLStatement sqlStatement) { + return sqlStatement instanceof MySQLShowTablesStatement; } } diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java new file mode 100644 index 0000000000000..8ebdb48084343 --- /dev/null +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java @@ -0,0 +1,37 @@ +/* + * 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.infra.route.engine; + +import org.apache.shardingsphere.infra.binder.LogicSQL; +import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.schema.ShardingSphereSchema; + +/** + * SQL route executor. + */ +public interface SQLRouteExecutor { + + /** + * Route. + * + * @param logicSQL logic SQL + * @param schema ShardingSphere schema + * @return route context + */ + RouteContext route(LogicSQL logicSQL, ShardingSphereSchema schema); +} diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/UnconfiguredSchemaSQLRouter.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java similarity index 50% rename from shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/UnconfiguredSchemaSQLRouter.java rename to shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java index 9f27624feb9e0..0c5077173cacc 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/UnconfiguredSchemaSQLRouter.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java @@ -15,40 +15,38 @@ * limitations under the License. */ -package org.apache.shardingsphere.infra.route; +package org.apache.shardingsphere.infra.route.engine.impl; +import org.apache.shardingsphere.infra.binder.LogicSQL; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.infra.route.engine.SQLRouteExecutor; import org.apache.shardingsphere.infra.schema.ShardingSphereSchema; -import org.apache.shardingsphere.infra.binder.LogicSQL; -import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; -import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement; +import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashSet; /** - * Unconfigured schema SQL router. + * All SQL route executor. */ -public final class UnconfiguredSchemaSQLRouter { +public final class AllSQLRouteExecutor implements SQLRouteExecutor { - /** - * Decorate route context. - * - * @param routeContext route context - * @param logicSQL logic SQL - * @param schema ShardingSphere schema - */ - public void decorate(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereSchema schema) { - if (isNeedUnconfiguredSchema(logicSQL.getSqlStatementContext().getSqlStatement())) { - for (String each : schema.getMetaData().getSchemaMetaData().getUnconfiguredSchemaMetaDataMap().keySet()) { - routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); - } + @Override + public RouteContext route(final LogicSQL logicSQL, final ShardingSphereSchema schema) { + RouteContext result = new RouteContext(); + for (String each : getAllDataSourceNames(schema)) { + result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); } + return result; } - // TODO use dynamic config to judge UnconfiguredSchema - private boolean isNeedUnconfiguredSchema(final SQLStatement sqlStatement) { - return sqlStatement instanceof MySQLShowTablesStatement; + private Collection getAllDataSourceNames(final ShardingSphereSchema schema) { + Collection result = new LinkedHashSet<>(); + for (Collection each : schema.getMetaData().getTableAddressingMetaData().getTableDataSourceNamesMapper().values()) { + result.addAll(each); + } + return result; } } diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java new file mode 100644 index 0000000000000..d5daa1a86de3e --- /dev/null +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java @@ -0,0 +1,66 @@ +/* + * 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.infra.route.engine.impl; + +import org.apache.shardingsphere.infra.binder.LogicSQL; +import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; +import org.apache.shardingsphere.infra.route.SQLRouter; +import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.route.engine.SQLRouteExecutor; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.schema.ShardingSphereSchema; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; +import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry; + +import java.util.Collection; +import java.util.Map; +import java.util.Map.Entry; + +/** + * Partial SQL route executor. + */ +public final class PartialSQLRouteExecutor implements SQLRouteExecutor { + + static { + ShardingSphereServiceLoader.register(SQLRouter.class); + } + + private final ConfigurationProperties props; + + @SuppressWarnings("rawtypes") + private final Map routers; + + public PartialSQLRouteExecutor(final Collection rules, final ConfigurationProperties props) { + this.props = props; + routers = OrderedSPIRegistry.getRegisteredServices(rules, SQLRouter.class); + } + + @Override + @SuppressWarnings({"unchecked", "rawtypes"}) + public RouteContext route(final LogicSQL logicSQL, final ShardingSphereSchema schema) { + RouteContext result = new RouteContext(); + for (Entry entry : routers.entrySet()) { + if (result.getRouteUnits().isEmpty()) { + result = entry.getValue().createRouteContext(logicSQL, schema, entry.getKey(), props); + } else { + entry.getValue().decorateRouteContext(result, logicSQL, schema, entry.getKey(), props); + } + } + return result; + } +} diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java index 25dbd01cdb1d7..b11b10b1b11b1 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java @@ -67,7 +67,7 @@ public void setUp() { public void assertRouteSuccess() { LogicSQL logicSQL = new LogicSQL(mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList()); ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(new RouteRuleFixture()), Collections.emptyMap(), metaData); - SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(props, Collections.singleton(new RouteRuleFixture())); + SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(Collections.singleton(new RouteRuleFixture()), props); setSPIRoutingHook(sqlRouteEngine); RouteContext actual = sqlRouteEngine.route(logicSQL, schema); assertThat(actual.getRouteUnits().size(), is(1)); @@ -83,7 +83,7 @@ public void assertRouteSuccess() { public void assertRouteFailure() { LogicSQL logicSQL = new LogicSQL(mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList()); ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(new RouteRuleFixture()), Collections.emptyMap(), metaData); - SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(props, Collections.singleton(new RouteFailureRuleFixture())); + SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(Collections.singleton(new RouteFailureRuleFixture()), props); setSPIRoutingHook(sqlRouteEngine); try { sqlRouteEngine.route(logicSQL, schema);