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

Fix rules order on DataNodes #33563

Merged
merged 5 commits into from
Nov 6, 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 @@ -20,6 +20,7 @@
import com.cedarsoftware.util.CaseInsensitiveSet;
import lombok.Getter;
import org.apache.shardingsphere.broadcast.config.BroadcastRuleConfiguration;
import org.apache.shardingsphere.broadcast.constant.BroadcastOrder;
import org.apache.shardingsphere.broadcast.rule.attribute.BroadcastDataNodeRuleAttribute;
import org.apache.shardingsphere.broadcast.rule.attribute.BroadcastTableNamesRuleAttribute;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
Expand Down Expand Up @@ -102,4 +103,9 @@ public Collection<String> filterBroadcastTableNames(final Collection<String> log
public boolean isAllBroadcastTables(final Collection<String> logicTableNames) {
return !logicTableNames.isEmpty() && tables.containsAll(logicTableNames);
}

@Override
public int getOrder() {
return BroadcastOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.config.rule.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.encrypt.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
import org.apache.shardingsphere.encrypt.exception.metadata.EncryptTableNotFoundException;
import org.apache.shardingsphere.encrypt.exception.metadata.MismatchedEncryptAlgorithmTypeException;
import org.apache.shardingsphere.encrypt.rule.attribute.EncryptTableMapperRuleAttribute;
Expand Down Expand Up @@ -189,4 +190,9 @@ private EncryptTableRuleConfiguration getTableRuleConfiguration(final String tab
Preconditions.checkState(result.isPresent());
return result.get();
}

@Override
public int getOrder() {
return EncryptOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.constant.MaskOrder;
import org.apache.shardingsphere.mask.rule.attribute.MaskTableMapperRuleAttribute;
import org.apache.shardingsphere.mask.spi.MaskAlgorithm;

Expand Down Expand Up @@ -118,4 +119,9 @@ private MaskTableRuleConfiguration getTableRuleConfiguration(final String tableN
Preconditions.checkState(result.isPresent());
return result.get();
}

@Override
public int getOrder() {
return MaskOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.constant.ReadwriteSplittingDataSourceType;
import org.apache.shardingsphere.readwritesplitting.constant.ReadwriteSplittingOrder;
import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingRuleExceptionIdentifier;
import org.apache.shardingsphere.readwritesplitting.exception.actual.InvalidReadwriteSplittingActualDataSourceInlineExpressionException;
import org.apache.shardingsphere.readwritesplitting.rule.attribute.ReadwriteSplittingDataSourceMapperRuleAttribute;
Expand Down Expand Up @@ -139,4 +140,9 @@ public ReadwriteSplittingDataSourceGroupRule getSingleDataSourceGroupRule() {
public Optional<ReadwriteSplittingDataSourceGroupRule> findDataSourceGroupRule(final String dataSourceName) {
return Optional.ofNullable(dataSourceRuleGroups.get(dataSourceName));
}

@Override
public int getOrder() {
return ReadwriteSplittingOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
import lombok.Getter;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration;
import org.apache.shardingsphere.shadow.constant.ShadowOrder;
import org.apache.shardingsphere.shadow.rule.attribute.ShadowDataSourceMapperRuleAttribute;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.spi.column.ColumnShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.hint.HintShadowAlgorithm;
import org.apache.shardingsphere.shadow.rule.attribute.ShadowDataSourceMapperRuleAttribute;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -242,4 +243,9 @@ public Optional<String> findProductionDataSourceName(final String logicDataSourc
ShadowDataSourceRule dataSourceRule = dataSourceRules.get(logicDataSourceName);
return null == dataSourceRule ? Optional.empty() : Optional.of(dataSourceRule.getProductionDataSource());
}

@Override
public int getOrder() {
return ShadowOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
import org.apache.shardingsphere.sharding.cache.ShardingCache;
import org.apache.shardingsphere.sharding.constant.ShardingOrder;
import org.apache.shardingsphere.sharding.exception.metadata.ShardingTableRuleNotFoundException;
import org.apache.shardingsphere.sharding.rule.attribute.ShardingDataNodeRuleAttribute;
import org.apache.shardingsphere.sharding.rule.attribute.ShardingTableNamesRuleAttribute;
Expand Down Expand Up @@ -634,4 +635,9 @@ private boolean isJoinConditionExpression(final ExpressionSegment expression) {
BinaryOperationExpression binaryExpression = (BinaryOperationExpression) expression;
return binaryExpression.getLeft() instanceof ColumnSegment && binaryExpression.getRight() instanceof ColumnSegment && "=".equals(binaryExpression.getOperator());
}

@Override
public int getOrder() {
return ShardingOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -49,7 +52,7 @@ public Collection<DataNode> getDataNodes(final String tableName) {
if (result.isEmpty()) {
return result;
}
for (ShardingSphereRule each : rules) {
for (ShardingSphereRule each : getOrderedRules()) {
Optional<DataSourceMapperRuleAttribute> dataSourceMapperRuleAttribute = each.getAttributes().findAttribute(DataSourceMapperRuleAttribute.class);
if (dataSourceMapperRuleAttribute.isPresent()) {
result = buildDataNodes(result, dataSourceMapperRuleAttribute.get());
Expand All @@ -72,6 +75,12 @@ private Collection<DataNode> getDataNodesByTableName(final ShardingSphereRule ru
return rule.getAttributes().findAttribute(DataNodeRuleAttribute.class).map(optional -> optional.getDataNodesByTableName(tableName)).orElse(Collections.emptyList());
}

private Collection<ShardingSphereRule> getOrderedRules() {
List<ShardingSphereRule> result = new ArrayList<>(rules);
result.sort(Comparator.comparingInt(ShardingSphereRule::getOrder));
return result;
}

private Collection<DataNode> buildDataNodes(final Collection<DataNode> dataNodes, final DataSourceMapperRuleAttribute dataSourceMapperRuleAttribute) {
Collection<DataNode> result = new LinkedList<>();
Map<String, Collection<String>> dataSourceMapper = dataSourceMapperRuleAttribute.getDataSourceMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public interface ShardingSphereRule {
default RuleAttributes getAttributes() {
return new RuleAttributes();
}

/**
* Get rule order.
*
* @return rule order
*/
int getOrder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public RuleConfiguration getConfiguration() {
public RuleAttributes getAttributes() {
return new RuleAttributes(mock(DataSourceMapperRuleAttribute.class));
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public class RuleMetaDataShardingSphereRuleFixture implements ShardingSphereRule
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public final class FixtureGlobalRule implements GlobalRule {
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ private DataNodeRuleAttribute getDataNodeRuleAttribute() {
when(result.findActualTableByCatalog(any(), any())).thenReturn(Optional.empty());
return result;
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public final class DecoratorRuleFixture implements ShardingSphereRule {
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public final class MergerRuleFixture implements ShardingSphereRule {
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public final class RouteFailureRuleFixture implements ShardingSphereRule {
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public final class RouteRuleFixture implements ShardingSphereRule {
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.Getter;
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.constant.AuthorityOrder;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
Expand Down Expand Up @@ -109,4 +110,9 @@ public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee)
}
return Optional.empty();
}

@Override
public int getOrder() {
return AuthorityOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Getter;
import org.apache.shardingsphere.globalclock.config.GlobalClockRuleConfiguration;
import org.apache.shardingsphere.globalclock.provider.GlobalClockProvider;
import org.apache.shardingsphere.globalclock.rule.constant.GlobalClockOrder;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

Expand Down Expand Up @@ -50,4 +51,9 @@ public GlobalClockRule(final GlobalClockRuleConfiguration ruleConfig) {
public Optional<GlobalClockProvider> getGlobalClockProvider() {
return Optional.ofNullable(globalClockProvider);
}

@Override
public int getOrder() {
return GlobalClockOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.logging.config.LoggingRuleConfiguration;
import org.apache.shardingsphere.logging.constant.LoggingConstants;
import org.apache.shardingsphere.logging.constant.LoggingOrder;
import org.apache.shardingsphere.logging.logger.ShardingSphereLogger;

import java.util.Optional;
Expand All @@ -45,4 +46,9 @@ public LoggingRule(final LoggingRuleConfiguration ruleConfig) {
public Optional<ShardingSphereLogger> getSQLLogger() {
return configuration.getLoggers().stream().filter(each -> LoggingConstants.SQL_LOG_TOPIC.equalsIgnoreCase(each.getLoggerName())).findFirst();
}

@Override
public int getOrder() {
return LoggingOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
import org.apache.shardingsphere.single.constant.SingleOrder;
import org.apache.shardingsphere.single.datanode.SingleTableDataNodeLoader;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;

Expand Down Expand Up @@ -186,4 +187,9 @@ private Collection<QualifiedTable> getQualifiedTables(final ShardingSphereDataba
}
return result;
}

@Override
public int getOrder() {
return SingleOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import org.apache.shardingsphere.sqlfederation.constant.SQLFederationOrder;
import org.apache.shardingsphere.sqlfederation.optimizer.context.OptimizerContext;
import org.apache.shardingsphere.sqlfederation.optimizer.context.OptimizerContextFactory;

Expand Down Expand Up @@ -55,4 +56,9 @@ public void refresh(final Map<String, ShardingSphereDatabase> databases, final G
public OptimizerContext getOptimizerContext() {
return optimizerContext.get();
}

@Override
public int getOrder() {
return SQLFederationOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public final class SQLFederationDeciderRuleMatchFixture implements ShardingSpher
public RuleConfiguration getConfiguration() {
return new SQLFederationRuleConfiguration(true, false, mock(CacheOption.class));
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public final class SQLFederationDeciderRuleNotMatchFixture implements ShardingSp
public RuleConfiguration getConfiguration() {
return new SQLFederationRuleConfiguration(false, false, mock(CacheOption.class));
}

@Override
public int getOrder() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
import org.apache.shardingsphere.parser.constant.SQLParserOrder;
import org.apache.shardingsphere.sql.parser.api.CacheOption;

/**
Expand Down Expand Up @@ -52,4 +53,9 @@ public SQLParserRule(final SQLParserRuleConfiguration ruleConfig) {
public SQLParserEngine getSQLParserEngine(final DatabaseType databaseType) {
return new ShardingSphereSQLParserEngine(databaseType, sqlStatementCache, parseTreeCache);
}

@Override
public int getOrder() {
return SQLParserOrder.ORDER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sqltranslator.config.SQLTranslatorRuleConfiguration;
import org.apache.shardingsphere.sqltranslator.constant.SQLTranslatorOrder;
import org.apache.shardingsphere.sqltranslator.context.SQLTranslatorContext;
import org.apache.shardingsphere.sqltranslator.exception.SQLTranslationException;
import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
Expand Down Expand Up @@ -75,4 +76,9 @@ public SQLTranslatorContext translate(final String sql, final List<Object> param
throw ex;
}
}

@Override
public int getOrder() {
return SQLTranslatorOrder.ORDER;
}
}
Loading