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 ed8429de0bc Enhanced validation of Alter Sharding Binding Table Rules.
(#21167)
ed8429de0bc is described below
commit ed8429de0bc21f450e7d9b94190d11b4d2a43d96
Author: yx9o <[email protected]>
AuthorDate: Sun Sep 25 11:26:40 2022 +0800
Enhanced validation of Alter Sharding Binding Table Rules. (#21167)
---
.../checker/ShardingTableRuleStatementChecker.java | 46 ++++++++++++++++------
...rShardingBindingTableRulesStatementUpdater.java | 8 ++--
...rdingBindingTableRulesStatementUpdaterTest.java | 25 ++++++++----
3 files changed, 55 insertions(+), 24 deletions(-)
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 667650a4014..25cee8de7fb 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
@@ -107,6 +107,21 @@ public final class ShardingTableRuleStatementChecker {
check(database, rules, currentRuleConfig, false);
}
+ /**
+ * Check binding table configuration.
+ *
+ * @param bindingTableGroups binding table groups
+ * @param currentRuleConfig current rule configuration
+ */
+ public static void checkBindingTableConfiguration(final Collection<String>
bindingTableGroups, final ShardingRuleConfiguration currentRuleConfig) {
+ ShardingRuleConfiguration toBeCheckedRuleConfig =
createToBeCheckedShardingRuleConfiguration(currentRuleConfig);
+ toBeCheckedRuleConfig.setBindingTableGroups(bindingTableGroups);
+ Collection<String> dataSourceNames =
getRequiredResource(toBeCheckedRuleConfig);
+ dataSourceNames.addAll(getRequiredResource(currentRuleConfig));
+ ShardingSpherePreconditions.checkState(check(toBeCheckedRuleConfig,
dataSourceNames),
+ () -> new InvalidRuleConfigurationException("sharding table",
bindingTableGroups, Collections.singleton("invalid binding table
configuration")));
+ }
+
private static void check(final ShardingSphereDatabase database,
final Collection<AbstractTableRuleSegment>
rules, final ShardingRuleConfiguration currentRuleConfig, final boolean
isCreate) {
String databaseName = database.getName();
@@ -334,18 +349,7 @@ public final class ShardingTableRuleStatementChecker {
if (toBeAlteredBindingTableNames.isEmpty()) {
return;
}
- ShardingRuleConfiguration toBeCheckedRuleConfig = new
ShardingRuleConfiguration();
- toBeCheckedRuleConfig.setTables(new
LinkedList<>(currentRuleConfig.getTables()));
- toBeCheckedRuleConfig.setAutoTables(new
LinkedList<>(currentRuleConfig.getAutoTables()));
- toBeCheckedRuleConfig.setBindingTableGroups(new
LinkedList<>(currentRuleConfig.getBindingTableGroups()));
- toBeCheckedRuleConfig.setBroadcastTables(new
LinkedList<>(currentRuleConfig.getBroadcastTables()));
-
toBeCheckedRuleConfig.setDefaultTableShardingStrategy(currentRuleConfig.getDefaultTableShardingStrategy());
-
toBeCheckedRuleConfig.setDefaultDatabaseShardingStrategy(currentRuleConfig.getDefaultDatabaseShardingStrategy());
-
toBeCheckedRuleConfig.setDefaultKeyGenerateStrategy(currentRuleConfig.getDefaultKeyGenerateStrategy());
-
toBeCheckedRuleConfig.setDefaultShardingColumn(currentRuleConfig.getDefaultShardingColumn());
- toBeCheckedRuleConfig.setShardingAlgorithms(new
LinkedHashMap<>(currentRuleConfig.getShardingAlgorithms()));
- toBeCheckedRuleConfig.setKeyGenerators(new
LinkedHashMap<>(currentRuleConfig.getKeyGenerators()));
- toBeCheckedRuleConfig.setAuditors(new
LinkedHashMap<>(currentRuleConfig.getAuditors()));
+ ShardingRuleConfiguration toBeCheckedRuleConfig =
createToBeCheckedShardingRuleConfiguration(currentRuleConfig);
removeRuleConfiguration(toBeCheckedRuleConfig, toBeAlteredRuleConfig);
addRuleConfiguration(toBeCheckedRuleConfig, toBeAlteredRuleConfig);
Collection<String> dataSourceNames =
getRequiredResource(toBeCheckedRuleConfig);
@@ -354,6 +358,22 @@ public final class ShardingTableRuleStatementChecker {
() -> new InvalidRuleConfigurationException("sharding table",
toBeAlteredLogicTableNames, Collections.singleton("invalid binding table
configuration")));
}
+ private static ShardingRuleConfiguration
createToBeCheckedShardingRuleConfiguration(final ShardingRuleConfiguration
currentRuleConfig) {
+ ShardingRuleConfiguration result = new ShardingRuleConfiguration();
+ result.setTables(new LinkedList<>(currentRuleConfig.getTables()));
+ result.setAutoTables(new
LinkedList<>(currentRuleConfig.getAutoTables()));
+ result.setBindingTableGroups(new
LinkedList<>(currentRuleConfig.getBindingTableGroups()));
+ result.setBroadcastTables(new
LinkedList<>(currentRuleConfig.getBroadcastTables()));
+
result.setDefaultTableShardingStrategy(currentRuleConfig.getDefaultTableShardingStrategy());
+
result.setDefaultDatabaseShardingStrategy(currentRuleConfig.getDefaultDatabaseShardingStrategy());
+
result.setDefaultKeyGenerateStrategy(currentRuleConfig.getDefaultKeyGenerateStrategy());
+
result.setDefaultShardingColumn(currentRuleConfig.getDefaultShardingColumn());
+ result.setShardingAlgorithms(new
LinkedHashMap<>(currentRuleConfig.getShardingAlgorithms()));
+ result.setKeyGenerators(new
LinkedHashMap<>(currentRuleConfig.getKeyGenerators()));
+ result.setAuditors(new
LinkedHashMap<>(currentRuleConfig.getAuditors()));
+ return result;
+ }
+
private static Collection<String> getRequiredResource(final
ShardingRuleConfiguration config) {
Collection<String> result = new LinkedHashSet<>();
result.addAll(config.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getActualDataSources)
@@ -367,7 +387,7 @@ public final class ShardingTableRuleStatementChecker {
for (String each : checkedConfig.getBindingTableGroups()) {
Collection<String> bindingTables =
Splitter.on(",").trimResults().splitToList(each.toLowerCase());
if (bindingTables.size() <= 1) {
- continue;
+ return false;
}
Iterator<String> iterator = bindingTables.iterator();
TableRule sampleTableRule = getTableRule(iterator.next(),
checkedConfig.getDataSourceNames(), tableRules,
checkedConfig.getBroadcastTables());
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingBindingTableRulesStatementUpdater.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingBindingTableRulesStatementUpd
[...]
index 1d18b897583..8b99f4c9602 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingBindingTableRulesStatementUpdater.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingBindingTableRulesStatementUpdater.java
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePrecondition
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import
org.apache.shardingsphere.sharding.distsql.handler.checker.ShardingTableRuleStatementChecker;
import
org.apache.shardingsphere.sharding.distsql.parser.segment.BindingTableRuleSegment;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.AlterShardingBindingTableRulesStatement;
@@ -35,7 +36,7 @@ import java.util.HashSet;
import java.util.stream.Collectors;
/**
- * Alter sharding binding table rule statement updater.
+ * Alter sharding binding table rules statement updater.
*/
public final class AlterShardingBindingTableRulesStatementUpdater implements
RuleDefinitionAlterUpdater<AlterShardingBindingTableRulesStatement,
ShardingRuleConfiguration> {
@@ -46,6 +47,7 @@ public final class
AlterShardingBindingTableRulesStatementUpdater implements Rul
checkCurrentRuleConfiguration(databaseName, currentRuleConfig);
checkToBeAlertedBindingTables(databaseName, sqlStatement,
currentRuleConfig);
checkToBeAlteredDuplicateBindingTables(databaseName, sqlStatement);
+
ShardingTableRuleStatementChecker.checkBindingTableConfiguration(((ShardingRuleConfiguration)
buildToBeAlteredRuleConfiguration(sqlStatement)).getBindingTableGroups(),
currentRuleConfig);
}
private void checkCurrentRuleConfiguration(final String databaseName,
final ShardingRuleConfiguration currentRuleConfig) throws
MissingRequiredRuleException {
@@ -53,10 +55,10 @@ public final class
AlterShardingBindingTableRulesStatementUpdater implements Rul
}
private void checkToBeAlertedBindingTables(final String databaseName,
final AlterShardingBindingTableRulesStatement sqlStatement,
- final ShardingRuleConfiguration
currentRuleConfig) throws DuplicateRuleException {
+ final ShardingRuleConfiguration
currentRuleConfig) throws MissingRequiredRuleException {
Collection<String> currentLogicTables =
getCurrentLogicTables(currentRuleConfig);
Collection<String> notExistedBindingTables =
sqlStatement.getBindingTables().stream().filter(each ->
!containsIgnoreCase(currentLogicTables, each)).collect(Collectors.toSet());
-
ShardingSpherePreconditions.checkState(notExistedBindingTables.isEmpty(), () ->
new DuplicateRuleException("binding", databaseName, notExistedBindingTables));
+
ShardingSpherePreconditions.checkState(notExistedBindingTables.isEmpty(), () ->
new MissingRequiredRuleException("Sharding", databaseName,
notExistedBindingTables));
}
private Collection<String> getCurrentLogicTables(final
ShardingRuleConfiguration currentRuleConfig) {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingBindingTableRulesStatementUpdaterTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingBindingTableRulesStatementUpdaterTest.java
index 88e7366a9b5..f18111d37f5 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingBindingTableRulesStatementUpdaterTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingBindingTableRulesStatementUpdaterTest.java
@@ -17,9 +17,10 @@
package org.apache.shardingsphere.sharding.distsql.update;
+import
org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateRuleException;
+import
org.apache.shardingsphere.infra.distsql.exception.rule.InvalidRuleConfigurationException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.MissingRequiredRuleException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViolationException;
-import
org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateRuleException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
@@ -46,25 +47,33 @@ public final class
AlterShardingBindingTableRulesStatementUpdaterTest {
@Test(expected = MissingRequiredRuleException.class)
public void assertCheckSQLStatementWithoutCurrentRule() throws
RuleDefinitionViolationException {
- updater.checkSQLStatement(database, createSQLStatement(), null);
+ updater.checkSQLStatement(database,
createSQLStatement(Arrays.asList(new
BindingTableRuleSegment("t_order,t_order_item"))), null);
+ }
+
+ @Test(expected = MissingRequiredRuleException.class)
+ public void assertCheckSQLStatementWithNotExistedTables() throws
RuleDefinitionViolationException {
+ updater.checkSQLStatement(database,
createSQLStatement(Arrays.asList(new BindingTableRuleSegment("t_3,t_4"))),
createCurrentRuleConfiguration());
+ }
+
+ @Test(expected = InvalidRuleConfigurationException.class)
+ public void assertCheckSQLStatementWithOneTable() throws
RuleDefinitionViolationException {
+ updater.checkSQLStatement(database,
createSQLStatement(Arrays.asList(new BindingTableRuleSegment("t_order"))),
createCurrentRuleConfiguration());
}
@Test(expected = DuplicateRuleException.class)
public void assertCheckSQLStatementWithDuplicateTables() throws
RuleDefinitionViolationException {
List<BindingTableRuleSegment> segments = Arrays.asList(new
BindingTableRuleSegment("t_order,t_order_item"), new
BindingTableRuleSegment("t_order,t_order_item"));
- AlterShardingBindingTableRulesStatement statement = new
AlterShardingBindingTableRulesStatement(segments);
- updater.checkSQLStatement(database, statement,
createCurrentRuleConfiguration());
+ updater.checkSQLStatement(database, createSQLStatement(segments),
createCurrentRuleConfiguration());
}
@Test(expected = DuplicateRuleException.class)
public void assertCheckSQLStatementWithDifferentCaseDuplicateTables()
throws RuleDefinitionViolationException {
List<BindingTableRuleSegment> segments = Arrays.asList(new
BindingTableRuleSegment("T_ORDER,T_ORDER_ITEM"), new
BindingTableRuleSegment("t_order,t_order_item"));
- AlterShardingBindingTableRulesStatement statement = new
AlterShardingBindingTableRulesStatement(segments);
- updater.checkSQLStatement(database, statement,
createCurrentRuleConfiguration());
+ updater.checkSQLStatement(database, createSQLStatement(segments),
createCurrentRuleConfiguration());
}
- private AlterShardingBindingTableRulesStatement createSQLStatement() {
- return new AlterShardingBindingTableRulesStatement(Arrays.asList(new
BindingTableRuleSegment("t_order,t_order_item"), new
BindingTableRuleSegment("t_1,t_2")));
+ private AlterShardingBindingTableRulesStatement createSQLStatement(final
List<BindingTableRuleSegment> segments) {
+ return new AlterShardingBindingTableRulesStatement(segments);
}
private ShardingRuleConfiguration createCurrentRuleConfiguration() {