未验证 提交 81124ed9 编写于 作者: ShardingSphere's avatar ShardingSphere 提交者: GitHub

Merge pull request #1728 from tristaZero/dev

Modify API for key generator
......@@ -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);
}
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
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<KeyGeneratorType> getKeyGeneratorType(final String keyGeneratorClassName) {
for (KeyGeneratorType each : KeyGeneratorType.values()) {
if (each.getKeyGeneratorClassName().equals(keyGeneratorClassName)) {
return Optional.of(each);
}
}
return Optional.absent();
}
}
......@@ -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.
*
* <p>
* Use snowflake algorithm. Length is 64 bit.
* </p>
*
* <pre>
* 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
* </pre>
*
* <p>
* Call @{@code DefaultKeyGenerator.setWorkerId} to set worker id, default value is 0.
* Call @{@code SnowflakeKeyGenerator.setWorkerId} to set worker id, default value is 0.
* </p>
*
* <p>
* 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.
* </p>
*
* @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;
......
......@@ -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<MasterSlaveRule> createMasterSlaveRules(final Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigurations) {
......
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
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.");
}
}
......@@ -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<String, YamlMasterSlaveRuleConfiguration> 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 = 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<MasterSlaveRuleConfiguration> masterSlaveRuleConfigs = new LinkedList<>();
for (Entry<String, YamlMasterSlaveRuleConfiguration> entry : masterSlaveRules.entrySet()) {
......
......@@ -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 = 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;
}
......
......@@ -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 {
}
......@@ -23,7 +23,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
DefaultKeyGeneratorTest.class,
SnowflakeKeyGeneratorTest.class,
KeyGeneratorFactoryTest.class
})
public final class AllKeygenTests {
......
......@@ -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() {
......
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
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"));
}
}
......@@ -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<Comparable<?>> actual = new HashSet<>();
for (int i = 0; i < taskNumber; i++) {
actual.add(executor.submit(new Callable<Comparable<?>>() {
......@@ -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<Comparable<?>> expected = Arrays.<Comparable<?>>asList(1L, 4194304L, 4194305L, 8388609L, 8388610L, 12582912L, 12582913L, 16777217L, 16777218L, 20971520L);
DefaultKeyGenerator keyGenerator = new DefaultKeyGenerator();
DefaultKeyGenerator.setTimeService(new FixedTimeService(1));
List<Comparable<?>> 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<Comparable<?>> expected = Arrays.<Comparable<?>>asList(4194305L, 8388608L, 8388609L, 12582913L, 12582914L, 16777216L, 16777217L, 20971521L, 20971522L, 25165824L);
List<Comparable<?>> 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<Comparable<?>> 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<Comparable<?>> expected = Arrays.<Comparable<?>>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"));
}
}
......@@ -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() {
......
......@@ -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();
......
......@@ -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
......
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/
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();
}
}
......@@ -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) {
......
......@@ -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());
}
......
......@@ -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"));
}
......
......@@ -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
......@@ -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
......@@ -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
......
......@@ -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:
......
......@@ -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;
......
......@@ -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();
......
......@@ -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
......@@ -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
......
......@@ -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
......@@ -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();
......
......@@ -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();
......
......@@ -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}
......
......@@ -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}
......
......@@ -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}
......
......@@ -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}
......
......@@ -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}
......
......@@ -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}
......
......@@ -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}
......
......@@ -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}
......
......@@ -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";
......
......@@ -23,6 +23,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
ConfigurationYamlConverterTest.class,
DefaultYamlRepresenterTest.class,
YamlOrchestrationConfigurationTest.class,
YamlDataSourceConfigurationTest.class
......
......@@ -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";
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册