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/04/14 07:01:48 UTC

[shardingsphere] branch master updated: add ShardingRuleAlteredDetectorTest unit test (#16809)

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 f8513f9585f add ShardingRuleAlteredDetectorTest unit test (#16809)
f8513f9585f is described below

commit f8513f9585f2a14e97289de1f438019ab1adb156
Author: azexcy <10...@users.noreply.github.com>
AuthorDate: Thu Apr 14 15:01:41 2022 +0800

    add ShardingRuleAlteredDetectorTest unit test (#16809)
---
 .../sharding/ShardingRuleAlteredDetectorTest.java  | 91 ++++++++++++++++++++++
 .../scaling/detector/datasource_config.yaml        | 32 ++++++++
 .../scaling/detector/source_rule_config.yaml       | 67 ++++++++++++++++
 .../scaling/detector/target_rule_config.yaml       | 73 +++++++++++++++++
 4 files changed, 263 insertions(+)

diff --git a/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/sharding/ShardingRuleAlteredDetectorTest.java b/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/sharding/ShardingRuleAlteredDetectorTest.java
new file mode 100644
index 00000000000..e2623a42161
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-pipeline-test/src/test/java/org/apache/shardingsphere/sharding/ShardingRuleAlteredDetectorTest.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sharding;
+
+import org.apache.shardingsphere.infra.config.rulealtered.OnRuleAlteredActionConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRuleConfiguration;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.schedule.ShardingRuleAlteredDetector;
+import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.junit.Assert.assertThat;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ShardingRuleAlteredDetectorTest {
+    
+    @Test
+    public void assertGetOnRuleAlteredActionConfigSuccess() {
+        ShardingRuleAlteredDetector shardingRuleAlteredDetector = new ShardingRuleAlteredDetector();
+        ShardingRuleConfiguration result = new ShardingRuleConfiguration();
+        Optional<OnRuleAlteredActionConfiguration> config = shardingRuleAlteredDetector.getOnRuleAlteredActionConfig(result);
+        Assert.assertFalse(config.isPresent());
+    }
+    
+    @Test
+    public void assertFindRuleAlteredLogicTablesSucceed() throws IOException {
+        URL sourceUrl = getClass().getClassLoader().getResource("scaling/detector/source_rule_config.yaml");
+        Assert.assertNotNull(sourceUrl);
+        YamlRuleConfiguration sourceRuleConfig = YamlEngine.unmarshal(new File(sourceUrl.getFile()), YamlShardingRuleConfiguration.class);
+        URL targetUrl = getClass().getClassLoader().getResource("scaling/detector/target_rule_config.yaml");
+        Assert.assertNotNull(targetUrl);
+        YamlRuleConfiguration targetRuleConfig = YamlEngine.unmarshal(new File(targetUrl.getFile()), YamlShardingRuleConfiguration.class);
+        Map<String, Map<String, Object>> sameDatasource = new HashMap<>(5, 1);
+        for (int i = 0; i < 5; i++) {
+            Map<String, Object> prop = new HashMap<>(2, 1);
+            prop.put("dataSourceClassName", "org.apache.shardingsphere.test.mock.MockedDataSource");
+            prop.put("jdbcUrl", "jdbc:h2:mem:test_ds_" + i + ";DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+            sameDatasource.put("ds_" + i, prop);
+        }
+        ShardingRuleAlteredDetector detector = new ShardingRuleAlteredDetector();
+        List<String> ruleAlteredLogicTables = detector.findRuleAlteredLogicTables(sourceRuleConfig, targetRuleConfig, sameDatasource, sameDatasource);
+        assertThat(ruleAlteredLogicTables.get(0), Matchers.is("t_order"));
+    }
+    
+    @Test
+    public void assertNoFindRuleAlteredLogicTables() throws IOException {
+        URL sourceUrl = getClass().getClassLoader().getResource("scaling/detector/source_rule_config.yaml");
+        Assert.assertNotNull(sourceUrl);
+        YamlRuleConfiguration sourceRuleConfig = YamlEngine.unmarshal(new File(sourceUrl.getFile()), YamlShardingRuleConfiguration.class);
+        // target = source
+        List<String> ruleAlteredLogicTables = new ShardingRuleAlteredDetector().findRuleAlteredLogicTables(sourceRuleConfig, sourceRuleConfig, null, null);
+        assertThat("not table rule alter", ruleAlteredLogicTables.size(), Matchers.is(0));
+    }
+    
+    @Test
+    public void assertExtractAllLogicTables() throws IOException {
+        URL sourceUrl = getClass().getClassLoader().getResource("scaling/detector/source_rule_config.yaml");
+        Assert.assertNotNull(sourceUrl);
+        YamlRuleConfiguration sourceRuleConfig = YamlEngine.unmarshal(new File(sourceUrl.getFile()), YamlShardingRuleConfiguration.class);
+        List<String> ruleAlteredLogicTables = new ShardingRuleAlteredDetector().findRuleAlteredLogicTables(sourceRuleConfig, null, null, null);
+        assertThat(ruleAlteredLogicTables.get(0), Matchers.is("t_order"));
+    }
+}
diff --git a/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/datasource_config.yaml b/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/datasource_config.yaml
new file mode 100644
index 00000000000..844a0d2ff2f
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/datasource_config.yaml
@@ -0,0 +1,32 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+ds_0:
+  dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource
+  jdbcUrl: jdbc:h2:mem:test_ds_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
+ds_1:
+  dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource
+  jdbcUrl: jdbc:h2:mem:test_ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
+ds_2:
+  dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource
+  jdbcUrl: jdbc:h2:mem:test_ds_2;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
+ds_3:
+  dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource
+  jdbcUrl: jdbc:h2:mem:test_ds_3;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
+ds_4:
+  dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource
+  jdbcUrl: jdbc:h2:mem:test_ds_4;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL
diff --git a/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/source_rule_config.yaml b/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/source_rule_config.yaml
new file mode 100644
index 00000000000..cc404644034
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/source_rule_config.yaml
@@ -0,0 +1,67 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+defaultDatabaseStrategy:
+  standard:
+    shardingAlgorithmName: database_inline
+    shardingColumn: user_id
+defaultTableStrategy:
+  none: ''
+keyGenerators:
+  snowflake:
+    type: SNOWFLAKE
+scaling:
+  default_scaling:
+    completionDetector:
+      props:
+        incremental-task-idle-minute-threshold: 30
+      type: IDLE
+    dataConsistencyChecker:
+      props:
+        chunk-size: 1000
+      type: DATA_MATCH
+    input:
+      batchSize: 1000
+      workerThread: 40
+    output:
+      batchSize: 1000
+      workerThread: 40
+    streamChannel:
+      props:
+        block-queue-size: 10000
+      type: MEMORY
+scalingName: default_scaling
+shardingAlgorithms:
+  database_inline:
+    props:
+      algorithm-expression: ds_${user_id % 2}
+    type: INLINE
+  t_order_inline:
+    props:
+      algorithm-expression: t_order_${order_id % 2}
+    type: INLINE
+tables:
+  t_order:
+    actualDataNodes: ds_${0..1}.t_order_${0..1}
+    keyGenerateStrategy:
+      column: order_id
+      keyGeneratorName: snowflake
+    logicTable: t_order
+    tableStrategy:
+      standard:
+        shardingAlgorithmName: t_order_inline
+        shardingColumn: order_id
diff --git a/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/target_rule_config.yaml b/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/target_rule_config.yaml
new file mode 100644
index 00000000000..db5bad3c943
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-pipeline-test/src/test/resources/scaling/detector/target_rule_config.yaml
@@ -0,0 +1,73 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+autoTables:
+  t_order:
+    actualDataSources: ds_2,ds_3,ds_4
+    keyGenerateStrategy:
+      column: order_id
+      keyGeneratorName: t_order_snowflake
+    logicTable: t_order
+    shardingStrategy:
+      standard:
+        shardingAlgorithmName: t_order_hash_mod
+        shardingColumn: order_id
+defaultDatabaseStrategy:
+  standard:
+    shardingAlgorithmName: database_inline
+    shardingColumn: user_id
+defaultTableStrategy:
+  none: ''
+keyGenerators:
+  snowflake:
+    type: SNOWFLAKE
+  t_order_snowflake:
+    type: snowflake
+scaling:
+  default_scaling:
+    completionDetector:
+      props:
+        incremental-task-idle-minute-threshold: 30
+      type: IDLE
+    dataConsistencyChecker:
+      props:
+        chunk-size: 1000
+      type: DATA_MATCH
+    input:
+      batchSize: 1000
+      workerThread: 4
+    output:
+      batchSize: 1000
+      workerThread: 4
+    streamChannel:
+      props:
+        block-queue-size: 10000
+      type: MEMORY
+scalingName: default_scaling
+shardingAlgorithms:
+  database_inline:
+    props:
+      algorithm-expression: ds_${user_id % 2}
+    type: INLINE
+  t_order_inline:
+    props:
+      algorithm-expression: t_order_${order_id % 2}
+    type: INLINE
+  t_order_hash_mod:
+    props:
+      sharding-count: '6'
+    type: hash_mod