提交 9fc2e916 编写于 作者: W wgy8283335

Modify LeafSegmentKeyGenerator and add test cases.

上级 7cf5a285
......@@ -47,8 +47,6 @@ public final class LeafSegmentKeyGenerator implements ShardingKeyGenerator {
private static final String DEFAULT_REGISTRY_CENTER = "zookeeper";
private static final float DEFAULT_THRESHOLD = 0.5F;
private static final String SLANTING_BAR = "/";
private static final String REGULAR_PATTERN = "^((?!/).)*$";
......@@ -82,73 +80,81 @@ public final class LeafSegmentKeyGenerator implements ShardingKeyGenerator {
String leafKey = getLeafKey();
if (null == leafRegistryCenter) {
initLeafSegmentKeyGenerator(leafKey);
return id;
}else{
increaseIdWhenLeafKeyStoredInCenter(leafKey);
}
increaseIdWhenLeafKeyStoredInCenter(leafKey);
return id;
}
private void initLeafSegmentKeyGenerator(final String leafKey) {
RegistryCenterConfiguration leafConfiguration = getRegistryCenterConfiguration();
leafRegistryCenter = new RegistryCenterServiceLoader().load(leafConfiguration);
if (leafRegistryCenter.isExisted(leafKey)) {
id = incrementCacheId(leafKey, getStep());
} else {
id = getInitialValue();
leafRegistryCenter.persist(leafKey, String.valueOf(id));
leafRegistryCenter.initLock(leafKey);
}
step = getStep();
id = initializeId(leafKey);
initializeLeafKeyInCenter(leafKey, id, step);
initializeCacheIdAsynchronous(id, step);
}
private void increaseIdWhenLeafKeyStoredInCenter(final String leafKey) {
++id;
if (((id % step) >= (step * DEFAULT_THRESHOLD - 1)) && cacheIdQueue.isEmpty()) {
incrementCacheIdAsynchronous(leafKey, step);
}
if ((id % step) == (step - 1)) {
if ((id % step) == 0) {
id = tryTakeCacheId();
incrementCacheIdAsynchronous(leafKey, step - (id % step));
}
++id;
}
private RegistryCenterConfiguration getRegistryCenterConfiguration() {
RegistryCenterConfiguration result = new RegistryCenterConfiguration(getRegistryCenterType(), properties);
result.setNamespace(DEFAULT_NAMESPACE);
result.setServerLists(getServerList());
result.setDigest(getDigest());
return result;
private long initializeId(final String leafKey){
if (leafRegistryCenter.isExisted(leafKey)) {
return Long.parseLong(leafRegistryCenter.getDirectly(leafKey))+1;
} else {
return getInitialValue();
}
}
private void incrementCacheIdAsynchronous(final String leafKey, final long step) {
private void initializeLeafKeyInCenter(final String leafKey, final long id, final long step){
leafRegistryCenter.initLock(leafKey);
while(!leafRegistryCenter.tryLock()){
}
leafRegistryCenter.persist(leafKey, String.valueOf(id + step - id % step));
leafRegistryCenter.tryRelease();
}
private void initializeCacheIdAsynchronous(final long id, final long step){
incrementCacheIdExecutor.execute(new Runnable() {
@Override
public void run() {
long id = incrementCacheId(leafKey, step);
tryPutCacheId(id);
tryPutCacheId(id + step - id % step);
}
});
}
@SneakyThrows
private long incrementCacheId(final String leafKey, final long step) {
long result = Long.MIN_VALUE;
boolean lockIsAcquired = leafRegistryCenter.tryLock();
if (lockIsAcquired) {
result = updateCacheIdInCenter(leafKey, step);
leafRegistryCenter.tryRelease();
}
return result;
private void incrementCacheIdAsynchronous(final String leafKey, final long step) {
incrementCacheIdExecutor.execute(new Runnable() {
@Override
public void run() {
long newId = incrementCacheId(leafKey, step);
tryPutCacheId(newId);
}
});
}
@SneakyThrows
private void tryPutCacheId(final long id) {
cacheIdQueue.put(id);
private RegistryCenterConfiguration getRegistryCenterConfiguration() {
RegistryCenterConfiguration result = new RegistryCenterConfiguration(getRegistryCenterType(), properties);
result.setNamespace(DEFAULT_NAMESPACE);
result.setServerLists(getServerList());
result.setDigest(getDigest());
return result;
}
@SneakyThrows
private long tryTakeCacheId() {
return cacheIdQueue.take();
private long incrementCacheId(final String leafKey, final long step) {
while(!leafRegistryCenter.tryLock()){
}
long result = updateCacheIdInCenter(leafKey, step);
leafRegistryCenter.tryRelease();
return result;
}
private long updateCacheIdInCenter(final String leafKey, final long step) {
......@@ -162,6 +168,16 @@ public final class LeafSegmentKeyGenerator implements ShardingKeyGenerator {
return result;
}
@SneakyThrows
private void tryPutCacheId(final long id) {
cacheIdQueue.put(id);
}
@SneakyThrows
private long tryTakeCacheId() {
return cacheIdQueue.take();
}
private long getStep() {
long result = Long.parseLong(properties.getProperty("step", DEFAULT_STEP));
Preconditions.checkArgument(result > 0L && result < Long.MAX_VALUE);
......
......@@ -17,7 +17,9 @@
package org.apache.shardingsphere.orchestration.internal.keygen;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -32,6 +34,7 @@ import java.util.concurrent.Executors;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public final class LeafSegmentKeyGeneratorTest {
private final LeafSegmentKeyGenerator leafSegmentKeyGenerator = new LeafSegmentKeyGenerator();
......@@ -54,7 +57,7 @@ public final class LeafSegmentKeyGeneratorTest {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "3");
properties.setProperty("step", "5");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_1");
properties.setProperty("registryCenterType", "ThirdTestRegistryCenter");
......@@ -67,6 +70,42 @@ public final class LeafSegmentKeyGeneratorTest {
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithFirstSpecialStep() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "3");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_6");
properties.setProperty("registryCenterType", "ThirdTestRegistryCenter");
leafSegmentKeyGenerator.setProperties(properties);
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(100001L, 100002L, 100003L, 100004L, 100005L, 100006L, 100007L, 100008L, 100009L, 100010L);
List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) {
actual.add(leafSegmentKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithSecondSpecialStep() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "7");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_7");
properties.setProperty("registryCenterType", "ThirdTestRegistryCenter");
leafSegmentKeyGenerator.setProperties(properties);
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(100001L, 100002L, 100003L, 100004L, 100005L, 100006L, 100007L, 100008L, 100009L, 100010L);
List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) {
actual.add(leafSegmentKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithMultipleThreads() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
......@@ -127,7 +166,7 @@ public final class LeafSegmentKeyGeneratorTest {
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("initialValue", "100001");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_6");
properties.setProperty("leafKey", "test_table_4");
properties.setProperty("registryCenterType", "ThirdTestRegistryCenter");
leafSegmentKeyGenerator.setProperties(properties);
Set<Comparable<?>> actual = new HashSet<>();
......@@ -152,7 +191,7 @@ public final class LeafSegmentKeyGeneratorTest {
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", "3");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_7");
properties.setProperty("leafKey", "test_table_5");
properties.setProperty("registryCenterType", "ThirdTestRegistryCenter");
leafSegmentKeyGenerator.setProperties(properties);
int taskNumber = threadNumber << 2;
......
......@@ -20,7 +20,9 @@ package org.apache.shardingsphere.orchestration.zookeeper.curator.integration.te
import org.apache.shardingsphere.orchestration.internal.keygen.LeafSegmentKeyGenerator;
import org.apache.shardingsphere.orchestration.zookeeper.curator.integration.util.EmbedTestingServer;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -40,6 +42,7 @@ import static org.junit.Assert.assertThat;
*
* @author wangguangyuan
*/
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public final class LeafSegmentKeyGeneratorIT {
private final LeafSegmentKeyGenerator leafSegmentKeyGenerator = new LeafSegmentKeyGenerator();
......@@ -67,6 +70,78 @@ public final class LeafSegmentKeyGeneratorIT {
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithFirstSpecialStep() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:3181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "5");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_6");
properties.setProperty("registryCenterType", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(100001L, 100002L, 100003L, 100004L, 100005L, 100006L, 100007L, 100008L, 100009L, 100010L);
List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) {
actual.add(leafSegmentKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithFirstSpecialStepAgain() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:3181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "5");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_6");
properties.setProperty("registryCenterType", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(100011L, 100012L, 100013L, 100014L, 100015L, 100016L, 100017L, 100018L, 100019L, 100020L);
List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) {
actual.add(leafSegmentKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithSecondSpecialStep() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:3181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "7");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_7");
properties.setProperty("registryCenterType", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(100001L, 100002L, 100003L, 100004L, 100005L, 100006L, 100007L, 100008L, 100009L, 100010L);
List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) {
actual.add(leafSegmentKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithSecondSpecialStepAgain() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:3181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "7");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_7");
properties.setProperty("registryCenterType", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(100017L, 100018L, 100019L, 100020L, 100021L, 100022L, 100023L, 100024L, 100025L, 100026L);
List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) {
actual.add(leafSegmentKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithMultipleThreads() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
......@@ -127,7 +202,7 @@ public final class LeafSegmentKeyGeneratorIT {
properties.setProperty("serverList", "127.0.0.1:3181");
properties.setProperty("initialValue", "100001");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_6");
properties.setProperty("leafKey", "test_table_4");
properties.setProperty("registryCenterType", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
Set<Comparable<?>> actual = new HashSet<>();
......@@ -152,7 +227,7 @@ public final class LeafSegmentKeyGeneratorIT {
properties.setProperty("serverList", "127.0.0.1:3181");
properties.setProperty("step", "3");
properties.setProperty("digest", "");
properties.setProperty("leafKey", "test_table_7");
properties.setProperty("leafKey", "test_table_5");
properties.setProperty("registryCenterType", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
int taskNumber = threadNumber << 2;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册