提交 5e78874c 编写于 作者: W wgy8283335

Add leaf key generator and related functions.

上级 7c13de20
......@@ -72,7 +72,7 @@ public final class LeafSnowflakeKeyGenerator implements ShardingKeyGenerator {
private static final String SLANTING_BAR = "/";
private static TimeService timeService = new TimeService();
private final TimeService timeService = new TimeService();
@Getter
@Setter
......
......@@ -277,10 +277,11 @@ public final class CuratorZookeeperRegistryCenter implements RegistryCenter {
CloseableUtils.closeQuietly(client);
}
/* TODO 等待500ms, cache先关闭再关闭client, 否则会抛异常
* 因为异步处理, 可能会导致client先关闭而cache还未关闭结束.
* 等待Curator新版本解决这个bug.
* BUG地址:https://issues.apache.org/jira/browse/CURATOR-157
/*
* TODO waits for 500 ms. Cache closes the client first and then closes the client. Otherwise, an exception will be thrown.
* Because of asynchronous processing, it may lead to client closing first and cache not closing yet.
* Wait for a new version of Curator to fix this bug.
* BUG address: https://issues.apache.org/jira/browse/CURATOR-157
*/
private void waitForCacheClose() {
try {
......@@ -311,4 +312,4 @@ public final class CuratorZookeeperRegistryCenter implements RegistryCenter {
public void tryRelease() {
leafLock.release();
}
}
\ No newline at end of file
}
......@@ -17,9 +17,9 @@
package org.apache.shardingsphere.integration.test;
import lombok.SneakyThrows;
import org.apache.shardingsphere.integration.util.EmbedTestingServer;
import org.apache.shardingsphere.orchestration.internal.keygen.LeafSegmentKeyGenerator;
import org.hamcrest.CoreMatchers;
import org.junit.BeforeClass;
import org.junit.Test;
......@@ -33,7 +33,6 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class LeafSegmentKeyGeneratorIT {
......@@ -60,12 +59,11 @@ public final class LeafSegmentKeyGeneratorIT {
for (int i = 0; i < 10; i++) {
actual.add(leafSegmentKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
assertThat(actual, CoreMatchers.is(expected));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithMultipleThreads() {
public void assertGenerateKeyWithMultipleThreads() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -86,12 +84,11 @@ public final class LeafSegmentKeyGeneratorIT {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDigest() {
public void assertGenerateKeyWithDigest() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -112,12 +109,11 @@ public final class LeafSegmentKeyGeneratorIT {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDefaultStep() {
public void assertGenerateKeyWithDefaultStep() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -137,12 +133,11 @@ public final class LeafSegmentKeyGeneratorIT {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDefaultInitialValue() {
public void assertGenerateKeyWithDefaultInitialValue() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -162,7 +157,7 @@ public final class LeafSegmentKeyGeneratorIT {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
}
......@@ -17,7 +17,6 @@
package org.apache.shardingsphere.integration.test;
import lombok.SneakyThrows;
import org.apache.shardingsphere.integration.util.EmbedTestingServer;
import org.apache.shardingsphere.orchestration.internal.keygen.LeafSnowflakeKeyGenerator;
import org.apache.shardingsphere.orchestration.internal.keygen.TimeService;
......@@ -25,6 +24,8 @@ import org.apache.shardingsphere.orchestration.internal.keygen.fixture.FixedTime
import org.apache.shardingsphere.orchestration.internal.registry.RegistryCenterServiceLoader;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenter;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration;
import org.apache.shardingsphere.orchestration.util.FieldUtil;
import org.hamcrest.CoreMatchers;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
......@@ -40,8 +41,6 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.apache.shardingsphere.orchestration.util.FieldUtil.setStaticFinalField;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
......@@ -55,7 +54,6 @@ public final class LeafSnowflakeKeyGeneratorIT {
}
@Test
@SneakyThrows
public void assertGenerateKeyWithSingleThread() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:3181");
......@@ -63,17 +61,16 @@ public final class LeafSnowflakeKeyGeneratorIT {
properties.setProperty("maxTimeDifference", "5000");
properties.setProperty("registryCenterType", "zookeeper");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1));
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(4198401L, 4198402L, 8392704L, 8392705L, 12587009L, 12587010L, 16781312L, 16781313L, 20975617L, 20975618L);
List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) {
actual.add(leafSnowflakeKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
assertThat(actual, CoreMatchers.is(expected));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithFixedWorkId() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:3181");
......@@ -81,16 +78,15 @@ public final class LeafSnowflakeKeyGeneratorIT {
properties.setProperty("maxTimeDifference", "5000");
properties.setProperty("registryCenterType", "zookeeper");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1));
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(4198401L);
List<Comparable<?>> actual = new ArrayList<>();
actual.add(leafSnowflakeKeyGenerator.generateKey());
assertThat(actual, is(expected));
assertThat(actual, CoreMatchers.is(expected));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithMultipleThreads() {
public void assertGenerateKeyWithMultipleThreads() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -99,7 +95,7 @@ public final class LeafSnowflakeKeyGeneratorIT {
properties.setProperty("maxTimeDifference", "5000");
properties.setProperty("registryCenterType", "zookeeper");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1));
Set<Comparable<?>> actual = new HashSet<>();
int taskNumber = threadNumber << 2;
for (int i = 0; i < taskNumber; i++) {
......@@ -110,12 +106,11 @@ public final class LeafSnowflakeKeyGeneratorIT {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDigest() {
public void assertGenerateKeyWithDigest() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -124,7 +119,7 @@ public final class LeafSnowflakeKeyGeneratorIT {
properties.setProperty("maxTimeDifference", "5000");
properties.setProperty("registryCenterType", "zookeeper");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1));
Set<Comparable<?>> actual = new HashSet<>();
int taskNumber = threadNumber << 2;
for (int i = 0; i < taskNumber; i++) {
......@@ -135,12 +130,11 @@ public final class LeafSnowflakeKeyGeneratorIT {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDefaultMaxTimeDifference() {
public void assertGenerateKeyWithDefaultMaxTimeDifference() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -148,7 +142,7 @@ public final class LeafSnowflakeKeyGeneratorIT {
properties.setProperty("serviceId", "testService1");
properties.setProperty("registryCenterType", "zookeeper");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1));
Set<Comparable<?>> actual = new HashSet<>();
int taskNumber = threadNumber << 2;
for (int i = 0; i < taskNumber; i++) {
......@@ -159,12 +153,10 @@ public final class LeafSnowflakeKeyGeneratorIT {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void generateKeySuccessWithTimeRollback() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:3181");
......@@ -177,14 +169,13 @@ public final class LeafSnowflakeKeyGeneratorIT {
leafConfiguration.setServerLists("127.0.0.1:3181");
RegistryCenter registryCenter = new RegistryCenterServiceLoader().load(leafConfiguration);
TimeService timeService = new FixedTimeService(1);
registryCenter.persist("/leaf_snowflake/specialService/time",String.valueOf(timeService.getCurrentMillis()+3000));
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",timeService);
registryCenter.persist("/leaf_snowflake/specialService/time", String.valueOf(timeService.getCurrentMillis() + 3000));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", timeService);
leafSnowflakeKeyGenerator.generateKey();
registryCenter.persist("/leaf_snowflake/specialService/time",String.valueOf(timeService.getCurrentMillis()));
registryCenter.persist("/leaf_snowflake/specialService/time", String.valueOf(timeService.getCurrentMillis()));
}
@Test(expected = IllegalStateException.class)
@SneakyThrows
public void generateKeyFailureWithTimeRollback() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:3181");
......@@ -197,10 +188,10 @@ public final class LeafSnowflakeKeyGeneratorIT {
leafConfiguration.setServerLists("127.0.0.1:3181");
RegistryCenter registryCenter = new RegistryCenterServiceLoader().load(leafConfiguration);
TimeService timeService = new FixedTimeService(1);
registryCenter.persist("/leaf_snowflake/specialService/time",String.valueOf(timeService.getCurrentMillis()+15000));
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",timeService);
registryCenter.persist("/leaf_snowflake/specialService/time", String.valueOf(timeService.getCurrentMillis() + 15000));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", timeService);
leafSnowflakeKeyGenerator.generateKey();
registryCenter.persist("/leaf_snowflake/specialService/time",String.valueOf(timeService.getCurrentMillis()));
registryCenter.persist("/leaf_snowflake/specialService/time", String.valueOf(timeService.getCurrentMillis()));
}
}
......@@ -62,4 +62,4 @@ public final class EmbedTestingServer {
private static boolean isIgnoredException(final Throwable cause) {
return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException;
}
}
\ No newline at end of file
}
......@@ -17,7 +17,7 @@
package org.apache.shardingsphere.orchestration.internal.keygen;
import lombok.SneakyThrows;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import java.util.Properties;
......@@ -30,7 +30,6 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class LeafSegmentKeyGeneratorTest {
......@@ -39,7 +38,7 @@ public final class LeafSegmentKeyGeneratorTest {
@Test
public void assertGetProperties() {
assertThat(leafSegmentKeyGenerator.getProperties().entrySet().size(), is(0));
assertThat(leafSegmentKeyGenerator.getProperties().entrySet().size(), CoreMatchers.is(0));
}
@Test
......@@ -47,7 +46,7 @@ public final class LeafSegmentKeyGeneratorTest {
Properties properties = new Properties();
properties.setProperty("key1", "value1");
leafSegmentKeyGenerator.setProperties(properties);
assertThat(leafSegmentKeyGenerator.getProperties().get("key1"), is((Object) "value1"));
assertThat(leafSegmentKeyGenerator.getProperties().get("key1"), CoreMatchers.is((Object) "value1"));
}
@Test
......@@ -65,12 +64,11 @@ public final class LeafSegmentKeyGeneratorTest {
for (int i = 0; i < 10; i++) {
actual.add(leafSegmentKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
assertThat(actual, CoreMatchers.is(expected));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithMultipleThreads() {
public void assertGenerateKeyWithMultipleThreads() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -91,12 +89,11 @@ public final class LeafSegmentKeyGeneratorTest {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDigest() {
public void assertGenerateKeyWithDigest() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -117,12 +114,11 @@ public final class LeafSegmentKeyGeneratorTest {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDefaultStep() {
public void assertGenerateKeyWithDefaultStep() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -142,12 +138,11 @@ public final class LeafSegmentKeyGeneratorTest {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDefaultInitialValue() {
public void assertGenerateKeyWithDefaultInitialValue() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -167,7 +162,7 @@ public final class LeafSegmentKeyGeneratorTest {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test(expected = IllegalArgumentException.class)
......
......@@ -17,9 +17,10 @@
package org.apache.shardingsphere.orchestration.internal.keygen;
import lombok.SneakyThrows;
import org.apache.shardingsphere.orchestration.internal.keygen.fixture.FixedTimeService;
import org.apache.shardingsphere.orchestration.internal.keygen.fixture.PreviousTimeService;
import org.apache.shardingsphere.orchestration.util.FieldUtil;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import java.util.ArrayList;
......@@ -32,8 +33,6 @@ import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.apache.shardingsphere.orchestration.util.FieldUtil.setStaticFinalField;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class LeafSnowflakeKeyGeneratorTest {
......@@ -42,7 +41,7 @@ public final class LeafSnowflakeKeyGeneratorTest {
@Test
public void assertGetProperties() {
assertThat(leafSnowflakeKeyGenerator.getProperties().entrySet().size(), is(0));
assertThat(leafSnowflakeKeyGenerator.getProperties().entrySet().size(), CoreMatchers.is(0));
}
@Test
......@@ -50,11 +49,10 @@ public final class LeafSnowflakeKeyGeneratorTest {
Properties properties = new Properties();
properties.setProperty("key1", "value1");
leafSnowflakeKeyGenerator.setProperties(properties);
assertThat(leafSnowflakeKeyGenerator.getProperties().get("key1"), is((Object) "value1"));
assertThat(leafSnowflakeKeyGenerator.getProperties().get("key1"), CoreMatchers.is((Object) "value1"));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithSingleThread() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
......@@ -62,17 +60,16 @@ public final class LeafSnowflakeKeyGeneratorTest {
properties.setProperty("maxTimeDifference", "5000");
properties.setProperty("registryCenterType", "ForthTestRegistryCenter");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1));
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(4198401L, 4198402L, 8392704L, 8392705L, 12587009L, 12587010L, 16781312L, 16781313L, 20975617L, 20975618L);
List<Comparable<?>> actual = new ArrayList<>();
for (int i = 0; i < 10; i++) {
actual.add(leafSnowflakeKeyGenerator.generateKey());
}
assertThat(actual, is(expected));
assertThat(actual, CoreMatchers.is(expected));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithFixedWorkId() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
......@@ -80,15 +77,14 @@ public final class LeafSnowflakeKeyGeneratorTest {
properties.setProperty("maxTimeDifference", "5000");
properties.setProperty("registryCenterType", "ForthTestRegistryCenter");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new FixedTimeService(1));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(1));
List<Comparable<?>> expected = Arrays.<Comparable<?>>asList(4198401L);
List<Comparable<?>> actual = new ArrayList<>();
actual.add(leafSnowflakeKeyGenerator.generateKey());
assertThat(actual, is(expected));
assertThat(actual, CoreMatchers.is(expected));
}
@Test(expected = IllegalStateException.class)
@SneakyThrows
public void generateKeyFailureWithTimeRollback() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
......@@ -96,12 +92,11 @@ public final class LeafSnowflakeKeyGeneratorTest {
properties.setProperty("maxTimeDifference", "5000");
properties.setProperty("registryCenterType", "FifthTestRegistryCenter");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new PreviousTimeService(15000));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new PreviousTimeService(15000));
leafSnowflakeKeyGenerator.generateKey();
}
@Test
@SneakyThrows
public void generateKeySuccessWithTimeRollback() {
Properties properties = new Properties();
properties.setProperty("serverList", "127.0.0.1:2181");
......@@ -109,13 +104,12 @@ public final class LeafSnowflakeKeyGeneratorTest {
properties.setProperty("maxTimeDifference", "5000");
properties.setProperty("registryCenterType", "FifthTestRegistryCenter");
leafSnowflakeKeyGenerator.setProperties(properties);
setStaticFinalField(leafSnowflakeKeyGenerator,"timeService",new PreviousTimeService(3000));
FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new PreviousTimeService(3000));
leafSnowflakeKeyGenerator.generateKey();
}
@Test
@SneakyThrows
public void assertGenerateKeyWithMultipleThreads() {
public void assertGenerateKeyWithMultipleThreads() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -134,12 +128,11 @@ public final class LeafSnowflakeKeyGeneratorTest {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDigest() {
public void assertGenerateKeyWithDigest() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -158,12 +151,11 @@ public final class LeafSnowflakeKeyGeneratorTest {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test
@SneakyThrows
public void assertGenerateKeyWithDefaultMaxTimeDifference() {
public void assertGenerateKeyWithDefaultMaxTimeDifference() throws Exception {
int threadNumber = Runtime.getRuntime().availableProcessors() << 1;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
Properties properties = new Properties();
......@@ -181,7 +173,7 @@ public final class LeafSnowflakeKeyGeneratorTest {
}
}).get());
}
assertThat(actual.size(), is(taskNumber));
assertThat(actual.size(), CoreMatchers.is(taskNumber));
}
@Test(expected = IllegalArgumentException.class)
......@@ -226,7 +218,6 @@ public final class LeafSnowflakeKeyGeneratorTest {
leafSnowflakeKeyGenerator.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetServiceIdFailureWithSlantingBar() {
Properties properties = new Properties();
......
......@@ -27,7 +27,7 @@ public final class PreviousTimeService extends TimeService {
@Override
public long getCurrentMillis() {
long result = System.currentTimeMillis()-timeDifference;
long result = System.currentTimeMillis() - timeDifference;
return result;
}
}
......@@ -40,18 +40,11 @@ public final class FifthTestRegistryCenter implements RegistryCenter {
private ReentrantLock lock = new ReentrantLock();
private static final String PARENT_NODE = "/leaf_snowflake";
private static final String TIME_NODE = "/time";
private static final String CURRENT_MAX_WORK_ID_NODE = "/current-max-work-id";
private static final String WORK_ID_NODE = "/work-id";
@Override
public void init(final RegistryCenterConfiguration config) {
keys.put("/leaf_snowflake/specialService/time",String.valueOf(System.currentTimeMillis()));
keys.put("/leaf_snowflake/specialService/work-id","1");
keys.put("/leaf_snowflake/current-max-work-id","1");
keys.put("/leaf_snowflake/specialService/time", String.valueOf(System.currentTimeMillis()));
keys.put("/leaf_snowflake/specialService/work-id", "1");
keys.put("/leaf_snowflake/current-max-work-id", "1");
}
@Override
......
......@@ -65,16 +65,15 @@ public final class FieldUtil {
* @param target target to be settled
* @param fieldName field name to be settled
* @param fieldValue field value to be settled
* @return field value
*/
@SneakyThrows
public static void setStaticFinalField(final Object target, final String fieldName, final Object fieldValue){
public static void setStaticFinalField(final Object target, final String fieldName, final Object fieldValue) {
Field timeServiceField = target.getClass().getDeclaredField(fieldName);
timeServiceField.setAccessible(true);
Field modifiers = timeServiceField.getClass().getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(timeServiceField, timeServiceField.getModifiers() & ~Modifier.FINAL);
timeServiceField.set(target,fieldValue);
timeServiceField.set(target, fieldValue);
modifiers.setInt(timeServiceField, timeServiceField.getModifiers() & ~Modifier.FINAL);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册