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/05/04 12:26:27 UTC
[shardingsphere] branch master updated: Refactor ClassBasedShardingAlgorithm (#17317)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 c3d0770b769 Refactor ClassBasedShardingAlgorithm (#17317)
c3d0770b769 is described below
commit c3d0770b7691f6db3988e2683bbe0fe4453afeb2
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed May 4 20:26:22 2022 +0800
Refactor ClassBasedShardingAlgorithm (#17317)
---
.../classbased/ClassBasedShardingAlgorithm.java | 40 +++++++----
.../ClassBasedShardingAlgorithmTest.java | 77 ++++++++--------------
2 files changed, 55 insertions(+), 62 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java
index b1e3dd061c2..cee9c006a51 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithm.java
@@ -34,6 +34,7 @@ import java.util.Properties;
/**
* Class based sharding algorithm.
*/
+@SuppressWarnings("rawtypes")
public final class ClassBasedShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>>, ComplexKeysShardingAlgorithm<Comparable<?>>, HintShardingAlgorithm<Comparable<?>> {
private static final String STRATEGY_KEY = "strategy";
@@ -44,29 +45,36 @@ public final class ClassBasedShardingAlgorithm implements StandardShardingAlgori
@Setter
private Properties props;
- private StandardShardingAlgorithm standardShardingAlgorithm;
+ private volatile ClassBasedShardingAlgorithmStrategyType strategy;
- private ComplexKeysShardingAlgorithm complexKeysShardingAlgorithm;
+ private volatile String algorithmClassName;
- private HintShardingAlgorithm hintShardingAlgorithm;
+ private volatile StandardShardingAlgorithm standardShardingAlgorithm;
- @Getter
- private ClassBasedShardingAlgorithmStrategyType strategy;
+ private volatile ComplexKeysShardingAlgorithm complexKeysShardingAlgorithm;
- @Getter
- private String algorithmClassName;
+ private volatile HintShardingAlgorithm hintShardingAlgorithm;
@Override
public void init(final Properties props) {
- String strategyKey = props.getProperty(STRATEGY_KEY);
- Preconditions.checkNotNull(strategyKey, "The props `%s` cannot be null when uses class based sharding strategy.", STRATEGY_KEY);
- strategy = ClassBasedShardingAlgorithmStrategyType.valueOf(strategyKey.toUpperCase().trim());
- algorithmClassName = props.getProperty(ALGORITHM_CLASS_NAME_KEY);
- Preconditions.checkNotNull(algorithmClassName, "The props `%s` cannot be null when uses class based sharding strategy.", ALGORITHM_CLASS_NAME_KEY);
- createAlgorithmInstance(props);
+ strategy = getStrategy(props);
+ algorithmClassName = getAlgorithmClassName(props);
+ initAlgorithmInstance(props);
+ }
+
+ private ClassBasedShardingAlgorithmStrategyType getStrategy(final Properties props) {
+ String strategy = props.getProperty(STRATEGY_KEY);
+ Preconditions.checkNotNull(strategy, "Properties `%s` can not be null when uses class based sharding strategy.", STRATEGY_KEY);
+ return ClassBasedShardingAlgorithmStrategyType.valueOf(strategy.toUpperCase().trim());
+ }
+
+ private String getAlgorithmClassName(final Properties props) {
+ String result = props.getProperty(ALGORITHM_CLASS_NAME_KEY);
+ Preconditions.checkNotNull(result, "Properties `%s` can not be null when uses class based sharding strategy.", ALGORITHM_CLASS_NAME_KEY);
+ return result;
}
- private void createAlgorithmInstance(final Properties props) {
+ private void initAlgorithmInstance(final Properties props) {
switch (strategy) {
case STANDARD:
standardShardingAlgorithm = ClassBasedShardingAlgorithmFactory.newInstance(algorithmClassName, StandardShardingAlgorithm.class, props);
@@ -82,21 +90,25 @@ public final class ClassBasedShardingAlgorithm implements StandardShardingAlgori
}
}
+ @SuppressWarnings("unchecked")
@Override
public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) {
return standardShardingAlgorithm.doSharding(availableTargetNames, shardingValue);
}
+ @SuppressWarnings("unchecked")
@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
return standardShardingAlgorithm.doSharding(availableTargetNames, shardingValue);
}
+ @SuppressWarnings("unchecked")
@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final ComplexKeysShardingValue<Comparable<?>> shardingValue) {
return complexKeysShardingAlgorithm.doSharding(availableTargetNames, shardingValue);
}
+ @SuppressWarnings("unchecked")
@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final HintShardingValue<Comparable<?>> shardingValue) {
return hintShardingAlgorithm.doSharding(availableTargetNames, shardingValue);
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
index aae2fc669b8..cdf3e7ac888 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/classbased/ClassBasedShardingAlgorithmTest.java
@@ -18,12 +18,14 @@
package org.apache.shardingsphere.sharding.algorithm.sharding.classbased;
import com.google.common.collect.Range;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
import org.apache.shardingsphere.sharding.fixture.ClassBasedComplexKeysShardingAlgorithmFixture;
import org.apache.shardingsphere.sharding.fixture.ClassBasedHintShardingAlgorithmFixture;
import org.apache.shardingsphere.sharding.fixture.ClassBasedStandardShardingAlgorithmFixture;
@@ -38,38 +40,25 @@ import static org.junit.Assert.assertThat;
public final class ClassBasedShardingAlgorithmTest {
- private static final DataNodeInfo DATA_NODE_INFO = new DataNodeInfo("t_order_", 1, '0');
-
- @Test
- public void assertStandardStrategyInit() {
- ClassBasedShardingAlgorithm shardingAlgorithm = getStandardShardingAlgorithm();
- assertThat(shardingAlgorithm.getType(), is("CLASS_BASED"));
- assertThat(shardingAlgorithm.getStrategy(), is(ClassBasedShardingAlgorithmStrategyType.STANDARD));
- assertThat(shardingAlgorithm.getAlgorithmClassName(), is(ClassBasedStandardShardingAlgorithmFixture.class.getName()));
- }
-
@Test(expected = NullPointerException.class)
public void assertInitWithNullStrategy() {
Properties props = new Properties();
props.setProperty("strategy", null);
- ClassBasedShardingAlgorithm shardingAlgorithm = new ClassBasedShardingAlgorithm();
- shardingAlgorithm.init(props);
+ ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("CLASS_BASED", props));
}
@Test(expected = IllegalArgumentException.class)
public void assertInitWithWrongStrategy() {
Properties props = new Properties();
props.setProperty("strategy", "wrong");
- ClassBasedShardingAlgorithm shardingAlgorithm = new ClassBasedShardingAlgorithm();
- shardingAlgorithm.init(props);
+ ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("CLASS_BASED", props));
}
@Test(expected = NullPointerException.class)
public void assertInitWithNullClass() {
Properties props = new Properties();
props.setProperty("strategy", "standard");
- ClassBasedShardingAlgorithm shardingAlgorithm = new ClassBasedShardingAlgorithm();
- shardingAlgorithm.init(props);
+ ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("CLASS_BASED", props));
}
@Test(expected = ClassNotFoundException.class)
@@ -77,8 +66,7 @@ public final class ClassBasedShardingAlgorithmTest {
Properties props = new Properties();
props.setProperty("strategy", "standard");
props.setProperty("algorithmClassName", "org.apache.shardingsphere.sharding.UndefinedClass");
- ClassBasedShardingAlgorithm shardingAlgorithm = new ClassBasedShardingAlgorithm();
- shardingAlgorithm.init(props);
+ ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("CLASS_BASED", props));
}
@Test(expected = ShardingSphereException.class)
@@ -86,67 +74,60 @@ public final class ClassBasedShardingAlgorithmTest {
Properties props = new Properties();
props.setProperty("strategy", "standard");
props.setProperty("algorithmClassName", ClassBasedComplexKeysShardingAlgorithmFixture.class.getName());
- ClassBasedShardingAlgorithm shardingAlgorithm = new ClassBasedShardingAlgorithm();
- shardingAlgorithm.init(props);
+ ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("CLASS_BASED", props));
}
@Test
public void assertPreciseDoSharding() {
- ClassBasedShardingAlgorithm shardingAlgorithm = getStandardShardingAlgorithm();
+ ClassBasedShardingAlgorithm algorithm = (ClassBasedShardingAlgorithm) ShardingAlgorithmFactory.newInstance(
+ new ShardingSphereAlgorithmConfiguration("CLASS_BASED", createStandardProperties()));
Collection<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
- assertThat(shardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, 0)), is("t_order_0"));
+ assertThat(algorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", new DataNodeInfo("t_order_", 1, '0'), 0)), is("t_order_0"));
}
@Test
public void assertRangeDoSharding() {
- ClassBasedShardingAlgorithm shardingAlgorithm = getStandardShardingAlgorithm();
+ ClassBasedShardingAlgorithm algorithm = (ClassBasedShardingAlgorithm) ShardingAlgorithmFactory.newInstance(
+ new ShardingSphereAlgorithmConfiguration("CLASS_BASED", createStandardProperties()));
Collection<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
- Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, new RangeShardingValue<>("t_order", "order_id", DATA_NODE_INFO, Range.closed(2, 15)));
+ Collection<String> actual = algorithm.doSharding(availableTargetNames, new RangeShardingValue<>("t_order", "order_id", new DataNodeInfo("t_order_", 1, '0'), Range.closed(2, 15)));
assertThat(actual.size(), is(4));
}
@Test
public void assertComplexKeysDoSharding() {
- ClassBasedShardingAlgorithm shardingAlgorithm = getComplexKeysShardingAlgorithm();
- assertThat(shardingAlgorithm.getStrategy(), is(ClassBasedShardingAlgorithmStrategyType.COMPLEX));
+ ClassBasedShardingAlgorithm algorithm = (ClassBasedShardingAlgorithm) ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("CLASS_BASED", createComplexProperties()));
Collection<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
- Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, new ComplexKeysShardingValue<>("t_order", null, null));
+ Collection<String> actual = algorithm.doSharding(availableTargetNames, new ComplexKeysShardingValue<>("t_order", null, null));
assertThat(actual.size(), is(4));
}
@Test
public void assertHintDoSharding() {
- ClassBasedShardingAlgorithm shardingAlgorithm = getHintShardingAlgorithm();
- assertThat(shardingAlgorithm.getStrategy(), is(ClassBasedShardingAlgorithmStrategyType.HINT));
+ ClassBasedShardingAlgorithm algorithm = (ClassBasedShardingAlgorithm) ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("CLASS_BASED", createHintProperties()));
Collection<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
- Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, new HintShardingValue<>("t_order", "order_id", null));
+ Collection<String> actual = algorithm.doSharding(availableTargetNames, new HintShardingValue<>("t_order", "order_id", null));
assertThat(actual.size(), is(4));
}
- private ClassBasedShardingAlgorithm getStandardShardingAlgorithm() {
- Properties props = new Properties();
- props.setProperty("strategy", "standard");
- props.setProperty("algorithmClassName", ClassBasedStandardShardingAlgorithmFixture.class.getName());
- ClassBasedShardingAlgorithm result = new ClassBasedShardingAlgorithm();
- result.init(props);
+ private Properties createStandardProperties() {
+ Properties result = new Properties();
+ result.setProperty("strategy", "standard");
+ result.setProperty("algorithmClassName", ClassBasedStandardShardingAlgorithmFixture.class.getName());
return result;
}
- private ClassBasedShardingAlgorithm getComplexKeysShardingAlgorithm() {
- Properties props = new Properties();
- props.setProperty("strategy", "complex");
- props.setProperty("algorithmClassName", ClassBasedComplexKeysShardingAlgorithmFixture.class.getName());
- ClassBasedShardingAlgorithm result = new ClassBasedShardingAlgorithm();
- result.init(props);
+ private Properties createComplexProperties() {
+ Properties result = new Properties();
+ result.setProperty("strategy", "complex");
+ result.setProperty("algorithmClassName", ClassBasedComplexKeysShardingAlgorithmFixture.class.getName());
return result;
}
- private ClassBasedShardingAlgorithm getHintShardingAlgorithm() {
- Properties props = new Properties();
- props.setProperty("strategy", "hint");
- props.setProperty("algorithmClassName", ClassBasedHintShardingAlgorithmFixture.class.getName());
- ClassBasedShardingAlgorithm result = new ClassBasedShardingAlgorithm();
- result.init(props);
+ private Properties createHintProperties() {
+ Properties result = new Properties();
+ result.setProperty("strategy", "hint");
+ result.setProperty("algorithmClassName", ClassBasedHintShardingAlgorithmFixture.class.getName());
return result;
}
}