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

Add ShadowDataSourceMappingsFinder #33519

Merged
merged 10 commits into from
Nov 3, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.shadow.constant.ShadowOrder;
import org.apache.shardingsphere.shadow.route.engine.ShadowRouteEngineFactory;
import org.apache.shardingsphere.shadow.route.engine.ShadowDataSourceMappingsFinderFactory;
import org.apache.shardingsphere.shadow.route.engine.ShadowRouteEngine;
import org.apache.shardingsphere.shadow.rule.ShadowRule;

/**
Expand All @@ -36,7 +37,7 @@ 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) {
ShadowRouteEngineFactory.newInstance(queryContext).route(routeContext, rule);
new ShadowRouteEngine().route(routeContext, rule, ShadowDataSourceMappingsFinderFactory.newInstance(queryContext));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,31 @@
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.dml.ShadowDeleteStatementRouteEngine;
import org.apache.shardingsphere.shadow.route.engine.dml.ShadowInsertStatementRouteEngine;
import org.apache.shardingsphere.shadow.route.engine.dml.ShadowSelectStatementRouteEngine;
import org.apache.shardingsphere.shadow.route.engine.dml.ShadowUpdateStatementRouteEngine;
import org.apache.shardingsphere.shadow.route.engine.impl.ShadowNonDMLStatementRouteEngine;
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.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 routing engine factory.
* Shadow data source mappings finder.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShadowRouteEngineFactory {
public final class ShadowDataSourceMappingsFinderFactory {

/**
* Create new instance of shadow route engine.
* Create new instance of shadow data source mappings finder.
*
* @param queryContext query context
* @return created instance
*/
public static ShadowRouteEngine newInstance(final QueryContext queryContext) {
public static ShadowDataSourceMappingsFinder newInstance(final QueryContext queryContext) {
SQLStatement sqlStatement = queryContext.getSqlStatementContext().getSqlStatement();
if (sqlStatement instanceof InsertStatement) {
return createShadowInsertStatementRouteEngine(queryContext);
Expand All @@ -64,23 +65,23 @@ public static ShadowRouteEngine newInstance(final QueryContext queryContext) {
return createShadowNonMDLStatementRouteEngine(queryContext);
}

private static ShadowRouteEngine createShadowNonMDLStatementRouteEngine(final QueryContext queryContext) {
return new ShadowNonDMLStatementRouteEngine(queryContext.getHintValueContext());
private static ShadowDataSourceMappingsFinder createShadowNonMDLStatementRouteEngine(final QueryContext queryContext) {
return new ShadowNonDMLStatementDataSourceMappingsFinder(queryContext.getHintValueContext());
}

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

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

private static ShadowRouteEngine createShadowDeleteStatementRouteEngine(final QueryContext queryContext) {
return new ShadowDeleteStatementRouteEngine((DeleteStatementContext) 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 ShadowRouteEngine createShadowInsertStatementRouteEngine(final QueryContext queryContext) {
return new ShadowInsertStatementRouteEngine((InsertStatementContext) queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
private static ShadowDataSourceMappingsFinder createShadowInsertStatementRouteEngine(final QueryContext queryContext) {
return new ShadowInsertStatementDataSourceMappingsFinder((InsertStatementContext) queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,47 @@

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

import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
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.shadow.route.engine.finder.ShadowDataSourceMappingsFinder;
import org.apache.shardingsphere.shadow.rule.ShadowRule;

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

/**
* Shadow route engine.
*/
public interface ShadowRouteEngine {
@HighFrequencyInvocation
public final class ShadowRouteEngine {

/**
* Route.
*
* @param routeContext route context
* @param rule shadow rule
* @param finder finder
*/
void route(RouteContext routeContext, ShadowRule rule);
public void route(final RouteContext routeContext, final ShadowRule rule, final ShadowDataSourceMappingsFinder finder) {
Collection<RouteUnit> toBeRemovedRouteUnit = new LinkedList<>();
Collection<RouteUnit> toBeAddedRouteUnit = new LinkedList<>();
Map<String, String> shadowDataSourceMappings = finder.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.shadow.route.engine.finder;

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

import java.util.Map;

/**
* Shadow data source mappings finder.
*/
public interface ShadowDataSourceMappingsFinder {

/**
* Find shadow data source mappings.
*
* @param rule shadow rule
* @return found shadow data source mappings
*/
Map<String, String> find(ShadowRule rule);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
* limitations under the License.
*/

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

import lombok.Getter;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.condition.ShadowDetermineCondition;
import org.apache.shardingsphere.shadow.route.engine.ShadowRouteContextDecorator;
import org.apache.shardingsphere.shadow.route.engine.ShadowRouteEngine;
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.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
Expand All @@ -43,10 +41,10 @@
import java.util.Optional;

/**
* Abstract shadow DML statement route engine.
* Abstract shadow DML statement data source mappings finder.
*/
@HighFrequencyInvocation
public abstract class AbstractShadowDMLStatementRouteEngine implements ShadowRouteEngine {
public abstract class AbstractShadowDMLStatementDataSourceMappingsFinder implements ShadowDataSourceMappingsFinder {

private final ShadowOperationType operationType;

Expand All @@ -55,7 +53,7 @@ public abstract class AbstractShadowDMLStatementRouteEngine implements ShadowRou
@Getter
private final Map<String, String> tableAliasAndNameMappings;

protected AbstractShadowDMLStatementRouteEngine(final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final ShadowOperationType operationType) {
protected AbstractShadowDMLStatementDataSourceMappingsFinder(final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final ShadowOperationType operationType) {
this.operationType = operationType;
isShadow = hintValueContext.isShadow();
tableAliasAndNameMappings = getTableAliasAndNameMappings(((TableAvailable) sqlStatementContext).getTablesContext().getSimpleTables());
Expand All @@ -72,11 +70,7 @@ private Map<String, String> getTableAliasAndNameMappings(final Collection<Simple
}

@Override
public final void route(final RouteContext routeContext, final ShadowRule rule) {
ShadowRouteContextDecorator.decorate(routeContext, rule, findShadowDataSourceMappings(rule));
}

private Map<String, String> findShadowDataSourceMappings(final ShadowRule rule) {
public Map<String, String> find(final ShadowRule rule) {
Collection<String> relatedShadowTables = rule.getRelatedShadowTables(tableAliasAndNameMappings.values());
if (relatedShadowTables.isEmpty() && isMatchDefaultAlgorithm(rule)) {
return rule.getAllShadowDataSourceMappings();
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.dml;
package org.apache.shardingsphere.shadow.route.engine.finder.dml;

import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext;
import org.apache.shardingsphere.infra.hint.HintValueContext;
Expand All @@ -32,15 +32,15 @@
import java.util.List;

/**
* Shadow delete statement route engine.
* Shadow delete statement data source mappings finder.
*/
public final class ShadowDeleteStatementRouteEngine extends AbstractShadowDMLStatementRouteEngine {
public final class ShadowDeleteStatementDataSourceMappingsFinder extends AbstractShadowDMLStatementDataSourceMappingsFinder {

private final DeleteStatementContext sqlStatementContext;

private final List<Object> parameters;

public ShadowDeleteStatementRouteEngine(final DeleteStatementContext sqlStatementContext, final List<Object> parameters, final HintValueContext hintValueContext) {
public ShadowDeleteStatementDataSourceMappingsFinder(final DeleteStatementContext sqlStatementContext, final List<Object> parameters, final HintValueContext hintValueContext) {
super(sqlStatementContext, hintValueContext, ShadowOperationType.DELETE);
this.sqlStatementContext = sqlStatementContext;
this.parameters = parameters;
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.dml;
package org.apache.shardingsphere.shadow.route.engine.finder.dml;

import org.apache.shardingsphere.infra.binder.context.segment.insert.values.InsertValueContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
Expand All @@ -30,13 +30,13 @@
import java.util.List;

/**
* Shadow insert statement route engine.
* Shadow insert statement data source mappings finder.
*/
public final class ShadowInsertStatementRouteEngine extends AbstractShadowDMLStatementRouteEngine {
public final class ShadowInsertStatementDataSourceMappingsFinder extends AbstractShadowDMLStatementDataSourceMappingsFinder {

private final InsertStatementContext sqlStatementContext;

public ShadowInsertStatementRouteEngine(final InsertStatementContext sqlStatementContext, final HintValueContext hintValueContext) {
public ShadowInsertStatementDataSourceMappingsFinder(final InsertStatementContext sqlStatementContext, final HintValueContext hintValueContext) {
super(sqlStatementContext, hintValueContext, ShadowOperationType.INSERT);
this.sqlStatementContext = sqlStatementContext;
}
Expand Down
Loading