Version 4.0.0-rc3 : generated by LeafSnowflakeKeyGeneratorkey mod 4 always is 0/1, expected 0/1/2/3
Bug Report
For English only, other languages will not accept.
Before report a bug, make sure you have:
- Searched open and closed GitHub issues.
- Read documentation: ShardingSphere Doc.
Please pay attention on issues you submitted, because we maybe need more details. If no response more than 7 days and we cannot reproduce it on current information, we will close it.
Please answer these questions before submitting your issue. Thanks!
Which version of ShardingSphere did you use?
4.0.0-rc3
Which project did you use? Sharding-JDBC or Sharding-Proxy?
sharding-jdbc
Expected behavior
mod 4 is 0/1/2/3
Actual behavior
mod 4 always is 0/1
Reason analyze (If you can)
Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
Example codes for reproduce this issue (such as a github link).
One of My unit test cases code as follows
public void assertLastDigitalOfGenerateKeySameMillisecond() { Properties properties = new Properties(); properties.setProperty("serverList", "127.0.0.1:2181"); properties.setProperty("serviceId", "testService1"); properties.setProperty("digest", "name:123456"); properties.setProperty("maxTimeDifference", "5000"); properties.setProperty("registryCenterType", "ForthTestRegistryCenter"); properties.setProperty("maxVibrationOffset", String.valueOf(3)); FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(6)); leafSnowflakeKeyGenerator.setProperties(properties); String actualGenerateKeyBinaryString0 = Long.toBinaryString(Long.parseLong(leafSnowflakeKeyGenerator.generateKey().toString())); assertThat(Integer.parseInt(actualGenerateKeyBinaryString0.substring(actualGenerateKeyBinaryString0.length() - 3), 2), is(0)); String actualGenerateKeyBinaryString1 = Long.toBinaryString(Long.parseLong(leafSnowflakeKeyGenerator.generateKey().toString())); assertThat(Integer.parseInt(actualGenerateKeyBinaryString1.substring(actualGenerateKeyBinaryString1.length() - 3), 2), is(1)); String actualGenerateKeyBinaryString2 = Long.toBinaryString(Long.parseLong(leafSnowflakeKeyGenerator.generateKey().toString())); assertThat(Integer.parseInt(actualGenerateKeyBinaryString2.substring(actualGenerateKeyBinaryString2.length() - 3), 2), is(2)); String actualGenerateKeyBinaryString3 = Long.toBinaryString(Long.parseLong(leafSnowflakeKeyGenerator.generateKey().toString())); assertThat(Integer.parseInt(actualGenerateKeyBinaryString3.substring(actualGenerateKeyBinaryString3.length() - 3), 2), is(3)); String actualGenerateKeyBinaryString4 = Long.toBinaryString(Long.parseLong(leafSnowflakeKeyGenerator.generateKey().toString())); assertThat(Integer.parseInt(actualGenerateKeyBinaryString4.substring(actualGenerateKeyBinaryString4.length() - 3), 2), is(4));
}
The key code is FieldUtil.setStaticFinalField(leafSnowflakeKeyGenerator, "timeService", new FixedTimeService(6));
My test case just invokes 5 times , but I set the invokeTimes of FixedTimeService.getCurrentMillis() is 6 .
Why should i do this ? The reason as follow:
When LeafSnowflakeKeyGenerator is being initialized , its method initializeTimeNodeIfNeed() invokes one time FixedTimeService.getCurrentMillis() , invokeTimes value is 0 . This first call of FixedTimeService.getCurrentMillis() is just for initialization . Then when
LeafSnowflakeKeyGenerator finishs its initialization and begins to generate the first key , the invokeTimes of FixedTimeService.getCurrentMillis() value change to 1. That means when we generate the first key , the actual call of FixedTimeService.getCurrentMillis() we make is not the first call.
So , this test case just invokes 5 times , but I set it to 6 .
Any good suggestions?
FixedTimeService code as follows
public final class FixedTimeService extends TimeService { private final int expectedInvokedTimes; private final AtomicInteger invokedTimes = new AtomicInteger(); private long current = LeafSnowflakeKeyGenerator.EPOCH; @Override public long getCurrentMillis() { if (invokedTimes.getAndIncrement() < expectedInvokedTimes) { return current; } invokedTimes.set(0); return ++current; } }