Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test cases on ComplexInlineShardingAlgorithm #33703

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,35 @@

class HintInlineShardingAlgorithmTest {

private HintInlineShardingAlgorithm hintInlineShardingAlgorithm;
private HintInlineShardingAlgorithm shardingAlgorithm;

@BeforeEach
void setUp() {
hintInlineShardingAlgorithm = (HintInlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class,
shardingAlgorithm = (HintInlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class,
"HINT_INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "t_order_$->{value % 4}")));
}

@Test
void assertDoShardingWithEmptyValue() {
List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
HintShardingValue<Comparable<?>> shardingValue = new HintShardingValue<>("t_order", "order_id", Collections.emptyList());
Collection<String> actual = hintInlineShardingAlgorithm.doSharding(availableTargetNames, shardingValue);
Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, shardingValue);
assertThat(actual, is(availableTargetNames));
}

@Test
void assertDoShardingWithSingleValue() {
List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
HintShardingValue<Comparable<?>> shardingValue = new HintShardingValue<>("t_order", "order_id", Collections.singleton(4));
Collection<String> actual = hintInlineShardingAlgorithm.doSharding(availableTargetNames, shardingValue);
Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, shardingValue);
assertThat(actual, is(Collections.singletonList("t_order_0")));
}

@Test
void assertDoShardingWithMultiValues() {
List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
HintShardingValue<Comparable<?>> shardingValue = new HintShardingValue<>("t_order", "order_id", Arrays.asList(1, 2, 3, 4));
Collection<String> actual = hintInlineShardingAlgorithm.doSharding(availableTargetNames, shardingValue);
Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, shardingValue);
assertTrue(actual.containsAll(availableTargetNames));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

import com.google.common.collect.Range;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingValue;
import org.apache.shardingsphere.sharding.exception.algorithm.MismatchedComplexInlineShardingAlgorithmColumnAndValueSizeException;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand All @@ -39,10 +42,18 @@

class ComplexInlineShardingAlgorithmTest {

private ComplexInlineShardingAlgorithm shardingAlgorithm;

@BeforeEach
void setUp() {
Properties props = PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${type % 2}_${order_id % 2}"), new Property("sharding-columns", "type,order_id"),
new Property("allow-range-query-with-inline-sharding", Boolean.TRUE.toString()));
shardingAlgorithm = (ComplexInlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "COMPLEX_INLINE", props);
}

@Test
void assertInitWithNullClass() {
assertThrows(AlgorithmInitializationException.class,
() -> TypedSPILoader.getService(ShardingAlgorithm.class, "COMPLEX_INLINE", PropertiesBuilder.build(new Property("wrong", ""))));
assertThrows(AlgorithmInitializationException.class, () -> TypedSPILoader.getService(ShardingAlgorithm.class, "COMPLEX_INLINE", PropertiesBuilder.build(new Property("wrong", ""))));
}

@Test
Expand All @@ -52,20 +63,16 @@ void assertInitWithEmptyClassName() {
}

@Test
void assertDoSharding() {
Properties props = PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${type % 2}_${order_id % 2}"), new Property("sharding-columns", "type,order_id"));
ComplexInlineShardingAlgorithm algorithm = (ComplexInlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "COMPLEX_INLINE", props);
void assertDoShardingWithSingleValue() {
List<String> availableTargetNames = Arrays.asList("t_order_0_0", "t_order_0_1", "t_order_1_0", "t_order_1_1");
Collection<String> actual = algorithm.doSharding(availableTargetNames, createComplexKeysShardingValue(Collections.singletonList(2)));
Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, createComplexKeysShardingValue(Collections.singletonList(2)));
assertTrue(1 == actual.size() && actual.contains("t_order_0_0"));
}

@Test
void assertDoShardingWithMultiValue() {
Properties props = PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${type % 2}_${order_id % 2}"), new Property("sharding-columns", "type,order_id"));
ComplexInlineShardingAlgorithm algorithm = (ComplexInlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "COMPLEX_INLINE", props);
void assertDoShardingWithMultiValues() {
List<String> availableTargetNames = Arrays.asList("t_order_0_0", "t_order_0_1", "t_order_1_0", "t_order_1_1");
Collection<String> actual = algorithm.doSharding(availableTargetNames, createComplexKeysShardingValue(Arrays.asList(1, 2)));
Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, createComplexKeysShardingValue(Arrays.asList(1, 2)));
assertTrue(actual.containsAll(availableTargetNames));
}

Expand All @@ -78,11 +85,36 @@ private ComplexKeysShardingValue<Comparable<?>> createComplexKeysShardingValue(f

@Test
void assertDoShardingWithRangeValue() {
Properties props = PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${type % 2}_${order_id % 2}"),
new Property("sharding-columns", "type,order_id"), new Property("allow-range-query-with-inline-sharding", Boolean.TRUE.toString()));
ComplexInlineShardingAlgorithm algorithm = (ComplexInlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "COMPLEX_INLINE", props);
List<String> availableTargetNames = Arrays.asList("t_order_0_0", "t_order_0_1", "t_order_1_0", "t_order_1_1");
Collection<String> actual = algorithm.doSharding(availableTargetNames, new ComplexKeysShardingValue<>("t_order", Collections.emptyMap(), Collections.singletonMap("type", Range.all())));
Collection<String> actual = shardingAlgorithm.doSharding(
availableTargetNames, new ComplexKeysShardingValue<>("t_order", Collections.emptyMap(), Collections.singletonMap("type", Range.all())));
assertTrue(actual.containsAll(availableTargetNames));
}

@Test
void assertDoShardingWithRangeValueAndEmptyColumns() {
List<String> availableTargetNames = Arrays.asList("t_order_0_0", "t_order_0_1", "t_order_1_0", "t_order_1_1");
Properties props = PropertiesBuilder.build(
new Property("algorithm-expression", "t_order_${type % 2}_${order_id % 2}"), new Property("allow-range-query-with-inline-sharding", Boolean.TRUE.toString()));
shardingAlgorithm = (ComplexInlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "COMPLEX_INLINE", props);
Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, new ComplexKeysShardingValue<>("t_order", Collections.emptyMap(), Collections.emptyMap()));
assertTrue(actual.isEmpty());
}

@Test
void assertDoShardingWithRangeValueButNotAllowRangeQuery() {
List<String> availableTargetNames = Arrays.asList("t_order_0_0", "t_order_0_1", "t_order_1_0", "t_order_1_1");
Map<String, Collection<Comparable<?>>> columnNameAndShardingValuesMap = Collections.singletonMap("type", Arrays.asList(1, 2));
Properties props = PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${type % 2}_${order_id % 2}"), new Property("sharding-columns", "type,order_id"));
shardingAlgorithm = (ComplexInlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "COMPLEX_INLINE", props);
assertThrows(UnsupportedSQLOperationException.class,
() -> shardingAlgorithm.doSharding(availableTargetNames, new ComplexKeysShardingValue<>("t_order", columnNameAndShardingValuesMap, Collections.singletonMap("type", Range.all()))));
}

@Test
void assertDoShardingWithRangeValueAndMismatchedComplexInlineShardingAlgorithmColumnAndValueSize() {
List<String> availableTargetNames = Arrays.asList("t_order_0_0", "t_order_0_1", "t_order_1_0", "t_order_1_1");
assertThrows(MismatchedComplexInlineShardingAlgorithmColumnAndValueSizeException.class,
() -> shardingAlgorithm.doSharding(availableTargetNames, new ComplexKeysShardingValue<>("t_order", Collections.emptyMap(), Collections.emptyMap())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class InlineShardingAlgorithmTest {

private static final DataNodeInfo DATA_NODE_INFO = new DataNodeInfo("t_order_", 1, '0');

private InlineShardingAlgorithm inlineShardingAlgorithm;
private InlineShardingAlgorithm shardingAlgorithm;

private InlineShardingAlgorithm negativeNumberInlineShardingAlgorithm;
private InlineShardingAlgorithm negativeNumberShardingAlgorithm;

@BeforeEach
void setUp() {
inlineShardingAlgorithm = (InlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "INLINE",
shardingAlgorithm = (InlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "INLINE",
PropertiesBuilder.build(new Property("algorithm-expression", "t_order_$->{order_id % 4}"), new Property("allow-range-query-with-inline-sharding", Boolean.TRUE.toString())));
negativeNumberInlineShardingAlgorithm = (InlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "INLINE",
negativeNumberShardingAlgorithm = (InlineShardingAlgorithm) TypedSPILoader.getService(ShardingAlgorithm.class, "INLINE",
PropertiesBuilder.build(new Property("algorithm-expression", "t_order_$->{(order_id % 4).abs()}"), new Property("allow-range-query-with-inline-sharding", Boolean.TRUE.toString())));
}

Expand All @@ -75,38 +75,38 @@ void assertInitWithEmptyClassName() {
@Test
void assertDoSharding() {
List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
assertThat(inlineShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, 0)), is("t_order_0"));
assertThat(shardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, 0)), is("t_order_0"));
assertThrows(MismatchedInlineShardingAlgorithmExpressionAndColumnException.class,
() -> inlineShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "non_existent_column1", DATA_NODE_INFO, 0)));
() -> shardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "non_existent_column1", DATA_NODE_INFO, 0)));
}

@Test
void assertDoShardingWithNonExistNodes() {
List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1");
assertThat(inlineShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, 0)), is("t_order_0"));
assertThat(shardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, 0)), is("t_order_0"));
}

@Test
void assertDoShardingWithNegative() {
List<String> availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
assertThat(negativeNumberInlineShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, -1)), is("t_order_1"));
assertThat(negativeNumberInlineShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, -4)), is("t_order_0"));
assertThat(negativeNumberShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, -1)), is("t_order_1"));
assertThat(negativeNumberShardingAlgorithm.doSharding(availableTargetNames, new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, -4)), is("t_order_0"));
}

@Test
void assertDoShardingWithLargeValues() {
List<String> availableTargetNames = Lists.newArrayList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
assertThat(inlineShardingAlgorithm.doSharding(availableTargetNames,
assertThat(shardingAlgorithm.doSharding(availableTargetNames,
new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, 787694822390497280L)), is("t_order_0"));
assertThat(inlineShardingAlgorithm.doSharding(availableTargetNames,
assertThat(shardingAlgorithm.doSharding(availableTargetNames,
new PreciseShardingValue<>("t_order", "order_id", DATA_NODE_INFO, new BigInteger("787694822390497280787694822390497280"))), is("t_order_0"));
}

@SuppressWarnings({"unchecked", "rawtypes"})
@Test
void assertDoShardingWithRangeShardingConditionValue() {
List<String> availableTargetNames = Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3");
Collection<String> actual = inlineShardingAlgorithm.doSharding(availableTargetNames, new RangeShardingValue<>("t_order", "order_id", DATA_NODE_INFO, mock(Range.class)));
Collection<String> actual = shardingAlgorithm.doSharding(availableTargetNames, new RangeShardingValue<>("t_order", "order_id", DATA_NODE_INFO, mock(Range.class)));
assertTrue(actual.containsAll(availableTargetNames));
}

Expand All @@ -122,6 +122,6 @@ void assertDoShardingWithNotAllowRangeQuery() {

@Test
void assertGetAlgorithmStructure() {
assertThat(inlineShardingAlgorithm.getAlgorithmStructure("foo_ds", "foo_col"), is(Optional.of("t_order_${order_id%4}")));
assertThat(shardingAlgorithm.getAlgorithmStructure("foo_ds", "foo_col"), is(Optional.of("t_order_${order_id%4}")));
}
}