This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new b5f970ec894 improve properties verification of key generate algorithms
(#25122)
b5f970ec894 is described below
commit b5f970ec894a72b45513f9ae08be2415bf4a6522
Author: PirretKe <[email protected]>
AuthorDate: Fri Apr 14 08:48:02 2023 +0800
improve properties verification of key generate algorithms (#25122)
* improve properties verification of key generate algorithms
* fix code style
* fix code style
* fix code style
---
.../sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithm.java | 4 +++-
.../algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java | 6 ++++++
.../cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithm.java | 5 ++++-
.../algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java | 5 +++++
4 files changed, 18 insertions(+), 2 deletions(-)
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithm.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithm.java
index 30624124da6..d6125d23b6f 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithm.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithm.java
@@ -113,7 +113,9 @@ public final class SnowflakeKeyGenerateAlgorithm implements
KeyGenerateAlgorithm
}
private int getMaxTolerateTimeDifferenceMilliseconds(final Properties
props) {
- return
Integer.parseInt(props.getOrDefault(MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS_KEY,
MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS).toString());
+ int result =
Integer.parseInt(props.getOrDefault(MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS_KEY,
MAX_TOLERATE_TIME_DIFFERENCE_MILLISECONDS).toString());
+ ShardingSpherePreconditions.checkState(result >= 0, () -> new
KeyGenerateAlgorithmInitializationException(getType(), "Illegal max tolerate
time difference milliseconds."));
+ return result;
}
@Override
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
index 7e168c31c5f..9287ffade15 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
@@ -232,4 +232,10 @@ class SnowflakeKeyGenerateAlgorithmTest {
KeyGenerateAlgorithm algorithm =
TypedSPILoader.getService(KeyGenerateAlgorithm.class, "SNOWFLAKE",
PropertiesBuilder.build(new
Property("max-tolerate-time-difference-milliseconds", "1")));
assertThat(((Properties)
Plugins.getMemberAccessor().get(algorithm.getClass().getDeclaredField("props"),
algorithm)).getProperty("max-tolerate-time-difference-milliseconds"), is("1"));
}
+
+ @Test
+ void assertMaxTolerateTimeDifferenceMillisecondsWhenNegative() {
+ assertThrows(KeyGenerateAlgorithmInitializationException.class,
+ () -> TypedSPILoader.getService(KeyGenerateAlgorithm.class,
"SNOWFLAKE", PropertiesBuilder.build(new
Property("max-tolerate-time-difference-milliseconds", "-1"))).generateKey());
+ }
}
diff --git
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithm.java
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithm.java
index 66cb32610a9..ac14e58e2e7 100644
---
a/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithm.java
+++
b/features/sharding/plugin/cosid/src/main/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithm.java
@@ -73,7 +73,10 @@ public final class CosIdSnowflakeKeyGenerateAlgorithm
implements KeyGenerateAlgo
}
private long getEpoch(final Properties props) {
- return Long.parseLong(props.getProperty(EPOCH_KEY, DEFAULT_EPOCH +
""));
+ long result = Long.parseLong(props.getProperty(EPOCH_KEY,
DEFAULT_EPOCH + ""));
+ ShardingSpherePreconditions.checkState(result > 0L,
+ () -> new ShardingPluginException("Key generate algorithm `%s`
initialization failed, reason is: %s.", getType(), "Epoch must be positive."));
+ return result;
}
@Override
diff --git
a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
index 3597af30806..e91ad2c7e73 100644
---
a/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
+++
b/features/sharding/plugin/cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
@@ -152,4 +152,9 @@ class CosIdSnowflakeKeyGenerateAlgorithmTest {
assertThrows(IllegalArgumentException.class, () ->
algorithm.setInstanceContext(new InstanceContext(new
ComputeNodeInstance(mock(InstanceMetaData.class)), new
WorkerIdGeneratorFixture(1024),
new ModeConfiguration("Standalone", null),
mock(ModeContextManager.class), mock(LockContext.class), eventBusContext)));
}
+
+ @Test
+ void assertEpochWhenOutOfRange() {
+ assertThrows(ShardingPluginException.class, () ->
TypedSPILoader.getService(KeyGenerateAlgorithm.class, "COSID_SNOWFLAKE",
PropertiesBuilder.build(new Property("epoch", "0"))).generateKey());
+ }
}