Skip to content

Commit

Permalink
Merge pull request #33513 from terrymanu/dev
Browse files Browse the repository at this point in the history
Add more test cases on ShadowRule
  • Loading branch information
iamhucong authored Nov 3, 2024
2 parents d658d87 + 6229bf6 commit 1b4b77a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -94,8 +96,9 @@ private void initShadowTableRules(final Map<String, ShadowTableConfiguration> ta
*
* @return shadow algorithm
*/
@HighFrequencyInvocation
public Optional<ShadowAlgorithm> getDefaultShadowAlgorithm() {
return null == defaultShadowAlgorithm ? Optional.empty() : Optional.of(defaultShadowAlgorithm);
return Optional.ofNullable(defaultShadowAlgorithm);
}

/**
Expand All @@ -104,6 +107,7 @@ public Optional<ShadowAlgorithm> getDefaultShadowAlgorithm() {
* @param tableNames table names
* @return related shadow tables
*/
@HighFrequencyInvocation
public Collection<String> getRelatedShadowTables(final Collection<String> tableNames) {
Collection<String> result = new LinkedList<>();
for (String each : tableNames) {
Expand All @@ -119,6 +123,7 @@ public Collection<String> getRelatedShadowTables(final Collection<String> tableN
*
* @return shadow table names
*/
@HighFrequencyInvocation
public Collection<String> getAllShadowTableNames() {
return shadowTableRules.keySet();
}
Expand All @@ -128,6 +133,7 @@ public Collection<String> getAllShadowTableNames() {
*
* @return related hint shadow algorithms
*/
@HighFrequencyInvocation
@SuppressWarnings("unchecked")
public Collection<HintShadowAlgorithm<Comparable<?>>> getAllHintShadowAlgorithms() {
Collection<HintShadowAlgorithm<Comparable<?>>> result = new LinkedList<>();
Expand All @@ -143,6 +149,7 @@ public Collection<HintShadowAlgorithm<Comparable<?>>> getAllHintShadowAlgorithms
* @param tableName table name
* @return hint shadow algorithms
*/
@HighFrequencyInvocation
@SuppressWarnings("unchecked")
public Collection<HintShadowAlgorithm<Comparable<?>>> getRelatedHintShadowAlgorithms(final String tableName) {
Collection<HintShadowAlgorithm<Comparable<?>>> result = new LinkedList<>();
Expand All @@ -161,15 +168,12 @@ public Collection<HintShadowAlgorithm<Comparable<?>>> getRelatedHintShadowAlgori
* @param shadowColumn shadow column
* @return column shadow algorithms
*/
@HighFrequencyInvocation
@SuppressWarnings("unchecked")
public Collection<ColumnShadowAlgorithm<Comparable<?>>> getRelatedColumnShadowAlgorithms(final ShadowOperationType shadowOperationType, final String tableName, final String shadowColumn) {
Collection<ColumnShadowAlgorithm<Comparable<?>>> result = new LinkedList<>();
Map<ShadowOperationType, Collection<ShadowAlgorithmNameRule>> columnShadowAlgorithmNames = shadowTableRules.get(tableName).getColumnShadowAlgorithmNames();
Collection<ShadowAlgorithmNameRule> 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<Comparable<?>>) shadowAlgorithms.get(each.getShadowAlgorithmName()));
}
Expand All @@ -184,14 +188,11 @@ public Collection<ColumnShadowAlgorithm<Comparable<?>>> getRelatedColumnShadowAl
* @param tableName table name
* @return related shadow column names
*/
@HighFrequencyInvocation
public Collection<String> getRelatedShadowColumnNames(final ShadowOperationType shadowOperationType, final String tableName) {
Collection<String> result = new LinkedList<>();
Map<ShadowOperationType, Collection<ShadowAlgorithmNameRule>> columnShadowAlgorithmNames = shadowTableRules.get(tableName).getColumnShadowAlgorithmNames();
Collection<ShadowAlgorithmNameRule> 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;
Expand All @@ -203,12 +204,12 @@ public Collection<String> getRelatedShadowColumnNames(final ShadowOperationType
* @param tableName table name
* @return shadow data source rules
*/
@HighFrequencyInvocation
public Map<String, String> getRelatedShadowDataSourceMappings(final String tableName) {
Map<String, String> result = new LinkedHashMap<>();
Collection<String> 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;
}
Expand All @@ -218,6 +219,7 @@ public Map<String, String> getRelatedShadowDataSourceMappings(final String table
*
* @return all shadow data source mappings
*/
@HighFrequencyInvocation
public Map<String, String> getAllShadowDataSourceMappings() {
Map<String, String> result = new LinkedHashMap<>();
for (Entry<String, ShadowDataSourceRule> entry : shadowDataSourceMappings.entrySet()) {
Expand All @@ -233,8 +235,9 @@ public Map<String, String> getAllShadowDataSourceMappings() {
* @param actualDataSourceName actual data source name
* @return source data source name
*/
@HighFrequencyInvocation
public Optional<String> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,73 +30,81 @@
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());
result.setShadowAlgorithms(createShadowAlgorithms());
return result;
}

private Map<String, AlgorithmConfiguration> createShadowAlgorithms() {
Map<String, AlgorithmConfiguration> 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<ShadowDataSourceConfiguration> createDataSources() {
return Arrays.asList(new ShadowDataSourceConfiguration("shadow_ds_0", "ds0", "ds0_shadow"),
new ShadowDataSourceConfiguration("shadow_ds_1", "ds1", "ds1_shadow"));
}

private Map<String, ShadowTableConfiguration> createTables() {
Map<String, ShadowTableConfiguration> 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<String> createShadowAlgorithmNames(final String tableName) {
Collection<String> 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<ShadowDataSourceConfiguration> createDataSources() {
Collection<ShadowDataSourceConfiguration> 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<String, AlgorithmConfiguration> createShadowAlgorithms() {
Map<String, AlgorithmConfiguration> 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<String, ShadowDataSourceRule> 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<String, ShadowTableRule> shadowTableRules) {
Expand All @@ -104,7 +113,7 @@ private void assertShadowTableRules(final Map<String, ShadowTableRule> 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 {
Expand All @@ -113,27 +122,59 @@ private void assertShadowTableRule(final String tableName, final ShadowTableRule
}
}

private void assertShadowDataSourceMappings(final Map<String, ShadowDataSourceRule> 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<String> 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<String> allShadowTableNames = shadowRule.getAllShadowTableNames();
assertThat(allShadowTableNames.size(), is(2));
Iterator<String> 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());
}
}

0 comments on commit 1b4b77a

Please sign in to comment.