You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/05/23 02:11:43 UTC
[shardingsphere] branch master updated: Add binding table rule check. (#17810)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 728feb878d1 Add binding table rule check. (#17810)
728feb878d1 is described below
commit 728feb878d1fc116ff10351a003e152e4f0efa74
Author: yx9o <ya...@163.com>
AuthorDate: Mon May 23 10:11:37 2022 +0800
Add binding table rule check. (#17810)
* Add binding table rule check.
* Update.
* Update.
* Update.
---
.../checker/ShardingTableRuleStatementChecker.java | 59 ++++++++++++++++++++++
.../AlterShardingTableRuleStatementUpdater.java | 1 +
2 files changed, 60 insertions(+)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
index da12bc6d5cf..eafe1232f6c 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
@@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
import org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateRuleException;
import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidRuleConfigurationException;
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredAlgorithmMissedException;
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
import org.apache.shardingsphere.infra.expr.InlineExpressionParser;
@@ -43,6 +44,7 @@ import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
@@ -233,4 +235,61 @@ public final class ShardingTableRuleStatementChecker {
private static Map<String, List<AbstractTableRuleSegment>> groupingByClassType(final Collection<AbstractTableRuleSegment> rules) {
return rules.stream().collect(Collectors.groupingBy(each -> each.getClass().getSimpleName()));
}
+
+ /**
+ * Check binding table rules.
+ *
+ * @param currentRuleConfig current rule configuration
+ * @param toBeAlteredRuleConfig to be altered rule configuration
+ * @throws DistSQLException definition violation exception
+ */
+ public static void checkBindingTableRules(final ShardingRuleConfiguration currentRuleConfig, final ShardingRuleConfiguration toBeAlteredRuleConfig) throws DistSQLException {
+ if (null == currentRuleConfig || currentRuleConfig.getBindingTableGroups().isEmpty()) {
+ return;
+ }
+ Collection<String> bindingTables = getCurrentBindingTables(currentRuleConfig);
+ if (bindingTables.size() <= 1) {
+ return;
+ }
+ Collection<String> toBeAlteredLogicTableNames = getAlteredLogicalTableNames(toBeAlteredRuleConfig);
+ Collection<String> toBeAlteredBindingTableNames = toBeAlteredLogicTableNames.stream().filter(each -> bindingTables.contains(each)).collect(Collectors.toSet());
+ if (toBeAlteredBindingTableNames.isEmpty()) {
+ return;
+ }
+ Map<String, String> currentBindingTableType = getBindingTablesType(bindingTables, currentRuleConfig);
+ Map<String, String> toBeAlteredBindingTableType = getBindingTablesType(toBeAlteredBindingTableNames, toBeAlteredRuleConfig);
+ Collection<String> invalidToBeAlteredBindingTableNames = toBeAlteredBindingTableNames.stream()
+ .filter(each -> !currentBindingTableType.get(each).equalsIgnoreCase(toBeAlteredBindingTableType.get(each))).collect(Collectors.toSet());
+ DistSQLException.predictionThrow(invalidToBeAlteredBindingTableNames.isEmpty(),
+ () -> new InvalidRuleConfigurationException("sharding table", invalidToBeAlteredBindingTableNames, Collections.singleton("invalid binding table configuration")));
+ // TODO check actualDataNodes
+ // TODO check algorithm-expression
+ }
+
+ private static Collection<String> getCurrentBindingTables(final ShardingRuleConfiguration currentRuleConfig) {
+ Collection<String> result = new LinkedHashSet<>();
+ currentRuleConfig.getBindingTableGroups().forEach(each -> result.addAll(Splitter.on(",").trimResults().splitToList(each)));
+ return result;
+ }
+
+ private static Collection<String> getAlteredLogicalTableNames(final ShardingRuleConfiguration toBeAlteredRuleConfig) {
+ Collection<String> result = toBeAlteredRuleConfig.getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toList());
+ result.addAll(toBeAlteredRuleConfig.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toList()));
+ return result;
+ }
+
+ private static Map<String, String> getBindingTablesType(final Collection<String> toBeAlteredBindingTableNames, final ShardingRuleConfiguration currentRuleConfig) {
+ Map<String, String> result = new HashMap<>(toBeAlteredBindingTableNames.size(), 1);
+ currentRuleConfig.getTables().forEach(each -> {
+ if (toBeAlteredBindingTableNames.contains(each.getLogicTable())) {
+ result.put(each.getLogicTable(), "tables");
+ }
+ });
+ currentRuleConfig.getAutoTables().forEach(each -> {
+ if (toBeAlteredBindingTableNames.contains(each.getLogicTable())) {
+ result.put(each.getLogicTable(), "autoTables");
+ }
+ });
+ return result;
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleStatementUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleStatementUpdater.java
index 0dfae6adca9..772ab6f1935 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleStatementUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleStatementUpdater.java
@@ -41,6 +41,7 @@ public final class AlterShardingTableRuleStatementUpdater implements RuleDefinit
final AlterShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) throws DistSQLException {
DistSQLException.predictionThrow(null != currentRuleConfig, () -> new RequiredRuleMissedException("Sharding", database.getName()));
ShardingTableRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), currentRuleConfig);
+ ShardingTableRuleStatementChecker.checkBindingTableRules(currentRuleConfig, buildToBeAlteredRuleConfiguration(sqlStatement));
}
@Override