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 ShadowDataSourceMappingsFinderFactory #33522

Merged
merged 14 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 @@ -22,12 +22,18 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.shadow.constant.ShadowOrder;
import org.apache.shardingsphere.shadow.route.engine.ShadowDataSourceMappingsFinderFactory;
import org.apache.shardingsphere.shadow.route.engine.ShadowRouteEngine;
import org.apache.shardingsphere.shadow.route.finder.ShadowDataSourceMappingsFinderFactory;
import org.apache.shardingsphere.shadow.rule.ShadowRule;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;

/**
* Shadow SQL router.
*/
Expand All @@ -37,7 +43,22 @@ public final class ShadowSQLRouter implements DecorateSQLRouter<ShadowRule> {
@Override
public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database,
final ShadowRule rule, final ConfigurationProperties props) {
new ShadowRouteEngine().route(routeContext, rule, ShadowDataSourceMappingsFinderFactory.newInstance(queryContext));
Collection<RouteUnit> toBeRemovedRouteUnit = new LinkedList<>();
Collection<RouteUnit> toBeAddedRouteUnit = new LinkedList<>();
Map<String, String> shadowDataSourceMappings = ShadowDataSourceMappingsFinderFactory.newInstance(queryContext).find(rule);
for (RouteUnit each : routeContext.getRouteUnits()) {
String logicName = each.getDataSourceMapper().getLogicName();
String actualName = each.getDataSourceMapper().getActualName();
Optional<String> productionDataSourceName = rule.findProductionDataSourceName(actualName);
if (productionDataSourceName.isPresent()) {
String shadowDataSourceName = shadowDataSourceMappings.get(productionDataSourceName.get());
toBeRemovedRouteUnit.add(each);
String dataSourceName = null == shadowDataSourceName ? productionDataSourceName.get() : shadowDataSourceName;
toBeAddedRouteUnit.add(new RouteUnit(new RouteMapper(logicName, dataSourceName), each.getTableMappers()));
}
}
routeContext.getRouteUnits().removeAll(toBeRemovedRouteUnit);
routeContext.getRouteUnits().addAll(toBeAddedRouteUnit);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine.determiner;
package org.apache.shardingsphere.shadow.route.determiner;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine.determiner;
package org.apache.shardingsphere.shadow.route.determiner;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -41,11 +41,11 @@ public final class HintShadowAlgorithmDeterminer {
* @return is shadow or not
*/
public static boolean isShadow(final HintShadowAlgorithm<Comparable<?>> shadowAlgorithm, final ShadowDetermineCondition shadowCondition, final ShadowRule shadowRule, final boolean useShadow) {
PreciseHintShadowValue<Comparable<?>> shadowValue = createNoteShadowValues(shadowCondition, useShadow);
PreciseHintShadowValue<Comparable<?>> shadowValue = createHintShadowValues(shadowCondition, useShadow);
return shadowAlgorithm.isShadow(shadowRule.getAllShadowTableNames(), shadowValue);
}

private static PreciseHintShadowValue<Comparable<?>> createNoteShadowValues(final ShadowDetermineCondition shadowDetermineCondition, final boolean useShadow) {
private static PreciseHintShadowValue<Comparable<?>> createHintShadowValues(final ShadowDetermineCondition shadowDetermineCondition, final boolean useShadow) {
ShadowOperationType shadowOperationType = shadowDetermineCondition.getShadowOperationType();
String tableName = shadowDetermineCondition.getTableName();
return new PreciseHintShadowValue<>(tableName, shadowOperationType, useShadow);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine.finder;
package org.apache.shardingsphere.shadow.route.finder;

import org.apache.shardingsphere.shadow.rule.ShadowRule;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine;
package org.apache.shardingsphere.shadow.route.finder;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -24,20 +24,19 @@
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.shadow.route.engine.finder.ShadowDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.engine.finder.dml.ShadowDeleteStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.engine.finder.dml.ShadowInsertStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.engine.finder.dml.ShadowSelectStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.engine.finder.dml.ShadowUpdateStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.engine.finder.other.ShadowNonDMLStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.dml.ShadowDeleteStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.dml.ShadowInsertStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.dml.ShadowSelectStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.dml.ShadowUpdateStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.finder.other.ShadowNonDMLStatementDataSourceMappingsFinder;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;

/**
* Shadow data source mappings finder.
* Shadow data source mappings finder factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShadowDataSourceMappingsFinderFactory {
Expand All @@ -51,37 +50,37 @@ public final class ShadowDataSourceMappingsFinderFactory {
public static ShadowDataSourceMappingsFinder newInstance(final QueryContext queryContext) {
SQLStatement sqlStatement = queryContext.getSqlStatementContext().getSqlStatement();
if (sqlStatement instanceof InsertStatement) {
return createShadowInsertStatementRouteEngine(queryContext);
return createShadowInsertStatementDataSourceMappingsFinder(queryContext);
}
if (sqlStatement instanceof DeleteStatement) {
return createShadowDeleteStatementRouteEngine(queryContext);
return createShadowDeleteStatementDataSourceMappingsFinder(queryContext);
}
if (sqlStatement instanceof UpdateStatement) {
return createShadowUpdateStatementRouteEngine(queryContext);
return createShadowUpdateStatementDataSourceMappingsFinder(queryContext);
}
if (sqlStatement instanceof SelectStatement) {
return createShadowSelectStatementRouteEngine(queryContext);
return createShadowSelectStatementDataSourceMappingsFinder(queryContext);
}
return createShadowNonMDLStatementRouteEngine(queryContext);
return createShadowNonMDLStatementDataSourceMappingsFinder(queryContext);
}

private static ShadowDataSourceMappingsFinder createShadowNonMDLStatementRouteEngine(final QueryContext queryContext) {
return new ShadowNonDMLStatementDataSourceMappingsFinder(queryContext.getHintValueContext());
private static ShadowDataSourceMappingsFinder createShadowInsertStatementDataSourceMappingsFinder(final QueryContext queryContext) {
return new ShadowInsertStatementDataSourceMappingsFinder((InsertStatementContext) queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
}

private static ShadowDataSourceMappingsFinder createShadowSelectStatementRouteEngine(final QueryContext queryContext) {
return new ShadowSelectStatementDataSourceMappingsFinder((SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext());
private static ShadowDataSourceMappingsFinder createShadowDeleteStatementDataSourceMappingsFinder(final QueryContext queryContext) {
return new ShadowDeleteStatementDataSourceMappingsFinder((DeleteStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext());
}

private static ShadowDataSourceMappingsFinder createShadowUpdateStatementRouteEngine(final QueryContext queryContext) {
private static ShadowDataSourceMappingsFinder createShadowUpdateStatementDataSourceMappingsFinder(final QueryContext queryContext) {
return new ShadowUpdateStatementDataSourceMappingsFinder((UpdateStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext());
}

private static ShadowDataSourceMappingsFinder createShadowDeleteStatementRouteEngine(final QueryContext queryContext) {
return new ShadowDeleteStatementDataSourceMappingsFinder((DeleteStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext());
private static ShadowDataSourceMappingsFinder createShadowSelectStatementDataSourceMappingsFinder(final QueryContext queryContext) {
return new ShadowSelectStatementDataSourceMappingsFinder((SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext());
}

private static ShadowDataSourceMappingsFinder createShadowInsertStatementRouteEngine(final QueryContext queryContext) {
return new ShadowInsertStatementDataSourceMappingsFinder((InsertStatementContext) queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
private static ShadowDataSourceMappingsFinder createShadowNonMDLStatementDataSourceMappingsFinder(final QueryContext queryContext) {
return new ShadowNonDMLStatementDataSourceMappingsFinder(queryContext.getHintValueContext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine.finder.dml;
package org.apache.shardingsphere.shadow.route.finder.dml;

import lombok.Getter;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
Expand All @@ -24,9 +24,9 @@
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
import org.apache.shardingsphere.shadow.route.engine.determiner.ColumnShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.engine.determiner.HintShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.engine.finder.ShadowDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.route.determiner.ColumnShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.determiner.HintShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.finder.ShadowDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine.finder.dml;
package org.apache.shardingsphere.shadow.route.finder.dml;

import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.route.engine.util.ShadowExtractor;
import org.apache.shardingsphere.shadow.route.util.ShadowExtractor;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine.finder.dml;
package org.apache.shardingsphere.shadow.route.finder.dml;

import org.apache.shardingsphere.infra.binder.context.segment.insert.values.InsertValueContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine.finder.dml;
package org.apache.shardingsphere.shadow.route.finder.dml;

import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.shadow.route.util.ShadowExtractor;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.route.engine.util.ShadowExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.engine.finder.dml;
package org.apache.shardingsphere.shadow.route.finder.dml;

import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.shadow.route.util.ShadowExtractor;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.route.engine.util.ShadowExtractor;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
Expand Down
Loading