You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/08/01 13:14:42 UTC
[shardingsphere] branch master updated: Remove unnecessary check of shadow rule configuration (#19749)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 779d4de2f19 Remove unnecessary check of shadow rule configuration (#19749)
779d4de2f19 is described below
commit 779d4de2f19ff1a313d16581466ef19e2ade573d
Author: gin <ja...@163.com>
AuthorDate: Mon Aug 1 21:14:35 2022 +0800
Remove unnecessary check of shadow rule configuration (#19749)
* Remove unnecessary check of shadow rule configuration
* Add init capacity for map
---
.../AbstractShadowRuleConfigurationChecker.java | 22 ++++++++--------------
...ithmProvidedShadowRuleConfigurationChecker.java | 10 +++++-----
.../checker/ShadowRuleConfigurationChecker.java | 10 +++++-----
...ProvidedShadowRuleConfigurationCheckerTest.java | 14 +++++++++++++-
.../ShadowRuleConfigurationCheckerTest.java | 12 +++++++++++-
.../spring/boot/SpringBootStarterTest.java | 4 ++--
.../src/test/resources/application.properties | 4 ++--
7 files changed, 46 insertions(+), 30 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AbstractShadowRuleConfigurationChecker.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AbstractShadowRuleConfigurationChecker.java
index 3394a16cd9d..ffc6c2e4d7e 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AbstractShadowRuleConfigurationChecker.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AbstractShadowRuleConfigurationChecker.java
@@ -42,26 +42,20 @@ public abstract class AbstractShadowRuleConfigurationChecker<T extends RuleConfi
@Override
public final void check(final String databaseName, final T config, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> rules) {
- checkShadowRuleConfiguration(config);
+ checkShadowRuleConfiguration(config, dataSourceMap);
}
- protected abstract void checkShadowRuleConfiguration(T config);
+ protected abstract void checkShadowRuleConfiguration(T config, Map<String, DataSource> dataSources);
- protected void sizeCheck(final Map<String, ShadowDataSourceConfiguration> dataSources, final Map<String, ShadowTableConfiguration> shadowTables, final String defaultShadowAlgorithmName) {
- Preconditions.checkState(!dataSources.isEmpty(), "No available shadow data sources mappings in shadow configuration.");
- if (null == defaultShadowAlgorithmName) {
- Preconditions.checkState(!shadowTables.isEmpty(), "No available shadow tables in shadow configuration.");
+ protected void checkDataSources(final Map<String, ShadowDataSourceConfiguration> shadowDataSources, final Map<String, DataSource> dataSourceMap) {
+ Set<String> dataSource = dataSourceMap.keySet();
+ for (Entry<String, ShadowDataSourceConfiguration> entry : shadowDataSources.entrySet()) {
+ ShadowDataSourceConfiguration shadowConfiguration = entry.getValue();
+ boolean shadowDataSourceState = dataSource.contains(shadowConfiguration.getSourceDataSourceName()) && dataSource.contains(shadowConfiguration.getShadowDataSourceName());
+ Preconditions.checkState(shadowDataSourceState, "No available data source for shadow data source mapping configuration");
}
}
- protected void shadowAlgorithmsSizeCheck(final Map<String, ShadowAlgorithm> shadowAlgorithms) {
- Preconditions.checkState(!shadowAlgorithms.isEmpty(), "No available shadow algorithms in shadow configuration.");
- }
-
- protected void shadowAlgorithmConfigurationsSizeCheck(final Map<String, ShardingSphereAlgorithmConfiguration> shadowAlgorithmConfigs) {
- Preconditions.checkState(!shadowAlgorithmConfigs.isEmpty(), "No available shadow data algorithms in shadow configuration.");
- }
-
protected void shadowTableDataSourcesAutoReferences(final Map<String, ShadowTableConfiguration> shadowTables, final Map<String, ShadowDataSourceConfiguration> dataSources) {
if (1 == dataSources.size()) {
String dataSourceName = dataSources.keySet().iterator().next();
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationChecker.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationChecker.java
index 429b68c3b26..50f297db98c 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationChecker.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationChecker.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguratio
import org.apache.shardingsphere.shadow.constant.ShadowOrder;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import javax.sql.DataSource;
import java.util.Map;
/**
@@ -31,15 +32,14 @@ import java.util.Map;
public final class AlgorithmProvidedShadowRuleConfigurationChecker extends AbstractShadowRuleConfigurationChecker<AlgorithmProvidedShadowRuleConfiguration> {
@Override
- protected void checkShadowRuleConfiguration(final AlgorithmProvidedShadowRuleConfiguration config) {
+ protected void checkShadowRuleConfiguration(final AlgorithmProvidedShadowRuleConfiguration config, final Map<String, DataSource> dataSourceMap) {
Map<String, ShadowDataSourceConfiguration> dataSources = config.getDataSources();
+ checkDataSources(dataSources, dataSourceMap);
Map<String, ShadowTableConfiguration> shadowTables = config.getTables();
- Map<String, ShadowAlgorithm> shadowAlgorithms = config.getShadowAlgorithms();
- String defaultShadowAlgorithmName = config.getDefaultShadowAlgorithmName();
- sizeCheck(dataSources, shadowTables, defaultShadowAlgorithmName);
- shadowAlgorithmsSizeCheck(shadowAlgorithms);
shadowTableDataSourcesAutoReferences(shadowTables, dataSources);
shadowTableDataSourcesReferencesCheck(shadowTables, dataSources);
+ Map<String, ShadowAlgorithm> shadowAlgorithms = config.getShadowAlgorithms();
+ String defaultShadowAlgorithmName = config.getDefaultShadowAlgorithmName();
defaultShadowAlgorithmCheck(defaultShadowAlgorithmName, shadowAlgorithms);
shadowTableAlgorithmsAutoReferences(shadowTables, shadowAlgorithms.keySet(), defaultShadowAlgorithmName);
shadowTableAlgorithmsReferencesCheck(shadowTables);
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
index 06fb112ba98..3b66a4f2992 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceCo
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
import org.apache.shardingsphere.shadow.constant.ShadowOrder;
+import javax.sql.DataSource;
import java.util.Map;
/**
@@ -31,15 +32,14 @@ import java.util.Map;
public final class ShadowRuleConfigurationChecker extends AbstractShadowRuleConfigurationChecker<ShadowRuleConfiguration> {
@Override
- protected void checkShadowRuleConfiguration(final ShadowRuleConfiguration config) {
+ protected void checkShadowRuleConfiguration(final ShadowRuleConfiguration config, final Map<String, DataSource> dataSourceMap) {
Map<String, ShadowDataSourceConfiguration> dataSources = config.getDataSources();
+ checkDataSources(dataSources, dataSourceMap);
Map<String, ShadowTableConfiguration> shadowTables = config.getTables();
- Map<String, ShardingSphereAlgorithmConfiguration> shadowAlgorithmConfigs = config.getShadowAlgorithms();
- String defaultShadowAlgorithmName = config.getDefaultShadowAlgorithmName();
- sizeCheck(dataSources, shadowTables, defaultShadowAlgorithmName);
- shadowAlgorithmConfigurationsSizeCheck(shadowAlgorithmConfigs);
shadowTableDataSourcesAutoReferences(shadowTables, dataSources);
shadowTableDataSourcesReferencesCheck(shadowTables, dataSources);
+ Map<String, ShardingSphereAlgorithmConfiguration> shadowAlgorithmConfigs = config.getShadowAlgorithms();
+ String defaultShadowAlgorithmName = config.getDefaultShadowAlgorithmName();
defaultShadowAlgorithmConfigurationCheck(defaultShadowAlgorithmName, shadowAlgorithmConfigs);
shadowTableAlgorithmsAutoReferences(shadowTables, shadowAlgorithmConfigs.keySet(), defaultShadowAlgorithmName);
shadowTableAlgorithmsReferencesCheck(shadowTables);
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationCheckerTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationCheckerTest.java
index a94589a1662..dae3c731fab 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationCheckerTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationCheckerTest.java
@@ -24,15 +24,27 @@ import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguratio
import org.apache.shardingsphere.shadow.factory.ShadowAlgorithmFactory;
import org.junit.Test;
+import javax.sql.DataSource;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
+import java.util.Map;
import java.util.Properties;
import java.util.Collections;
+import static org.mockito.Mockito.mock;
+
public final class AlgorithmProvidedShadowRuleConfigurationCheckerTest {
@Test
public void assertCheck() {
- new AlgorithmProvidedShadowRuleConfigurationChecker().check("", createAlgorithmProvidedShadowRuleConfiguration(), Collections.emptyMap(), Collections.emptyList());
+ new AlgorithmProvidedShadowRuleConfigurationChecker().check("", createAlgorithmProvidedShadowRuleConfiguration(), createDataSourceMap(), Collections.emptyList());
+ }
+
+ private Map<String, DataSource> createDataSourceMap() {
+ Map<String, DataSource> result = new LinkedHashMap<>(2, 1);
+ result.put("ds", mock(DataSource.class));
+ result.put("ds_shadow", mock(DataSource.class));
+ return result;
}
private AlgorithmProvidedShadowRuleConfiguration createAlgorithmProvidedShadowRuleConfiguration() {
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java
index 857e4a1f5dc..051deff9ded 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceCo
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
import org.junit.Test;
+import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@@ -30,11 +31,20 @@ import java.util.Map;
import java.util.Properties;
import java.util.Collections;
+import static org.mockito.Mockito.mock;
+
public final class ShadowRuleConfigurationCheckerTest {
@Test
public void assertCheck() {
- new ShadowRuleConfigurationChecker().check("", createShadowRuleConfiguration(), Collections.emptyMap(), Collections.emptyList());
+ new ShadowRuleConfigurationChecker().check("", createShadowRuleConfiguration(), createDataSourceMap(), Collections.emptyList());
+ }
+
+ private Map<String, DataSource> createDataSourceMap() {
+ Map<String, DataSource> result = new LinkedHashMap<>(2, 1);
+ result.put("ds", mock(DataSource.class));
+ result.put("ds_shadow", mock(DataSource.class));
+ return result;
}
private ShadowRuleConfiguration createShadowRuleConfiguration() {
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterTest.java
index 86446c7e915..029e9052cee 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterTest.java
@@ -170,8 +170,8 @@ public class SpringBootStarterTest {
private void assertShadowDataSourceMappings(final Map<String, ShadowDataSourceRule> actual) {
assertThat(actual.size(), is(1));
- assertThat(actual.get("shadow-data-source").getSourceDataSource(), is("ds"));
- assertThat(actual.get("shadow-data-source").getShadowDataSource(), is("ds-shadow"));
+ assertThat(actual.get("shadow-data-source").getSourceDataSource(), is("ds0"));
+ assertThat(actual.get("shadow-data-source").getShadowDataSource(), is("ds1"));
}
private void assertShadowAlgorithms(final Map<String, ShadowAlgorithm> actual) {
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application.properties b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application.properties
index f6dcc0e00cd..6a71135f75f 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application.properties
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application.properties
@@ -52,8 +52,8 @@ spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.plain-column=pwd_
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.encryptor-name=aesEncryptor
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.assisted-query-encryptor-name=aesEncryptor
-spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.source-data-source-name=ds
-spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.shadow-data-source-name=ds-shadow
+spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.source-data-source-name=ds0
+spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.shadow-data-source-name=ds1
spring.shardingsphere.rules.shadow.tables.t_order.data-source-names=shadow-data-source
spring.shardingsphere.rules.shadow.tables.t_order.shadow-algorithm-names=user-id-match-algorithm,order-id-match-algorithm,simple-hint-algorithm