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 2023/04/12 10:11:13 UTC
[shardingsphere] branch master updated: Add checkDuplicatedWithLogicalDataSource for ShadowRuleStatementChecker (#25132)
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 ae77c2a39f5 Add checkDuplicatedWithLogicalDataSource for ShadowRuleStatementChecker (#25132)
ae77c2a39f5 is described below
commit ae77c2a39f56c5907338b4db4e2464d996e9686c
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Wed Apr 12 18:11:01 2023 +0800
Add checkDuplicatedWithLogicalDataSource for ShadowRuleStatementChecker (#25132)
* Add checkDuplicatedWithLogicalDataSource for ShadowRuleStatementChecker
* Refactor checkDuplicatedWithLogicDataSource
* Optimize code
---
.../ReadwriteSplittingRuleStatementChecker.java | 5 +----
...ReadwriteSplittingRuleStatementUpdaterTest.java | 1 +
.../checker/ShadowRuleStatementChecker.java | 24 ++++++++++++++++++++++
.../update/CreateShadowRuleStatementUpdater.java | 9 ++++----
.../CreateShadowRuleStatementUpdaterTest.java | 15 ++++++++++++++
5 files changed, 46 insertions(+), 8 deletions(-)
diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java
index bfac8fe1d1d..ad830fe3196 100644
--- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java
+++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java
@@ -122,10 +122,7 @@ public final class ReadwriteSplittingRuleStatementChecker {
if (null != resourceMetaData && null != resourceMetaData.getDataSources()) {
currentRuleNames.addAll(resourceMetaData.getDataSources().keySet());
}
- Collection<String> logicDataSources = getLogicDataSources(database);
- if (null != logicDataSources && !logicDataSources.isEmpty()) {
- currentRuleNames.addAll(getLogicDataSources(database));
- }
+ currentRuleNames.addAll(getLogicDataSources(database));
Collection<String> toBeCreatedRuleNames = segments.stream().map(ReadwriteSplittingRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toList());
ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new InvalidRuleConfigurationException("Readwrite-splitting", toBeCreatedRuleNames,
Collections.singleton(String.format("%s already exists in storage unit", toBeCreatedRuleNames))));
diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
index 34d4d6e2f85..074c75d6da4 100644
--- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
+++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java
@@ -72,6 +72,7 @@ class CreateReadwriteSplittingRuleStatementUpdaterTest {
@BeforeEach
void before() {
when(database.getResourceMetaData()).thenReturn(resourceMetaData);
+ when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.emptyList());
}
@Test
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/checker/ShadowRuleStatementChecker.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/checker/ShadowRuleStatementChecker.java
index d5ee95a261c..dca900e54e1 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/checker/ShadowRuleStatementChecker.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/checker/ShadowRuleStatementChecker.java
@@ -18,13 +18,17 @@
package org.apache.shardingsphere.shadow.distsql.handler.checker;
import org.apache.shardingsphere.distsql.handler.exception.DistSQLException;
+import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -102,4 +106,24 @@ public class ShadowRuleStatementChecker {
private static Collection<String> getNotExisted(final Collection<String> required, final Collection<String> current) {
return required.stream().filter(each -> !current.contains(each)).collect(Collectors.toSet());
}
+
+ /**
+ * Check if there are duplicated names with logical data sources.
+ *
+ * @param toBeCreatedRuleNames rule names
+ * @param database ShardingSphere database
+ */
+ public static void checkDuplicatedWithLogicDataSource(final Collection<String> toBeCreatedRuleNames, final ShardingSphereDatabase database) {
+ Collection<String> logicDataSources = getLogicDataSources(database);
+ if (!logicDataSources.isEmpty()) {
+ Collection<String> duplicatedNames = toBeCreatedRuleNames.stream().filter(logicDataSources::contains).collect(Collectors.toList());
+ ShardingSpherePreconditions.checkState(duplicatedNames.isEmpty(), () -> new InvalidRuleConfigurationException("shadow", duplicatedNames,
+ Collections.singleton(String.format("%s already exists in storage unit", duplicatedNames))));
+ }
+ }
+
+ private static Collection<String> getLogicDataSources(final ShardingSphereDatabase database) {
+ return database.getRuleMetaData().findRules(DataSourceContainedRule.class).stream()
+ .map(each -> each.getDataSourceMapper().keySet()).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedHashSet::new));
+ }
}
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java
index b07c1f145e7..e5754eb24b8 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java
@@ -43,8 +43,8 @@ public final class CreateShadowRuleStatementUpdater implements RuleDefinitionCre
@Override
public void checkSQLStatement(final ShardingSphereDatabase database, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
+ checkDuplicatedRules(database, sqlStatement, currentRuleConfig);
String databaseName = database.getName();
- checkDuplicatedRules(databaseName, sqlStatement, currentRuleConfig);
checkStorageUnits(database, sqlStatement.getRules());
checkAlgorithms(databaseName, sqlStatement.getRules());
checkAlgorithmType(sqlStatement.getRules());
@@ -72,12 +72,13 @@ public final class CreateShadowRuleStatementUpdater implements RuleDefinitionCre
toBeCreateTables.forEach((key, value) -> currentTables.merge(key, value, ShadowRuleStatementSupporter::mergeConfiguration));
}
- private void checkDuplicatedRules(final String databaseName, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
+ private void checkDuplicatedRules(final ShardingSphereDatabase database, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
Collection<String> toBeCreatedRuleNames = ShadowRuleStatementSupporter.getRuleNames(sqlStatement.getRules());
- ShadowRuleStatementChecker.checkDuplicated(toBeCreatedRuleNames, duplicated -> new DuplicateRuleException("shadow", databaseName, duplicated));
+ ShadowRuleStatementChecker.checkDuplicated(toBeCreatedRuleNames, duplicated -> new DuplicateRuleException("shadow", database.getName(), duplicated));
+ ShadowRuleStatementChecker.checkDuplicatedWithLogicDataSource(toBeCreatedRuleNames, database);
if (!sqlStatement.isIfNotExists()) {
toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig));
- ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException("shadow", databaseName, toBeCreatedRuleNames));
+ ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException("shadow", database.getName(), toBeCreatedRuleNames));
}
}
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleStatementUpdaterTest.java
index 3958d00868c..ce51c0ee553 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleStatementUpdaterTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleStatementUpdaterTest.java
@@ -18,10 +18,14 @@
package org.apache.shardingsphere.shadow.distsql.update;
import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
+import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
+import org.apache.shardingsphere.infra.datasource.mapper.DataSourceRole;
+import org.apache.shardingsphere.infra.datasource.mapper.DataSourceRoleInfo;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResourceMetaData;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.util.spi.exception.ServiceProviderNotFoundServerException;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
@@ -45,6 +49,7 @@ import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -67,6 +72,7 @@ class CreateShadowRuleStatementUpdaterTest {
when(database.getResourceMetaData()).thenReturn(resourceMetaData);
when(database.getName()).thenReturn("shadow_db");
when(currentConfig.getDataSources()).thenReturn(Collections.singleton(new ShadowDataSourceConfiguration("initRuleName", "initDs0", "initDs0Shadow")));
+ when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.emptyList());
}
@Test
@@ -82,6 +88,15 @@ class CreateShadowRuleStatementUpdaterTest {
assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(database, new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)), currentConfig));
}
+ @Test
+ void assertExecuteWithDuplicateLogicResource() {
+ DataSourceContainedRule dataSourceContainedRule = mock(DataSourceContainedRule.class);
+ when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("duplicate_ds", Collections.singleton(new DataSourceRoleInfo("ds_0", DataSourceRole.PRIMARY))));
+ when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule));
+ ShadowRuleSegment ruleSegment = new ShadowRuleSegment("duplicate_ds", null, null, null);
+ assertThrows(InvalidRuleConfigurationException.class, () -> updater.checkSQLStatement(database, new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)), null));
+ }
+
@Test
void assertExecuteWithNotExistResource() {
when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(Arrays.asList("ds0", "ds1"));