Skip to content

Commit

Permalink
Use SQLRouteExecutor to refactor SQL route engine (#8009)
Browse files Browse the repository at this point in the history
* For code format

* Use SQLRouteExecutor to refactor SQL route engine
  • Loading branch information
terrymanu authored Nov 2, 2020
1 parent e240277 commit 9d3f7e4
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected Collection<SQLRewriteUnit> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected Collection<SQLRewriteUnit> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected Collection<SQLRewriteUnit> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected final RouteContext assertRoute(final String sql, final List<Object> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public Optional<PhysicalTableMetaData> load(final DatabaseType databaseType, fin
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -64,7 +53,8 @@ public SQLRouteEngine(final ConfigurationProperties props, final Collection<Shar
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
Expand All @@ -75,17 +65,8 @@ public RouteContext route(final LogicSQL logicSQL, final ShardingSphereSchema sc
}
}

@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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand Down

0 comments on commit 9d3f7e4

Please sign in to comment.