You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2024/05/02 16:21:39 UTC
(shardingsphere) branch master updated: Add ShardingConfigurationRepositoryTupleSwapperIT (#31104)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 31fc9afcf04 Add ShardingConfigurationRepositoryTupleSwapperIT (#31104)
31fc9afcf04 is described below
commit 31fc9afcf04eb95d433ac119a722fdc82d24b203
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri May 3 00:21:33 2024 +0800
Add ShardingConfigurationRepositoryTupleSwapperIT (#31104)
* Refactor RepositoryTupleSwapperIT's impl
* Add ShardingConfigurationRepositoryTupleSwapperIT
---
...ingRuleConfigurationRepositoryTupleSwapper.java | 12 +-
...rdingConfigurationRepositoryTupleSwapperIT.java | 79 +++++++
.../ShardingRuleConfigurationYamlIT.java | 23 +-
...uleConfigurationRepositoryTupleSwapperTest.java | 245 ---------------------
.../src/test/resources/yaml/sharding-rule.yaml | 21 +-
5 files changed, 98 insertions(+), 282 deletions(-)
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationRepositoryTupleSwapper.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationRepositoryTupleSwapper.java
index 17c22d3c2fd..9ae03ea89a8 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationRepositoryTupleSwapper.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationRepositoryTupleSwapper.java
@@ -31,6 +31,7 @@ import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfigur
import org.apache.shardingsphere.sharding.yaml.config.strategy.audit.YamlShardingAuditStrategyConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.strategy.keygen.YamlKeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration;
+import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingTableReferenceRuleConfigurationConverter;
import java.util.Collection;
import java.util.LinkedHashMap;
@@ -94,11 +95,12 @@ public final class ShardingRuleConfigurationRepositoryTupleSwapper implements Re
}
private void swapTableRules(final YamlShardingRuleConfiguration yamlRuleConfig, final Collection<RepositoryTuple> repositoryTuples) {
- for (YamlTableRuleConfiguration each : yamlRuleConfig.getTables().values()) {
- repositoryTuples.add(new RepositoryTuple(ruleNodePath.getNamedItem(ShardingRuleNodePathProvider.TABLES).getPath(each.getLogicTable()), YamlEngine.marshal(each)));
+ for (Entry<String, YamlTableRuleConfiguration> entry : yamlRuleConfig.getTables().entrySet()) {
+ repositoryTuples.add(new RepositoryTuple(ruleNodePath.getNamedItem(ShardingRuleNodePathProvider.TABLES).getPath(entry.getKey()), YamlEngine.marshal(entry.getValue())));
}
- for (YamlShardingAutoTableRuleConfiguration each : yamlRuleConfig.getAutoTables().values()) {
- repositoryTuples.add(new RepositoryTuple(ruleNodePath.getNamedItem(ShardingRuleNodePathProvider.AUTO_TABLES).getPath(each.getLogicTable()), YamlEngine.marshal(each)));
+ for (Entry<String, YamlShardingAutoTableRuleConfiguration> entry : yamlRuleConfig.getAutoTables().entrySet()) {
+ repositoryTuples.add(
+ new RepositoryTuple(ruleNodePath.getNamedItem(ShardingRuleNodePathProvider.AUTO_TABLES).getPath(entry.getKey()), YamlEngine.marshal(entry.getValue())));
}
for (String each : yamlRuleConfig.getBindingTables()) {
repositoryTuples.add(new RepositoryTuple(ruleNodePath.getNamedItem(ShardingRuleNodePathProvider.BINDING_TABLES).getPath(getBindingGroupName(each)), each));
@@ -106,7 +108,7 @@ public final class ShardingRuleConfigurationRepositoryTupleSwapper implements Re
}
private String getBindingGroupName(final String bindingGroup) {
- return bindingGroup.contains(":") ? bindingGroup.substring(0, bindingGroup.indexOf(":")) : bindingGroup;
+ return YamlShardingTableReferenceRuleConfigurationConverter.convertToObject(bindingGroup).getName();
}
@Override
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingConfigurationRepositoryTupleSwapperIT.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingConfigurationRepositoryTupleSwapperIT.java
new file mode 100644
index 00000000000..c3fcaf476f7
--- /dev/null
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingConfigurationRepositoryTupleSwapperIT.java
@@ -0,0 +1,79 @@
+/*
+ * 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.it;
+
+import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
+import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
+import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.yaml.swapper.ShardingRuleConfigurationRepositoryTupleSwapper;
+import org.apache.shardingsphere.test.it.yaml.RepositoryTupleSwapperIT;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class ShardingConfigurationRepositoryTupleSwapperIT extends RepositoryTupleSwapperIT {
+
+ ShardingConfigurationRepositoryTupleSwapperIT() {
+ super("yaml/sharding-rule.yaml", new ShardingRuleConfigurationRepositoryTupleSwapper(), false);
+ }
+
+ @Override
+ protected void assertRepositoryTuples(final List<RepositoryTuple> actualRepositoryTuples, final YamlRuleConfiguration expectedYamlRuleConfig) {
+ assertThat(actualRepositoryTuples.size(), is(19));
+ assertRepositoryTuple(actualRepositoryTuples.get(0),
+ "algorithms/core_standard_fixture", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getShardingAlgorithms().get("core_standard_fixture"));
+ assertRepositoryTuple(actualRepositoryTuples.get(1),
+ "algorithms/core_complex_fixture", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getShardingAlgorithms().get("core_complex_fixture"));
+ assertRepositoryTuple(actualRepositoryTuples.get(2),
+ "algorithms/core_hint_fixture", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getShardingAlgorithms().get("core_hint_fixture"));
+ assertRepositoryTuple(actualRepositoryTuples.get(3),
+ "algorithms/database_inline", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getShardingAlgorithms().get("database_inline"));
+ assertRepositoryTuple(actualRepositoryTuples.get(4),
+ "algorithms/table_inline", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getShardingAlgorithms().get("table_inline"));
+ assertRepositoryTuple(actualRepositoryTuples.get(5), "key_generators/snowflake", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getKeyGenerators().get("snowflake"));
+ assertRepositoryTuple(actualRepositoryTuples.get(6),
+ "auditors/sharding_key_required_auditor", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getAuditors().get("sharding_key_required_auditor"));
+ assertRepositoryTuple(actualRepositoryTuples.get(7),
+ "default_strategies/default_database_strategy", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getDefaultDatabaseStrategy());
+ assertRepositoryTuple(actualRepositoryTuples.get(8),
+ "default_strategies/default_table_strategy", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getDefaultTableStrategy());
+ assertRepositoryTuple(actualRepositoryTuples.get(9),
+ "default_strategies/default_key_generate_strategy", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getDefaultKeyGenerateStrategy());
+ assertRepositoryTuple(actualRepositoryTuples.get(10),
+ "default_strategies/default_audit_strategy", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getDefaultAuditStrategy());
+ assertRepositoryTuple(actualRepositoryTuples.get(11),
+ "default_strategies/default_sharding_column", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getDefaultShardingColumn());
+ assertRepositoryTuple(actualRepositoryTuples.get(12),
+ "sharding_cache", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getShardingCache());
+ assertRepositoryTuple(actualRepositoryTuples.get(13),
+ "tables/t_user", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getTables().get("t_user"));
+ assertRepositoryTuple(actualRepositoryTuples.get(14),
+ "tables/t_stock", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getTables().get("t_stock"));
+ assertRepositoryTuple(actualRepositoryTuples.get(15),
+ "tables/t_order", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getTables().get("t_order"));
+ assertRepositoryTuple(actualRepositoryTuples.get(16),
+ "tables/t_order_item", ((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getTables().get("t_order_item"));
+ assertRepositoryTuple(actualRepositoryTuples.get(17),
+ "binding_tables" + actualRepositoryTuples.get(17).getKey().substring(actualRepositoryTuples.get(17).getKey().indexOf("/")),
+ new ArrayList<>(((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getBindingTables()).get(0));
+ assertRepositoryTuple(actualRepositoryTuples.get(18), "binding_tables/foo", new ArrayList<>(((YamlShardingRuleConfiguration) expectedYamlRuleConfig).getBindingTables()).get(1));
+ }
+}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/ShardingRuleConfigurationYamlIT.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingRuleConfigurationYamlIT.java
similarity index 85%
rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/ShardingRuleConfigurationYamlIT.java
rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingRuleConfigurationYamlIT.java
index d31fd2ab2ea..6f0169685b9 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/ShardingRuleConfigurationYamlIT.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingRuleConfigurationYamlIT.java
@@ -15,15 +15,15 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.yaml;
+package org.apache.shardingsphere.sharding.it;
-import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.cache.YamlShardingCacheConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.cache.YamlShardingCacheOptionsConfiguration;
import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationIT;
+import java.util.ArrayList;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
@@ -38,7 +38,6 @@ class ShardingRuleConfigurationYamlIT extends YamlRuleConfigurationIT {
@Override
protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
- assertDataSourceMap(actual);
Optional<YamlShardingRuleConfiguration> shardingRuleConfig = actual.getRules().stream()
.filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(optional -> (YamlShardingRuleConfiguration) optional);
assertTrue(shardingRuleConfig.isPresent());
@@ -49,17 +48,9 @@ class ShardingRuleConfigurationYamlIT extends YamlRuleConfigurationIT {
assertTOrderItem(shardingRuleConfig.get());
assertBindingTable(shardingRuleConfig.get());
assertShardingCache(shardingRuleConfig.get());
- assertProps(actual);
assertThat(shardingRuleConfig.get().getDefaultShardingColumn(), is("order_id"));
}
- private void assertDataSourceMap(final YamlRootConfiguration actual) {
- assertThat(actual.getDataSources().size(), is(3));
- assertTrue(actual.getDataSources().containsKey("ds_0"));
- assertTrue(actual.getDataSources().containsKey("ds_1"));
- assertTrue(actual.getDataSources().containsKey("default_ds"));
- }
-
private void assertTUser(final YamlShardingRuleConfiguration actual) {
assertThat(actual.getTables().get("t_user").getActualDataNodes(), is("ds_${0..1}.t_user_${0..15}"));
assertThat(actual.getTables().get("t_user").getDatabaseStrategy().getComplex().getShardingColumns(), is("region_id, user_id"));
@@ -90,8 +81,9 @@ class ShardingRuleConfigurationYamlIT extends YamlRuleConfigurationIT {
}
private void assertBindingTable(final YamlShardingRuleConfiguration actual) {
- assertThat(actual.getBindingTables().size(), is(1));
- assertThat(actual.getBindingTables().iterator().next(), is("t_order, t_order_item"));
+ assertThat(actual.getBindingTables().size(), is(2));
+ assertThat(new ArrayList<>(actual.getBindingTables()).get(0), is("t_order, t_order_item"));
+ assertThat(new ArrayList<>(actual.getBindingTables()).get(1), is("foo:t_order, t_order_item"));
}
private void assertShardingCache(final YamlShardingRuleConfiguration actual) {
@@ -102,9 +94,4 @@ class ShardingRuleConfigurationYamlIT extends YamlRuleConfigurationIT {
assertThat(actualRouteCacheConfig.getMaximumSize(), is(262144));
assertTrue(actualRouteCacheConfig.isSoftValues());
}
-
- private void assertProps(final YamlRootConfiguration actual) {
- assertThat(actual.getProps().size(), is(1));
- assertTrue((boolean) actual.getProps().get(ConfigurationPropertyKey.SQL_SHOW.getKey()));
- }
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationRepositoryTupleSwapperTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationRepositoryTupleSwapperTest.java
deleted file mode 100644
index d1f327fe607..00000000000
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationRepositoryTupleSwapperTest.java
+++ /dev/null
@@ -1,245 +0,0 @@
-/*
- * 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.yaml.swapper;
-
-import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
-import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
-import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
-import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
-import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
-import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
-import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
-import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
-import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.junit.jupiter.api.Test;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Optional;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertInstanceOf;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-class ShardingRuleConfigurationRepositoryTupleSwapperTest {
-
- private final ShardingRuleConfigurationRepositoryTupleSwapper swapper = new ShardingRuleConfigurationRepositoryTupleSwapper();
-
- @Test
- void assertSwapToRepositoryTuplesWithEmptyRule() {
- assertTrue(swapper.swapToRepositoryTuples(new YamlShardingRuleConfiguration()).isEmpty());
- }
-
- @Test
- void assertSwapToRepositoryTuples() {
- YamlShardingRuleConfiguration yamlRuleConfig = (YamlShardingRuleConfiguration) new YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfiguration(createMaximumShardingRule());
- Collection<RepositoryTuple> actual = swapper.swapToRepositoryTuples(yamlRuleConfig);
- assertThat(actual.size(), is(15));
- Iterator<RepositoryTuple> iterator = actual.iterator();
- assertThat(iterator.next().getKey(), is("algorithms/core_standard_fixture"));
- assertThat(iterator.next().getKey(), is("algorithms/hash_mod"));
- assertThat(iterator.next().getKey(), is("key_generators/uuid"));
- assertThat(iterator.next().getKey(), is("key_generators/default"));
- assertThat(iterator.next().getKey(), is("key_generators/auto_increment"));
- assertThat(iterator.next().getKey(), is("auditors/audit_algorithm"));
- assertThat(iterator.next().getKey(), is("default_strategies/default_database_strategy"));
- assertThat(iterator.next().getKey(), is("default_strategies/default_table_strategy"));
- assertThat(iterator.next().getKey(), is("default_strategies/default_key_generate_strategy"));
- assertThat(iterator.next().getKey(), is("default_strategies/default_audit_strategy"));
- assertThat(iterator.next().getKey(), is("default_strategies/default_sharding_column"));
- assertThat(iterator.next().getKey(), is("tables/LOGIC_TABLE"));
- assertThat(iterator.next().getKey(), is("tables/SUB_LOGIC_TABLE"));
- assertThat(iterator.next().getKey(), is("auto_tables/auto_table"));
- assertThat(iterator.next().getKey(), is("binding_tables/foo"));
- }
-
- private ShardingRuleConfiguration createMaximumShardingRule() {
- ShardingRuleConfiguration result = new ShardingRuleConfiguration();
- ShardingTableRuleConfiguration shardingTableRuleConfig = createTableRuleConfiguration("LOGIC_TABLE", "ds_${0..1}.table_${0..2}");
- shardingTableRuleConfig.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "uuid"));
- shardingTableRuleConfig.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("audit_algorithm"), false));
- result.getTables().add(shardingTableRuleConfig);
- ShardingTableRuleConfiguration subTableRuleConfig = createTableRuleConfiguration("SUB_LOGIC_TABLE", "ds_${0..1}.sub_table_${0..2}");
- subTableRuleConfig.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "auto_increment"));
- result.getTables().add(subTableRuleConfig);
- ShardingAutoTableRuleConfiguration autoTableRuleConfig = new ShardingAutoTableRuleConfiguration("auto_table", "ds_1,ds_2");
- autoTableRuleConfig.setShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "hash_mod"));
- autoTableRuleConfig.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "auto_increment"));
- autoTableRuleConfig.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("audit_algorithm"), true));
- result.getAutoTables().add(autoTableRuleConfig);
- result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", shardingTableRuleConfig.getLogicTable() + "," + subTableRuleConfig.getLogicTable()));
- result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("ds_id", "standard"));
- result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("table_id", "standard"));
- result.setDefaultShardingColumn("table_id");
- result.setDefaultKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "default"));
- result.setDefaultAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singletonList("audit_algorithm"), false));
- result.getShardingAlgorithms().put("core_standard_fixture", new AlgorithmConfiguration("CORE.STANDARD.FIXTURE", new Properties()));
- result.getShardingAlgorithms().put("hash_mod", new AlgorithmConfiguration("hash_mod", PropertiesBuilder.build(new PropertiesBuilder.Property("sharding-count", "4"))));
- result.getKeyGenerators().put("uuid", new AlgorithmConfiguration("UUID", new Properties()));
- result.getKeyGenerators().put("default", new AlgorithmConfiguration("UUID", new Properties()));
- result.getKeyGenerators().put("auto_increment", new AlgorithmConfiguration("AUTO_INCREMENT.FIXTURE", new Properties()));
- result.getAuditors().put("audit_algorithm", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
- return result;
- }
-
- private ShardingTableRuleConfiguration createTableRuleConfiguration(final String logicTableName, final String actualDataNodes) {
- ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration(logicTableName, actualDataNodes);
- result.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline"));
- result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "table_inline"));
- return result;
- }
-
- @Test
- void assertSwapToObject() {
- Collection<RepositoryTuple> tuples = new LinkedList<>();
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/tables/LOGIC_TABLE/versions/0", "actualDataNodes: ds_${0..1}.table_${0..2}\n"
- + "auditStrategy:\n"
- + " allowHintDisable: false\n"
- + " auditorNames:\n"
- + " - audit_algorithm\n"
- + "databaseStrategy:\n"
- + " standard:\n"
- + " shardingAlgorithmName: database_inline\n"
- + " shardingColumn: user_id\n"
- + "keyGenerateStrategy:\n"
- + " column: id\n"
- + " keyGeneratorName: uuid\n"
- + "logicTable: LOGIC_TABLE\n"
- + "tableStrategy:\n"
- + " standard:\n"
- + " shardingAlgorithmName: table_inline\n"
- + " shardingColumn: order_id\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/tables/SUB_LOGIC_TABLE/versions/0", "actualDataNodes: ds_${0..1}.sub_table_${0..2}\n"
- + "databaseStrategy:\n"
- + " standard:\n"
- + " shardingAlgorithmName: database_inline\n"
- + " shardingColumn: user_id\n"
- + "keyGenerateStrategy:\n"
- + " column: id\n"
- + " keyGeneratorName: auto_increment\n"
- + "logicTable: SUB_LOGIC_TABLE\n"
- + "tableStrategy:\n"
- + " standard:\n"
- + " shardingAlgorithmName: table_inline\n"
- + " shardingColumn: order_id\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/auto_tables/auto_table/versions/0", "actualDataSources: ds_1,ds_2\n"
- + "auditStrategy:\n"
- + " allowHintDisable: true\n"
- + " auditorNames:\n"
- + " - audit_algorithm\n"
- + "keyGenerateStrategy:\n"
- + " column: id\n"
- + " keyGeneratorName: auto_increment\n"
- + "logicTable: auto_table\n"
- + "shardingStrategy:\n"
- + " standard:\n"
- + " shardingAlgorithmName: hash_mod\n"
- + " shardingColumn: user_id\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/binding_tables/foo/versions/0", "foo:LOGIC_TABLE,SUB_LOGIC_TABLE"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/default_strategies/default_database_strategy/versions/0", "standard:\n"
- + " shardingAlgorithmName: standard\n"
- + " shardingColumn: ds_id\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/default_strategies/default_table_strategy/versions/0", "standard:\n"
- + " shardingAlgorithmName: standard\n"
- + " shardingColumn: table_id\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/default_strategies/default_key_generate_strategy/versions/0", "column: id\n"
- + "keyGeneratorName: default\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/default_strategies/default_audit_strategy/versions/0", "allowHintDisable: false\n"
- + "auditorNames:\n"
- + "- audit_algorithm\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/algorithms/core_standard_fixture/versions/0", "type: CORE.STANDARD.FIXTURE\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/algorithms/hash_mod/versions/0", "props:\n"
- + " sharding-count: '4'\n"
- + "type: hash_mod\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/key_generators/uuid/versions/0", "type: UUID\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/key_generators/default/versions/0", "type: UUID\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/key_generators/auto_increment/versions/0", "type: AUTO_INCREMENT.FIXTURE\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/auditors/audit_algorithm/versions/0", "type: DML_SHARDING_CONDITIONS\n"));
- tuples.add(new RepositoryTuple("/metadata/foo_db/rules/sharding/default_strategies/default_sharding_column/versions/0", "table_id"));
- Optional<YamlShardingRuleConfiguration> yamlRuleConfig = swapper.swapToObject(tuples);
- assertTrue(yamlRuleConfig.isPresent());
- ShardingRuleConfiguration actual = (ShardingRuleConfiguration) new YamlRuleConfigurationSwapperEngine().swapToRuleConfiguration(yamlRuleConfig.get());
- assertThat(actual.getTables().size(), is(2));
- assertThat(actual.getTables().iterator().next().getLogicTable(), is("LOGIC_TABLE"));
- assertThat(actual.getTables().iterator().next().getActualDataNodes(), is("ds_${0..1}.table_${0..2}"));
- assertInstanceOf(StandardShardingStrategyConfiguration.class, actual.getTables().iterator().next().getDatabaseShardingStrategy());
- assertThat(((StandardShardingStrategyConfiguration) actual.getTables().iterator().next().getDatabaseShardingStrategy()).getShardingColumn(), is("user_id"));
- assertThat(actual.getTables().iterator().next().getDatabaseShardingStrategy().getShardingAlgorithmName(), is("database_inline"));
- assertThat(actual.getTables().iterator().next().getDatabaseShardingStrategy().getType(), is("STANDARD"));
- assertInstanceOf(StandardShardingStrategyConfiguration.class, actual.getTables().iterator().next().getTableShardingStrategy());
- assertThat(((StandardShardingStrategyConfiguration) actual.getTables().iterator().next().getTableShardingStrategy()).getShardingColumn(), is("order_id"));
- assertThat(actual.getTables().iterator().next().getTableShardingStrategy().getShardingAlgorithmName(), is("table_inline"));
- assertThat(actual.getTables().iterator().next().getTableShardingStrategy().getType(), is("STANDARD"));
- assertThat(actual.getTables().iterator().next().getKeyGenerateStrategy().getColumn(), is("id"));
- assertThat(actual.getTables().iterator().next().getKeyGenerateStrategy().getKeyGeneratorName(), is("uuid"));
- assertThat(actual.getTables().iterator().next().getAuditStrategy().getAuditorNames().size(), is(1));
- assertThat(actual.getTables().iterator().next().getAuditStrategy().getAuditorNames().iterator().next(), is("audit_algorithm"));
- assertFalse(actual.getTables().iterator().next().getAuditStrategy().isAllowHintDisable());
- assertThat(actual.getAutoTables().size(), is(1));
- assertThat(actual.getAutoTables().iterator().next().getLogicTable(), is("auto_table"));
- assertThat(actual.getAutoTables().iterator().next().getActualDataSources(), is("ds_1,ds_2"));
- assertInstanceOf(StandardShardingStrategyConfiguration.class, actual.getAutoTables().iterator().next().getShardingStrategy());
- assertThat(((StandardShardingStrategyConfiguration) actual.getAutoTables().iterator().next().getShardingStrategy()).getShardingColumn(), is("user_id"));
- assertThat(actual.getAutoTables().iterator().next().getShardingStrategy().getShardingAlgorithmName(), is("hash_mod"));
- assertThat(actual.getAutoTables().iterator().next().getShardingStrategy().getType(), is("STANDARD"));
- assertThat(actual.getAutoTables().iterator().next().getKeyGenerateStrategy().getColumn(), is("id"));
- assertThat(actual.getAutoTables().iterator().next().getKeyGenerateStrategy().getKeyGeneratorName(), is("auto_increment"));
- assertThat(actual.getAutoTables().iterator().next().getAuditStrategy().getAuditorNames().size(), is(1));
- assertThat(actual.getAutoTables().iterator().next().getAuditStrategy().getAuditorNames().iterator().next(), is("audit_algorithm"));
- assertTrue(actual.getAutoTables().iterator().next().getAuditStrategy().isAllowHintDisable());
- assertThat(actual.getBindingTableGroups().size(), is(1));
- assertThat(actual.getBindingTableGroups().iterator().next().getName(), is("foo"));
- assertThat(actual.getBindingTableGroups().iterator().next().getReference(), is("LOGIC_TABLE,SUB_LOGIC_TABLE"));
- assertInstanceOf(StandardShardingStrategyConfiguration.class, actual.getDefaultDatabaseShardingStrategy());
- assertThat(((StandardShardingStrategyConfiguration) actual.getDefaultDatabaseShardingStrategy()).getType(), is("STANDARD"));
- assertThat(((StandardShardingStrategyConfiguration) actual.getDefaultDatabaseShardingStrategy()).getShardingColumn(), is("ds_id"));
- assertThat(actual.getDefaultDatabaseShardingStrategy().getShardingAlgorithmName(), is("standard"));
- assertInstanceOf(StandardShardingStrategyConfiguration.class, actual.getDefaultTableShardingStrategy());
- assertThat(((StandardShardingStrategyConfiguration) actual.getDefaultTableShardingStrategy()).getType(), is("STANDARD"));
- assertThat(((StandardShardingStrategyConfiguration) actual.getDefaultTableShardingStrategy()).getShardingColumn(), is("table_id"));
- assertThat(actual.getDefaultTableShardingStrategy().getShardingAlgorithmName(), is("standard"));
- assertThat(actual.getDefaultKeyGenerateStrategy().getColumn(), is("id"));
- assertThat(actual.getDefaultKeyGenerateStrategy().getKeyGeneratorName(), is("default"));
- assertThat(actual.getDefaultAuditStrategy().getAuditorNames().size(), is(1));
- assertThat(actual.getDefaultAuditStrategy().getAuditorNames().iterator().next(), is("audit_algorithm"));
- assertFalse(actual.getDefaultAuditStrategy().isAllowHintDisable());
- assertThat(actual.getDefaultShardingColumn(), is("table_id"));
- assertThat(actual.getShardingAlgorithms().size(), is(2));
- assertThat(actual.getShardingAlgorithms().get("core_standard_fixture").getType(), is("CORE.STANDARD.FIXTURE"));
- assertThat(actual.getShardingAlgorithms().get("hash_mod").getType(), is("hash_mod"));
- assertThat(actual.getShardingAlgorithms().get("hash_mod").getProps().size(), is(1));
- assertThat(actual.getShardingAlgorithms().get("hash_mod").getProps().get("sharding-count"), is("4"));
- assertThat(actual.getKeyGenerators().size(), is(3));
- assertThat(actual.getKeyGenerators().get("uuid").getType(), is("UUID"));
- assertTrue(actual.getKeyGenerators().get("uuid").getProps().isEmpty());
- assertThat(actual.getKeyGenerators().get("auto_increment").getType(), is("AUTO_INCREMENT.FIXTURE"));
- assertThat(actual.getAuditors().size(), is(1));
- assertThat(actual.getAuditors().get("audit_algorithm").getType(), is("DML_SHARDING_CONDITIONS"));
- assertTrue(actual.getAuditors().get("audit_algorithm").getProps().isEmpty());
- assertNull(actual.getShardingCache());
- }
-}
diff --git a/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml b/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml
index d1fb307864f..5fc2064d4eb 100644
--- a/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml
+++ b/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml
@@ -15,14 +15,6 @@
# limitations under the License.
#
-dataSources:
- ds_0:
- dataSourceClassName: org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource
- ds_1:
- dataSourceClassName: org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource
- default_ds:
- dataSourceClassName: org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource
-
rules:
- !SHARDING
tables:
@@ -60,17 +52,21 @@ rules:
shardingColumn: order_id
shardingAlgorithmName: core_standard_fixture
bindingTables:
- - t_order, t_order_item
- defaultDatabaseStrategy:
+ - t_order, t_order_item
+ - foo:t_order, t_order_item
+ defaultDatabaseStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: database_inline
defaultTableStrategy:
none:
defaultShardingColumn: order_id
+ defaultKeyGenerateStrategy:
+ column: id
+ keyGeneratorName: snowflake
defaultAuditStrategy:
auditorNames:
- - sharding_key_required_auditor
+ - sharding_key_required_auditor
allowHintDisable: true
shardingAlgorithms:
@@ -103,6 +99,3 @@ rules:
initialCapacity: 65536
maximumSize: 262144
softValues: true
-
-props:
- sql-show: true