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 0fb4a76a4ff Refactor check checkDuplicateRuleNames of
ReadwriteSplittingRuleStatementChecker (#25114)
0fb4a76a4ff is described below
commit 0fb4a76a4ff722eb9671e4570d40c8e3a77caa25
Author: ChenJiaHao <[email protected]>
AuthorDate: Wed Apr 12 10:10:45 2023 +0800
Refactor check checkDuplicateRuleNames of
ReadwriteSplittingRuleStatementChecker (#25114)
---
.../ReadwriteSplittingRuleStatementChecker.java | 38 +++++++++-------------
...ReadwriteSplittingRuleStatementUpdaterTest.java | 11 ++++---
...ReadwriteSplittingRuleStatementUpdaterTest.java | 28 +++++++++++-----
.../exportable/constant/ExportableConstants.java | 2 --
4 files changed, 42 insertions(+), 37 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 3c4f3846e3a..bfac8fe1d1d 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
@@ -24,9 +24,7 @@ import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredR
import
org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
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.exportable.ExportableRule;
-import
org.apache.shardingsphere.infra.rule.identifier.type.exportable.RuleExportEngine;
-import
org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableConstants;
+import
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
@@ -41,7 +39,6 @@ import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
-import java.util.Optional;
import java.util.stream.Collectors;
/**
@@ -59,8 +56,8 @@ public final class ReadwriteSplittingRuleStatementChecker {
*/
public static void checkCreation(final ShardingSphereDatabase database,
final Collection<ReadwriteSplittingRuleSegment> segments,
final ReadwriteSplittingRuleConfiguration
currentRuleConfig, final boolean ifNotExists) {
+ checkDuplicateRuleNames(database, segments, currentRuleConfig,
ifNotExists);
String databaseName = database.getName();
- checkDuplicateRuleNames(databaseName, segments, currentRuleConfig,
database.getResourceMetaData(), ifNotExists);
checkDataSourcesExist(databaseName, segments, database);
checkDuplicatedDataSourceNames(databaseName, segments,
currentRuleConfig, true);
checkLoadBalancers(segments);
@@ -100,12 +97,12 @@ public final class ReadwriteSplittingRuleStatementChecker {
ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(),
() -> new MissingRequiredRuleException(databaseName, notExistedRuleNames));
}
- private static void checkDuplicateRuleNames(final String databaseName,
final Collection<ReadwriteSplittingRuleSegment> segments, final
ReadwriteSplittingRuleConfiguration currentRuleConfig,
- final
ShardingSphereResourceMetaData resourceMetaData, final boolean ifNotExists) {
- checkDuplicateRuleNamesWithSelf(databaseName, segments);
- checkDuplicateRuleNamesWithResourceMetaData(resourceMetaData,
segments);
+ private static void checkDuplicateRuleNames(final ShardingSphereDatabase
database,
+ final
Collection<ReadwriteSplittingRuleSegment> segments, final
ReadwriteSplittingRuleConfiguration currentRuleConfig, final boolean
ifNotExists) {
+ checkDuplicateRuleNamesWithSelf(database.getName(), segments);
+ checkDuplicateRuleNamesWithExistsDataSources(database, segments);
if (!ifNotExists) {
- checkDuplicateRuleNamesWithRuleConfiguration(databaseName,
currentRuleConfig, segments);
+ checkDuplicateRuleNamesWithRuleConfiguration(database.getName(),
currentRuleConfig, segments);
}
}
@@ -119,11 +116,16 @@ public final class ReadwriteSplittingRuleStatementChecker
{
.filter(each -> each.getValue() >
1).map(Map.Entry::getKey).collect(Collectors.toSet());
}
- private static void checkDuplicateRuleNamesWithResourceMetaData(final
ShardingSphereResourceMetaData resourceMetaData, final
Collection<ReadwriteSplittingRuleSegment> segments) {
- Collection<String> currentRuleNames = new LinkedList<>();
+ private static void checkDuplicateRuleNamesWithExistsDataSources(final
ShardingSphereDatabase database, final
Collection<ReadwriteSplittingRuleSegment> segments) {
+ Collection<String> currentRuleNames = new HashSet<>();
+ ShardingSphereResourceMetaData resourceMetaData =
database.getResourceMetaData();
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));
+ }
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))));
@@ -157,17 +159,9 @@ public final class ReadwriteSplittingRuleStatementChecker {
ShardingSpherePreconditions.checkState(notExistedLogicalDataSources.isEmpty(),
() -> new MissingRequiredStorageUnitsException(databaseName,
notExistedLogicalDataSources));
}
- @SuppressWarnings("unchecked")
private static Collection<String> getLogicDataSources(final
ShardingSphereDatabase database) {
- Collection<String> result = new LinkedHashSet<>();
- Optional<ExportableRule> exportableRule =
database.getRuleMetaData().findRules(ExportableRule.class).stream()
- .filter(each -> new
RuleExportEngine(each).containExportableKey(Collections.singletonList(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES))).findAny();
- exportableRule.ifPresent(optional -> {
- Map<String, Object> exportData = new
RuleExportEngine(optional).export(Collections.singletonList(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES));
- Collection<String> logicalDataSources = ((Map<String, String>)
exportData.getOrDefault(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES,
Collections.emptyMap())).keySet();
- result.addAll(logicalDataSources);
- });
- return result;
+ return
database.getRuleMetaData().findRules(DataSourceContainedRule.class).stream()
+ .map(each ->
each.getDataSourceMapper().keySet()).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedHashSet::new));
}
private static void checkDuplicatedDataSourceNames(final String
databaseName, final Collection<ReadwriteSplittingRuleSegment> segments,
diff --git
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java
index 730c0c1fc62..c9d5a803055 100644
---
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java
+++
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java
@@ -21,10 +21,11 @@ import
org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfi
import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
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.exportable.ExportableRule;
-import
org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableConstants;
+import
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import
org.apache.shardingsphere.infra.util.spi.exception.ServiceProviderNotFoundServerException;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
@@ -85,9 +86,9 @@ class AlterReadwriteSplittingRuleStatementUpdaterTest {
@Test
void assertCheckSQLStatementWithoutExistedAutoAwareResources() {
- ExportableRule exportableRule = mock(ExportableRule.class);
-
when(exportableRule.getExportData()).thenReturn(Collections.singletonMap(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES,
Collections.singletonMap("ms_group", "ds_0")));
-
when(database.getRuleMetaData().findRules(ExportableRule.class)).thenReturn(Collections.singleton(exportableRule));
+ DataSourceContainedRule dataSourceContainedRule =
mock(DataSourceContainedRule.class);
+
when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("ms_group",
Collections.singleton(new DataSourceRoleInfo("ds_0",
DataSourceRole.PRIMARY))));
+
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule));
ReadwriteSplittingRuleSegment ruleSegment = new
ReadwriteSplittingRuleSegment("readwrite_ds", "ha_group", new
AlgorithmSegment("TEST", new Properties()));
assertThrows(MissingRequiredStorageUnitsException.class,
() -> updater.checkSQLStatement(database, new
AlterReadwriteSplittingRuleStatement(Collections.singleton(ruleSegment)),
createCurrentRuleConfiguration()));
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 05bc86f46ff..34d4d6e2f85 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
@@ -21,10 +21,11 @@ import
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleExc
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.exportable.ExportableRule;
-import
org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableConstants;
+import
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import
org.apache.shardingsphere.infra.util.spi.exception.ServiceProviderNotFoundServerException;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
@@ -91,11 +92,21 @@ class CreateReadwriteSplittingRuleStatementUpdaterTest {
assertThrows(MissingRequiredStorageUnitsException.class, () ->
updater.checkSQLStatement(database, createSQLStatement("TEST"), null));
}
+ @Test
+ void assertCheckSQLStatementWithDuplicateLogicResource() {
+ 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));
+ ReadwriteSplittingRuleSegment ruleSegment = new
ReadwriteSplittingRuleSegment("duplicate_ds", "write_ds_0",
Arrays.asList("read_ds_0", "read_ds_1"),
+ new AlgorithmSegment(null, new Properties()));
+ assertThrows(InvalidRuleConfigurationException.class, () ->
updater.checkSQLStatement(database, createSQLStatement(false, ruleSegment),
null));
+ }
+
@Test
void assertCheckSQLStatementWithoutExistedAutoAwareResources() {
- ExportableRule exportableRule = mock(ExportableRule.class);
-
when(exportableRule.getExportData()).thenReturn(Collections.singletonMap(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES,
Collections.singletonMap("ms_group", "ds_0")));
-
when(database.getRuleMetaData().findRules(ExportableRule.class)).thenReturn(Collections.singleton(exportableRule));
+ DataSourceContainedRule dataSourceContainedRule =
mock(DataSourceContainedRule.class);
+
when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("ms_group",
Collections.singleton(new DataSourceRoleInfo("ds_0",
DataSourceRole.PRIMARY))));
+
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule));
ReadwriteSplittingRuleSegment ruleSegment = new
ReadwriteSplittingRuleSegment("dynamic_rule", "ha_group", new
AlgorithmSegment("TEST", new Properties()));
assertThrows(MissingRequiredStorageUnitsException.class, () ->
updater.checkSQLStatement(database, createSQLStatement(false, ruleSegment),
null));
}
@@ -139,9 +150,10 @@ class CreateReadwriteSplittingRuleStatementUpdaterTest {
@Test
void assertUpdateSuccess() {
- ExportableRule exportableRule = mock(ExportableRule.class);
-
when(exportableRule.getExportData()).thenReturn(Collections.singletonMap(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES,
Collections.singletonMap("ms_group", "ds_0")));
-
when(database.getRuleMetaData().findRules(ExportableRule.class)).thenReturn(Collections.singleton(exportableRule));
+ DataSourceContainedRule dataSourceContainedRule =
mock(DataSourceContainedRule.class);
+
when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("ms_group",
Collections.singleton(new DataSourceRoleInfo("ds_0",
DataSourceRole.PRIMARY))));
+
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule));
+
when(TypedSPILoader.contains(ReadQueryLoadBalanceAlgorithm.class,
"TEST")).thenReturn(true);
ReadwriteSplittingRuleSegment staticSegment = new
ReadwriteSplittingRuleSegment("static_rule", "write_ds_0",
Arrays.asList("read_ds_0", "read_ds_1"),
new AlgorithmSegment("TEST", new Properties()));
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/identifier/type/exportable/constant/ExportableConstants.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/identifier/type/exportable/constant/ExportableConstants.java
index dfac1c1a54e..a52728bf888 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/identifier/type/exportable/constant/ExportableConstants.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/identifier/type/exportable/constant/ExportableConstants.java
@@ -28,8 +28,6 @@ public final class ExportableConstants {
public static final String EXPORT_SINGLE_TABLES = "single_tables";
- public static final String EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES =
"primary_data_source";
-
public static final String EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE =
"dynamic_readwrite_splitting_rules";
public static final String EXPORT_STATIC_READWRITE_SPLITTING_RULE =
"static_readwrite_splitting_rules";