From 6229bf6a4ce6f33cce6f4c6de12cf01ea7bc915b Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sun, 3 Nov 2024 19:38:26 +0800 Subject: [PATCH] Add more test cases on ShadowRule --- .../shadow/rule/ShadowRule.java | 37 +++--- .../shadow/rule/ShadowRuleTest.java | 123 ++++++++++++------ 2 files changed, 102 insertions(+), 58 deletions(-) diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java index 46e3cd3a5ca15..63cdd85d7a840 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java @@ -19,6 +19,7 @@ import lombok.Getter; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation; import org.apache.shardingsphere.infra.rule.scope.DatabaseRule; import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; @@ -32,6 +33,7 @@ import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; import java.util.Collection; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; @@ -94,8 +96,9 @@ private void initShadowTableRules(final Map ta * * @return shadow algorithm */ + @HighFrequencyInvocation public Optional getDefaultShadowAlgorithm() { - return null == defaultShadowAlgorithm ? Optional.empty() : Optional.of(defaultShadowAlgorithm); + return Optional.ofNullable(defaultShadowAlgorithm); } /** @@ -104,6 +107,7 @@ public Optional getDefaultShadowAlgorithm() { * @param tableNames table names * @return related shadow tables */ + @HighFrequencyInvocation public Collection getRelatedShadowTables(final Collection tableNames) { Collection result = new LinkedList<>(); for (String each : tableNames) { @@ -119,6 +123,7 @@ public Collection getRelatedShadowTables(final Collection tableN * * @return shadow table names */ + @HighFrequencyInvocation public Collection getAllShadowTableNames() { return shadowTableRules.keySet(); } @@ -128,6 +133,7 @@ public Collection getAllShadowTableNames() { * * @return related hint shadow algorithms */ + @HighFrequencyInvocation @SuppressWarnings("unchecked") public Collection>> getAllHintShadowAlgorithms() { Collection>> result = new LinkedList<>(); @@ -143,6 +149,7 @@ public Collection>> getAllHintShadowAlgorithms * @param tableName table name * @return hint shadow algorithms */ + @HighFrequencyInvocation @SuppressWarnings("unchecked") public Collection>> getRelatedHintShadowAlgorithms(final String tableName) { Collection>> result = new LinkedList<>(); @@ -161,15 +168,12 @@ public Collection>> getRelatedHintShadowAlgori * @param shadowColumn shadow column * @return column shadow algorithms */ + @HighFrequencyInvocation @SuppressWarnings("unchecked") public Collection>> getRelatedColumnShadowAlgorithms(final ShadowOperationType shadowOperationType, final String tableName, final String shadowColumn) { Collection>> result = new LinkedList<>(); Map> columnShadowAlgorithmNames = shadowTableRules.get(tableName).getColumnShadowAlgorithmNames(); - Collection names = columnShadowAlgorithmNames.get(shadowOperationType); - if (null == names) { - return result; - } - for (ShadowAlgorithmNameRule each : names) { + for (ShadowAlgorithmNameRule each : columnShadowAlgorithmNames.getOrDefault(shadowOperationType, Collections.emptyList())) { if (shadowColumn.equals(each.getShadowColumnName())) { result.add((ColumnShadowAlgorithm>) shadowAlgorithms.get(each.getShadowAlgorithmName())); } @@ -184,14 +188,11 @@ public Collection>> getRelatedColumnShadowAl * @param tableName table name * @return related shadow column names */ + @HighFrequencyInvocation public Collection getRelatedShadowColumnNames(final ShadowOperationType shadowOperationType, final String tableName) { Collection result = new LinkedList<>(); Map> columnShadowAlgorithmNames = shadowTableRules.get(tableName).getColumnShadowAlgorithmNames(); - Collection names = columnShadowAlgorithmNames.get(shadowOperationType); - if (null == names) { - return result; - } - for (ShadowAlgorithmNameRule each : names) { + for (ShadowAlgorithmNameRule each : columnShadowAlgorithmNames.getOrDefault(shadowOperationType, Collections.emptyList())) { result.add(each.getShadowColumnName()); } return result; @@ -203,12 +204,12 @@ public Collection getRelatedShadowColumnNames(final ShadowOperationType * @param tableName table name * @return shadow data source rules */ + @HighFrequencyInvocation public Map getRelatedShadowDataSourceMappings(final String tableName) { Map result = new LinkedHashMap<>(); - Collection shadowDataSources = shadowTableRules.get(tableName).getShadowDataSources(); - for (String each : shadowDataSources) { - ShadowDataSourceRule shadowDataSourceRule = shadowDataSourceMappings.get(each); - result.put(shadowDataSourceRule.getProductionDataSource(), shadowDataSourceRule.getShadowDataSource()); + for (String each : shadowTableRules.get(tableName).getShadowDataSources()) { + ShadowDataSourceRule dataSourceRule = shadowDataSourceMappings.get(each); + result.put(dataSourceRule.getProductionDataSource(), dataSourceRule.getShadowDataSource()); } return result; } @@ -218,6 +219,7 @@ public Map getRelatedShadowDataSourceMappings(final String table * * @return all shadow data source mappings */ + @HighFrequencyInvocation public Map getAllShadowDataSourceMappings() { Map result = new LinkedHashMap<>(); for (Entry entry : shadowDataSourceMappings.entrySet()) { @@ -233,8 +235,9 @@ public Map getAllShadowDataSourceMappings() { * @param actualDataSourceName actual data source name * @return source data source name */ + @HighFrequencyInvocation public Optional getSourceDataSourceName(final String actualDataSourceName) { - ShadowDataSourceRule shadowDataSourceRule = shadowDataSourceMappings.get(actualDataSourceName); - return null == shadowDataSourceRule ? Optional.empty() : Optional.of(shadowDataSourceRule.getProductionDataSource()); + ShadowDataSourceRule dataSourceRule = shadowDataSourceMappings.get(actualDataSourceName); + return null == dataSourceRule ? Optional.empty() : Optional.of(dataSourceRule.getProductionDataSource()); } } diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/ShadowRuleTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/ShadowRuleTest.java index 0ce1fc2a609ef..cc36b7b01ca4b 100644 --- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/ShadowRuleTest.java +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/ShadowRuleTest.java @@ -21,6 +21,7 @@ import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration; import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration; +import org.apache.shardingsphere.shadow.spi.ShadowOperationType; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; @@ -29,24 +30,26 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Optional; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; class ShadowRuleTest { - private ShadowRule shadowRule; + private ShadowRule rule; @BeforeEach void init() { - shadowRule = new ShadowRule(createShadowRuleConfiguration()); + rule = new ShadowRule(createRuleConfiguration()); } - private ShadowRuleConfiguration createShadowRuleConfiguration() { + private ShadowRuleConfiguration createRuleConfiguration() { ShadowRuleConfiguration result = new ShadowRuleConfiguration(); result.setDataSources(createDataSources()); result.setTables(createTables()); @@ -54,48 +57,54 @@ private ShadowRuleConfiguration createShadowRuleConfiguration() { return result; } - private Map createShadowAlgorithms() { - Map result = new LinkedHashMap<>(); - result.put("sql-hint-algorithm", new AlgorithmConfiguration("SQL_HINT", PropertiesBuilder.build(new Property("shadow", Boolean.TRUE.toString())))); - result.put("user-id-insert-regex-algorithm", new AlgorithmConfiguration("REGEX_MATCH", - PropertiesBuilder.build(new Property("column", "user_id"), new Property("operation", "insert"), new Property("regex", "[1]")))); - result.put("user-id-update-regex-algorithm", new AlgorithmConfiguration("REGEX_MATCH", - PropertiesBuilder.build(new Property("column", "user_id"), new Property("operation", "update"), new Property("regex", "[1]")))); - result.put("order-id-insert-regex-algorithm", new AlgorithmConfiguration("REGEX_MATCH", - PropertiesBuilder.build(new Property("column", "order_id"), new Property("operation", "insert"), new Property("regex", "[1]")))); - return result; + private Collection createDataSources() { + return Arrays.asList(new ShadowDataSourceConfiguration("shadow_ds_0", "ds0", "ds0_shadow"), + new ShadowDataSourceConfiguration("shadow_ds_1", "ds1", "ds1_shadow")); } private Map createTables() { Map result = new LinkedHashMap<>(); - result.put("t_user", new ShadowTableConfiguration(Collections.singleton("shadow-data-source-0"), createShadowAlgorithmNames("t_user"))); - result.put("t_order", new ShadowTableConfiguration(Collections.singleton("shadow-data-source-1"), createShadowAlgorithmNames("t_order"))); + result.put("foo_tbl", new ShadowTableConfiguration(Collections.singleton("shadow_ds_0"), createShadowAlgorithmNames("foo_tbl"))); + result.put("bar_tbl", new ShadowTableConfiguration(Collections.singleton("shadow_ds_1"), createShadowAlgorithmNames("bar_tbl"))); return result; } private Collection createShadowAlgorithmNames(final String tableName) { Collection result = new LinkedList<>(); result.add("sql-hint-algorithm"); - if ("t_user".equals(tableName)) { - result.add("user-id-insert-regex-algorithm"); - result.add("user-id-update-regex-algorithm"); + if ("foo_tbl".equals(tableName)) { + result.add("foo-id-insert-regex-algorithm"); + result.add("foo-id-update-regex-algorithm"); } else { - result.add("order-id-insert-regex-algorithm"); + result.add("bar-id-insert-regex-algorithm"); } return result; } - private Collection createDataSources() { - Collection result = new LinkedList<>(); - result.add(new ShadowDataSourceConfiguration("shadow-data-source-0", "ds", "ds_shadow")); - result.add(new ShadowDataSourceConfiguration("shadow-data-source-1", "ds1", "ds1_shadow")); + private Map createShadowAlgorithms() { + Map result = new LinkedHashMap<>(); + result.put("sql-hint-algorithm", new AlgorithmConfiguration("SQL_HINT", PropertiesBuilder.build(new Property("shadow", Boolean.TRUE.toString())))); + result.put("foo-id-insert-regex-algorithm", new AlgorithmConfiguration("REGEX_MATCH", + PropertiesBuilder.build(new Property("column", "foo_id"), new Property("operation", "insert"), new Property("regex", "[1]")))); + result.put("foo-id-update-regex-algorithm", new AlgorithmConfiguration("REGEX_MATCH", + PropertiesBuilder.build(new Property("column", "foo_id"), new Property("operation", "update"), new Property("regex", "[1]")))); + result.put("bar-id-insert-regex-algorithm", new AlgorithmConfiguration("REGEX_MATCH", + PropertiesBuilder.build(new Property("column", "bar_id"), new Property("operation", "insert"), new Property("regex", "[1]")))); return result; } @Test void assertNewShadowRulSuccessByShadowRuleConfiguration() { - assertShadowDataSourceMappings(shadowRule.getShadowDataSourceMappings()); - assertShadowTableRules(shadowRule.getShadowTableRules()); + assertShadowDataSourceMappings(rule.getShadowDataSourceMappings()); + assertShadowTableRules(rule.getShadowTableRules()); + } + + private void assertShadowDataSourceMappings(final Map shadowDataSourceMappings) { + assertThat(shadowDataSourceMappings.size(), is(2)); + assertThat(shadowDataSourceMappings.get("shadow_ds_0").getProductionDataSource(), is("ds0")); + assertThat(shadowDataSourceMappings.get("shadow_ds_0").getShadowDataSource(), is("ds0_shadow")); + assertThat(shadowDataSourceMappings.get("shadow_ds_1").getProductionDataSource(), is("ds1")); + assertThat(shadowDataSourceMappings.get("shadow_ds_1").getShadowDataSource(), is("ds1_shadow")); } private void assertShadowTableRules(final Map shadowTableRules) { @@ -104,7 +113,7 @@ private void assertShadowTableRules(final Map shadowTab } private void assertShadowTableRule(final String tableName, final ShadowTableRule shadowTableRule) { - if ("t_user".equals(tableName)) { + if ("foo_tbl".equals(tableName)) { assertThat(shadowTableRule.getHintShadowAlgorithmNames().size(), is(1)); assertThat(shadowTableRule.getColumnShadowAlgorithmNames().size(), is(2)); } else { @@ -113,27 +122,59 @@ private void assertShadowTableRule(final String tableName, final ShadowTableRule } } - private void assertShadowDataSourceMappings(final Map shadowDataSourceMappings) { - assertThat(shadowDataSourceMappings.size(), is(2)); - assertThat(shadowDataSourceMappings.get("shadow-data-source-0").getProductionDataSource(), is("ds")); - assertThat(shadowDataSourceMappings.get("shadow-data-source-0").getShadowDataSource(), is("ds_shadow")); - assertThat(shadowDataSourceMappings.get("shadow-data-source-1").getProductionDataSource(), is("ds1")); - assertThat(shadowDataSourceMappings.get("shadow-data-source-1").getShadowDataSource(), is("ds1_shadow")); + @Test + void assertGetDefaultShadowAlgorithm() { + assertFalse(rule.getDefaultShadowAlgorithm().isPresent()); } @Test void assertGetRelatedShadowTables() { - Collection relatedShadowTables = shadowRule.getRelatedShadowTables(Arrays.asList("t_user", "t_auto")); - assertThat(relatedShadowTables.size(), is(1)); - assertThat(relatedShadowTables.iterator().next(), is("t_user")); + assertThat(rule.getRelatedShadowTables(Arrays.asList("foo_tbl", "no_tbl")), is(Collections.singletonList("foo_tbl"))); } @Test void assertGetAllShadowTableNames() { - Collection allShadowTableNames = shadowRule.getAllShadowTableNames(); - assertThat(allShadowTableNames.size(), is(2)); - Iterator iterator = allShadowTableNames.iterator(); - assertThat(iterator.next(), is("t_user")); - assertThat(iterator.next(), is("t_order")); + assertThat(rule.getAllShadowTableNames(), is(new HashSet<>(Arrays.asList("foo_tbl", "bar_tbl")))); + } + + @Test + void assertGetAllHintShadowAlgorithms() { + assertThat(rule.getAllHintShadowAlgorithms().size(), is(1)); + } + + @Test + void assertGetRelatedHintShadowAlgorithms() { + assertThat(rule.getRelatedHintShadowAlgorithms("foo_tbl").size(), is(1)); + } + + @Test + void assertGetRelatedColumnShadowAlgorithms() { + assertThat(rule.getRelatedColumnShadowAlgorithms(ShadowOperationType.INSERT, "foo_tbl", "foo_id").size(), is(1)); + } + + @Test + void assertGetRelatedShadowColumnNames() { + assertThat(rule.getRelatedShadowColumnNames(ShadowOperationType.INSERT, "foo_tbl").size(), is(1)); + } + + @Test + void assertGetRelatedShadowDataSourceMappings() { + assertThat(rule.getRelatedShadowDataSourceMappings("foo_tbl").size(), is(1)); + } + + @Test + void assertGetAllShadowDataSourceMappings() { + assertThat(rule.getAllShadowDataSourceMappings().size(), is(2)); + } + + @Test + void assertGetDataSourceMapper() { + assertThat(rule.getSourceDataSourceName("shadow_ds_0"), is(Optional.of("ds0"))); + assertThat(rule.getSourceDataSourceName("shadow_ds_1"), is(Optional.of("ds1"))); + } + + @Test + void assertNotGetDataSourceMapper() { + assertFalse(rule.getSourceDataSourceName("shadow_ds_2").isPresent()); } }