提交 2db3fcb4 authored 作者: 陈世营's avatar 陈世营

【修改】 leaf 配置修改

上级 1a0ef56e
package com.loit.component.keygen.leaf;
import com.google.common.base.Preconditions;
......@@ -10,6 +11,7 @@ import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
......@@ -19,7 +21,7 @@ import java.util.concurrent.SynchronousQueue;
* Key generator implemented by leaf segment algorithms.
*
*/
public final class LeafSegmentLeafKeyGenerator implements LeafKeyGenerator {
public final class LeafSegmentKeyGenerator implements LeafKeyGenerator {
private static final String DEFAULT_NAMESPACE = "leaf_segment";
......@@ -47,7 +49,7 @@ public final class LeafSegmentLeafKeyGenerator implements LeafKeyGenerator {
@Setter
private Properties properties = new Properties();
public LeafSegmentLeafKeyGenerator() {
public LeafSegmentKeyGenerator() {
incrementCacheIdExecutor = Executors.newSingleThreadExecutor();
cacheIdQueue = new SynchronousQueue<>();
}
......@@ -163,35 +165,35 @@ public final class LeafSegmentLeafKeyGenerator implements LeafKeyGenerator {
}
private long getStep() {
long result = Long.parseLong(properties.getProperty("step", DEFAULT_STEP));
long result = Long.parseLong(properties.getProperty("leaf.segment.step", DEFAULT_STEP));
Preconditions.checkArgument(result > 0L && result < Long.MAX_VALUE);
return result;
}
private long getInitialValue() {
long result = Long.parseLong(properties.getProperty("initialValue", DEFAULT_INITIAL_VALUE));
long result = Long.parseLong(properties.getProperty("leaf.segment.id.initial.value", DEFAULT_INITIAL_VALUE));
Preconditions.checkArgument(result >= 0L && result < Long.MAX_VALUE);
return result;
}
private String getLeafKey() {
String leafKey = properties.getProperty("leafKey");
String leafKey = properties.getProperty("leaf.key");
Preconditions.checkArgument(!Strings.isNullOrEmpty(leafKey));
Preconditions.checkArgument(leafKey.matches(REGULAR_PATTERN));
return SLANTING_BAR + leafKey;
}
private String getServerList() {
String result = properties.getProperty("serverList");
String result = properties.getProperty("server.list");
Preconditions.checkArgument(!Strings.isNullOrEmpty(result));
return result;
}
private String getDigest() {
return properties.getProperty("digest");
return properties.getProperty("registry.center.digest");
}
private String getRegistryCenterType() {
return properties.getProperty("registryCenterType", DEFAULT_REGISTRY_CENTER);
return properties.getProperty("registry.center.type", DEFAULT_REGISTRY_CENTER);
}
}
......@@ -13,15 +13,15 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public final class LeafSegmentLeafKeyGeneratorTest {
private final LeafSegmentLeafKeyGenerator leafSegmentKeyGenerator = new LeafSegmentLeafKeyGenerator();
public final class LeafSegmentKeyGeneratorTest {
private final LeafSegmentKeyGenerator leafSegmentKeyGenerator = new LeafSegmentKeyGenerator();
@Test
public void assertGetProperties() {
assertThat(leafSegmentKeyGenerator.getProperties().entrySet().size(), is(0));
}
@Test
public void assertSetProperties() {
Properties properties = new Properties();
......@@ -29,15 +29,15 @@ public final class LeafSegmentLeafKeyGeneratorTest {
leafSegmentKeyGenerator.setProperties(properties);
assertThat(leafSegmentKeyGenerator.getProperties().get("key1"), is((Object) "value1"));
}
@Test
public void assertGenerateKeyWithSingleThread() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "5");
properties.setProperty("leafKey", "test_table_1");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.segment.step", "5");
properties.setProperty("leaf.key", "test_table_1");
properties.setProperty("registry.center.type", "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<>();
......@@ -46,15 +46,15 @@ public final class LeafSegmentLeafKeyGeneratorTest {
}
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("leafKey", "test_table_6");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.key", "test_table_6");
properties.setProperty("registry.center.type", "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<>();
......@@ -63,15 +63,15 @@ public final class LeafSegmentLeafKeyGeneratorTest {
}
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("leafKey", "test_table_7");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.segment.step", "7");
properties.setProperty("leaf.key", "test_table_7");
properties.setProperty("registry.center.type", "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<>();
......@@ -80,17 +80,17 @@ public final class LeafSegmentLeafKeyGeneratorTest {
}
assertThat(actual, is(expected));
}
@Test
public void assertGenerateKeyWithMultipleThreads() throws Exception {
int threadNumber = 2;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "3");
properties.setProperty("leafKey", "test_table_2");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.key", "test_table_2");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
Set<Comparable<?>> actual = new HashSet<>();
int taskNumber = threadNumber * 2;
......@@ -105,18 +105,18 @@ public final class LeafSegmentLeafKeyGeneratorTest {
}
assertThat(actual.size(), is(taskNumber));
}
@Test
public void assertGenerateKeyWithDigest() throws Exception {
int threadNumber = 2;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("initialValue", "100001");
properties.setProperty("step", "3");
properties.setProperty("digest", "name:123456");
properties.setProperty("leafKey", "test_table_3");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("registry.center.digest", "name:123456");
properties.setProperty("leaf.key", "test_table_3");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
Set<Comparable<?>> actual = new HashSet<>();
int taskNumber = threadNumber * 2;
......@@ -131,16 +131,16 @@ public final class LeafSegmentLeafKeyGeneratorTest {
}
assertThat(actual.size(), is(taskNumber));
}
@Test
public void assertGenerateKeyWithDefaultStep() throws Exception {
int threadNumber = 2;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "test_table_4");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "test_table_4");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
Set<Comparable<?>> actual = new HashSet<>();
int taskNumber = threadNumber * 2;
......@@ -155,16 +155,16 @@ public final class LeafSegmentLeafKeyGeneratorTest {
}
assertThat(actual.size(), is(taskNumber));
}
@Test
public void assertGenerateKeyWithDefaultInitialValue() throws Exception {
int threadNumber = 2;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", "3");
properties.setProperty("leafKey", "test_table_5");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.key", "test_table_5");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
int taskNumber = threadNumber * 2;
Set<Comparable<?>> actual = new HashSet<>();
......@@ -179,133 +179,133 @@ public final class LeafSegmentLeafKeyGeneratorTest {
}
assertThat(actual.size(), is(taskNumber));
}
@Test(expected = IllegalArgumentException.class)
public void assertSetStepFailureWhenNegative() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", String.valueOf(-1L));
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "test_table_9");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", String.valueOf(-1L));
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "test_table_9");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetStepFailureWhenZero() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", String.valueOf(0L));
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "test_table_10");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", String.valueOf(0L));
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "test_table_10");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetStepFailureWhenTooMuch() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", String.valueOf(Long.MAX_VALUE));
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "test_table_11");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", String.valueOf(Long.MAX_VALUE));
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "test_table_11");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetInitialValueFailureWhenNegative() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", "3");
properties.setProperty("initialValue", String.valueOf(-1L));
properties.setProperty("leafKey", "test_table_12");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.segment.id.initial.value", String.valueOf(-1L));
properties.setProperty("leaf.key", "test_table_12");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetInitialValueFailureWhenTooMuch() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", "3");
properties.setProperty("initialValue", String.valueOf(Long.MAX_VALUE));
properties.setProperty("leafKey", "test_table_13");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.segment.id.initial.value", String.valueOf(Long.MAX_VALUE));
properties.setProperty("leaf.key", "test_table_13");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetServerListFailureWhenNull() {
Properties properties = new Properties();
properties.setProperty("step", "3");
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "test_table_14");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "test_table_14");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetServerListFailureWhenArgumentEmpty() {
Properties properties = new Properties();
properties.setProperty("serverList", "");
properties.setProperty("step", "3");
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "test_table_15");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "test_table_15");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetLeafKeyFailureWhenArgumentIllegal() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", "3");
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "/test_table_16");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "/test_table_16");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetLeafKeyFailureWhenArgumentEmpty() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", "3");
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetLeafKeyFailureWhenNull() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", "3");
properties.setProperty("initialValue", "100001");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetRegistryCenterTypeFailureWhenWrongType() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
properties.setProperty("step", "3");
properties.setProperty("initialValue", "100001");
properties.setProperty("leafKey", "/test_table_17");
properties.setProperty("registryCenterType", "zookeeper");
properties.setProperty("server.list", "127.0.0.1:2181");
properties.setProperty("leaf.segment.step", "3");
properties.setProperty("leaf.segment.id.initial.value", "100001");
properties.setProperty("leaf.key", "/test_table_17");
properties.setProperty("registry.center.type", "zookeeper");
leafSegmentKeyGenerator.setProperties(properties);
leafSegmentKeyGenerator.generateKey();
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论