diff --git a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/route/BroadcastSQLRouter.java b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/route/BroadcastSQLRouter.java index 25e0c05d8ea24..7b44e74d57de6 100644 --- a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/route/BroadcastSQLRouter.java +++ b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/route/BroadcastSQLRouter.java @@ -68,13 +68,13 @@ public final class BroadcastSQLRouter implements EntranceSQLRouter tableNames, final ConfigurationProperties props) { return BroadcastRouteEngineFactory.newInstance(rule, database, queryContext).route(new RouteContext(), rule); } @Override public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, - final ShardingSphereDatabase database, final BroadcastRule rule, final ConfigurationProperties props) { + final ShardingSphereDatabase database, final BroadcastRule rule, final Collection tableNames, final ConfigurationProperties props) { SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); if (sqlStatement instanceof TCLStatement) { diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/route/BroadcastSQLRouterTest.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/route/BroadcastSQLRouterTest.java index 5caad2ffe31e6..26ce3cc15a254 100644 --- a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/route/BroadcastSQLRouterTest.java +++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/route/BroadcastSQLRouterTest.java @@ -74,7 +74,7 @@ void assertCreateRouteContext() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); BroadcastRouteEngine routeEngine = mock(BroadcastRouteEngine.class); when(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext)).thenReturn(routeEngine); - getSQLRouter(rule).createRouteContext(queryContext, mock(RuleMetaData.class), database, rule, new ConfigurationProperties(new Properties())); + getSQLRouter(rule).createRouteContext(queryContext, mock(RuleMetaData.class), database, rule, Collections.emptyList(), new ConfigurationProperties(new Properties())); verify(routeEngine).route(any(), eq(rule)); } @@ -86,7 +86,7 @@ void assertDecorateBroadcastRouteContextWithSingleDataSource() { RouteContext routeContext = new RouteContext(); routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("foo_ds", "foo_ds"), Lists.newArrayList())); BroadcastSQLRouter sqlRouter = getSQLRouter(rule); - sqlRouter.decorateRouteContext(routeContext, createQueryContext(), mockSingleDatabase(), rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(routeContext, createQueryContext(), mockSingleDatabase(), rule, Collections.emptyList(), new ConfigurationProperties(new Properties())); Iterator routedDataSourceNames = routeContext.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is("foo_ds")); } diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouter.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouter.java index aa3457ca7ecd6..ece2708b6838e 100644 --- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouter.java +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouter.java @@ -40,7 +40,7 @@ public final class ReadwriteSplittingSQLRouter implements DecorateSQLRouter tableNames, final ConfigurationProperties props) { Collection toBeRemoved = new LinkedList<>(); Collection toBeAdded = new LinkedList<>(); for (RouteUnit each : routeContext.getRouteUnits()) { diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java index e0df7dd33d8f3..273f45604b18d 100644 --- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java +++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/ReadwriteSplittingSQLRouterTest.java @@ -93,7 +93,7 @@ void assertDecorateRouteContextToPrimaryDataSource() { RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(staticRule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - sqlRouter.decorateRouteContext(actual, queryContext, database, staticRule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(actual, queryContext, database, staticRule, Collections.emptyList(), new ConfigurationProperties(new Properties())); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(NONE_READWRITE_SPLITTING_DATASOURCE_NAME)); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); @@ -111,7 +111,7 @@ void assertDecorateRouteContextToReplicaDataSource() { ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); RouteContext actual = mockRouteContext(); - sqlRouter.decorateRouteContext(actual, queryContext, database, staticRule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(actual, queryContext, database, staticRule, Collections.emptyList(), new ConfigurationProperties(new Properties())); assertThat(actual.getActualDataSourceNames(), is(new HashSet<>(Arrays.asList(NONE_READWRITE_SPLITTING_DATASOURCE_NAME, READ_DATASOURCE)))); } @@ -125,7 +125,7 @@ void assertDecorateRouteContextToPrimaryDataSourceWithLock() { RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(staticRule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), ruleMetaData, Collections.emptyMap()); - sqlRouter.decorateRouteContext(actual, queryContext, database, staticRule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(actual, queryContext, database, staticRule, Collections.emptyList(), new ConfigurationProperties(new Properties())); Iterator routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(NONE_READWRITE_SPLITTING_DATASOURCE_NAME)); assertThat(routedDataSourceNames.next(), is(WRITE_DATASOURCE)); diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java index a1bde9334d665..adb914fd2a5c1 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java @@ -43,7 +43,7 @@ public final class ShadowSQLRouter implements DecorateSQLRouter, Dat @Override public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, - final ShadowRule rule, final ConfigurationProperties props) { + final ShadowRule rule, final Collection tableNames, final ConfigurationProperties props) { Collection toBeRemovedRouteUnit = new LinkedList<>(); Collection toBeAddedRouteUnit = new LinkedList<>(); Map shadowDataSourceMappings = ShadowDataSourceMappingsRetrieverFactory.newInstance(queryContext).retrieve(rule); diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/cache/route/CachedShardingSQLRouter.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/cache/route/CachedShardingSQLRouter.java index fb4895dc2f1f1..74fc25e42a171 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/cache/route/CachedShardingSQLRouter.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/cache/route/CachedShardingSQLRouter.java @@ -29,6 +29,7 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Optional; @@ -45,11 +46,12 @@ public final class CachedShardingSQLRouter { * @param globalRuleMetaData global rule meta data * @param database database * @param shardingCache sharding cache + * @param tableNames table names * @param props configuration properties * @return route context */ public Optional loadRouteContext(final OriginSQLRouter originSQLRouter, final QueryContext queryContext, final RuleMetaData globalRuleMetaData, - final ShardingSphereDatabase database, final ShardingCache shardingCache, final ConfigurationProperties props) { + final ShardingSphereDatabase database, final ShardingCache shardingCache, final Collection tableNames, final ConfigurationProperties props) { if (queryContext.getSql().length() > shardingCache.getConfiguration().getAllowedMaxSqlLength()) { return Optional.empty(); } @@ -66,7 +68,7 @@ public Optional loadRouteContext(final OriginSQLRouter originSQLRo } Optional cachedResult = shardingCache.getRouteCache().get(new ShardingRouteCacheKey(queryContext.getSql(), shardingConditionParams)) .flatMap(ShardingRouteCacheValue::getCachedRouteContext); - RouteContext result = cachedResult.orElseGet(() -> originSQLRouter.createRouteContext(queryContext, globalRuleMetaData, database, shardingCache.getShardingRule(), props)); + RouteContext result = cachedResult.orElseGet(() -> originSQLRouter.createRouteContext(queryContext, globalRuleMetaData, database, shardingCache.getShardingRule(), tableNames, props)); if (!cachedResult.isPresent() && hitOneShardOnly(result)) { shardingCache.getRouteCache().put(new ShardingRouteCacheKey(queryContext.getSql(), shardingConditionParams), new ShardingRouteCacheValue(result)); } @@ -88,9 +90,11 @@ public interface OriginSQLRouter { * @param globalRuleMetaData global rule meta data * @param database database * @param rule rule + * @param tableNames table names * @param props configuration properties * @return route context */ - RouteContext createRouteContext(QueryContext queryContext, RuleMetaData globalRuleMetaData, ShardingSphereDatabase database, ShardingRule rule, ConfigurationProperties props); + RouteContext createRouteContext(QueryContext queryContext, RuleMetaData globalRuleMetaData, ShardingSphereDatabase database, ShardingRule rule, Collection tableNames, + ConfigurationProperties props); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java index d47ac97445b69..1dac768b2a8ab 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingSQLRouter.java @@ -38,6 +38,7 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -50,19 +51,23 @@ public final class ShardingSQLRouter implements EntranceSQLRouter, @Override public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, - final ShardingRule rule, final ConfigurationProperties props) { + final ShardingRule rule, final Collection tableNames, final ConfigurationProperties props) { if (rule.isShardingCacheEnabled()) { Optional result = new CachedShardingSQLRouter() - .loadRouteContext(this::createRouteContext0, queryContext, globalRuleMetaData, database, rule.getShardingCache(), props); + .loadRouteContext(this::createRouteContext0, queryContext, globalRuleMetaData, database, rule.getShardingCache(), tableNames, props); if (result.isPresent()) { return result.get(); } } - return createRouteContext0(queryContext, globalRuleMetaData, database, rule, props); + return createRouteContext0(queryContext, globalRuleMetaData, database, rule, tableNames, props); } private RouteContext createRouteContext0(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final ShardingRule rule, - final ConfigurationProperties props) { + final Collection tableNames, final ConfigurationProperties props) { + Collection logicTableNames = rule.getShardingLogicTableNames(tableNames); + if (logicTableNames.isEmpty()) { + return new RouteContext(); + } SQLStatement sqlStatement = queryContext.getSqlStatementContext().getSqlStatement(); ShardingConditions shardingConditions = createShardingConditions(queryContext, globalRuleMetaData, database, rule); Optional validator = ShardingStatementValidatorFactory.newInstance(sqlStatement, shardingConditions); @@ -70,7 +75,7 @@ private RouteContext createRouteContext0(final QueryContext queryContext, final if (sqlStatement instanceof DMLStatement && shardingConditions.isNeedMerge()) { shardingConditions.merge(); } - RouteContext result = ShardingRouteEngineFactory.newInstance(rule, database, queryContext, shardingConditions, props).route(rule); + RouteContext result = ShardingRouteEngineFactory.newInstance(rule, database, queryContext, shardingConditions, logicTableNames, props).route(rule); validator.ifPresent(optional -> optional.postValidate(rule, queryContext.getSqlStatementContext(), queryContext.getHintValueContext(), queryContext.getParameters(), database, props, result)); return result; } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java index a9475ea962b5a..42598156f0d42 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java @@ -20,7 +20,6 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; -import org.apache.shardingsphere.infra.binder.context.statement.ddl.CloseStatementContext; import org.apache.shardingsphere.infra.binder.context.type.CursorAvailable; import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; @@ -30,7 +29,6 @@ import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition; import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions; -import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingDataSourceGroupBroadcastRouteEngine; import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingDatabaseBroadcastRouteEngine; import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingInstanceBroadcastRouteEngine; import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingTableBroadcastRouteEngine; @@ -42,31 +40,16 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.AnalyzeTableStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.LoadStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ResetParameterStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.SetStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.OptimizeTableStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dcl.DCLStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterFunctionStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterProcedureStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTablespaceStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateFunctionStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateProcedureStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTablespaceStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropFunctionStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropProcedureStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTablespaceStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.TCLStatement; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLCreateResourceGroupStatement; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLOptimizeTableStatement; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLSetResourceGroupStatement; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLShowDatabasesStatement; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLUseStatement; import java.util.Collection; import java.util.Collections; -import java.util.stream.Collectors; /** * Sharding routing engine factory. @@ -81,58 +64,40 @@ public final class ShardingRouteEngineFactory { * @param database database * @param queryContext query context * @param shardingConditions shardingConditions + * @param logicTableNames logic table names * @param props ShardingSphere properties * @return created instance */ public static ShardingRouteEngine newInstance(final ShardingRule shardingRule, final ShardingSphereDatabase database, final QueryContext queryContext, - final ShardingConditions shardingConditions, final ConfigurationProperties props) { + final ShardingConditions shardingConditions, final Collection logicTableNames, final ConfigurationProperties props) { SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); - if (sqlStatement instanceof TCLStatement) { - return new ShardingDatabaseBroadcastRouteEngine(); - } if (sqlStatement instanceof DDLStatement) { - if (sqlStatementContext instanceof CursorAvailable) { - return getCursorRouteEngine(shardingRule, database, sqlStatementContext, queryContext.getHintValueContext(), shardingConditions, props); - } - return getDDLRouteEngine(shardingRule, database, sqlStatementContext); + return sqlStatementContext instanceof CursorAvailable + ? getCursorRouteEngine(shardingRule, database, sqlStatementContext, queryContext.getHintValueContext(), shardingConditions, logicTableNames, props) + : getDDLRouteEngine(database, sqlStatementContext, logicTableNames); } if (sqlStatement instanceof DALStatement) { - return getDALRouteEngine(shardingRule, database, sqlStatementContext, queryContext.getConnectionContext()); + return getDALRouteEngine(database, sqlStatementContext, queryContext.getConnectionContext(), logicTableNames); } if (sqlStatement instanceof DCLStatement) { - return getDCLRouteEngine(shardingRule, database, sqlStatementContext); + return getDCLRouteEngine(database, sqlStatementContext, logicTableNames); } - return getDQLRouteEngine(shardingRule, database, sqlStatementContext, queryContext.getHintValueContext(), shardingConditions, props, queryContext.getConnectionContext()); + return getDQLRouteEngine(shardingRule, database, sqlStatementContext, queryContext.getHintValueContext(), shardingConditions, props, queryContext.getConnectionContext(), logicTableNames); } - private static ShardingRouteEngine getDDLRouteEngine(final ShardingRule shardingRule, final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext) { + private static ShardingRouteEngine getDDLRouteEngine(final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, final Collection logicTableNames) { SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); - boolean functionStatement = sqlStatement instanceof CreateFunctionStatement || sqlStatement instanceof AlterFunctionStatement || sqlStatement instanceof DropFunctionStatement; boolean procedureStatement = sqlStatement instanceof CreateProcedureStatement || sqlStatement instanceof AlterProcedureStatement || sqlStatement instanceof DropProcedureStatement; - if (functionStatement || procedureStatement) { + if (procedureStatement) { return new ShardingDatabaseBroadcastRouteEngine(); } - if (sqlStatement instanceof CreateTablespaceStatement || sqlStatement instanceof AlterTablespaceStatement || sqlStatement instanceof DropTablespaceStatement) { - return new ShardingInstanceBroadcastRouteEngine(database.getResourceMetaData()); - } - Collection tableNames = sqlStatementContext instanceof TableAvailable - ? ((TableAvailable) sqlStatementContext).getTablesContext().getSimpleTables().stream().map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toSet()) - : Collections.emptyList(); - Collection shardingRuleTableNames = shardingRule.getShardingRuleTableNames(tableNames); - if (!tableNames.isEmpty() && shardingRuleTableNames.isEmpty()) { - return new ShardingIgnoreRouteEngine(); - } - return new ShardingTableBroadcastRouteEngine(database, sqlStatementContext, shardingRuleTableNames); + return new ShardingTableBroadcastRouteEngine(database, sqlStatementContext, logicTableNames); } private static ShardingRouteEngine getCursorRouteEngine(final ShardingRule shardingRule, final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, - final HintValueContext hintValueContext, final ShardingConditions shardingConditions, final ConfigurationProperties props) { - if (sqlStatementContext instanceof CloseStatementContext && ((CloseStatementContext) sqlStatementContext).getSqlStatement().isCloseAll()) { - return new ShardingDatabaseBroadcastRouteEngine(); - } - Collection tableNames = sqlStatementContext instanceof TableAvailable ? ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames() : Collections.emptyList(); - Collection logicTableNames = shardingRule.getShardingLogicTableNames(tableNames); + final HintValueContext hintValueContext, final ShardingConditions shardingConditions, final Collection logicTableNames, + final ConfigurationProperties props) { boolean allBindingTables = logicTableNames.size() > 1 && shardingRule.isAllBindingTables(database, sqlStatementContext, logicTableNames); if (isShardingStandardQuery(shardingRule, logicTableNames, allBindingTables)) { return new ShardingStandardRouteEngine(getLogicTableName(shardingConditions, logicTableNames), shardingConditions, sqlStatementContext, hintValueContext, props); @@ -140,48 +105,18 @@ private static ShardingRouteEngine getCursorRouteEngine(final ShardingRule shard return new ShardingIgnoreRouteEngine(); } - private static ShardingRouteEngine getDALRouteEngine(final ShardingRule shardingRule, final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, - final ConnectionContext connectionContext) { + private static ShardingRouteEngine getDALRouteEngine(final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, + final ConnectionContext connectionContext, final Collection logicTableNames) { SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); - if (sqlStatement instanceof MySQLUseStatement) { - return new ShardingIgnoreRouteEngine(); - } - if (sqlStatement instanceof SetStatement || sqlStatement instanceof ResetParameterStatement || sqlStatement instanceof MySQLShowDatabasesStatement || sqlStatement instanceof LoadStatement) { - return new ShardingDatabaseBroadcastRouteEngine(); - } - if (isResourceGroupStatement(sqlStatement)) { - return new ShardingInstanceBroadcastRouteEngine(database.getResourceMetaData()); - } - Collection tableNames = sqlStatementContext instanceof TableAvailable ? ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames() : Collections.emptyList(); - Collection shardingRuleTableNames = shardingRule.getShardingRuleTableNames(tableNames); - if (!tableNames.isEmpty() && shardingRuleTableNames.isEmpty()) { - return new ShardingIgnoreRouteEngine(); - } - if (sqlStatement instanceof MySQLOptimizeTableStatement) { - return new ShardingTableBroadcastRouteEngine(database, sqlStatementContext, shardingRuleTableNames); - } - if (sqlStatement instanceof AnalyzeTableStatement) { - return shardingRuleTableNames.isEmpty() ? new ShardingDatabaseBroadcastRouteEngine() - : new ShardingTableBroadcastRouteEngine(database, sqlStatementContext, shardingRuleTableNames); + if (sqlStatement instanceof OptimizeTableStatement || sqlStatement instanceof AnalyzeTableStatement) { + return new ShardingTableBroadcastRouteEngine(database, sqlStatementContext, logicTableNames); } - if (!shardingRuleTableNames.isEmpty()) { - return new ShardingUnicastRouteEngine(sqlStatementContext, shardingRuleTableNames, connectionContext); - } - return new ShardingDataSourceGroupBroadcastRouteEngine(); + return new ShardingUnicastRouteEngine(sqlStatementContext, logicTableNames, connectionContext); } - private static boolean isResourceGroupStatement(final SQLStatement sqlStatement) { - // TODO add dropResourceGroupStatement, alterResourceGroupStatement - return sqlStatement instanceof MySQLCreateResourceGroupStatement || sqlStatement instanceof MySQLSetResourceGroupStatement; - } - - private static ShardingRouteEngine getDCLRouteEngine(final ShardingRule shardingRule, final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext) { - if (isDCLForSingleTable(sqlStatementContext)) { - Collection tableNames = sqlStatementContext instanceof TableAvailable ? ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames() : Collections.emptyList(); - Collection shardingRuleTableNames = shardingRule.getShardingRuleTableNames(tableNames); - return shardingRuleTableNames.isEmpty() ? new ShardingIgnoreRouteEngine() : new ShardingTableBroadcastRouteEngine(database, sqlStatementContext, shardingRuleTableNames); - } - return new ShardingInstanceBroadcastRouteEngine(database.getResourceMetaData()); + private static ShardingRouteEngine getDCLRouteEngine(final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, final Collection logicTableNames) { + return isDCLForSingleTable(sqlStatementContext) ? new ShardingTableBroadcastRouteEngine(database, sqlStatementContext, logicTableNames) + : new ShardingInstanceBroadcastRouteEngine(database.getResourceMetaData()); } private static boolean isDCLForSingleTable(final SQLStatementContext sqlStatementContext) { @@ -195,27 +130,23 @@ private static boolean isDCLForSingleTable(final SQLStatementContext sqlStatemen private static ShardingRouteEngine getDQLRouteEngine(final ShardingRule shardingRule, final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final ShardingConditions shardingConditions, final ConfigurationProperties props, - final ConnectionContext connectionContext) { + final ConnectionContext connectionContext, final Collection logicTableNames) { Collection tableNames = sqlStatementContext instanceof TableAvailable ? ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames() : Collections.emptyList(); if (sqlStatementContext.getSqlStatement() instanceof DMLStatement && shardingConditions.isAlwaysFalse() || tableNames.isEmpty()) { return new ShardingUnicastRouteEngine(sqlStatementContext, tableNames, connectionContext); } - Collection shardingLogicTableNames = shardingRule.getShardingLogicTableNames(tableNames); - if (shardingLogicTableNames.isEmpty()) { - return new ShardingIgnoreRouteEngine(); - } - return getDQLRouteEngineForShardingTable(shardingRule, database, sqlStatementContext, hintValueContext, shardingConditions, props, shardingLogicTableNames); + return getDQLRouteEngineForShardingTable(shardingRule, database, sqlStatementContext, hintValueContext, shardingConditions, props, logicTableNames); } private static ShardingRouteEngine getDQLRouteEngineForShardingTable(final ShardingRule shardingRule, final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, - final ShardingConditions shardingConditions, final ConfigurationProperties props, final Collection tableNames) { - boolean allBindingTables = tableNames.size() > 1 && shardingRule.isAllBindingTables(database, sqlStatementContext, tableNames); - if (isShardingStandardQuery(shardingRule, tableNames, allBindingTables)) { - return new ShardingStandardRouteEngine(getLogicTableName(shardingConditions, tableNames), shardingConditions, sqlStatementContext, hintValueContext, props); + final ShardingConditions shardingConditions, final ConfigurationProperties props, final Collection logicTableNames) { + boolean allBindingTables = logicTableNames.size() > 1 && shardingRule.isAllBindingTables(database, sqlStatementContext, logicTableNames); + if (isShardingStandardQuery(shardingRule, logicTableNames, allBindingTables)) { + return new ShardingStandardRouteEngine(getLogicTableName(shardingConditions, logicTableNames), shardingConditions, sqlStatementContext, hintValueContext, props); } // TODO config for cartesian set - return new ShardingComplexRouteEngine(shardingConditions, sqlStatementContext, hintValueContext, props, tableNames); + return new ShardingComplexRouteEngine(shardingConditions, sqlStatementContext, hintValueContext, props, logicTableNames); } private static String getLogicTableName(final ShardingConditions shardingConditions, final Collection tableNames) { @@ -226,7 +157,7 @@ private static String getLogicTableName(final ShardingConditions shardingConditi return shardingCondition.getValues().isEmpty() ? tableNames.iterator().next() : shardingCondition.getValues().iterator().next().getTableName(); } - private static boolean isShardingStandardQuery(final ShardingRule shardingRule, final Collection tableNames, final boolean allBindingTables) { - return 1 == tableNames.size() && shardingRule.isAllShardingTables(tableNames) || allBindingTables; + private static boolean isShardingStandardQuery(final ShardingRule shardingRule, final Collection logicTableNames, final boolean allBindingTables) { + return 1 == logicTableNames.size() && shardingRule.isAllShardingTables(logicTableNames) || allBindingTables; } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java index 037f8c349820c..c8fa96184811d 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java @@ -56,12 +56,12 @@ import org.apache.shardingsphere.sharding.rule.checker.ShardingRuleChecker; import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm; import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm; +import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment; -import org.apache.shardingsphere.sql.parser.statement.core.extractor.ExpressionExtractor; import javax.sql.DataSource; import java.util.Arrays; @@ -543,16 +543,6 @@ public Collection getShardingLogicTableNames(final Collection lo return result; } - /** - * Get sharding rule table names. - * - * @param logicTableNames logic table names - * @return sharding rule table names - */ - public Collection getShardingRuleTableNames(final Collection logicTableNames) { - return logicTableNames.stream().filter(this::isShardingTable).collect(Collectors.toList()); - } - /** * Get logic and actual binding tables. * diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/route/CachedShardingSQLRouterTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/route/CachedShardingSQLRouterTest.java index caa90e396e9dc..d0a0eac8e9502 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/route/CachedShardingSQLRouterTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/route/CachedShardingSQLRouterTest.java @@ -42,6 +42,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Optional; @@ -70,7 +71,7 @@ void assertCreateRouteContextWithSQLExceedMaxAllowedLength() { when(shardingCache.getConfiguration()).thenReturn(new ShardingCacheConfiguration(1, null)); QueryContext queryContext = new QueryContext(sqlStatementContext, "select 1", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - Optional actual = new CachedShardingSQLRouter().loadRouteContext(null, queryContext, mock(RuleMetaData.class), null, shardingCache, null); + Optional actual = new CachedShardingSQLRouter().loadRouteContext(null, queryContext, mock(RuleMetaData.class), null, shardingCache, Collections.emptyList(), null); assertFalse(actual.isPresent()); } @@ -87,7 +88,7 @@ void assertCreateRouteContextWithNotCacheableQuery() { when(shardingCache.getConfiguration()).thenReturn(new ShardingCacheConfiguration(100, null)); when(shardingCache.getRouteCacheableChecker()).thenReturn(mock(ShardingRouteCacheableChecker.class)); when(shardingCache.getRouteCacheableChecker().check(null, queryContext)).thenReturn(new ShardingRouteCacheableCheckResult(false, Collections.emptyList())); - Optional actual = new CachedShardingSQLRouter().loadRouteContext(null, queryContext, mock(RuleMetaData.class), null, shardingCache, null); + Optional actual = new CachedShardingSQLRouter().loadRouteContext(null, queryContext, mock(RuleMetaData.class), null, shardingCache, Collections.singletonList("t"), null); assertFalse(actual.isPresent()); } @@ -98,7 +99,7 @@ void assertCreateRouteContextWithUnmatchedActualParameterSize() { when(shardingCache.getConfiguration()).thenReturn(new ShardingCacheConfiguration(100, null)); when(shardingCache.getRouteCacheableChecker()).thenReturn(mock(ShardingRouteCacheableChecker.class)); when(shardingCache.getRouteCacheableChecker().check(null, queryContext)).thenReturn(new ShardingRouteCacheableCheckResult(true, Collections.singletonList(1))); - Optional actual = new CachedShardingSQLRouter().loadRouteContext(null, queryContext, mock(RuleMetaData.class), null, shardingCache, null); + Optional actual = new CachedShardingSQLRouter().loadRouteContext(null, queryContext, mock(RuleMetaData.class), null, shardingCache, Collections.singletonList("t"), null); assertFalse(actual.isPresent()); } @@ -114,8 +115,9 @@ void assertCreateRouteContextWithCacheableQueryButCacheMissed() { expected.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t", "t")))); expected.getOriginalDataNodes().add(Collections.singletonList(new DataNode("ds_0", "t"))); when(shardingCache.getRouteCache().get(any(ShardingRouteCacheKey.class))).thenReturn(Optional.empty()); - OriginSQLRouter router = (unused, globalRuleMetaData, database, rule, props) -> expected; - Optional actual = new CachedShardingSQLRouter().loadRouteContext(router, queryContext, mock(RuleMetaData.class), null, shardingCache, null); + OriginSQLRouter router = (unused, globalRuleMetaData, database, rule, tableNames, props) -> expected; + Collection tableNames = Collections.singletonList("t"); + Optional actual = new CachedShardingSQLRouter().loadRouteContext(router, queryContext, mock(RuleMetaData.class), null, shardingCache, tableNames, null); assertTrue(actual.isPresent()); assertThat(actual.get(), is(expected)); verify(shardingCache.getRouteCache()).put(any(ShardingRouteCacheKey.class), any(ShardingRouteCacheValue.class)); @@ -133,7 +135,7 @@ void assertCreateRouteContextWithCacheHit() { expected.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t", "t")))); expected.getOriginalDataNodes().add(Collections.singletonList(new DataNode("ds_0", "t"))); when(shardingCache.getRouteCache().get(any(ShardingRouteCacheKey.class))).thenReturn(Optional.of(new ShardingRouteCacheValue(expected))); - Optional actual = new CachedShardingSQLRouter().loadRouteContext(null, queryContext, mock(RuleMetaData.class), null, shardingCache, null); + Optional actual = new CachedShardingSQLRouter().loadRouteContext(null, queryContext, mock(RuleMetaData.class), null, shardingCache, Collections.singletonList("t"), null); assertTrue(actual.isPresent()); RouteContext actualRouteContext = actual.get(); assertThat(actualRouteContext, not(expected)); @@ -152,9 +154,10 @@ void assertCreateRouteContextWithQueryRoutedToMultiDataNodes() { RouteContext expected = new RouteContext(); expected.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Arrays.asList(new RouteMapper("t", "t_0"), new RouteMapper("t", "t_1")))); expected.getOriginalDataNodes().add(Collections.singletonList(new DataNode("ds_0", "t_0"))); - OriginSQLRouter router = (unused, globalRuleMetaData, database, rule, props) -> expected; + OriginSQLRouter router = (unused, globalRuleMetaData, database, rule, tableNames, props) -> expected; RuleMetaData globalRuleMetaData = mock(RuleMetaData.class); - Optional actual = new CachedShardingSQLRouter().loadRouteContext(router, queryContext, globalRuleMetaData, null, shardingCache, null); + Collection tableNames = Collections.singletonList("t"); + Optional actual = new CachedShardingSQLRouter().loadRouteContext(router, queryContext, globalRuleMetaData, null, shardingCache, tableNames, null); assertTrue(actual.isPresent()); assertThat(actual.get(), is(expected)); verify(shardingCache.getRouteCache(), never()).put(any(ShardingRouteCacheKey.class), any(ShardingRouteCacheValue.class)); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java index 3d201348a0537..a64cab217c54f 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java @@ -20,7 +20,6 @@ import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dcl.GrantStatementContext; -import org.apache.shardingsphere.infra.binder.context.statement.ddl.CloseStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.CursorStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; @@ -33,8 +32,6 @@ import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions; -import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingDataSourceGroupBroadcastRouteEngine; -import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingDatabaseBroadcastRouteEngine; import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingInstanceBroadcastRouteEngine; import org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingTableBroadcastRouteEngine; import org.apache.shardingsphere.sharding.route.engine.type.complex.ShardingComplexRouteEngine; @@ -49,21 +46,14 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.dcl.DCLStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dcl.GrantStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement; -import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.TCLStatement; import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLCreateResourceGroupStatement; import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLOptimizeTableStatement; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLSetResourceGroupStatement; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLSetStatement; import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLShowColumnsStatement; import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLShowCreateTableStatement; -import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLShowDatabasesStatement; import org.apache.shardingsphere.sql.parser.statement.mysql.dcl.MySQLGrantStatement; import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLSelectStatement; -import org.apache.shardingsphere.sql.parser.statement.opengauss.ddl.OpenGaussCloseStatement; import org.apache.shardingsphere.sql.parser.statement.opengauss.ddl.OpenGaussCursorStatement; import org.apache.shardingsphere.sql.parser.statement.oracle.dcl.OracleGrantStatement; -import org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLSetStatement; import org.apache.shardingsphere.sql.parser.statement.postgresql.dcl.PostgreSQLGrantStatement; import org.apache.shardingsphere.sql.parser.statement.sql92.dcl.SQL92GrantStatement; import org.apache.shardingsphere.sql.parser.statement.sqlserver.dcl.SQLServerGrantStatement; @@ -121,16 +111,6 @@ void setUp() { when(tablesContext.getTableNames()).thenReturn(tableNames); } - @Test - void assertNewInstanceForTCL() { - TCLStatement tclStatement = mock(TCLStatement.class); - when(sqlStatementContext.getSqlStatement()).thenReturn(tclStatement); - QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingDatabaseBroadcastRouteEngine.class)); - } - private ConnectionContext mockConnectionContext() { ConnectionContext result = mock(ConnectionContext.class); when(result.getCurrentDatabaseName()).thenReturn(Optional.of(DefaultDatabase.LOGIC_NAME)); @@ -141,106 +121,54 @@ private ConnectionContext mockConnectionContext() { void assertNewInstanceForDDLWithShardingRule() { when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DDLStatement.class)); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, Collections.emptyList(), props); assertThat(actual, instanceOf(ShardingTableBroadcastRouteEngine.class)); } @Test void assertNewInstanceForDALWithTables() { tableNames.add("tbl"); - when(shardingRule.getShardingRuleTableNames(tableNames)).thenReturn(tableNames); + when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); DALStatement dalStatement = mock(DALStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, tableNames, props); assertThat(actual, instanceOf(ShardingUnicastRouteEngine.class)); } - @Test - void assertNewInstanceForDALWithoutTables() { - DALStatement dalStatement = mock(DALStatement.class); - when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); - QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingDataSourceGroupBroadcastRouteEngine.class)); - } - - @Test - void assertNewInstanceForDALShow() { - DALStatement dalStatement = mock(MySQLShowDatabasesStatement.class); - when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); - QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingDatabaseBroadcastRouteEngine.class)); - } - - @Test - void assertNewInstanceForDALSetForMySQL() { - assertNewInstanceForDALSet(mock(MySQLSetStatement.class)); - } - - @Test - void assertNewInstanceForDALSetForPostgreSQL() { - assertNewInstanceForDALSet(mock(PostgreSQLSetStatement.class)); - } - - private void assertNewInstanceForDALSet(final DALStatement dalStatement) { - when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); - QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingDatabaseBroadcastRouteEngine.class)); - } - @Test void assertNewInstanceForDCLForSingleTableForMySQL() { - assertNewInstanceForDCLForSingleTableWithShardingRule(new MySQLGrantStatement()); - assertNewInstanceForDCLForSingleTableWithoutShardingRule(new MySQLGrantStatement()); + assertNewInstanceForDCLForSingleTable(new MySQLGrantStatement()); } @Test void assertNewInstanceForDCLForSingleTableForOracle() { - assertNewInstanceForDCLForSingleTableWithShardingRule(new OracleGrantStatement()); - assertNewInstanceForDCLForSingleTableWithoutShardingRule(new OracleGrantStatement()); + assertNewInstanceForDCLForSingleTable(new OracleGrantStatement()); } @Test void assertNewInstanceForDCLForSingleTableForPostgreSQL() { - assertNewInstanceForDCLForSingleTableWithShardingRule(new PostgreSQLGrantStatement()); - assertNewInstanceForDCLForSingleTableWithoutShardingRule(new PostgreSQLGrantStatement()); + assertNewInstanceForDCLForSingleTable(new PostgreSQLGrantStatement()); } @Test void assertNewInstanceForDCLForSingleTableForSQLServer() { - assertNewInstanceForDCLForSingleTableWithShardingRule(new SQLServerGrantStatement()); - assertNewInstanceForDCLForSingleTableWithoutShardingRule(new SQLServerGrantStatement()); + assertNewInstanceForDCLForSingleTable(new SQLServerGrantStatement()); } @Test void assertNewInstanceForDCLForSingleTableForSQL92() { - assertNewInstanceForDCLForSingleTableWithShardingRule(new SQL92GrantStatement()); - assertNewInstanceForDCLForSingleTableWithoutShardingRule(new SQL92GrantStatement()); - } - - private void assertNewInstanceForDCLForSingleTableWithShardingRule(final GrantStatement grantStatement) { - grantStatement.getTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("tbl")))); - GrantStatementContext sqlStatementContext = new GrantStatementContext(grantStatement, DefaultDatabase.LOGIC_NAME); - QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingIgnoreRouteEngine.class)); + assertNewInstanceForDCLForSingleTable(new SQL92GrantStatement()); } - private void assertNewInstanceForDCLForSingleTableWithoutShardingRule(final GrantStatement grantStatement) { + private void assertNewInstanceForDCLForSingleTable(final GrantStatement grantStatement) { grantStatement.getTables().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("tbl")))); GrantStatementContext sqlStatementContext = new GrantStatementContext(grantStatement, DefaultDatabase.LOGIC_NAME); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingIgnoreRouteEngine.class)); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, Collections.singletonList("tbl"), props); + assertThat(actual, instanceOf(ShardingTableBroadcastRouteEngine.class)); } @Test @@ -249,7 +177,7 @@ void assertNewInstanceForDCLForNoSingleTable() { when(sqlStatementContext.getSqlStatement()).thenReturn(dclStatement); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, Collections.emptyList(), props); assertThat(actual, instanceOf(ShardingInstanceBroadcastRouteEngine.class)); } @@ -259,7 +187,7 @@ void assertNewInstanceForSelectWithoutSingleTable() { when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, Collections.emptyList(), props); assertThat(actual, instanceOf(ShardingUnicastRouteEngine.class)); } @@ -269,7 +197,7 @@ void assertNewInstanceForSelectBroadcastTable() { when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, Collections.emptyList(), props); assertThat(actual, instanceOf(ShardingUnicastRouteEngine.class)); } @@ -279,7 +207,7 @@ void assertNewInstanceForAlwaysFalse() { when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, Collections.emptyList(), props); assertThat(actual, instanceOf(ShardingUnicastRouteEngine.class)); } @@ -292,7 +220,7 @@ void assertNewInstanceForStandard() { when(shardingRule.isAllShardingTables(tableNames)).thenReturn(true); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, tableNames, props); assertThat(actual, instanceOf(ShardingStandardRouteEngine.class)); } @@ -305,7 +233,7 @@ void assertNewInstanceForComplex() { when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, tableNames, props); assertThat(actual, instanceOf(ShardingComplexRouteEngine.class)); } @@ -314,10 +242,10 @@ void assertNewInstanceForShowCreateTableWithTableRule() { DALStatement dalStatement = mock(MySQLShowCreateTableStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); tableNames.add("table_1"); - when(shardingRule.getShardingRuleTableNames(tableNames)).thenReturn(tableNames); + when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, tableNames, props); assertThat(actual, instanceOf(ShardingUnicastRouteEngine.class)); } @@ -326,10 +254,10 @@ void assertNewInstanceForShowColumnsWithTableRule() { DALStatement dalStatement = mock(MySQLShowColumnsStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); tableNames.add("table_1"); - when(shardingRule.getShardingRuleTableNames(tableNames)).thenReturn(tableNames); + when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, tableNames, props); assertThat(actual, instanceOf(ShardingUnicastRouteEngine.class)); } @@ -344,54 +272,22 @@ void assertNewInstanceForSubqueryWithSameConditions() { when(shardingRule.getShardingTable("t_order").getActualDataSourceNames()).thenReturn(Arrays.asList("ds_0", "ds_1")); when(shardingRule.isAllShardingTables(Collections.singletonList("t_order"))).thenReturn(true); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, mock(ConfigurationProperties.class)); + ShardingRouteEngine actual = ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, tableNames, mock(ConfigurationProperties.class)); assertThat(actual, instanceOf(ShardingStandardRouteEngine.class)); } - @Test - void assertNewInstanceForCreateResourceGroup() { - MySQLCreateResourceGroupStatement resourceGroupStatement = mock(MySQLCreateResourceGroupStatement.class); - when(sqlStatementContext.getSqlStatement()).thenReturn(resourceGroupStatement); - QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingInstanceBroadcastRouteEngine.class)); - } - - @Test - void assertNewInstanceForSetResourceGroup() { - MySQLSetResourceGroupStatement resourceGroupStatement = mock(MySQLSetResourceGroupStatement.class); - when(sqlStatementContext.getSqlStatement()).thenReturn(resourceGroupStatement); - QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingInstanceBroadcastRouteEngine.class)); - } - @Test void assertNewInstanceForOptimizeTableWithShardingTable() { MySQLOptimizeTableStatement optimizeTableStatement = mock(MySQLOptimizeTableStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(optimizeTableStatement); tableNames.add("table_1"); - when(shardingRule.getShardingRuleTableNames(tableNames)).thenReturn(tableNames); + when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, tableNames, props); assertThat(actual, instanceOf(ShardingTableBroadcastRouteEngine.class)); } - @Test - void assertNewInstanceForOptimizeTableWithSingleTable() { - MySQLOptimizeTableStatement optimizeTableStatement = mock(MySQLOptimizeTableStatement.class); - when(sqlStatementContext.getSqlStatement()).thenReturn(optimizeTableStatement); - tableNames.add("table_1"); - when(shardingRule.getShardingRuleTableNames(tableNames)).thenReturn(Collections.emptyList()); - QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingIgnoreRouteEngine.class)); - } - @Test void assertNewInstanceForCursorStatementWithShardingTable() { CursorStatementContext cursorStatementContext = mock(CursorStatementContext.class, RETURNS_DEEP_STUBS); @@ -403,11 +299,10 @@ void assertNewInstanceForCursorStatementWithShardingTable() { when(cursorStatementContext.getTablesContext().getTableNames()).thenReturn(tableNames); when(cursorStatementContext.getTablesContext().getDatabaseName()).thenReturn(Optional.empty()); when(shardingRule.isAllShardingTables(tableNames)).thenReturn(true); - when(shardingRule.getShardingRuleTableNames(tableNames)).thenReturn(tableNames); when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); QueryContext queryContext = new QueryContext(cursorStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, tableNames, props); assertThat(actual, instanceOf(ShardingStandardRouteEngine.class)); } @@ -421,26 +316,10 @@ void assertNewInstanceForCursorStatementWithSingleTable() { when(cursorStatementContext.getTablesContext().getDatabaseName()).thenReturn(Optional.empty()); QueryContext queryContext = new QueryContext(cursorStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); + ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, Collections.emptyList(), props); assertThat(actual, instanceOf(ShardingIgnoreRouteEngine.class)); } - @Test - void assertNewInstanceForCloseAllStatement() { - CloseStatementContext closeStatementContext = mock(CloseStatementContext.class, RETURNS_DEEP_STUBS); - OpenGaussCloseStatement closeStatement = mock(OpenGaussCloseStatement.class); - when(closeStatement.isCloseAll()).thenReturn(true); - tableNames.add("t_order"); - when(closeStatementContext.getTablesContext().getTableNames()).thenReturn(tableNames); - when(closeStatementContext.getTablesContext().getDatabaseName()).thenReturn(Optional.empty()); - when(closeStatementContext.getSqlStatement()).thenReturn(closeStatement); - when(shardingRule.getShardingRuleTableNames(tableNames)).thenReturn(tableNames); - QueryContext queryContext = new QueryContext(closeStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); - ShardingRouteEngine actual = - ShardingRouteEngineFactory.newInstance(shardingRule, database, queryContext, shardingConditions, props); - assertThat(actual, instanceOf(ShardingDatabaseBroadcastRouteEngine.class)); - } - private Collection createSimpleTableSegments() { return Collections.singletonList(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java index b8f581fd98620..0da3538ef090e 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java @@ -790,7 +790,7 @@ void assertIsGenerateKeyColumn() { @Test void assertGetShardingRuleTableNames() { ShardingRule actual = createMaximumShardingRule(); - Collection shardingRuleTableNames = actual.getShardingRuleTableNames(Collections.singleton("Logic_Table")); + Collection shardingRuleTableNames = actual.getShardingLogicTableNames(Collections.singleton("Logic_Table")); assertTrue(shardingRuleTableNames.contains("Logic_Table")); } diff --git a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java index fa951c44162c2..90facba37f6bf 100644 --- a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java +++ b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.route.engine; import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; +import org.apache.shardingsphere.infra.binder.context.extractor.SQLStatementContextExtractor; 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; @@ -93,9 +94,10 @@ public RouteContext route(final QueryContext queryContext, final RuleMetaData gl result.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName.get(), dataSourceName.get()), Collections.emptyList())); return result; } - result = route(queryContext, globalRuleMetaData, database, tableRouters, result); + Collection tableNames = SQLStatementContextExtractor.getTableNames(database, queryContext.getSqlStatementContext()); + result = route(queryContext, globalRuleMetaData, database, tableRouters, tableNames, result); result = new TablelessSQLRouter().route(queryContext, globalRuleMetaData, database, result); - result = route(queryContext, globalRuleMetaData, database, dataSourceRouters, result); + result = route(queryContext, globalRuleMetaData, database, dataSourceRouters, tableNames, 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())); @@ -104,14 +106,14 @@ public RouteContext route(final QueryContext queryContext, final RuleMetaData gl } @SuppressWarnings({"unchecked", "rawtypes"}) - private RouteContext route(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final Map routers, - final RouteContext routeContext) { + private RouteContext route(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, + final Map routers, final Collection tableNames, final RouteContext routeContext) { RouteContext result = routeContext; for (Entry entry : routers.entrySet()) { if (result.getRouteUnits().isEmpty() && entry.getValue() instanceof EntranceSQLRouter) { - result = ((EntranceSQLRouter) entry.getValue()).createRouteContext(queryContext, globalRuleMetaData, database, entry.getKey(), props); + result = ((EntranceSQLRouter) entry.getValue()).createRouteContext(queryContext, globalRuleMetaData, database, entry.getKey(), tableNames, props); } else if (entry.getValue() instanceof DecorateSQLRouter) { - ((DecorateSQLRouter) entry.getValue()).decorateRouteContext(result, queryContext, database, entry.getKey(), props); + ((DecorateSQLRouter) entry.getValue()).decorateRouteContext(result, queryContext, database, entry.getKey(), tableNames, props); } } return result; diff --git a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/tableless/TablelessRouteEngineFactory.java b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/tableless/TablelessRouteEngineFactory.java index 498788e644211..ddac7f3d707a1 100644 --- a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/tableless/TablelessRouteEngineFactory.java +++ b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/tableless/TablelessRouteEngineFactory.java @@ -20,12 +20,32 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; +import org.apache.shardingsphere.infra.binder.context.statement.ddl.CloseStatementContext; +import org.apache.shardingsphere.infra.binder.context.type.CursorAvailable; import org.apache.shardingsphere.infra.route.engine.tableless.type.broadcast.DataSourceBroadcastRouteEngine; +import org.apache.shardingsphere.infra.route.engine.tableless.type.broadcast.InstanceBroadcastRouteEngine; 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.AlterResourceGroupStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.CreateResourceGroupStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DropResourceGroupStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.LoadStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ResetParameterStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.SetResourceGroupStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.SetStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowDatabasesStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowTableStatusStatement; import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.ShowTablesStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterFunctionStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.AlterTablespaceStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateFunctionStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CreateTablespaceStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropFunctionStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DropTablespaceStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.TCLStatement; /** * Tableless route engine factory. @@ -40,11 +60,58 @@ public final class TablelessRouteEngineFactory { * @return created instance */ public static TablelessRouteEngine newInstance(final QueryContext queryContext) { - SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); + SQLStatement sqlStatement = queryContext.getSqlStatementContext().getSqlStatement(); + if (sqlStatement instanceof DALStatement) { + return getDALRouteEngine(sqlStatement); + } + // TODO remove this logic when savepoint handle in proxy and jdbc adapter + if (sqlStatement instanceof TCLStatement) { + return new DataSourceBroadcastRouteEngine(); + } + if (sqlStatement instanceof DDLStatement) { + return getDDLRouteEngine(queryContext.getSqlStatementContext()); + } + return new IgnoreRouteEngine(); + } + + private static TablelessRouteEngine getDALRouteEngine(final SQLStatement sqlStatement) { + if (sqlStatement instanceof ShowTablesStatement || sqlStatement instanceof ShowTableStatusStatement || sqlStatement instanceof SetStatement) { + return new DataSourceBroadcastRouteEngine(); + } + if (sqlStatement instanceof ResetParameterStatement || sqlStatement instanceof ShowDatabasesStatement || sqlStatement instanceof LoadStatement) { + return new DataSourceBroadcastRouteEngine(); + } + if (isResourceGroupStatement(sqlStatement)) { + return new InstanceBroadcastRouteEngine(); + } + return new IgnoreRouteEngine(); + } + + private static boolean isResourceGroupStatement(final SQLStatement sqlStatement) { + return sqlStatement instanceof CreateResourceGroupStatement || sqlStatement instanceof AlterResourceGroupStatement || sqlStatement instanceof DropResourceGroupStatement + || sqlStatement instanceof SetResourceGroupStatement; + } + + private static TablelessRouteEngine getDDLRouteEngine(final SQLStatementContext sqlStatementContext) { + if (sqlStatementContext instanceof CursorAvailable) { + return getCursorRouteEngine(sqlStatementContext); + } SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); - if (sqlStatement instanceof ShowTablesStatement || sqlStatement instanceof ShowTableStatusStatement) { + boolean functionStatement = sqlStatement instanceof CreateFunctionStatement || sqlStatement instanceof AlterFunctionStatement || sqlStatement instanceof DropFunctionStatement; + if (functionStatement) { + return new DataSourceBroadcastRouteEngine(); + } + return new IgnoreRouteEngine(); + } + + private static TablelessRouteEngine getCursorRouteEngine(final SQLStatementContext sqlStatementContext) { + if (sqlStatementContext instanceof CloseStatementContext && ((CloseStatementContext) sqlStatementContext).getSqlStatement().isCloseAll()) { return new DataSourceBroadcastRouteEngine(); } + SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); + if (sqlStatement instanceof CreateTablespaceStatement || sqlStatement instanceof AlterTablespaceStatement || sqlStatement instanceof DropTablespaceStatement) { + return new InstanceBroadcastRouteEngine(); + } return new IgnoreRouteEngine(); } } diff --git a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/tableless/type/broadcast/InstanceBroadcastRouteEngine.java b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/tableless/type/broadcast/InstanceBroadcastRouteEngine.java new file mode 100644 index 0000000000000..c785381317eec --- /dev/null +++ b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/tableless/type/broadcast/InstanceBroadcastRouteEngine.java @@ -0,0 +1,44 @@ +/* + * 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.type.broadcast; + +import lombok.RequiredArgsConstructor; +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.tableless.TablelessRouteEngine; + +import java.util.Collections; + +/** + * Instance broadcast route engine. + */ +@RequiredArgsConstructor +public final class InstanceBroadcastRouteEngine implements TablelessRouteEngine { + + @Override + public RouteContext route(final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database) { + RouteContext result = new RouteContext(); + for (String each : database.getResourceMetaData().getAllInstanceDataSourceNames()) { + result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); + } + return result; + } +} diff --git a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/type/DecorateSQLRouter.java b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/type/DecorateSQLRouter.java index 25048efa3e480..7bdaaf402a2c4 100644 --- a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/type/DecorateSQLRouter.java +++ b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/type/DecorateSQLRouter.java @@ -24,6 +24,8 @@ import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.session.query.QueryContext; +import java.util.Collection; + /** * Decorate SQL Router. * @@ -38,7 +40,8 @@ public interface DecorateSQLRouter extends SQLRout * @param queryContext query context * @param database database * @param rule rule + * @param tableNames table names * @param props configuration properties */ - void decorateRouteContext(RouteContext routeContext, QueryContext queryContext, ShardingSphereDatabase database, T rule, ConfigurationProperties props); + void decorateRouteContext(RouteContext routeContext, QueryContext queryContext, ShardingSphereDatabase database, T rule, Collection tableNames, ConfigurationProperties props); } diff --git a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/type/EntranceSQLRouter.java b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/type/EntranceSQLRouter.java index 1f3daca6259f4..d0ca1d8de1b8a 100644 --- a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/type/EntranceSQLRouter.java +++ b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/type/EntranceSQLRouter.java @@ -25,6 +25,8 @@ import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.session.query.QueryContext; +import java.util.Collection; + /** * Entrance SQL Router. * @@ -39,9 +41,9 @@ public interface EntranceSQLRouter extends SQLRout * @param globalRuleMetaData global rule meta data * @param database database * @param rule rule + * @param tableNames table names * @param props configuration properties * @return route context */ - RouteContext createRouteContext(QueryContext queryContext, - RuleMetaData globalRuleMetaData, ShardingSphereDatabase database, T rule, ConfigurationProperties props); + RouteContext createRouteContext(QueryContext queryContext, RuleMetaData globalRuleMetaData, ShardingSphereDatabase database, T rule, Collection tableNames, ConfigurationProperties props); } diff --git a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/tableless/TablelessRouteEngineFactoryTest.java b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/tableless/TablelessRouteEngineFactoryTest.java new file mode 100644 index 0000000000000..8bc50eb1390a7 --- /dev/null +++ b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/tableless/TablelessRouteEngineFactoryTest.java @@ -0,0 +1,152 @@ +/* + * 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 org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext; +import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; +import org.apache.shardingsphere.infra.binder.context.statement.ddl.CloseStatementContext; +import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; +import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.hint.HintValueContext; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.infra.route.engine.tableless.type.broadcast.DataSourceBroadcastRouteEngine; +import org.apache.shardingsphere.infra.route.engine.tableless.type.broadcast.InstanceBroadcastRouteEngine; +import org.apache.shardingsphere.infra.session.connection.ConnectionContext; +import org.apache.shardingsphere.infra.session.query.QueryContext; +import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement; +import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.TCLStatement; +import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLCreateResourceGroupStatement; +import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLSetResourceGroupStatement; +import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLSetStatement; +import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLShowDatabasesStatement; +import org.apache.shardingsphere.sql.parser.statement.opengauss.ddl.OpenGaussCloseStatement; +import org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLSetStatement; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class TablelessRouteEngineFactoryTest { + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private ShardingSphereDatabase database; + + @Mock(extraInterfaces = TableAvailable.class) + private SQLStatementContext sqlStatementContext; + + @Mock + private TablesContext tablesContext; + + @BeforeEach + void setUp() { + when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext); + when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); + Collection tableNames = new ArrayList<>(); + when(tablesContext.getTableNames()).thenReturn(tableNames); + } + + private ConnectionContext mockConnectionContext() { + ConnectionContext result = mock(ConnectionContext.class); + when(result.getCurrentDatabaseName()).thenReturn(Optional.of(DefaultDatabase.LOGIC_NAME)); + return result; + } + + @Test + void assertNewInstanceForSetResourceGroup() { + MySQLSetResourceGroupStatement resourceGroupStatement = mock(MySQLSetResourceGroupStatement.class); + when(sqlStatementContext.getSqlStatement()).thenReturn(resourceGroupStatement); + QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); + TablelessRouteEngine actual = TablelessRouteEngineFactory.newInstance(queryContext); + assertThat(actual, instanceOf(InstanceBroadcastRouteEngine.class)); + } + + @Test + void assertNewInstanceForDALShow() { + DALStatement dalStatement = mock(MySQLShowDatabasesStatement.class); + when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); + QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); + TablelessRouteEngine actual = TablelessRouteEngineFactory.newInstance(queryContext); + assertThat(actual, instanceOf(DataSourceBroadcastRouteEngine.class)); + } + + @Test + void assertNewInstanceForDALSetForPostgreSQL() { + assertNewInstanceForDALSet(mock(PostgreSQLSetStatement.class)); + } + + @Test + void assertNewInstanceForTCL() { + TCLStatement tclStatement = mock(TCLStatement.class); + when(sqlStatementContext.getSqlStatement()).thenReturn(tclStatement); + QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); + TablelessRouteEngine actual = TablelessRouteEngineFactory.newInstance(queryContext); + assertThat(actual, instanceOf(DataSourceBroadcastRouteEngine.class)); + } + + @Test + void assertNewInstanceForDALSetForMySQL() { + assertNewInstanceForDALSet(mock(MySQLSetStatement.class)); + } + + private void assertNewInstanceForDALSet(final DALStatement dalStatement) { + when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); + QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); + TablelessRouteEngine actual = TablelessRouteEngineFactory.newInstance(queryContext); + assertThat(actual, instanceOf(DataSourceBroadcastRouteEngine.class)); + } + + @Test + void assertNewInstanceForCreateResourceGroup() { + MySQLCreateResourceGroupStatement resourceGroupStatement = mock(MySQLCreateResourceGroupStatement.class); + when(sqlStatementContext.getSqlStatement()).thenReturn(resourceGroupStatement); + QueryContext queryContext = new QueryContext(sqlStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); + TablelessRouteEngine actual = TablelessRouteEngineFactory.newInstance(queryContext); + assertThat(actual, instanceOf(InstanceBroadcastRouteEngine.class)); + } + + @Test + void assertNewInstanceForCloseAllStatement() { + CloseStatementContext closeStatementContext = mock(CloseStatementContext.class, RETURNS_DEEP_STUBS); + OpenGaussCloseStatement closeStatement = mock(OpenGaussCloseStatement.class); + when(closeStatement.isCloseAll()).thenReturn(true); + when(closeStatementContext.getTablesContext().getDatabaseName()).thenReturn(Optional.empty()); + when(closeStatementContext.getSqlStatement()).thenReturn(closeStatement); + QueryContext queryContext = new QueryContext(closeStatementContext, "", Collections.emptyList(), new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); + TablelessRouteEngine actual = TablelessRouteEngineFactory.newInstance(queryContext); + assertThat(actual, instanceOf(DataSourceBroadcastRouteEngine.class)); + } +} diff --git a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/DataSourceSQLRouterFixture.java b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/DataSourceSQLRouterFixture.java index b6fa5198f811a..5453e622e9cf0 100644 --- a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/DataSourceSQLRouterFixture.java +++ b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/DataSourceSQLRouterFixture.java @@ -29,13 +29,14 @@ import org.apache.shardingsphere.infra.route.type.EntranceSQLRouter; import org.apache.shardingsphere.infra.session.query.QueryContext; +import java.util.Collection; import java.util.Collections; public final class DataSourceSQLRouterFixture implements EntranceSQLRouter, DecorateSQLRouter, DataSourceSQLRouter { @Override public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final DataSourceRouteRuleFixture rule, - final ConfigurationProperties props) { + final Collection tableNames, final ConfigurationProperties props) { RouteContext result = new RouteContext(); result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList())); return result; @@ -43,7 +44,7 @@ public RouteContext createRouteContext(final QueryContext queryContext, final Ru @Override public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, final DataSourceRouteRuleFixture rule, - final ConfigurationProperties props) { + final Collection tableNames, final ConfigurationProperties props) { routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList())); } diff --git a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/TableSQLRouterFailureFixture.java b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/TableSQLRouterFailureFixture.java index ef8c990436e32..778c4b8141de4 100644 --- a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/TableSQLRouterFailureFixture.java +++ b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/TableSQLRouterFailureFixture.java @@ -25,11 +25,13 @@ import org.apache.shardingsphere.infra.route.type.TableSQLRouter; import org.apache.shardingsphere.infra.session.query.QueryContext; +import java.util.Collection; + public final class TableSQLRouterFailureFixture implements DecorateSQLRouter, TableSQLRouter { @Override public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, - final RouteFailureRuleFixture rule, final ConfigurationProperties props) { + final RouteFailureRuleFixture rule, final Collection tableNames, final ConfigurationProperties props) { throw new UnsupportedOperationException("Route failure."); } diff --git a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/TableSQLRouterFixture.java b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/TableSQLRouterFixture.java index ffec33290b006..a3cc7486e7540 100644 --- a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/TableSQLRouterFixture.java +++ b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/fixture/router/TableSQLRouterFixture.java @@ -29,13 +29,14 @@ import org.apache.shardingsphere.infra.route.type.TableSQLRouter; import org.apache.shardingsphere.infra.session.query.QueryContext; +import java.util.Collection; import java.util.Collections; public final class TableSQLRouterFixture implements EntranceSQLRouter, DecorateSQLRouter, TableSQLRouter { @Override public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final TableRouteRuleFixture rule, - final ConfigurationProperties props) { + final Collection tableNames, final ConfigurationProperties props) { RouteContext result = new RouteContext(); result.getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList())); return result; @@ -43,7 +44,7 @@ public RouteContext createRouteContext(final QueryContext queryContext, final Ru @Override public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, final TableRouteRuleFixture rule, - final ConfigurationProperties props) { + final Collection tableNames, final ConfigurationProperties props) { routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList())); } diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java index 6035bbe35fee4..f278ff348549d 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java @@ -49,7 +49,7 @@ public final class SingleSQLRouter implements EntranceSQLRouter, Dec @Override public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, - final SingleRule rule, final ConfigurationProperties props) { + final SingleRule rule, final Collection tableNames, final ConfigurationProperties props) { if (1 == database.getResourceMetaData().getStorageUnits().size()) { return createSingleDataSourceRouteContext(rule, database, queryContext); } @@ -68,7 +68,7 @@ private Collection getSingleTables(final ShardingSphereDatabase @Override public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, - final SingleRule rule, final ConfigurationProperties props) { + final SingleRule rule, final Collection tableNames, final ConfigurationProperties props) { SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); Collection singleTables = getSingleTables(database, rule, routeContext, sqlStatementContext); SingleRouteEngineFactory.newInstance(singleTables, sqlStatementContext.getSqlStatement(), queryContext.getHintValueContext()).ifPresent(optional -> optional.route(routeContext, rule)); diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java index e21b72da467be..fa0beb370a4e4 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java @@ -83,7 +83,7 @@ void assertCreateRouteContextWithSingleDataSource() throws SQLException { rule.getAttributes().getAttribute(DataNodeRuleAttribute.class).getAllDataNodes().put("t_order", Collections.singleton(createDataNode("foo_ds"))); ShardingSphereDatabase database = mockSingleDatabase(); RouteContext actual = new SingleSQLRouter().createRouteContext( - createQueryContext(), mock(RuleMetaData.class), database, rule, new ConfigurationProperties(new Properties())); + createQueryContext(), mock(RuleMetaData.class), database, rule, Collections.singletonList("t_order"), new ConfigurationProperties(new Properties())); assertThat(actual.getRouteUnits().size(), is(1)); RouteUnit routeUnit = actual.getRouteUnits().iterator().next(); assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("foo_ds")); @@ -104,7 +104,7 @@ void assertCreateRouteContextWithReadwriteSplittingDataSource() throws SQLExcept rule.getAttributes().getAttribute(DataNodeRuleAttribute.class).getAllDataNodes().put("t_order", Collections.singletonList(createDataNode("write_ds"))); ShardingSphereDatabase database = mockReadwriteSplittingDatabase(); RouteContext actual = new SingleSQLRouter().createRouteContext( - createQueryContext(), mock(RuleMetaData.class), database, rule, new ConfigurationProperties(new Properties())); + createQueryContext(), mock(RuleMetaData.class), database, rule, Collections.singletonList("t_order"), new ConfigurationProperties(new Properties())); assertThat(actual.getRouteUnits().size(), is(1)); RouteUnit routeUnit = actual.getRouteUnits().iterator().next(); assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("readwrite_ds")); @@ -124,7 +124,7 @@ void assertCreateRouteContextWithMultiDataSource() throws SQLException { SingleRule rule = new SingleRule(new SingleRuleConfiguration(), DefaultDatabase.LOGIC_NAME, new H2DatabaseType(), createMultiDataSourceMap(), Collections.emptyList()); ShardingSphereDatabase database = mockDatabaseWithMultipleResources(); RouteContext actual = new SingleSQLRouter().createRouteContext( - createQueryContext(), mock(RuleMetaData.class), database, rule, new ConfigurationProperties(new Properties())); + createQueryContext(), mock(RuleMetaData.class), database, rule, Collections.singletonList("t_order"), new ConfigurationProperties(new Properties())); List routeUnits = new ArrayList<>(actual.getRouteUnits()); assertThat(actual.getRouteUnits().size(), is(1)); assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is(routeUnits.get(0).getDataSourceMapper().getActualName())); @@ -179,7 +179,7 @@ void assertDecorateRouteContextWithSingleDataSource() { routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("foo_ds", "foo_ds"), Collections.singletonList(new RouteMapper("t_order", "t_order")))); SingleSQLRouter sqlRouter = (SingleSQLRouter) OrderedSPILoader.getServices(SQLRouter.class, Collections.singleton(rule)).get(rule); sqlRouter.decorateRouteContext( - routeContext, createQueryContext(), mockReadwriteSplittingDatabase(), rule, new ConfigurationProperties(new Properties())); + routeContext, createQueryContext(), mockReadwriteSplittingDatabase(), rule, Collections.singletonList("t_order"), new ConfigurationProperties(new Properties())); Iterator routedDataSourceNames = routeContext.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is("foo_ds")); } @@ -196,7 +196,7 @@ void assertDecorateRouteContextWithReadwriteSplittingDataSource() { SingleSQLRouter sqlRouter = (SingleSQLRouter) OrderedSPILoader.getServices(SQLRouter.class, Collections.singleton(rule)).get(rule); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new RuleMetaData(Collections.singleton(rule)), Collections.emptyMap()); - sqlRouter.decorateRouteContext(routeContext, createQueryContext(), database, rule, new ConfigurationProperties(new Properties())); + sqlRouter.decorateRouteContext(routeContext, createQueryContext(), database, rule, Collections.singletonList("t_order"), new ConfigurationProperties(new Properties())); assertThat(routeContext.getActualDataSourceNames(), is(new HashSet<>(Arrays.asList("write_ds", "readwrite_ds")))); } @@ -208,7 +208,7 @@ void assertDecorateRouteContextWithMultiDataSource() throws SQLException { routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.emptyList())); SingleSQLRouter sqlRouter = (SingleSQLRouter) OrderedSPILoader.getServices(SQLRouter.class, Collections.singleton(rule)).get(rule); sqlRouter.decorateRouteContext( - routeContext, createQueryContext(), mockDatabaseWithMultipleResources(), rule, new ConfigurationProperties(new Properties())); + routeContext, createQueryContext(), mockDatabaseWithMultipleResources(), rule, Collections.singletonList("t_order"), new ConfigurationProperties(new Properties())); Iterator routedDataSourceNames = routeContext.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is("ds_1")); assertThat(routedDataSourceNames.next(), is("ds_0")); diff --git a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java index a67d88e71a99e..1742dd6e4aca2 100644 --- a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java +++ b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/migration/AbstractMigrationE2EIT.java @@ -76,7 +76,7 @@ protected void addMigrationTargetResource(final PipelineContainerComposer contai .replace("${ds3}", containerComposer.getActualJdbcUrlTemplate(PipelineContainerComposer.DS_3, true)) .replace("${ds4}", containerComposer.getActualJdbcUrlTemplate(PipelineContainerComposer.DS_4, true)); containerComposer.proxyExecuteWithLog(addTargetResource, 0); - Awaitility.await().ignoreExceptions().atMost(15L, TimeUnit.SECONDS).pollInterval(3L, TimeUnit.SECONDS).until(() -> 3 == containerComposer.showStorageUnitsName().size()); + Awaitility.await().ignoreExceptions().atMost(60L, TimeUnit.SECONDS).pollInterval(3L, TimeUnit.SECONDS).until(() -> 3 == containerComposer.showStorageUnitsName().size()); } protected void createSourceSchema(final PipelineContainerComposer containerComposer, final String schemaName) throws SQLException {