Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor BroadcastDatabaseBroadcastRouteEngine #33508

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,21 +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) {
routeToAllDatabases(routeContext, rule);
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()) {
routeToAllDatabases(routeContext, rule);
doDatabaseBroadcastRoute(routeContext, rule);
}
return;
}
Expand All @@ -106,17 +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) {
routeToAllDatabases(routeContext, rule);
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<String> tableNames = sqlStatementContext instanceof TableAvailable ? getTableNames((TableAvailable) sqlStatementContext) : Collections.emptyList();
if (rule.isAllBroadcastTables(tableNames)) {
routeToAllDatabaseInstances(routeContext, database, rule);
doInstanceBroadcastRoute(routeContext, database, rule);
}
}

Expand Down Expand Up @@ -152,20 +154,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(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading