diff --git a/sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGenerator.java b/sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGenerator.java index cbda3696031ee7723dd8dce5f693a41b7875b803..3b847f7be318c71986d24616aa1af7480b7bdb46 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGenerator.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGenerator.java @@ -17,6 +17,8 @@ package io.shardingsphere.core.keygen; +import java.util.Properties; + /** * Key generator interface. * @@ -31,4 +33,18 @@ public interface KeyGenerator { * @return generated key */ Comparable generateKey(); + + /** + * Get properties. + * + * @return The properties of key generator + */ + Properties getProperties(); + + /** + * Set Properties. + * + * @param properties properties + */ + void setProperties(Properties properties); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGeneratorType.java b/sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGeneratorType.java new file mode 100644 index 0000000000000000000000000000000000000000..402d7d945ededd37ee5a1fa1bb7d64c69545ac1e --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/keygen/KeyGeneratorType.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.keygen; + +import com.google.common.base.Optional; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * Key generator type. + * + * @author panjuan + */ +@RequiredArgsConstructor +@Getter +public enum KeyGeneratorType { + + SNOWFLAKE("io.shardingsphere.core.keygen.SnowflakeKeyGenerator"), + UUID(""), + LEAF(""); + + private final String keyGeneratorClassName; + + /** + * Get key generator type. + * + * @param keyGeneratorClassName key generator class name + * @return key generator type + */ + public static Optional getKeyGeneratorType(final String keyGeneratorClassName) { + for (KeyGeneratorType each : KeyGeneratorType.values()) { + if (each.getKeyGeneratorClassName().equals(keyGeneratorClassName)) { + return Optional.of(each); + } + } + return Optional.absent(); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/keygen/DefaultKeyGenerator.java b/sharding-core/src/main/java/io/shardingsphere/core/keygen/SnowflakeKeyGenerator.java similarity index 74% rename from sharding-core/src/main/java/io/shardingsphere/core/keygen/DefaultKeyGenerator.java rename to sharding-core/src/main/java/io/shardingsphere/core/keygen/SnowflakeKeyGenerator.java index 99290c97b2c7be795e93e1b59c459b66ed63a7a1..d7c9c55d24fc8f4c2900edd92aacf125ded8b251 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/keygen/DefaultKeyGenerator.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/keygen/SnowflakeKeyGenerator.java @@ -18,36 +18,39 @@ package io.shardingsphere.core.keygen; import com.google.common.base.Preconditions; +import lombok.Getter; import lombok.Setter; import lombok.SneakyThrows; import java.util.Calendar; +import java.util.Properties; /** - * Default distributed primary key generator. + * Snowflake distributed primary key generator. * *

* Use snowflake algorithm. Length is 64 bit. *

* *
- * 1bit   sign bit.
+ * 1bit sign bit.
  * 41bits timestamp offset from 2016.11.01(ShardingSphere distributed primary key published data) to now.
  * 10bits worker process id.
  * 12bits auto increment offset in one mills
  * 
* *

- * Call @{@code DefaultKeyGenerator.setWorkerId} to set worker id, default value is 0. + * Call @{@code SnowflakeKeyGenerator.setWorkerId} to set worker id, default value is 0. *

* *

- * Call @{@code DefaultKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds} to set max tolerate time difference milliseconds, default value is 0. + * Call @{@code SnowflakeKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds} to set max tolerate time difference milliseconds, default value is 0. *

* * @author gaohongtao + * @author panjuan */ -public final class DefaultKeyGenerator implements KeyGenerator { +public final class SnowflakeKeyGenerator implements KeyGenerator { public static final long EPOCH; @@ -63,12 +66,16 @@ public final class DefaultKeyGenerator implements KeyGenerator { private static final long WORKER_ID_MAX_VALUE = 1L << WORKER_ID_BITS; + private static final long WORKER_ID = 0; + + private static final int MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS = 10; + @Setter private static TimeService timeService = new TimeService(); - private static long workerId; - - private static int maxTolerateTimeDifferenceMilliseconds = 10; + @Getter + @Setter + private Properties properties = new Properties(); static { Calendar calendar = Calendar.getInstance(); @@ -86,23 +93,14 @@ public final class DefaultKeyGenerator implements KeyGenerator { private long lastMilliseconds; - /** - * Set work process id. - * - * @param workerId work process id - */ - public static void setWorkerId(final long workerId) { - Preconditions.checkArgument(workerId >= 0L && workerId < WORKER_ID_MAX_VALUE); - DefaultKeyGenerator.workerId = workerId; + private long getWorkerId() { + long result = Long.valueOf(properties.getProperty("worker.id", String.valueOf(WORKER_ID))); + Preconditions.checkArgument(result >= 0L && result < WORKER_ID_MAX_VALUE); + return result; } - /** - * Set max tolerate time difference milliseconds. - * - * @param maxTolerateTimeDifferenceMilliseconds max tolerate time difference milliseconds - */ - public static void setMaxTolerateTimeDifferenceMilliseconds(final int maxTolerateTimeDifferenceMilliseconds) { - DefaultKeyGenerator.maxTolerateTimeDifferenceMilliseconds = maxTolerateTimeDifferenceMilliseconds; + private int getMaxTolerateTimeDifferenceMilliseconds() { + return Integer.valueOf(properties.getProperty("max.tolerate.time.difference.milliseconds", String.valueOf(MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS))); } /** @@ -125,7 +123,7 @@ public final class DefaultKeyGenerator implements KeyGenerator { sequence = sequenceOffset; } lastMilliseconds = currentMilliseconds; - return ((currentMilliseconds - EPOCH) << TIMESTAMP_LEFT_SHIFT_BITS) | (workerId << WORKER_ID_LEFT_SHIFT_BITS) | sequence; + return ((currentMilliseconds - EPOCH) << TIMESTAMP_LEFT_SHIFT_BITS) | (getWorkerId() << WORKER_ID_LEFT_SHIFT_BITS) | sequence; } @SneakyThrows @@ -134,7 +132,7 @@ public final class DefaultKeyGenerator implements KeyGenerator { return false; } long timeDifferenceMilliseconds = lastMilliseconds - currentMilliseconds; - Preconditions.checkState(timeDifferenceMilliseconds < maxTolerateTimeDifferenceMilliseconds, + Preconditions.checkState(timeDifferenceMilliseconds < getMaxTolerateTimeDifferenceMilliseconds(), "Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds", lastMilliseconds, currentMilliseconds); Thread.sleep(timeDifferenceMilliseconds); return true; diff --git a/sharding-core/src/main/java/io/shardingsphere/core/rule/ShardingRule.java b/sharding-core/src/main/java/io/shardingsphere/core/rule/ShardingRule.java index 752caeae17bc23239f008288cc0b652657a5eaa3..a8858d1fbd1217aab104b29a4a6f14990c199f29 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/rule/ShardingRule.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/rule/ShardingRule.java @@ -27,7 +27,7 @@ import io.shardingsphere.api.config.rule.TableRuleConfiguration; import io.shardingsphere.api.config.strategy.ShardingStrategyConfiguration; import io.shardingsphere.core.exception.ShardingConfigurationException; import io.shardingsphere.core.exception.ShardingException; -import io.shardingsphere.core.keygen.DefaultKeyGenerator; +import io.shardingsphere.core.keygen.SnowflakeKeyGenerator; import io.shardingsphere.core.keygen.KeyGenerator; import io.shardingsphere.core.parsing.parser.context.condition.Column; import io.shardingsphere.core.routing.strategy.ShardingStrategy; @@ -111,7 +111,7 @@ public class ShardingRule { } private KeyGenerator createDefaultKeyGenerator(final KeyGenerator defaultKeyGenerator) { - return null == defaultKeyGenerator ? new DefaultKeyGenerator() : defaultKeyGenerator; + return null == defaultKeyGenerator ? new SnowflakeKeyGenerator() : defaultKeyGenerator; } private Collection createMasterSlaveRules(final Collection masterSlaveRuleConfigurations) { diff --git a/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfiguration.java b/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..eb75730aae5fab6d307916a937fafab94dcecea7 --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfiguration.java @@ -0,0 +1,77 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.yaml.sharding; + +import com.google.common.base.Strings; +import io.shardingsphere.core.exception.ShardingConfigurationException; +import io.shardingsphere.core.keygen.KeyGenerator; +import io.shardingsphere.core.keygen.KeyGeneratorFactory; +import io.shardingsphere.core.keygen.KeyGeneratorType; +import lombok.Getter; +import lombok.Setter; + +import java.util.Properties; + +/** + * Yaml key generator configuration. + * + * @author panjuan + */ +@Getter +@Setter +public final class YamlKeyGeneratorConfiguration { + + private String column; + + private String type; + + private String className; + + private Properties props = new Properties(); + + /** + * Build table rule configuration. + * + * @return table rule configuration + */ + public KeyGenerator getKeyGenerator() { + KeyGenerator result; + if (!Strings.isNullOrEmpty(className)) { + result = KeyGeneratorFactory.newInstance(className); + } else if (!Strings.isNullOrEmpty(type)) { + result = KeyGeneratorFactory.newInstance(getKeyGeneratorClassName()); + } else { + result = KeyGeneratorFactory.newInstance(KeyGeneratorType.SNOWFLAKE.getKeyGeneratorClassName()); + } + result.setProperties(props); + return result; + } + + private String getKeyGeneratorClassName() { + if (type.equalsIgnoreCase(KeyGeneratorType.SNOWFLAKE.name())) { + return KeyGeneratorType.SNOWFLAKE.getKeyGeneratorClassName(); + } + if (type.equalsIgnoreCase(KeyGeneratorType.UUID.name())) { + return KeyGeneratorType.UUID.getKeyGeneratorClassName(); + } + if (type.equalsIgnoreCase(KeyGeneratorType.LEAF.name())) { + return KeyGeneratorType.LEAF.getKeyGeneratorClassName(); + } + throw new ShardingConfigurationException("Invalid key generator type."); + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfiguration.java b/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfiguration.java index 42463fee041ead376b0ff034825700fd3caed9b4..a0949b5329b4ce2c815665d80787a71f1dfe84b0 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfiguration.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfiguration.java @@ -17,10 +17,11 @@ package io.shardingsphere.core.yaml.sharding; +import com.google.common.base.Optional; import io.shardingsphere.api.config.rule.MasterSlaveRuleConfiguration; import io.shardingsphere.api.config.rule.ShardingRuleConfiguration; import io.shardingsphere.api.config.rule.TableRuleConfiguration; -import io.shardingsphere.core.keygen.KeyGeneratorFactory; +import io.shardingsphere.core.keygen.KeyGeneratorType; import io.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration; import lombok.Getter; import lombok.NoArgsConstructor; @@ -57,7 +58,7 @@ public class YamlShardingRuleConfiguration { private YamlShardingStrategyConfiguration defaultTableStrategy; - private String defaultKeyGeneratorClassName; + private YamlKeyGeneratorConfiguration defaultKeyGenerator; private Map masterSlaveRules = new LinkedHashMap<>(); @@ -70,12 +71,25 @@ public class YamlShardingRuleConfiguration { bindingTables.addAll(shardingRuleConfiguration.getBroadcastTables()); defaultDatabaseStrategy = new YamlShardingStrategyConfiguration(shardingRuleConfiguration.getDefaultDatabaseShardingStrategyConfig()); defaultTableStrategy = new YamlShardingStrategyConfiguration(shardingRuleConfiguration.getDefaultTableShardingStrategyConfig()); - defaultKeyGeneratorClassName = null == shardingRuleConfiguration.getDefaultKeyGenerator() ? null : shardingRuleConfiguration.getDefaultKeyGenerator().getClass().getName(); + defaultKeyGenerator = null == shardingRuleConfiguration.getDefaultKeyGenerator() ? null : getYamlKeyGeneratorConfiguration(shardingRuleConfiguration); for (MasterSlaveRuleConfiguration each : shardingRuleConfiguration.getMasterSlaveRuleConfigs()) { masterSlaveRules.put(each.getName(), new YamlMasterSlaveRuleConfiguration(each)); } } + private YamlKeyGeneratorConfiguration getYamlKeyGeneratorConfiguration(final ShardingRuleConfiguration shardingRuleConfiguration) { + YamlKeyGeneratorConfiguration result = new YamlKeyGeneratorConfiguration(); + String keyGeneratorClassName = shardingRuleConfiguration.getDefaultKeyGenerator().getClass().getName(); + Optional keyGeneratorType = KeyGeneratorType.getKeyGeneratorType(keyGeneratorClassName); + if (!keyGeneratorType.isPresent()) { + result.setClassName(keyGeneratorClassName); + } else { + result.setType(keyGeneratorType.get().name()); + } + result.setProps(shardingRuleConfiguration.getDefaultKeyGenerator().getProperties()); + return result; + } + /** * Get sharding rule configuration. * @@ -97,8 +111,8 @@ public class YamlShardingRuleConfiguration { if (null != defaultTableStrategy) { result.setDefaultTableShardingStrategyConfig(defaultTableStrategy.build()); } - if (null != defaultKeyGeneratorClassName) { - result.setDefaultKeyGenerator(KeyGeneratorFactory.newInstance(defaultKeyGeneratorClassName)); + if (null != defaultKeyGenerator) { + result.setDefaultKeyGenerator(defaultKeyGenerator.getKeyGenerator()); } Collection masterSlaveRuleConfigs = new LinkedList<>(); for (Entry entry : masterSlaveRules.entrySet()) { diff --git a/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfiguration.java b/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfiguration.java index 16034ffb2b1b8545293343315c00e4d53c23207b..da2645744ae1198d44931511c5b50fccc4f446cc 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfiguration.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfiguration.java @@ -17,10 +17,10 @@ package io.shardingsphere.core.yaml.sharding; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import io.shardingsphere.api.config.rule.TableRuleConfiguration; -import io.shardingsphere.core.keygen.KeyGeneratorFactory; +import io.shardingsphere.core.keygen.KeyGeneratorType; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -44,9 +44,7 @@ public class YamlTableRuleConfiguration { private YamlShardingStrategyConfiguration tableStrategy; - private String keyGeneratorColumnName; - - private String keyGeneratorClassName; + private YamlKeyGeneratorConfiguration keyGenerator; private String logicIndex; @@ -55,9 +53,21 @@ public class YamlTableRuleConfiguration { actualDataNodes = tableRuleConfiguration.getActualDataNodes(); databaseStrategy = new YamlShardingStrategyConfiguration(tableRuleConfiguration.getDatabaseShardingStrategyConfig()); tableStrategy = new YamlShardingStrategyConfiguration(tableRuleConfiguration.getTableShardingStrategyConfig()); - keyGeneratorColumnName = tableRuleConfiguration.getKeyGeneratorColumnName(); - keyGeneratorClassName = null == tableRuleConfiguration.getKeyGenerator() - ? null : tableRuleConfiguration.getKeyGenerator().getClass().getName(); + keyGenerator = null == tableRuleConfiguration.getKeyGenerator() ? null : getYamlKeyGeneratorConfiguration(tableRuleConfiguration); + } + + private YamlKeyGeneratorConfiguration getYamlKeyGeneratorConfiguration(final TableRuleConfiguration tableRuleConfiguration) { + YamlKeyGeneratorConfiguration result = new YamlKeyGeneratorConfiguration(); + String keyGeneratorClassName = tableRuleConfiguration.getKeyGenerator().getClass().getName(); + Optional keyGeneratorType = KeyGeneratorType.getKeyGeneratorType(keyGeneratorClassName); + if (!keyGeneratorType.isPresent()) { + result.setClassName(keyGeneratorClassName); + } else { + result.setType(keyGeneratorType.get().name()); + } + result.setColumn(tableRuleConfiguration.getKeyGeneratorColumnName()); + result.setProps(tableRuleConfiguration.getKeyGenerator().getProperties()); + return result; } /** @@ -76,10 +86,10 @@ public class YamlTableRuleConfiguration { if (null != tableStrategy) { result.setTableShardingStrategyConfig(tableStrategy.build()); } - if (!Strings.isNullOrEmpty(keyGeneratorClassName)) { - result.setKeyGenerator(KeyGeneratorFactory.newInstance(keyGeneratorClassName)); + if (null != keyGenerator) { + result.setKeyGenerator(keyGenerator.getKeyGenerator()); + result.setKeyGeneratorColumnName(keyGenerator.getColumn()); } - result.setKeyGeneratorColumnName(keyGeneratorColumnName); result.setLogicIndex(logicIndex); return result; } diff --git a/sharding-core/src/test/java/io/shardingsphere/core/AllCoreTests.java b/sharding-core/src/test/java/io/shardingsphere/core/AllCoreTests.java index eeef91b7855948121d220194c93967e6907d886d..6a0277d853028389a486c3ead0ac5b8ca4aa6867 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/AllCoreTests.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/AllCoreTests.java @@ -21,6 +21,7 @@ import io.shardingsphere.core.config.AllConfigTests; import io.shardingsphere.core.constant.AllConstantsTests; import io.shardingsphere.core.executor.AllExecutorTests; import io.shardingsphere.core.hint.AllHintTests; +import io.shardingsphere.core.keygen.AllKeygenTests; import io.shardingsphere.core.merger.AllMergerTests; import io.shardingsphere.core.metadata.AllMetaDataTests; import io.shardingsphere.core.optimizer.AllOptimizerTests; @@ -46,7 +47,8 @@ import org.junit.runners.Suite.SuiteClasses; AllExecutorTests.class, AllMergerTests.class, AllHintTests.class, - AllYamlTests.class + AllYamlTests.class, + AllKeygenTests.class }) public final class AllCoreTests { } diff --git a/sharding-core/src/test/java/io/shardingsphere/core/keygen/AllKeygenTests.java b/sharding-core/src/test/java/io/shardingsphere/core/keygen/AllKeygenTests.java index 04e5910f285d441e9224cd873e08a88522a8c5e0..e79650c2043d577417e9ba936e5ad32aceea5204 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/keygen/AllKeygenTests.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/keygen/AllKeygenTests.java @@ -23,7 +23,7 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ - DefaultKeyGeneratorTest.class, + SnowflakeKeyGeneratorTest.class, KeyGeneratorFactoryTest.class }) public final class AllKeygenTests { diff --git a/sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorFactoryTest.java b/sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorFactoryTest.java index 7397d2fd17f42d115e48f2480cc26c784f844651..6900648e99e9ba464e77b7ac14ae7a798100858e 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorFactoryTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorFactoryTest.java @@ -18,10 +18,14 @@ package io.shardingsphere.core.keygen; import lombok.AccessLevel; +import lombok.Getter; import lombok.NoArgsConstructor; import lombok.RequiredArgsConstructor; +import lombok.Setter; import org.junit.Test; +import java.util.Properties; + import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertThat; @@ -29,7 +33,7 @@ public final class KeyGeneratorFactoryTest { @Test public void assertCreateKeyGeneratorSuccess() { - assertThat(KeyGeneratorFactory.newInstance(DefaultKeyGenerator.class.getName()), instanceOf(DefaultKeyGenerator.class)); + assertThat(KeyGeneratorFactory.newInstance(SnowflakeKeyGenerator.class.getName()), instanceOf(SnowflakeKeyGenerator.class)); } @Test(expected = IllegalArgumentException.class) @@ -46,6 +50,10 @@ public final class KeyGeneratorFactoryTest { public static final class InstantiationKeyGenerator implements KeyGenerator { private final int field; + + @Getter + @Setter + private Properties properties = new Properties(); @Override public Comparable generateKey() { @@ -55,6 +63,10 @@ public final class KeyGeneratorFactoryTest { @NoArgsConstructor(access = AccessLevel.PRIVATE) public static final class IllegalAccessKeyGenerator implements KeyGenerator { + + @Getter + @Setter + private Properties properties = new Properties(); @Override public Comparable generateKey() { diff --git a/sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorTypeTest.java b/sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorTypeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..35b9811d795b829fa646dbf5b5b453214a23c1e3 --- /dev/null +++ b/sharding-core/src/test/java/io/shardingsphere/core/keygen/KeyGeneratorTypeTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.keygen; + +import com.google.common.base.Optional; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class KeyGeneratorTypeTest { + + @Test + public void assertGetKeyGeneratorType() { + assertThat(KeyGeneratorType.getKeyGeneratorType("io.shardingsphere.core.keygen.SnowflakeKeyGenerator"), + is(Optional.of(KeyGeneratorType.SNOWFLAKE))); + assertThat(KeyGeneratorType.getKeyGeneratorType(""), + is(Optional.of(KeyGeneratorType.UUID))); + } + + @Test + public void assertGetKeyGeneratorClassName() { + assertThat(KeyGeneratorType.SNOWFLAKE.getKeyGeneratorClassName(), is("io.shardingsphere.core.keygen.SnowflakeKeyGenerator")); + } +} diff --git a/sharding-core/src/test/java/io/shardingsphere/core/keygen/DefaultKeyGeneratorTest.java b/sharding-core/src/test/java/io/shardingsphere/core/keygen/SnowflakeKeyGeneratorTest.java similarity index 60% rename from sharding-core/src/test/java/io/shardingsphere/core/keygen/DefaultKeyGeneratorTest.java rename to sharding-core/src/test/java/io/shardingsphere/core/keygen/SnowflakeKeyGeneratorTest.java index 61ea30c92e2834ac0ba256aeee1c971b09ac6f0a..7aca6db210e4bd7c8ac575532347a9dbdf97f672 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/keygen/DefaultKeyGeneratorTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/keygen/SnowflakeKeyGeneratorTest.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Properties; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; @@ -35,11 +36,11 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertThat; -public final class DefaultKeyGeneratorTest { +public final class SnowflakeKeyGeneratorTest { - static final long DEFAULT_SEQUENCE_BITS = 12L; + private static final long DEFAULT_SEQUENCE_BITS = 12L; - static final int DEFAULT_KEY_AMOUNT = 10; + private static final int DEFAULT_KEY_AMOUNT = 10; @Test @SneakyThrows @@ -47,7 +48,8 @@ public final class DefaultKeyGeneratorTest { int threadNumber = Runtime.getRuntime().availableProcessors() << 1; ExecutorService executor = Executors.newFixedThreadPool(threadNumber); int taskNumber = threadNumber << 2; - final DefaultKeyGenerator keyGenerator = new DefaultKeyGenerator(); + final SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); + keyGenerator.setProperties(new Properties()); Set> actual = new HashSet<>(); for (int i = 0; i < taskNumber; i++) { actual.add(executor.submit(new Callable>() { @@ -63,9 +65,10 @@ public final class DefaultKeyGeneratorTest { @Test public void assertGenerateKeyWithSingleThread() { + SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); + keyGenerator.setProperties(new Properties()); + SnowflakeKeyGenerator.setTimeService(new FixedTimeService(1)); List> expected = Arrays.>asList(1L, 4194304L, 4194305L, 8388609L, 8388610L, 12582912L, 12582913L, 16777217L, 16777218L, 20971520L); - DefaultKeyGenerator keyGenerator = new DefaultKeyGenerator(); - DefaultKeyGenerator.setTimeService(new FixedTimeService(1)); List> actual = new ArrayList<>(); for (int i = 0; i < DEFAULT_KEY_AMOUNT; i++) { actual.add(keyGenerator.generateKey()); @@ -76,9 +79,10 @@ public final class DefaultKeyGeneratorTest { @Test @SneakyThrows public void assertGenerateKeyWithClockCallBack() { - DefaultKeyGenerator keyGenerator = new DefaultKeyGenerator(); + SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); TimeService timeService = new FixedTimeService(1); - DefaultKeyGenerator.setTimeService(timeService); + SnowflakeKeyGenerator.setTimeService(timeService); + keyGenerator.setProperties(new Properties()); setLastMilliseconds(keyGenerator, timeService.getCurrentMillis() + 2); List> expected = Arrays.>asList(4194305L, 8388608L, 8388609L, 12582913L, 12582914L, 16777216L, 16777217L, 20971521L, 20971522L, 25165824L); List> actual = new ArrayList<>(); @@ -91,10 +95,13 @@ public final class DefaultKeyGeneratorTest { @Test(expected = IllegalStateException.class) @SneakyThrows public void assertGenerateKeyWithClockCallBackBeyondTolerateTime() { - DefaultKeyGenerator keyGenerator = new DefaultKeyGenerator(); + SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); TimeService timeService = new FixedTimeService(1); - DefaultKeyGenerator.setTimeService(timeService); - DefaultKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds(0); + SnowflakeKeyGenerator.setTimeService(timeService); + keyGenerator.setProperties(new Properties()); + Properties properties = new Properties(); + properties.setProperty("max.tolerate.time.difference.milliseconds", String.valueOf(0)); + keyGenerator.setProperties(properties); setLastMilliseconds(keyGenerator, timeService.getCurrentMillis() + 2); List> actual = new ArrayList<>(); for (int i = 0; i < DEFAULT_KEY_AMOUNT; i++) { @@ -105,9 +112,10 @@ public final class DefaultKeyGeneratorTest { @Test public void assertGenerateKeyBeyondMaxSequencePerMilliSecond() { - final DefaultKeyGenerator keyGenerator = new DefaultKeyGenerator(); + final SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); TimeService timeService = new FixedTimeService(2); - DefaultKeyGenerator.setTimeService(timeService); + SnowflakeKeyGenerator.setTimeService(timeService); + keyGenerator.setProperties(new Properties()); setLastMilliseconds(keyGenerator, timeService.getCurrentMillis()); setSequence(keyGenerator, (1 << DEFAULT_SEQUENCE_BITS) - 1); List> expected = Arrays.>asList(4194304L, 4194305L, 4194306L, 8388609L, 8388610L, 8388611L, 12582912L, 12582913L, 12582914L, 16777217L); @@ -119,46 +127,58 @@ public final class DefaultKeyGeneratorTest { } @SneakyThrows - private void setSequence(final DefaultKeyGenerator keyGenerator, final Number value) { - Field sequence = DefaultKeyGenerator.class.getDeclaredField("sequence"); + private void setSequence(final SnowflakeKeyGenerator keyGenerator, final Number value) { + Field sequence = SnowflakeKeyGenerator.class.getDeclaredField("sequence"); sequence.setAccessible(true); sequence.set(keyGenerator, value); } @SneakyThrows - private void setLastMilliseconds(final DefaultKeyGenerator keyGenerator, final Number value) { - Field lastMilliseconds = DefaultKeyGenerator.class.getDeclaredField("lastMilliseconds"); + private void setLastMilliseconds(final SnowflakeKeyGenerator keyGenerator, final Number value) { + Field lastMilliseconds = SnowflakeKeyGenerator.class.getDeclaredField("lastMilliseconds"); lastMilliseconds.setAccessible(true); lastMilliseconds.set(keyGenerator, value); } @Test(expected = IllegalArgumentException.class) public void assertSetWorkerIdFailureWhenNegative() { - DefaultKeyGenerator.setWorkerId(-1L); + SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); + Properties properties = new Properties(); + properties.setProperty("worker.id", String.valueOf(-1L)); + keyGenerator.setProperties(properties); + keyGenerator.generateKey(); } @Test(expected = IllegalArgumentException.class) public void assertSetWorkerIdFailureWhenTooMuch() { - DefaultKeyGenerator.setWorkerId(-Long.MAX_VALUE); + SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); + Properties properties = new Properties(); + properties.setProperty("worker.id", String.valueOf(-Long.MAX_VALUE)); + keyGenerator.setProperties(properties); + keyGenerator.generateKey(); } @Test @SneakyThrows public void assertSetWorkerIdSuccess() { - DefaultKeyGenerator.setWorkerId(1L); - Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId"); - workerIdField.setAccessible(true); - assertThat(workerIdField.getLong(DefaultKeyGenerator.class), is(1L)); - DefaultKeyGenerator.setWorkerId(0L); + SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); + Properties properties = new Properties(); + properties.setProperty("worker.id", String.valueOf(1L)); + keyGenerator.setProperties(properties); + Field props = keyGenerator.getClass().getDeclaredField("properties"); + props.setAccessible(true); + assertThat(((Properties) props.get(keyGenerator)).get("worker.id"), is((Object) "1")); } @Test @SneakyThrows public void assertSetMaxTolerateTimeDifferenceMilliseconds() { - DefaultKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds(1); - Field maxTolerateTimeDifferenceMillisecondsField = DefaultKeyGenerator.class.getDeclaredField("maxTolerateTimeDifferenceMilliseconds"); - maxTolerateTimeDifferenceMillisecondsField.setAccessible(true); - assertThat(maxTolerateTimeDifferenceMillisecondsField.getInt(DefaultKeyGenerator.class), is(1)); - DefaultKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds(10); + SnowflakeKeyGenerator keyGenerator = new SnowflakeKeyGenerator(); + Properties properties = new Properties(); + properties.setProperty("max.tolerate.time.difference.milliseconds", String.valueOf(1)); + keyGenerator.setProperties(properties); + Field props = keyGenerator.getClass().getDeclaredField("properties"); + props.setAccessible(true); + assertThat(((Properties) props.get(keyGenerator)).get("max.tolerate.time.difference.milliseconds"), is((Object) "1")); } } diff --git a/sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/FixedTimeService.java b/sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/FixedTimeService.java index 6f056fd6c50feee23dd080df2ab25cc2fcb7f483..26053d0b18e304818d0b3477e37857585fcb2572 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/FixedTimeService.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/FixedTimeService.java @@ -17,7 +17,7 @@ package io.shardingsphere.core.keygen.fixture; -import io.shardingsphere.core.keygen.DefaultKeyGenerator; +import io.shardingsphere.core.keygen.SnowflakeKeyGenerator; import io.shardingsphere.core.keygen.TimeService; import lombok.RequiredArgsConstructor; @@ -30,7 +30,7 @@ public final class FixedTimeService extends TimeService { private final AtomicInteger invokedTimes = new AtomicInteger(); - private long current = DefaultKeyGenerator.EPOCH; + private long current = SnowflakeKeyGenerator.EPOCH; @Override public long getCurrentMillis() { diff --git a/sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/IncrementKeyGenerator.java b/sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/IncrementKeyGenerator.java index 36a4cba94f540b4084080982d687208311e2c228..01716ad5231149352f578b32939dca4e0c1e9962 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/IncrementKeyGenerator.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/keygen/fixture/IncrementKeyGenerator.java @@ -18,13 +18,20 @@ package io.shardingsphere.core.keygen.fixture; import io.shardingsphere.core.keygen.KeyGenerator; +import lombok.Getter; +import lombok.Setter; +import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; public final class IncrementKeyGenerator implements KeyGenerator { private final AtomicInteger count = new AtomicInteger(); + @Getter + @Setter + private Properties properties = new Properties(); + @Override public Comparable generateKey() { return count.incrementAndGet(); diff --git a/sharding-core/src/test/java/io/shardingsphere/core/rule/ShardingRuleTest.java b/sharding-core/src/test/java/io/shardingsphere/core/rule/ShardingRuleTest.java index 35ec2b84cdf39e27a529c93e4c62d1e31a7af692..07782182fc21b2f6e97df7e712f89676a6bae624 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/rule/ShardingRuleTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/rule/ShardingRuleTest.java @@ -26,7 +26,7 @@ import io.shardingsphere.api.config.strategy.InlineShardingStrategyConfiguration import io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration; import io.shardingsphere.api.config.strategy.StandardShardingStrategyConfiguration; import io.shardingsphere.core.exception.ShardingConfigurationException; -import io.shardingsphere.core.keygen.DefaultKeyGenerator; +import io.shardingsphere.core.keygen.SnowflakeKeyGenerator; import io.shardingsphere.core.keygen.fixture.IncrementKeyGenerator; import io.shardingsphere.core.parsing.parser.context.condition.Column; import io.shardingsphere.core.routing.strategy.inline.InlineShardingStrategy; @@ -73,7 +73,7 @@ public final class ShardingRuleTest { assertTrue(actual.getBroadcastTables().isEmpty()); assertThat(actual.getDefaultDatabaseShardingStrategy(), instanceOf(NoneShardingStrategy.class)); assertThat(actual.getDefaultTableShardingStrategy(), instanceOf(NoneShardingStrategy.class)); - assertThat(actual.getDefaultKeyGenerator(), instanceOf(DefaultKeyGenerator.class)); + assertThat(actual.getDefaultKeyGenerator(), instanceOf(SnowflakeKeyGenerator.class)); } @Test diff --git a/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfigurationTest.java b/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfigurationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0d835215a804a9f5ffd8d2dfa77cc11c5e85f545 --- /dev/null +++ b/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlKeyGeneratorConfigurationTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.yaml.sharding; + +import io.shardingsphere.core.exception.ShardingConfigurationException; +import io.shardingsphere.core.keygen.SnowflakeKeyGenerator; +import org.junit.Test; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class YamlKeyGeneratorConfigurationTest { + + @Test + public void assertGetKeyGeneratorWithClassName() { + YamlKeyGeneratorConfiguration keyGeneratorConfiguration = new YamlKeyGeneratorConfiguration(); + keyGeneratorConfiguration.setClassName("io.shardingsphere.core.keygen.SnowflakeKeyGenerator"); + assertThat(keyGeneratorConfiguration.getKeyGenerator().getClass().getName(), is(SnowflakeKeyGenerator.class.getName())); + } + + @Test + public void assertGetKeyGeneratorWithSnowflakeType() { + YamlKeyGeneratorConfiguration keyGeneratorConfiguration = new YamlKeyGeneratorConfiguration(); + keyGeneratorConfiguration.setType("SNOWFLAKE"); + assertThat(keyGeneratorConfiguration.getKeyGenerator().getClass().getName(), is(SnowflakeKeyGenerator.class.getName())); + } + + @Test + public void assertGetKeyGeneratorWithoutTypeAndClassName() { + YamlKeyGeneratorConfiguration keyGeneratorConfiguration = new YamlKeyGeneratorConfiguration(); + assertThat(keyGeneratorConfiguration.getKeyGenerator().getClass().getName(), is(SnowflakeKeyGenerator.class.getName())); + } + + @Test(expected = IllegalArgumentException.class) + public void assertGetKeyGeneratorClassNameWithLeaf() { + YamlKeyGeneratorConfiguration keyGeneratorConfiguration = new YamlKeyGeneratorConfiguration(); + keyGeneratorConfiguration.setType("LEAF"); + keyGeneratorConfiguration.getKeyGenerator(); + } + + @Test(expected = IllegalArgumentException.class) + public void assertGetKeyGeneratorClassNameWithUUID() { + YamlKeyGeneratorConfiguration keyGeneratorConfiguration = new YamlKeyGeneratorConfiguration(); + keyGeneratorConfiguration.setType("UUID"); + keyGeneratorConfiguration.getKeyGenerator(); + } + + @Test(expected = ShardingConfigurationException.class) + public void assertGetKeyGeneratorClassNameWithException() { + YamlKeyGeneratorConfiguration keyGeneratorConfiguration = new YamlKeyGeneratorConfiguration(); + keyGeneratorConfiguration.setType("DEFAULT"); + keyGeneratorConfiguration.getKeyGenerator(); + } +} diff --git a/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingConfigurationTest.java b/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingConfigurationTest.java index 0fca439b25fcd0bfcddfcda53643b8b7ecb58dec..d7d8ebe2aa35aae513d6927bc56e0065592961a2 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingConfigurationTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingConfigurationTest.java @@ -18,7 +18,7 @@ package io.shardingsphere.core.yaml.sharding; import io.shardingsphere.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType; -import io.shardingsphere.core.keygen.DefaultKeyGenerator; +import io.shardingsphere.core.keygen.SnowflakeKeyGenerator; import org.hamcrest.CoreMatchers; import org.junit.Test; @@ -104,8 +104,8 @@ public final class YamlShardingConfigurationTest { assertThat(actual.getShardingRule().getTables().get("t_order").getActualDataNodes(), is("ds_${0..1}.t_order_${0..1}")); assertThat(actual.getShardingRule().getTables().get("t_order").getTableStrategy().getInline().getShardingColumn(), is("order_id")); assertThat(actual.getShardingRule().getTables().get("t_order").getTableStrategy().getInline().getAlgorithmExpression(), is("t_order_${order_id % 2}")); - assertThat(actual.getShardingRule().getTables().get("t_order").getKeyGeneratorColumnName(), is("order_id")); - assertThat(actual.getShardingRule().getTables().get("t_order").getKeyGeneratorClassName(), is(DefaultKeyGenerator.class.getName())); + assertThat(actual.getShardingRule().getTables().get("t_order").getKeyGenerator().getColumn(), is("order_id")); + assertThat(actual.getShardingRule().getTables().get("t_order").getKeyGenerator().getClassName(), is(SnowflakeKeyGenerator.class.getName())); assertThat(actual.getShardingRule().getTables().get("t_order").getLogicIndex(), is("order_index")); } @@ -130,7 +130,7 @@ public final class YamlShardingConfigurationTest { assertThat(actual.getShardingRule().getDefaultDataSourceName(), is("default_ds")); assertThat(actual.getShardingRule().getDefaultDatabaseStrategy().getInline().getShardingColumn(), is("order_id")); assertThat(actual.getShardingRule().getDefaultDatabaseStrategy().getInline().getAlgorithmExpression(), is("ds_${order_id % 2}")); - assertThat(actual.getShardingRule().getDefaultKeyGeneratorClassName(), is(DefaultKeyGenerator.class.getName())); + assertThat(actual.getShardingRule().getDefaultKeyGenerator().getKeyGenerator().getClass().getName(), is(SnowflakeKeyGenerator.class.getName())); } private void assertMasterSlaveRules(final YamlShardingConfiguration actual) { diff --git a/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfigurationTest.java b/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfigurationTest.java index a31c8607a6e1d1f68387c7684276e640e6a67e2c..b7e3aeedf589f28600653ea758f080a101d16abe 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfigurationTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlShardingRuleConfigurationTest.java @@ -23,7 +23,7 @@ import io.shardingsphere.api.config.rule.MasterSlaveRuleConfiguration; import io.shardingsphere.api.config.rule.ShardingRuleConfiguration; import io.shardingsphere.api.config.rule.TableRuleConfiguration; import io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration; -import io.shardingsphere.core.keygen.DefaultKeyGenerator; +import io.shardingsphere.core.keygen.SnowflakeKeyGenerator; import io.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration; import io.shardingsphere.core.yaml.sharding.strategy.YamlNoneShardingStrategyConfiguration; import org.hamcrest.CoreMatchers; @@ -61,7 +61,9 @@ public final class YamlShardingRuleConfigurationTest { result.getTables().put("t_order_item", new YamlTableRuleConfiguration()); result.getBindingTables().add("t_order, t_order_item"); result.getBroadcastTables().add("t_config"); - result.setDefaultKeyGeneratorClassName(DefaultKeyGenerator.class.getName()); + YamlKeyGeneratorConfiguration keyGeneratorConfiguration = new YamlKeyGeneratorConfiguration(); + keyGeneratorConfiguration.setClassName(SnowflakeKeyGenerator.class.getName()); + result.setDefaultKeyGenerator(keyGeneratorConfiguration); result.getMasterSlaveRules().put("master_slave_ds", createYamlMasterSlaveRuleConfig()); return result; } @@ -95,7 +97,7 @@ public final class YamlShardingRuleConfigurationTest { assertThat(actual.getBindingTableGroups().iterator().next(), is("t_order, t_order_item")); assertThat(actual.getBroadcastTables().size(), is(1)); assertThat(actual.getBroadcastTables().iterator().next(), is("t_config")); - assertThat(actual.getDefaultKeyGenerator(), instanceOf(DefaultKeyGenerator.class)); + assertThat(actual.getDefaultKeyGenerator(), instanceOf(SnowflakeKeyGenerator.class)); assertMasterSlaveRuleConfig(actual.getMasterSlaveRuleConfigs().iterator().next()); } diff --git a/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfigurationTest.java b/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfigurationTest.java index 8c3b29f79ecdb1ff0d434050ce49eb5b50a537e0..6cd9555ea53846250aae5427fa6538c29d1564f3 100644 --- a/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfigurationTest.java +++ b/sharding-core/src/test/java/io/shardingsphere/core/yaml/sharding/YamlTableRuleConfigurationTest.java @@ -19,7 +19,7 @@ package io.shardingsphere.core.yaml.sharding; import io.shardingsphere.api.config.rule.TableRuleConfiguration; import io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration; -import io.shardingsphere.core.keygen.DefaultKeyGenerator; +import io.shardingsphere.core.keygen.SnowflakeKeyGenerator; import io.shardingsphere.core.yaml.sharding.strategy.YamlNoneShardingStrategyConfiguration; import org.junit.Test; @@ -53,8 +53,9 @@ public final class YamlTableRuleConfigurationTest { YamlTableRuleConfiguration result = new YamlTableRuleConfiguration(); result.setLogicTable("t_order"); result.setActualDataNodes("ds_${0..1}.t_order_${0..1}"); - result.setKeyGeneratorColumnName("order_id"); - result.setKeyGeneratorClassName(DefaultKeyGenerator.class.getName()); + result.setKeyGenerator(new YamlKeyGeneratorConfiguration()); + result.getKeyGenerator().setColumn("order_id"); + result.getKeyGenerator().setClassName(SnowflakeKeyGenerator.class.getName()); result.setLogicIndex("order_index"); return result; } @@ -72,7 +73,7 @@ public final class YamlTableRuleConfigurationTest { assertThat(actual.getLogicTable(), is("t_order")); assertThat(actual.getActualDataNodes(), is("ds_${0..1}.t_order_${0..1}")); assertThat(actual.getKeyGeneratorColumnName(), is("order_id")); - assertThat(actual.getKeyGenerator(), instanceOf(DefaultKeyGenerator.class)); + assertThat(actual.getKeyGenerator(), instanceOf(SnowflakeKeyGenerator.class)); assertThat(actual.getLogicIndex(), is("order_index")); } diff --git a/sharding-core/src/test/resources/yaml/optimize-rule.yaml b/sharding-core/src/test/resources/yaml/optimize-rule.yaml index 5ccf5fc85ee3aac887e2071852d1e51958f58256..5c0a92f045d7d7e36845e7244c2cb20623d121f0 100644 --- a/sharding-core/src/test/resources/yaml/optimize-rule.yaml +++ b/sharding-core/src/test/resources/yaml/optimize-rule.yaml @@ -22,6 +22,7 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.core.keygen.SnowflakeKeyGenerator logicIndex: order_index diff --git a/sharding-core/src/test/resources/yaml/parser-rule.yaml b/sharding-core/src/test/resources/yaml/parser-rule.yaml index 4ebcfe56865e672fe172d57c3b7f01e005c2b8d3..5117bed6f7d0c6c504ed83dff56da01f28995a06 100644 --- a/sharding-core/src/test/resources/yaml/parser-rule.yaml +++ b/sharding-core/src/test/resources/yaml/parser-rule.yaml @@ -25,7 +25,9 @@ shardingRule: complex: shardingColumns: user_id, order_id, item_id algorithmClassName: io.shardingsphere.api.algorithm.fixture.TestComplexKeysShardingAlgorithm - keyGeneratorColumnName: item_id + keyGenerator: + column: item_id + className: io.shardingsphere.core.keygen.SnowflakeKeyGenerator t_place: actualDataNodes: db${0..1}.t_place tableStrategy: @@ -34,4 +36,5 @@ shardingRule: algorithmClassName: io.shardingsphere.api.algorithm.fixture.TestComplexKeysShardingAlgorithm bindingTables: - t_order, t_order_item - defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.fixture.IncrementKeyGenerator + defaultKeyGenerator: + type: SNOWFLAKE diff --git a/sharding-core/src/test/resources/yaml/rewrite-rule.yaml b/sharding-core/src/test/resources/yaml/rewrite-rule.yaml index 06719ca16986b51e77aa784773040cc4c2a69b56..1c9e9b148cfce0ef1b9df30f41b1ffe3dd6e64bc 100644 --- a/sharding-core/src/test/resources/yaml/rewrite-rule.yaml +++ b/sharding-core/src/test/resources/yaml/rewrite-rule.yaml @@ -14,7 +14,9 @@ shardingRule: tables: table_x: actualDataNodes: db${0..1}.table_x - keyGeneratorColumnName: id + keyGenerator: + column: id + type: SNOWFLAKE logicIndex: logic_index table_y: actualDataNodes: db${0..1}.table_y diff --git a/sharding-core/src/test/resources/yaml/sharding-rule.yaml b/sharding-core/src/test/resources/yaml/sharding-rule.yaml index 87d93909b7668b40538d2f4eea7a7c36085a4ad0..c0d89949560d760cde3b1640b7905764db072da1 100644 --- a/sharding-core/src/test/resources/yaml/sharding-rule.yaml +++ b/sharding-core/src/test/resources/yaml/sharding-rule.yaml @@ -61,8 +61,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.core.keygen.SnowflakeKeyGenerator logicIndex: order_index t_order_item: actualDataNodes: ds_${0..1}.t_order_item_${0..1} @@ -82,7 +83,8 @@ shardingRule: algorithmExpression: ds_${order_id % 2} defaultTableStrategy: none: - defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator + defaultKeyGenerator: + type: SNOWFLAKE masterSlaveRules: ds_0: diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java b/sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java index c75d405e0ce618b03dfe8af519d94bfee386f426..7f95951a43cdaf9d76a0d39e9e0eaac3bd255523 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java +++ b/sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/dbtest/fixture/ConstantKeyGenerator.java @@ -18,9 +18,17 @@ package io.shardingsphere.dbtest.fixture; import io.shardingsphere.core.keygen.KeyGenerator; +import lombok.Getter; +import lombok.Setter; + +import java.util.Properties; public final class ConstantKeyGenerator implements KeyGenerator { + @Getter + @Setter + private Properties properties = new Properties(); + @Override public Comparable generateKey() { return 1; diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/shardingjdbc/fixture/IncrementKeyGenerator.java b/sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/shardingjdbc/fixture/IncrementKeyGenerator.java index e7ae9fd981136b9aef8d08d794253ce0618bc7cc..58767be1e66a7a2044499100731ba4995c3500b8 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/shardingjdbc/fixture/IncrementKeyGenerator.java +++ b/sharding-jdbc/sharding-jdbc-core/src/test/java/io/shardingsphere/shardingjdbc/fixture/IncrementKeyGenerator.java @@ -18,13 +18,20 @@ package io.shardingsphere.shardingjdbc.fixture; import io.shardingsphere.core.keygen.KeyGenerator; +import lombok.Getter; +import lombok.Setter; +import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; public final class IncrementKeyGenerator implements KeyGenerator { private final AtomicInteger count = new AtomicInteger(); + @Getter + @Setter + private Properties properties = new Properties(); + @Override public Comparable generateKey() { return count.incrementAndGet(); diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/db/sharding-rule.yaml b/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/db/sharding-rule.yaml index 56da490f0fe8b04e8feced97f103ee5b0fc38987..29e59b2056461c248554a27d0980159956ee90fc 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/db/sharding-rule.yaml +++ b/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/db/sharding-rule.yaml @@ -15,8 +15,9 @@ shardingRule: shardingColumn: user_id preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm - keyGeneratorColumnName: item_id - keyGeneratorClassName: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator + keyGenerator: + column: item_id + className: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: db_0 diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/dbtbl_with_masterslave/sharding-rule.yaml b/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/dbtbl_with_masterslave/sharding-rule.yaml index 4ba2079d161769fc0e0744609d7281736dd8e31e..bafc16f3e9beb12b9db7a2dbb366e74bd0bd85ad 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/dbtbl_with_masterslave/sharding-rule.yaml +++ b/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/dbtbl_with_masterslave/sharding-rule.yaml @@ -27,8 +27,9 @@ shardingRule: shardingColumn: order_id preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm - keyGeneratorColumnName: item_id - keyGeneratorClassName: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator + keyGenerator: + column: item_id + className: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: db_ms_0 diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/tbl/sharding-rule.yaml b/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/tbl/sharding-rule.yaml index e302316703dd7b6ea8a474c8c5d17f1c4e9542e0..14d2ba74014d1861f868cdb11d8d12f3044334c0 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/tbl/sharding-rule.yaml +++ b/sharding-jdbc/sharding-jdbc-core/src/test/resources/integrate/env/tbl/sharding-rule.yaml @@ -15,8 +15,9 @@ shardingRule: shardingColumn: order_id preciseAlgorithmClassName: io.shardingsphere.dbtest.fixture.PreciseModuloAlgorithm rangeAlgorithmClassName: io.shardingsphere.dbtest.fixture.RangeModuloAlgorithm - keyGeneratorColumnName: item_id - keyGeneratorClassName: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator + keyGenerator: + column: item_id + className: io.shardingsphere.dbtest.fixture.ConstantKeyGenerator bindingTables: - t_order,t_order_item defaultDataSourceName: tbl diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/DecrementKeyGenerator.java b/sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/DecrementKeyGenerator.java index 483521e0f050f3cc71891f9c06bc562c0225d4b3..0bedb1ec6d8ac61ebf0c044d1dc1370d66835dd8 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/DecrementKeyGenerator.java +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/DecrementKeyGenerator.java @@ -18,13 +18,20 @@ package io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture; import io.shardingsphere.core.keygen.KeyGenerator; +import lombok.Getter; +import lombok.Setter; +import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; public final class DecrementKeyGenerator implements KeyGenerator { private final AtomicInteger sequence = new AtomicInteger(100); + @Getter + @Setter + private Properties properties = new Properties(); + @Override public Comparable generateKey() { return sequence.decrementAndGet(); diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/IncrementKeyGenerator.java b/sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/IncrementKeyGenerator.java index effb1328a39847a29c2c7e8131fb96ad9080aeed..43c97b0bfdf7375bf128dc633f53bdf841a2b4c2 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/IncrementKeyGenerator.java +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/java/io/shardingsphere/shardingjdbc/orchestration/api/yaml/fixture/IncrementKeyGenerator.java @@ -18,13 +18,20 @@ package io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture; import io.shardingsphere.core.keygen.KeyGenerator; +import lombok.Getter; +import lombok.Setter; +import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; public final class IncrementKeyGenerator implements KeyGenerator { private static final AtomicInteger SEQUENCE = new AtomicInteger(100); + @Getter + @Setter + private Properties properties = new Properties(); + @Override public Comparable generateKey() { return SEQUENCE.incrementAndGet(); diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithProps.yaml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithProps.yaml index a737dd9d54aecb790ffa2cb12858d87133b02b3d..d0acb68c2538a1b8f931c68fc5d45c2bbef99d18 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithProps.yaml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithProps.yaml @@ -27,8 +27,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator t_order_item: actualDataNodes: db${0..1}.t_order_item_${0..1} diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithoutProps.yaml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithoutProps.yaml index f995c543f64a60f4ad517b2e488757aed05fda02..a200edf344dbfcf9e774358508da52737e6c679c 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithoutProps.yaml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithDataSourceWithoutProps.yaml @@ -27,8 +27,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator t_order_item: actualDataNodes: db${0..1}.t_order_item_${0..1} diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithProps.yaml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithProps.yaml index 70b95f3a3a968c5d90c1547d97f49c1e8cfb49bf..aa27d4a0b92718c5ba8250dae4855d6f025ed8e8 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithProps.yaml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithProps.yaml @@ -13,8 +13,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator t_order_item: actualDataNodes: db${0..1}.t_order_item_${0..1} diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithoutProps.yaml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithoutProps.yaml index a94deef1f82505cc9902605d3f0b8291e87adf39..cd4b833d40681c375d3f6eee8d1cf671c5005713 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithoutProps.yaml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding/configWithoutDataSourceWithoutProps.yaml @@ -12,8 +12,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator t_order_item: actualDataNodes: db${0..1}.t_order_item_${0..1} diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithProps.yaml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithProps.yaml index 5d619911dce5c4b879a97e8191536949cf49ef00..8c3c107bf03ad83dde340695c4199d770a1d9136 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithProps.yaml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithProps.yaml @@ -39,8 +39,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator t_order_item: actualDataNodes: db_ms_${0..1}.t_order_item_${0..1} diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithoutProps.yaml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithoutProps.yaml index b8fb93dde80d08a801d0e19cad3065137345ccd1..56d7706b2dd6efa50bef3418a733604de691e437 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithoutProps.yaml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithDataSourceWithoutProps.yaml @@ -39,8 +39,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator t_order_item: actualDataNodes: db_ms_${0..1}.t_order_item_${0..1} diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithProps.yaml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithProps.yaml index 8bae83a55d8e85a31a1568c310d05af38e804be2..6409dbd70c89ca781360dc32286b03f8a0872d47 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithProps.yaml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithProps.yaml @@ -13,8 +13,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator t_order_item: actualDataNodes: db_ms_${0..1}.t_order_item_${0..1} diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithoutProps.yaml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithoutProps.yaml index ee5c3d7bddea53c15a5e1eaa95f2f3260424ee83..87551810a5428a2d690b9dcc6a13ea572836ccce 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithoutProps.yaml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/yaml/integrate/sharding_ms/configWithoutDataSourceWithoutProps.yaml @@ -13,8 +13,9 @@ shardingRule: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} - keyGeneratorColumnName: order_id - keyGeneratorClassName: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator + keyGenerator: + column: order_id + className: io.shardingsphere.shardingjdbc.orchestration.api.yaml.fixture.IncrementKeyGenerator t_order_item: actualDataNodes: db_ms_${0..1}.t_order_item_${0..1} diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/internal/registry/config/service/ConfigurationServiceTest.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/internal/registry/config/service/ConfigurationServiceTest.java index fe5bc402ba179797740e095853b9c843d489227d..104ec512d23925f197adae8a15981e770b430a33 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/internal/registry/config/service/ConfigurationServiceTest.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/internal/registry/config/service/ConfigurationServiceTest.java @@ -70,18 +70,12 @@ public final class ConfigurationServiceTest { + "ds_1: !!io.shardingsphere.core.rule.DataSourceParameter\n" + " url: jdbc:mysql://localhost:3306/ds_1\n" + " username: root\n" + " password: root\n"; - private static final String SHARDING_RULE_YAML = "tables:\n" + " t_order:\n" - + " actualDataNodes: ds_${0..1}.t_order_${0..1}\n" + " logicTable: t_order\n" + " " - + "tableStrategy:\n" + " inline:\n" + " algorithmExpression: t_order_${order_id % 2}\n" + " shardingColumn: order_id\n"; + private static final String SHARDING_RULE_YAML = "tables:\n" + " t_order:\n" + " actualDataNodes: ds_${0..1}.t_order_${0..1}\n" + " keyGenerator:\n" + + " type: SNOWFLAKE\n" + " logicTable: t_order\n" + " tableStrategy:\n" + " inline:\n" + " algorithmExpression: t_order_${order_id % 2}\n" + + " shardingColumn: order_id\n"; private static final String MASTER_SLAVE_RULE_YAML = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n" + "- slave_ds_0\n" + "- slave_ds_1\n"; - private static final String SHARDING_MASTER_SLAVE_RULE_YAML = "tables:\n" + " t_order: \n" + " actualDataNodes: db_ms_${0..1}.t_order_${0..1}\n" + " databaseStrategy: \n" - + " inline:\n" + " shardingColumn: user_id\n" + " algorithmExpression: db_ms_${user_id % 2}\n" + " tableStrategy: \n" + " inline:\n" - + " shardingColumn: order_id\n" + " algorithmExpression: t_order_${order_id % 2}\n" + " keyGeneratorColumnName: order_id\n" - + "bindingTables:\n" + " - t_order,t_order_item\n" + "masterSlaveRules:\n" + " db_ms_0:\n" + " masterDataSourceName: db0_master\n" + " slaveDataSourceNames:\n" - + " - db0_slave\n" + " db_ms_1:\n" + " masterDataSourceName: db1_master\n" + " slaveDataSourceNames:\n" + " - db1_slave"; - private static final String AUTHENTICATION_YAML = "password: root\n" + "username: root\n"; private static final String CONFIG_MAP_YAML = "{}\n"; diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/AllYamlTests.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/AllYamlTests.java index 912827bf2577adc77a64cf4e55f72b259adbf1f5..fb594e559fc7a43f1354808820eb5af853263269 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/AllYamlTests.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/AllYamlTests.java @@ -23,6 +23,7 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ + ConfigurationYamlConverterTest.class, DefaultYamlRepresenterTest.class, YamlOrchestrationConfigurationTest.class, YamlDataSourceConfigurationTest.class diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/ConfigurationYamlConverterTest.java b/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/ConfigurationYamlConverterTest.java index a2c1b50fa932d81273e9d7c8d987166ea4dccb7f..b1170828ebf9d40a8cd195df08650704967bf239 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/ConfigurationYamlConverterTest.java +++ b/sharding-orchestration/sharding-orchestration-core/src/test/java/io/shardingsphere/orchestration/yaml/ConfigurationYamlConverterTest.java @@ -43,12 +43,12 @@ public class ConfigurationYamlConverterTest { + " dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n" + " properties:\n" + " url: jdbc:mysql://localhost:3306/demo_ds_master\n" + " username: root\n" + " password: null\n"; - private static final String SHARDING_RULE_YAML = " tables:\n" + " t_order:\n" + " actualDataNodes: ds_${0..1}.t_order_${0..1}\n" + " tableStrategy:\n" - + " inline:\n" + " shardingColumn: order_id\n" + " algorithmExpression: t_order_${order_id % 2}\n" - + " keyGeneratorColumnName: order_id\n" + " t_order_item:\n" + " actualDataNodes: ds_${0..1}.t_order_item_${0..1}\n" + " tableStrategy:\n" + " inline:\n" - + " shardingColumn: order_id\n" + " algorithmExpression: t_order_item_${order_id % 2}\n" + " keyGeneratorColumnName: order_item_id\n" + " bindingTables:\n" - + " - t_order,t_order_item\n" + " defaultDataSourceName: ds_1\n" + " defaultDatabaseStrategy:\n" + " inline:\n" + " shardingColumn: user_id\n" - + " algorithmExpression: ds_${user_id % 2}"; + private static final String SHARDING_RULE_YAML = " tables:\n" + " t_order:\n" + " actualDataNodes: ds_${0..1}.t_order_${0..1}\n" + " tableStrategy:\n" + + " inline:\n" + " shardingColumn: order_id\n" + " algorithmExpression: t_order_${order_id % 2}\n" + " keyGenerator:\n" + + " column: order_id\n" + " t_order_item:\n" + " actualDataNodes: ds_${0..1}.t_order_item_${0..1}\n" + " tableStrategy:\n" + + " inline:\n" + " shardingColumn: order_id\n" + " algorithmExpression: t_order_item_${order_id % 2}\n" + " keyGenerator:\n" + + " column: order_item_id\n" + " bindingTables:\n" + " - t_order,t_order_item\n" + " defaultDataSourceName: ds_1\n" + + " defaultDatabaseStrategy:\n" + " inline:\n" + " shardingColumn: user_id\n" + " algorithmExpression: ds_${user_id % 2}"; private static final String MASTER_SLAVE_RULE_YAML = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n" + "- slave_ds_0\n" + "- slave_ds_1\n"; diff --git a/sharding-proxy/src/main/resources/conf/config-sharding.yaml b/sharding-proxy/src/main/resources/conf/config-sharding.yaml index 17002ae04a89577b24f7a464cb38491435682ae1..2e80ed933c1b6aae34151bfb7a9de5fee20199c1 100644 --- a/sharding-proxy/src/main/resources/conf/config-sharding.yaml +++ b/sharding-proxy/src/main/resources/conf/config-sharding.yaml @@ -7,7 +7,7 @@ # if you want to use master-slave, please refer to the config-master_slave.yaml. # ###################################################################################################### - +# #schemaName: sharding_db # #dataSources: @@ -36,14 +36,16 @@ # inline: # shardingColumn: order_id # algorithmExpression: t_order_${order_id % 2} -# keyGeneratorColumnName: order_id +# keyGenerator: +# column: order_id # t_order_item: # actualDataNodes: ds_${0..1}.t_order_item_${0..1} # tableStrategy: # inline: # shardingColumn: order_id # algorithmExpression: t_order_item_${order_id % 2} -# keyGeneratorColumnName: order_item_id +# keyGenerator: +# column: order_item_id # bindingTables: # - t_order,t_order_item # defaultDatabaseStrategy: @@ -52,4 +54,5 @@ # algorithmExpression: ds_${user_id % 2} # defaultTableStrategy: # none: -# defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator +# defaultKeyGenerator: +# className: io.shardingsphere.core.keygen.SnowflakeKeyGenerator