From f416eb45d466f43e3028ff08b493ccc88ad5a089 Mon Sep 17 00:00:00 2001 From: ZhangCheng Date: Mon, 4 Nov 2024 14:43:32 +0800 Subject: [PATCH] Refactor broadcast sql router --- .../broadcast/route/BroadcastSQLRouter.java | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 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..fb732d9edecc2 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,8 @@ 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; @@ -66,9 +68,7 @@ public final class BroadcastSQLRouter implements EntranceSQLRouter tableNames = sqlStatementContext instanceof TableAvailable ? getTableNames((TableAvailable) sqlStatementContext) : Collections.emptyList(); if (rule.isAllBroadcastTables(tableNames)) { - routeToAllDatabaseInstances(routeContext, database, rule); + doInstanceBroadcastRoute(routeContext, database, rule); } } @@ -152,20 +152,14 @@ private boolean isDCLForSingleTable(final SQLStatementContext sqlStatementContex return false; } - private void routeToAllDatabases(final RouteContext routeContext, final BroadcastRule rule) { + private void doDatabaseBroadcastRoute(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())); - } + routeContext.getRouteUnits().addAll(new BroadcastDatabaseBroadcastRouteEngine().route(new 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(new RouteContext(), rule).getRouteUnits()); } @Override