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 shadow router module #33555

Merged
merged 6 commits into from
Nov 5, 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,16 +19,17 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
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.retriever.dml.ShadowDeleteStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowInsertStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowSelectStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowUpdateStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.ShadowDMLStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.hint.ShadowHintDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;

import java.util.Optional;

/**
* Shadow data source mappings retriever factory.
Expand All @@ -43,22 +44,25 @@ public final class ShadowDataSourceMappingsRetrieverFactory {
* @return created instance
*/
public static ShadowDataSourceMappingsRetriever newInstance(final QueryContext queryContext) {
if (queryContext.getSqlStatementContext() instanceof InsertStatementContext) {
return new ShadowInsertStatementDataSourceMappingsRetriever(
(InsertStatementContext) queryContext.getSqlStatementContext(), queryContext.getHintValueContext());
Optional<ShadowOperationType> operationType = getShadowOperationType(queryContext.getSqlStatementContext());
return operationType.isPresent()
? new ShadowDMLStatementDataSourceMappingsRetriever(queryContext, operationType.get())
: new ShadowHintDataSourceMappingsRetriever(queryContext.getHintValueContext());
}

private static Optional<ShadowOperationType> getShadowOperationType(final SQLStatementContext sqlStatementContext) {
if (sqlStatementContext instanceof InsertStatementContext) {
return Optional.of(ShadowOperationType.INSERT);
}
if (queryContext.getSqlStatementContext() instanceof DeleteStatementContext) {
return new ShadowDeleteStatementDataSourceMappingsRetriever(
(DeleteStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext());
if (sqlStatementContext instanceof DeleteStatementContext) {
return Optional.of(ShadowOperationType.DELETE);
}
if (queryContext.getSqlStatementContext() instanceof UpdateStatementContext) {
return new ShadowUpdateStatementDataSourceMappingsRetriever(
(UpdateStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext());
if (sqlStatementContext instanceof UpdateStatementContext) {
return Optional.of(ShadowOperationType.UPDATE);
}
if (queryContext.getSqlStatementContext() instanceof SelectStatementContext) {
return new ShadowSelectStatementDataSourceMappingsRetriever(
(SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), queryContext.getHintValueContext());
if (sqlStatementContext instanceof SelectStatementContext) {
return Optional.of(ShadowOperationType.SELECT);
}
return new ShadowHintDataSourceMappingsRetriever(queryContext.getHintValueContext());
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.retriever.dml;

import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
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.binder.context.type.TableAvailable;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.shadow.route.retriever.ShadowDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowColumnDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowDeleteStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowInsertStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowSelectStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.table.column.ShadowUpdateStatementDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.table.hint.ShadowTableHintDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Shadow DML statement data source mappings retriever.
*/
@HighFrequencyInvocation
public final class ShadowDMLStatementDataSourceMappingsRetriever implements ShadowDataSourceMappingsRetriever {

private final Map<String, String> tableAliasAndNameMappings;

private final ShadowTableHintDataSourceMappingsRetriever tableHintDataSourceMappingsRetriever;

private final ShadowColumnDataSourceMappingsRetriever shadowColumnDataSourceMappingsRetriever;

public ShadowDMLStatementDataSourceMappingsRetriever(final QueryContext queryContext, final ShadowOperationType operationType) {
tableAliasAndNameMappings = getTableAliasAndNameMappings(((TableAvailable) queryContext.getSqlStatementContext()).getTablesContext().getSimpleTables());
tableHintDataSourceMappingsRetriever = new ShadowTableHintDataSourceMappingsRetriever(operationType, queryContext.getHintValueContext().isShadow());
shadowColumnDataSourceMappingsRetriever = createShadowDataSourceMappingsRetriever(queryContext, tableAliasAndNameMappings);
}

private Map<String, String> getTableAliasAndNameMappings(final Collection<SimpleTableSegment> tableSegments) {
Map<String, String> result = new LinkedHashMap<>(tableSegments.size(), 1F);
for (SimpleTableSegment each : tableSegments) {
String tableName = each.getTableName().getIdentifier().getValue();
String alias = each.getAliasName().isPresent() ? each.getAliasName().get() : tableName;
result.put(alias, tableName);
}
return result;
}

private ShadowColumnDataSourceMappingsRetriever createShadowDataSourceMappingsRetriever(final QueryContext queryContext, final Map<String, String> tableAliasAndNameMappings) {
if (queryContext.getSqlStatementContext() instanceof InsertStatementContext) {
return new ShadowInsertStatementDataSourceMappingsRetriever((InsertStatementContext) queryContext.getSqlStatementContext(), tableAliasAndNameMappings);
}
if (queryContext.getSqlStatementContext() instanceof DeleteStatementContext) {
return new ShadowDeleteStatementDataSourceMappingsRetriever((DeleteStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings);
}
if (queryContext.getSqlStatementContext() instanceof UpdateStatementContext) {
return new ShadowUpdateStatementDataSourceMappingsRetriever((UpdateStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings);
}
if (queryContext.getSqlStatementContext() instanceof SelectStatementContext) {
return new ShadowSelectStatementDataSourceMappingsRetriever((SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings);
}
throw new UnsupportedOperationException(String.format("unsupported SQL statement context `%s`", queryContext.getSqlStatementContext().getClass().getName()));
}

@Override
public Map<String, String> retrieve(final ShadowRule rule) {
Collection<String> shadowTables = rule.filterShadowTables(tableAliasAndNameMappings.values());
Map<String, String> result = tableHintDataSourceMappingsRetriever.retrieve(rule, shadowTables);
return result.isEmpty() ? shadowColumnDataSourceMappingsRetriever.retrieve(rule, shadowTables) : result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.retriever.dml.table;

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

import java.util.Collection;
import java.util.Map;

/**
* Shadow table data source mappings retriever.
*/
public interface ShadowTableDataSourceMappingsRetriever {

/**
* Retrieve shadow data source mappings.
*
* @param rule shadow rule
* @param shadowTables shadow tables
* @return retrieved shadow data source mappings
*/
Map<String, String> retrieve(ShadowRule rule, Collection<String> shadowTables);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,37 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.retriever.dml;
package org.apache.shardingsphere.shadow.route.retriever.dml.table.column;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
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.shadow.condition.ShadowColumnCondition;
import org.apache.shardingsphere.shadow.condition.ShadowCondition;
import org.apache.shardingsphere.shadow.route.determiner.ColumnShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.retriever.ShadowDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.hint.ShadowTableHintDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.route.retriever.dml.table.ShadowTableDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.spi.column.ColumnShadowAlgorithm;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* Abstract shadow DML statement data source mappings retriever.
* Shadow column data source mappings retriever.
*/
@HighFrequencyInvocation
public abstract class AbstractShadowDMLStatementDataSourceMappingsRetriever implements ShadowDataSourceMappingsRetriever {
@RequiredArgsConstructor
public abstract class ShadowColumnDataSourceMappingsRetriever implements ShadowTableDataSourceMappingsRetriever {

private final ShadowOperationType operationType;

@Getter
private final Map<String, String> tableAliasAndNameMappings;

private final ShadowTableHintDataSourceMappingsRetriever tableHintDataSourceMappingsRetriever;

protected AbstractShadowDMLStatementDataSourceMappingsRetriever(final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final ShadowOperationType operationType) {
this.operationType = operationType;
tableAliasAndNameMappings = getTableAliasAndNameMappings(((TableAvailable) sqlStatementContext).getTablesContext().getSimpleTables());
tableHintDataSourceMappingsRetriever = new ShadowTableHintDataSourceMappingsRetriever(operationType, hintValueContext.isShadow(), tableAliasAndNameMappings);
}

private Map<String, String> getTableAliasAndNameMappings(final Collection<SimpleTableSegment> tableSegments) {
Map<String, String> result = new LinkedHashMap<>(tableSegments.size(), 1F);
for (SimpleTableSegment each : tableSegments) {
String tableName = each.getTableName().getIdentifier().getValue();
String alias = each.getAliasName().isPresent() ? each.getAliasName().get() : tableName;
result.put(alias, tableName);
}
return result;
}

@Override
public Map<String, String> retrieve(final ShadowRule rule) {
Map<String, String> result = tableHintDataSourceMappingsRetriever.retrieve(rule);
return result.isEmpty() ? findByShadowColumn(rule, rule.filterShadowTables(tableAliasAndNameMappings.values())) : result;
}

private Map<String, String> findByShadowColumn(final ShadowRule rule, final Collection<String> shadowTables) {
public Map<String, String> retrieve(final ShadowRule rule, final Collection<String> shadowTables) {
for (String each : shadowTables) {
Collection<String> shadowColumnNames = rule.getShadowColumnNames(operationType, each);
if (!shadowColumnNames.isEmpty() && isMatchAnyColumnShadowAlgorithms(rule, each, shadowColumnNames)) {
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.retriever.dml;
package org.apache.shardingsphere.shadow.route.retriever.dml.table.column;

import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
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.util.ShadowExtractor;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
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 All @@ -30,18 +30,20 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* Shadow delete statement data source mappings retriever.
*/
public final class ShadowDeleteStatementDataSourceMappingsRetriever extends AbstractShadowDMLStatementDataSourceMappingsRetriever {
@HighFrequencyInvocation
public final class ShadowDeleteStatementDataSourceMappingsRetriever extends ShadowColumnDataSourceMappingsRetriever {

private final DeleteStatementContext sqlStatementContext;

private final List<Object> parameters;

public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementContext sqlStatementContext, final List<Object> parameters, final HintValueContext hintValueContext) {
super(sqlStatementContext, hintValueContext, ShadowOperationType.DELETE);
public ShadowDeleteStatementDataSourceMappingsRetriever(final DeleteStatementContext sqlStatementContext, final List<Object> parameters, final Map<String, String> tableAliasAndNameMappings) {
super(ShadowOperationType.DELETE, tableAliasAndNameMappings);
this.sqlStatementContext = sqlStatementContext;
this.parameters = parameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@
* limitations under the License.
*/

package org.apache.shardingsphere.shadow.route.retriever.dml;
package org.apache.shardingsphere.shadow.route.retriever.dml.table.column;

import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.binder.context.segment.insert.values.InsertValueContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
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.exception.syntax.UnsupportedShadowInsertValueException;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;

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

/**
* Shadow insert statement data source mappings retriever.
*/
public final class ShadowInsertStatementDataSourceMappingsRetriever extends AbstractShadowDMLStatementDataSourceMappingsRetriever {
@HighFrequencyInvocation
public final class ShadowInsertStatementDataSourceMappingsRetriever extends ShadowColumnDataSourceMappingsRetriever {

private final InsertStatementContext sqlStatementContext;

public ShadowInsertStatementDataSourceMappingsRetriever(final InsertStatementContext sqlStatementContext, final HintValueContext hintValueContext) {
super(sqlStatementContext, hintValueContext, ShadowOperationType.INSERT);
public ShadowInsertStatementDataSourceMappingsRetriever(final InsertStatementContext sqlStatementContext, final Map<String, String> tableAliasAndNameMappings) {
super(ShadowOperationType.INSERT, tableAliasAndNameMappings);
this.sqlStatementContext = sqlStatementContext;
}

Expand Down
Loading