Skip to content

Commit

Permalink
Add SQLRouter.getType() (apache#34012)
Browse files Browse the repository at this point in the history
* Add SQLRouter.getType()

* Add SQLRouter.getType()
  • Loading branch information
terrymanu authored Dec 11, 2024
1 parent 278c6cc commit 956bfb8
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
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.type.EntranceSQLRouter;
import org.apache.shardingsphere.infra.route.type.TableSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.EntranceSQLRouter;
import org.apache.shardingsphere.infra.session.query.QueryContext;

import java.util.Collection;
Expand All @@ -35,7 +34,7 @@
* Broadcast SQL router.
*/
@HighFrequencyInvocation
public final class BroadcastSQLRouter implements EntranceSQLRouter<BroadcastRule>, TableSQLRouter<BroadcastRule> {
public final class BroadcastSQLRouter implements EntranceSQLRouter<BroadcastRule> {

@Override
public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database,
Expand All @@ -47,6 +46,11 @@ public RouteContext createRouteContext(final QueryContext queryContext, final Ru
return BroadcastRouteEngineFactory.newInstance(queryContext, broadcastTableNames).route(rule);
}

@Override
public Type getType() {
return Type.DATA_NODE;
}

@Override
public int getOrder() {
return BroadcastOrder.ORDER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
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.type.DataSourceSQLRouter;
import org.apache.shardingsphere.infra.route.type.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.DecorateSQLRouter;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.readwritesplitting.constant.ReadwriteSplittingOrder;
import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
Expand All @@ -36,7 +35,7 @@
* Readwrite-splitting SQL router.
*/
@HighFrequencyInvocation
public final class ReadwriteSplittingSQLRouter implements DecorateSQLRouter<ReadwriteSplittingRule>, DataSourceSQLRouter<ReadwriteSplittingRule> {
public final class ReadwriteSplittingSQLRouter implements DecorateSQLRouter<ReadwriteSplittingRule> {

@Override
public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database,
Expand All @@ -56,6 +55,11 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon
routeContext.getRouteUnits().addAll(toBeAdded);
}

@Override
public Type getType() {
return Type.DATA_SOURCE;
}

@Override
public int getOrder() {
return ReadwriteSplittingOrder.ORDER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.type.DataSourceSQLRouter;
import org.apache.shardingsphere.infra.route.type.DecorateSQLRouter;
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.lifecycle.DecorateSQLRouter;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.shadow.constant.ShadowOrder;
import org.apache.shardingsphere.shadow.route.retriever.ShadowDataSourceMappingsRetrieverFactory;
Expand All @@ -39,7 +38,7 @@
* Shadow SQL router.
*/
@HighFrequencyInvocation
public final class ShadowSQLRouter implements DecorateSQLRouter<ShadowRule>, DataSourceSQLRouter<ShadowRule> {
public final class ShadowSQLRouter implements DecorateSQLRouter<ShadowRule> {

@Override
public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database,
Expand All @@ -62,6 +61,11 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon
routeContext.getRouteUnits().addAll(toBeAddedRouteUnit);
}

@Override
public Type getType() {
return Type.DATA_SOURCE;
}

@Override
public int getOrder() {
return ShadowOrder.ORDER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
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.type.EntranceSQLRouter;
import org.apache.shardingsphere.infra.route.type.TableSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.EntranceSQLRouter;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.sharding.cache.route.CachedShardingSQLRouter;
import org.apache.shardingsphere.sharding.constant.ShardingOrder;
Expand All @@ -46,7 +45,7 @@
* Sharding SQL router.
*/
@HighFrequencyInvocation
public final class ShardingSQLRouter implements EntranceSQLRouter<ShardingRule>, TableSQLRouter<ShardingRule> {
public final class ShardingSQLRouter implements EntranceSQLRouter<ShardingRule> {

@Override
public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database,
Expand Down Expand Up @@ -93,6 +92,11 @@ private void checkRouteContext(final QueryContext queryContext, final ShardingSp
ShardingRouteContextCheckerFactory.newInstance(sqlStatement, shardingConditions).ifPresent(optional -> optional.check(rule, queryContext, database, props, routeContext));
}

@Override
public Type getType() {
return Type.DATA_NODE;
}

@Override
public int getOrder() {
return ShardingOrder.ORDER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,25 @@
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPI;

/**
* SQL Router.
* SQL router.
*
* @param <T> type of rule
*/
@SingletonSPI
public interface SQLRouter<T extends ShardingSphereRule> extends OrderedSPI<T> {

/**
* Get SQL router type.
*
* @return SQL router type
*/
Type getType();

/**
* SQL router type.
*/
enum Type {

DATA_NODE, DATA_SOURCE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
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.SQLRouter.Type;
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.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.route.lifecycle.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.EntranceSQLRouter;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
Expand All @@ -55,7 +54,7 @@ public final class SQLRouteEngine {
private final ConfigurationProperties props;

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

@SuppressWarnings("rawtypes")
private final Map<ShardingSphereRule, SQLRouter> dataSourceRouters;
Expand All @@ -64,15 +63,15 @@ public final class SQLRouteEngine {
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);
dataNodeRouters = filterRouters(routers, Type.DATA_NODE);
dataSourceRouters = filterRouters(routers, Type.DATA_SOURCE);
}

@SuppressWarnings("rawtypes")
private Map<ShardingSphereRule, SQLRouter> filterRouters(final Map<ShardingSphereRule, SQLRouter> routers, final Class<? extends SQLRouter> targetClass) {
private Map<ShardingSphereRule, SQLRouter> filterRouters(final Map<ShardingSphereRule, SQLRouter> routers, final Type type) {
Map<ShardingSphereRule, SQLRouter> result = new LinkedHashMap<>();
for (Entry<ShardingSphereRule, SQLRouter> entry : routers.entrySet()) {
if (targetClass.isAssignableFrom(entry.getValue().getClass())) {
if (type == entry.getValue().getType()) {
result.put(entry.getKey(), entry.getValue());
}
}
Expand All @@ -95,7 +94,7 @@ public RouteContext route(final QueryContext queryContext, final RuleMetaData gl
return result;
}
Collection<String> tableNames = SQLStatementContextExtractor.getTableNames(database, queryContext.getSqlStatementContext());
result = route(queryContext, globalRuleMetaData, database, tableRouters, tableNames, result);
result = route(queryContext, globalRuleMetaData, database, dataNodeRouters, tableNames, result);
result = new TablelessSQLRouter().route(queryContext, globalRuleMetaData, database, tableNames, result);
result = route(queryContext, globalRuleMetaData, database, dataSourceRouters, tableNames, result);
if (result.getRouteUnits().isEmpty() && 1 == database.getResourceMetaData().getStorageUnits().size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.route.type;
package org.apache.shardingsphere.infra.route.lifecycle;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
Expand All @@ -27,7 +27,7 @@
import java.util.Collection;

/**
* Decorate SQL Router.
* Decorate SQL router.
*
* @param <T> type of rule
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.route.type;
package org.apache.shardingsphere.infra.route.lifecycle;

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
Expand All @@ -28,7 +28,7 @@
import java.util.Collection;

/**
* Entrance SQL Router.
* Entrance SQL router.
*
* @param <T> type of rule
*/
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.fixture.rule.RouteFailureRuleFixture;
import org.apache.shardingsphere.infra.route.type.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.type.TableSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.DecorateSQLRouter;
import org.apache.shardingsphere.infra.session.query.QueryContext;

import java.util.Collection;

public final class TableSQLRouterFailureFixture implements DecorateSQLRouter<RouteFailureRuleFixture>, TableSQLRouter<RouteFailureRuleFixture> {
public final class DataNodeSQLRouterFailureFixture implements DecorateSQLRouter<RouteFailureRuleFixture> {

@Override
public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database,
final RouteFailureRuleFixture rule, final Collection<String> tableNames, final ConfigurationProperties props) {
throw new UnsupportedOperationException("Route failure.");
}

@Override
public Type getType() {
return Type.DATA_NODE;
}

@Override
public int getOrder() {
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.route.type.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.type.EntranceSQLRouter;
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.fixture.rule.TableRouteRuleFixture;
import org.apache.shardingsphere.infra.route.type.TableSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.EntranceSQLRouter;
import org.apache.shardingsphere.infra.session.query.QueryContext;

import java.util.Collection;
import java.util.Collections;

public final class TableSQLRouterFixture implements EntranceSQLRouter<TableRouteRuleFixture>, DecorateSQLRouter<TableRouteRuleFixture>, TableSQLRouter<TableRouteRuleFixture> {
public final class DataNodeSQLRouterFixture implements EntranceSQLRouter<TableRouteRuleFixture>, DecorateSQLRouter<TableRouteRuleFixture> {

@Override
public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final TableRouteRuleFixture rule,
Expand All @@ -48,6 +47,11 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon
routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList()));
}

@Override
public Type getType() {
return Type.DATA_NODE;
}

@Override
public int getOrder() {
return -10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.route.fixture.rule.DataSourceRouteRuleFixture;
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.lifecycle.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.EntranceSQLRouter;
import org.apache.shardingsphere.infra.session.query.QueryContext;

import java.util.Collection;
import java.util.Collections;

public final class DataSourceSQLRouterFixture implements EntranceSQLRouter<DataSourceRouteRuleFixture>, DecorateSQLRouter<DataSourceRouteRuleFixture>, DataSourceSQLRouter<DataSourceRouteRuleFixture> {
public final class DataSourceSQLRouterFixture implements EntranceSQLRouter<DataSourceRouteRuleFixture>, DecorateSQLRouter<DataSourceRouteRuleFixture> {

@Override
public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final DataSourceRouteRuleFixture rule,
Expand All @@ -48,6 +47,11 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon
routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList()));
}

@Override
public Type getType() {
return Type.DATA_SOURCE;
}

@Override
public int getOrder() {
return 0;
Expand Down
Loading

0 comments on commit 956bfb8

Please sign in to comment.