Skip to content

Commit

Permalink
Move PartialSQLRouteExecutor logic to TablelessRouteEngine to handle …
Browse files Browse the repository at this point in the history
…tableless sql route (#33820)

* Move PartialSQLRouteExecutor logic to TablelessRouteEngine to handle tableless sql route

* Remove useless spi file
  • Loading branch information
strongduanmu authored Nov 26, 2024
1 parent 67c768c commit 68a0ace
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 559 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,6 @@
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.route.engine.type.PartialSQLRouteExecutor"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.infra.route.SQLRouter\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.infra.route.engine.SQLRouteEngine"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.infra.route.engine.SQLRouteExecutorDecider\\E"
}, {
"condition":{"typeReachable":"org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query.MySQLComQueryPacketExecutor"},
"pattern":"\\QMETA-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder\\E"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,111 @@

package org.apache.shardingsphere.infra.route.engine;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.exception.kernel.syntax.hint.DataSourceHintNotExistsException;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.route.SQLRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.engine.type.AllSQLRouteExecutor;
import org.apache.shardingsphere.infra.route.engine.type.PartialSQLRouteExecutor;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.route.engine.tableless.router.TablelessSQLRouter;
import org.apache.shardingsphere.infra.route.type.DataSourceSQLRouter;
import org.apache.shardingsphere.infra.route.type.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.type.EntranceSQLRouter;
import org.apache.shardingsphere.infra.route.type.TableSQLRouter;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

/**
* SQL route engine.
*/
@HighFrequencyInvocation
@RequiredArgsConstructor
public final class SQLRouteEngine {

private final Collection<ShardingSphereRule> rules;

private final ConfigurationProperties props;

@SuppressWarnings("rawtypes")
private final Map<ShardingSphereRule, SQLRouter> tableRouters;

@SuppressWarnings("rawtypes")
private final Map<ShardingSphereRule, SQLRouter> dataSourceRouters;

@SuppressWarnings("rawtypes")
public SQLRouteEngine(final Collection<ShardingSphereRule> rules, final ConfigurationProperties props) {
this.props = props;
Map<ShardingSphereRule, SQLRouter> routers = OrderedSPILoader.getServices(SQLRouter.class, rules);
tableRouters = filterRouters(routers, TableSQLRouter.class);
dataSourceRouters = filterRouters(routers, DataSourceSQLRouter.class);
}

@SuppressWarnings("rawtypes")
private Map<ShardingSphereRule, SQLRouter> filterRouters(final Map<ShardingSphereRule, SQLRouter> routers, final Class<? extends SQLRouter> targetClass) {
Map<ShardingSphereRule, SQLRouter> result = new LinkedHashMap<>();
for (Entry<ShardingSphereRule, SQLRouter> entry : routers.entrySet()) {
if (targetClass.isAssignableFrom(entry.getValue().getClass())) {
result.put(entry.getKey(), entry.getValue());
}
}
return result;
}

/**
* Route SQL.
* Route.
*
* @param queryContext query context
* @param globalRuleMetaData global rule meta data
* @param database database
* @param database sharding sphere database
* @return route context
*/
public RouteContext route(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database) {
SQLRouteExecutor executor = isNeedRouteAll(queryContext.getSqlStatementContext().getSqlStatement()) ? new AllSQLRouteExecutor() : new PartialSQLRouteExecutor(rules, props);
return executor.route(queryContext, globalRuleMetaData, database);
RouteContext result = new RouteContext();
Optional<String> dataSourceName = findDataSourceByHint(queryContext.getHintValueContext(), database.getResourceMetaData().getStorageUnits());
if (dataSourceName.isPresent()) {
result.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName.get(), dataSourceName.get()), Collections.emptyList()));
return result;
}
result = route(queryContext, globalRuleMetaData, database, tableRouters, result);
result = new TablelessSQLRouter().route(queryContext, globalRuleMetaData, database, result);
result = route(queryContext, globalRuleMetaData, database, dataSourceRouters, result);
if (result.getRouteUnits().isEmpty() && 1 == database.getResourceMetaData().getStorageUnits().size()) {
String singleDataSourceName = database.getResourceMetaData().getStorageUnits().keySet().iterator().next();
result.getRouteUnits().add(new RouteUnit(new RouteMapper(singleDataSourceName, singleDataSourceName), Collections.emptyList()));
}
return result;
}

private boolean isNeedRouteAll(final SQLStatement sqlStatement) {
for (SQLRouteExecutorDecider each : ShardingSphereServiceLoader.getServiceInstances(SQLRouteExecutorDecider.class)) {
if (each.isNeedRouteAll(sqlStatement)) {
return true;
@SuppressWarnings({"unchecked", "rawtypes"})
private RouteContext route(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final Map<ShardingSphereRule, SQLRouter> routers,
final RouteContext routeContext) {
RouteContext result = routeContext;
for (Entry<ShardingSphereRule, SQLRouter> entry : routers.entrySet()) {
if (result.getRouteUnits().isEmpty() && entry.getValue() instanceof EntranceSQLRouter) {
result = ((EntranceSQLRouter) entry.getValue()).createRouteContext(queryContext, globalRuleMetaData, database, entry.getKey(), props);
} else if (entry.getValue() instanceof DecorateSQLRouter) {
((DecorateSQLRouter) entry.getValue()).decorateRouteContext(result, queryContext, database, entry.getKey(), props);
}
}
return false;
return result;
}

private Optional<String> findDataSourceByHint(final HintValueContext hintValueContext, final Map<String, StorageUnit> storageUnits) {
Optional<String> result = HintManager.isInstantiated() && HintManager.getDataSourceName().isPresent() ? HintManager.getDataSourceName() : hintValueContext.findHintDataSourceName();
if (result.isPresent() && !storageUnits.containsKey(result.get())) {
throw new DataSourceHintNotExistsException(result.get());
}
return result;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.route.engine;
package org.apache.shardingsphere.infra.route.engine.tableless;

import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.session.query.QueryContext;

/**
* SQL route executor.
* Tableless route engine.
*/
public interface SQLRouteExecutor {
public interface TablelessRouteEngine {

/**
* Route.
*
* @param queryContext query context
* @param globalRuleMetaData global rule meta data
* @param database database
* @return route context
*/
RouteContext route(QueryContext queryContext, RuleMetaData globalRuleMetaData, ShardingSphereDatabase database);
RouteContext route(RuleMetaData globalRuleMetaData, ShardingSphereDatabase database);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.tableless;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.route.engine.tableless.type.broadcast.DataSourceBroadcastRouteEngine;
import org.apache.shardingsphere.infra.route.engine.tableless.type.ignore.IgnoreRouteEngine;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowTableStatusStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowTablesStatement;

/**
* Tableless route engine factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TablelessRouteEngineFactory {

/**
* Create new instance of route engine.
*
* @param queryContext query context
* @return created instance
*/
public static TablelessRouteEngine newInstance(final QueryContext queryContext) {
SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext();
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
if (sqlStatement instanceof ShowTablesStatement || sqlStatement instanceof ShowTableStatusStatement) {
return new DataSourceBroadcastRouteEngine();
}
return new IgnoreRouteEngine();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.tableless.router;

import org.apache.shardingsphere.infra.binder.context.extractor.SQLStatementContextExtractor;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.engine.tableless.TablelessRouteEngineFactory;
import org.apache.shardingsphere.infra.session.query.QueryContext;

/**
* Tableless sql router.
*/
public final class TablelessSQLRouter {

/**
* Route.
*
* @param queryContext query context
* @param globalRuleMetaData global rule meta data
* @param database sharding sphere database
* @param routeContext route context
* @return route context
*/
public RouteContext route(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final RouteContext routeContext) {
if (routeContext.getRouteUnits().isEmpty() && SQLStatementContextExtractor.getTableNames(database, queryContext.getSqlStatementContext()).isEmpty()) {
return TablelessRouteEngineFactory.newInstance(queryContext).route(globalRuleMetaData, database);
}
return routeContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,24 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.route.engine.type;
package org.apache.shardingsphere.infra.route.engine.tableless.type.broadcast;

import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
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.session.query.QueryContext;
import org.apache.shardingsphere.infra.route.engine.tableless.TablelessRouteEngine;

import java.util.Collections;

/**
* All SQL route executor.
* Datasource broadcast route engine.
*/
public final class AllSQLRouteExecutor implements SQLRouteExecutor {
public final class DataSourceBroadcastRouteEngine implements TablelessRouteEngine {

@Override
public RouteContext route(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database) {
public RouteContext route(final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database) {
RouteContext result = new RouteContext();
for (String each : database.getResourceMetaData().getStorageUnits().keySet()) {
result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.route.fixture.decider;
package org.apache.shardingsphere.infra.route.engine.tableless.type.ignore;

import org.apache.shardingsphere.infra.route.engine.SQLRouteExecutorDecider;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.engine.tableless.TablelessRouteEngine;

public final class SQLRouteExecutorDeciderFixture implements SQLRouteExecutorDecider {
/**
* Ignore route engine.
*/
public final class IgnoreRouteEngine implements TablelessRouteEngine {

@Override
public boolean isNeedRouteAll(final SQLStatement sqlStatement) {
return sqlStatement instanceof RouteAllSQLStatementFixture;
public RouteContext route(final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database) {
return new RouteContext();
}
}
Loading

0 comments on commit 68a0ace

Please sign in to comment.