You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2019/12/02 09:24:31 UTC
[GitHub] [incubator-shardingsphere] haetao commented on issue #3633: Version
4.0.0-rc3 : generated by LeafSnowflakeKeyGeneratorkey mod 4 always is 0/1,
expected 0/1/2/3
haetao commented on issue #3633: Version 4.0.0-rc3 : generated by LeafSnowflakeKeyGeneratorkey mod 4 always is 0/1, expected 0/1/2/3
URL: https://github.com/apache/incubator-shardingsphere/issues/3633#issuecomment-560308727
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` .
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services