未验证 提交 9d3f7e41 编写于 作者: L Liang Zhang 提交者: GitHub

Use SQLRouteExecutor to refactor SQL route engine (#8009)

* For code format

* Use SQLRouteExecutor to refactor SQL route engine
上级 e240277d
......@@ -82,7 +82,7 @@ public final class EncryptSQLRewriterParameterizedTest extends AbstractSQLRewrit
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
......
......@@ -38,10 +38,13 @@ public final class ShardingTableAddressingMetaDataDecorator implements TableAddr
}
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
......
......@@ -89,7 +89,7 @@ public final class MixSQLRewriterParameterizedTest extends AbstractSQLRewriterPa
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
......
......@@ -89,7 +89,7 @@ public final class ShardingSQLRewriterParameterizedTest extends AbstractSQLRewri
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
......
......@@ -62,7 +62,7 @@ public abstract class AbstractSQLRouteTest extends AbstractRoutingEngineTest {
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;
}
......
......@@ -136,8 +136,8 @@ public final class LogicSchemaMetaDataLoader {
return load(databaseType, dataSourceMap, tableName, props);
}
private Map<String, Collection<String>> loadUnConfiguredSchemaMetaData(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final Collection<String> excludedTableNames) throws SQLException {
private Map<String, Collection<String>> loadUnConfiguredSchemaMetaData(final DatabaseType databaseType,
final Map<String, DataSource> dataSourceMap, final Collection<String> excludedTableNames) throws SQLException {
Map<String, Collection<String>> result = new HashMap<>(dataSourceMap.size(), 1);
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
Collection<String> tableNames = PhysicalSchemaMetaDataLoader.loadTableNames(entry.getValue(), databaseType, excludedTableNames);
......
......@@ -47,7 +47,7 @@ public final class KernelProcessor {
*/
public ExecutionContext generateExecutionContext(final LogicSQL logicSQL, final ShardingSphereSchema schema, final ConfigurationProperties props) {
Collection<ShardingSphereRule> 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);
......
......@@ -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<ShardingSphereRule> rules;
private final ConfigurationProperties props;
@SuppressWarnings("rawtypes")
private final Map<ShardingSphereRule, SQLRouter> routers;
private final SPIRoutingHook routingHook;
public SQLRouteEngine(final ConfigurationProperties props, final Collection<ShardingSphereRule> 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 final class SQLRouteEngine {
public RouteContext route(final LogicSQL logicSQL, final ShardingSphereSchema schema) {
routingHook.start(logicSQL.getSql());
try {
RouteContext result = doRoute(logicSQL, schema);
SQLRouteExecutor executor = isNeedAllSchemas(logicSQL.getSqlStatementContext().getSqlStatement()) ? new AllSQLRouteExecutor() : new PartialSQLRouteExecutor(rules, props);
RouteContext result = executor.route(logicSQL, schema);
routingHook.finishSuccess(result, schema.getMetaData().getSchemaMetaData().getConfiguredSchemaMetaData());
return result;
// CHECKSTYLE:OFF
......@@ -75,17 +65,8 @@ public final class SQLRouteEngine {
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
private RouteContext doRoute(final LogicSQL logicSQL, final ShardingSphereSchema schema) {
RouteContext result = new RouteContext();
for (Entry<ShardingSphereRule, SQLRouter> 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;
}
}
/*
* 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);
}
......@@ -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<String> getAllDataSourceNames(final ShardingSphereSchema schema) {
Collection<String> result = new LinkedHashSet<>();
for (Collection<String> each : schema.getMetaData().getTableAddressingMetaData().getTableDataSourceNamesMapper().values()) {
result.addAll(each);
}
return result;
}
}
/*
* 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<ShardingSphereRule, SQLRouter> routers;
public PartialSQLRouteExecutor(final Collection<ShardingSphereRule> 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<ShardingSphereRule, SQLRouter> 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;
}
}
......@@ -67,7 +67,7 @@ public final class SQLRouteEngineTest {
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 final class SQLRouteEngineTest {
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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册