From 1a0131f5ad5fc675b36196280688fc658e528d06 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sun, 3 Nov 2024 14:30:24 +0800 Subject: [PATCH 1/2] Refactor BroadcastSQLRouter --- .../broadcast/route/BroadcastSQLRouter.java | 17 +++++++++-------- .../BroadcastDatabaseBroadcastRouteEngine.java | 5 +++-- 2 files changed, 12 insertions(+), 10 deletions(-) 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 3922375cda0e5..15b091b3b8e6c 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 @@ -19,6 +19,7 @@ import org.apache.shardingsphere.broadcast.constant.BroadcastOrder; import org.apache.shardingsphere.broadcast.route.engine.BroadcastRouteEngineFactory; +import org.apache.shardingsphere.broadcast.route.engine.type.broadcast.BroadcastDatabaseBroadcastRouteEngine; import org.apache.shardingsphere.broadcast.rule.BroadcastRule; import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; @@ -88,14 +89,16 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon } private void decorateRouteContextWhenTCLStatement(final RouteContext routeContext, final BroadcastRule rule) { - routeToAllDatabases(routeContext, rule); + routeContext.getRouteUnits().clear(); + routeContext.getRouteUnits().addAll(doDatabaseBroadcastRoute(routeContext, rule).getRouteUnits()); } private void decorateRouteContextWhenDDLStatement(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, final BroadcastRule rule) { SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); if (sqlStatementContext instanceof CursorAvailable) { if (sqlStatementContext instanceof CloseStatementContext && ((CloseStatementContext) sqlStatementContext).getSqlStatement().isCloseAll()) { - routeToAllDatabases(routeContext, rule); + routeContext.getRouteUnits().clear(); + routeContext.getRouteUnits().addAll(doDatabaseBroadcastRoute(routeContext, rule).getRouteUnits()); } return; } @@ -106,7 +109,8 @@ private void decorateRouteContextWhenDDLStatement(final RouteContext routeContex 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) { - routeToAllDatabases(routeContext, rule); + routeContext.getRouteUnits().clear(); + routeContext.getRouteUnits().addAll(doDatabaseBroadcastRoute(routeContext, rule).getRouteUnits()); return; } // TODO BEGIN extract db route logic to common database router, eg: DCL in instance route @duanzhengqiang @@ -152,11 +156,8 @@ private boolean isDCLForSingleTable(final SQLStatementContext sqlStatementContex return false; } - private void routeToAllDatabases(final RouteContext routeContext, final BroadcastRule rule) { - routeContext.getRouteUnits().clear(); - for (String each : rule.getDataSourceNames()) { - routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); - } + private RouteContext doDatabaseBroadcastRoute(final RouteContext routeContext, final BroadcastRule rule) { + return new BroadcastDatabaseBroadcastRouteEngine().route(routeContext, rule); } private void routeToAllDatabaseInstances(final RouteContext routeContext, final ShardingSphereDatabase database, final BroadcastRule rule) { diff --git a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/route/engine/type/broadcast/BroadcastDatabaseBroadcastRouteEngine.java b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/route/engine/type/broadcast/BroadcastDatabaseBroadcastRouteEngine.java index 9269f64e41925..5c5212539691b 100644 --- a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/route/engine/type/broadcast/BroadcastDatabaseBroadcastRouteEngine.java +++ b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/route/engine/type/broadcast/BroadcastDatabaseBroadcastRouteEngine.java @@ -34,9 +34,10 @@ public final class BroadcastDatabaseBroadcastRouteEngine implements BroadcastRou @Override public RouteContext route(final RouteContext routeContext, final BroadcastRule rule) { + RouteContext result = new RouteContext(); for (String each : rule.getDataSourceNames()) { - routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); + result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); } - return routeContext; + return result; } } From 26f88e9e42c74431c58afc8783aa4f9c4a9c53bf Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sun, 3 Nov 2024 14:38:33 +0800 Subject: [PATCH 2/2] Refactor BroadcastDatabaseBroadcastRouteEngine --- .../broadcast/route/BroadcastSQLRouter.java | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) 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 15b091b3b8e6c..bb902965b6994 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 @@ -20,6 +20,7 @@ import org.apache.shardingsphere.broadcast.constant.BroadcastOrder; import org.apache.shardingsphere.broadcast.route.engine.BroadcastRouteEngineFactory; import org.apache.shardingsphere.broadcast.route.engine.type.broadcast.BroadcastDatabaseBroadcastRouteEngine; +import org.apache.shardingsphere.broadcast.route.engine.type.broadcast.BroadcastInstanceBroadcastRouteEngine; import org.apache.shardingsphere.broadcast.rule.BroadcastRule; import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; @@ -82,23 +83,21 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon } else if (sqlStatement instanceof DDLStatement) { decorateRouteContextWhenDDLStatement(routeContext, queryContext, database, rule); } else if (sqlStatement instanceof DALStatement && isResourceGroupStatement(sqlStatement)) { - routeToAllDatabaseInstances(routeContext, database, rule); + doInstanceBroadcastRoute(routeContext, database, rule); } else if (sqlStatement instanceof DCLStatement && !isDCLForSingleTable(queryContext.getSqlStatementContext())) { - routeToAllDatabaseInstances(routeContext, database, rule); + doInstanceBroadcastRoute(routeContext, database, rule); } } private void decorateRouteContextWhenTCLStatement(final RouteContext routeContext, final BroadcastRule rule) { - routeContext.getRouteUnits().clear(); - routeContext.getRouteUnits().addAll(doDatabaseBroadcastRoute(routeContext, rule).getRouteUnits()); + doDatabaseBroadcastRoute(routeContext, rule); } private void decorateRouteContextWhenDDLStatement(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database, final BroadcastRule rule) { SQLStatementContext sqlStatementContext = queryContext.getSqlStatementContext(); if (sqlStatementContext instanceof CursorAvailable) { if (sqlStatementContext instanceof CloseStatementContext && ((CloseStatementContext) sqlStatementContext).getSqlStatement().isCloseAll()) { - routeContext.getRouteUnits().clear(); - routeContext.getRouteUnits().addAll(doDatabaseBroadcastRoute(routeContext, rule).getRouteUnits()); + doDatabaseBroadcastRoute(routeContext, rule); } return; } @@ -109,18 +108,17 @@ private void decorateRouteContextWhenDDLStatement(final RouteContext routeContex 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) { - routeContext.getRouteUnits().clear(); - routeContext.getRouteUnits().addAll(doDatabaseBroadcastRoute(routeContext, rule).getRouteUnits()); + doDatabaseBroadcastRoute(routeContext, rule); return; } // TODO BEGIN extract db route logic to common database router, eg: DCL in instance route @duanzhengqiang if (sqlStatement instanceof CreateTablespaceStatement || sqlStatement instanceof AlterTablespaceStatement || sqlStatement instanceof DropTablespaceStatement) { - routeToAllDatabaseInstances(routeContext, database, rule); + doInstanceBroadcastRoute(routeContext, database, rule); } // TODO END extract db route logic to common database router, eg: DCL in instance route Collection tableNames = sqlStatementContext instanceof TableAvailable ? getTableNames((TableAvailable) sqlStatementContext) : Collections.emptyList(); if (rule.isAllBroadcastTables(tableNames)) { - routeToAllDatabaseInstances(routeContext, database, rule); + doInstanceBroadcastRoute(routeContext, database, rule); } } @@ -156,17 +154,14 @@ private boolean isDCLForSingleTable(final SQLStatementContext sqlStatementContex return false; } - private RouteContext doDatabaseBroadcastRoute(final RouteContext routeContext, final BroadcastRule rule) { - return new BroadcastDatabaseBroadcastRouteEngine().route(routeContext, rule); + private void doDatabaseBroadcastRoute(final RouteContext routeContext, final BroadcastRule rule) { + routeContext.getRouteUnits().clear(); + routeContext.getRouteUnits().addAll(new BroadcastDatabaseBroadcastRouteEngine().route(routeContext, rule).getRouteUnits()); } - private void routeToAllDatabaseInstances(final RouteContext routeContext, final ShardingSphereDatabase database, final BroadcastRule rule) { + private void doInstanceBroadcastRoute(final RouteContext routeContext, final ShardingSphereDatabase database, final BroadcastRule rule) { routeContext.getRouteUnits().clear(); - for (String each : rule.getDataSourceNames()) { - if (database.getResourceMetaData().getAllInstanceDataSourceNames().contains(each)) { - routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); - } - } + routeContext.getRouteUnits().addAll(new BroadcastInstanceBroadcastRouteEngine(database.getResourceMetaData()).route(routeContext, rule).getRouteUnits()); } @Override