From ecace2211571b8f814807dd0a26d040999e693b4 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 22 Jul 2021 22:10:06 +0800 Subject: [PATCH] enhancement: add defensive parameter check to enhance robustness. add: add uuid_generate jmh-test --- README.md | 14 +++++++------- README.zh-CN.md | 14 +++++++------- .../java/me/ahoo/cosid/SegmentIdBenchmark.java | 9 ++++++++- .../ahoo/cosid/segment/DefaultIdSegment.java | 4 +++- .../ahoo/cosid/segment/DefaultSegmentId.java | 4 ++-- .../cosid/segment/IdSegmentDistributor.java | 7 ++++++- .../PrefetchWorkerExecutorService.java | 5 ++++- .../DefaultClockBackwardsSynchronizer.java | 4 ++++ .../machine/AbstractMachineIdDistributor.java | 9 +++++++++ .../machine/LocalMachineStateStorage.java | 18 ++++++++++++++++++ .../k8s/StatefulSetMachineIdDistributor.java | 2 +- .../ahoo/cosid/MachineIdDistributorTest.java | 2 +- .../cosid/jdbc/JdbcIdSegmentDistributor.java | 10 +++++++++- .../cosid/jdbc/JdbcIdSegmentInitializer.java | 9 +++++++++ .../cosid/redis/RedisIdSegmentDistributor.java | 9 ++++++++- .../redis/SpringRedisIdSegmentDistributor.java | 11 +++++++++-- gradle.properties | 2 +- 17 files changed, 106 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 30dd4180c6..ed56325407 100644 --- a/README.md +++ b/README.md @@ -312,7 +312,7 @@ In actual use, we generally do not use the same `IdGenerator` for all business s > Kotlin DSL ``` kotlin - val cosidVersion = "1.3.2"; + val cosidVersion = "1.3.3"; implementation("me.ahoo.cosid:spring-boot-starter-cosid:${cosidVersion}") ``` @@ -328,7 +328,7 @@ In actual use, we generally do not use the same `IdGenerator` for all business s 4.0.0 demo - 1.3.2 + 1.3.3 @@ -418,7 +418,7 @@ cosid: ``` shell gradle cosid-core:jmh # or -java -jar cosid-core/build/libs/cosid-core-1.3.2-jmh.jar -bm thrpt -wi 1 -rf json -f 1 +java -jar cosid-core/build/libs/cosid-core-1.3.3-jmh.jar -bm thrpt -wi 1 -rf json -f 1 ``` ``` @@ -439,7 +439,7 @@ SnowflakeIdBenchmark.secondSnowflakeId_generate thrpt 4206843. ``` shell gradle cosid-redis:jmh # or -java -jar cosid-redis/build/libs/cosid-redis-1.3.2-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark +java -jar cosid-redis/build/libs/cosid-redis-1.3.3-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark ``` ``` @@ -457,7 +457,7 @@ RedisChainIdBenchmark.step_1000 thrpt 5 127439148.104 ± 1833743 ![RedisChainIdBenchmark-Sample](./docs/jmh/RedisChainIdBenchmark-Sample.png) ```shell -java -jar cosid-redis/build/libs/cosid-redis-1.3.2-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000 +java -jar cosid-redis/build/libs/cosid-redis-1.3.3-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000 ``` ``` @@ -482,7 +482,7 @@ RedisChainIdBenchmark.step_1000:step_1000·p1.00 sample 37.440 ``` shell gradle cosid-jdbc:jmh # or -java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.2-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark +java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.3-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark ``` ``` @@ -498,7 +498,7 @@ MySqlChainIdBenchmark.step_1000 thrpt 5 123131804.260 ± 1488004. ![MySqlChainIdBenchmark-Sample](./docs/jmh/MySqlChainIdBenchmark-Sample.png) ```shell -java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.2-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000 +java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.3-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000 ``` ``` Benchmark Mode Cnt Score Error Units diff --git a/README.zh-CN.md b/README.zh-CN.md index f9c707838e..53c401a0eb 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -307,7 +307,7 @@ IdGenerator idGenerator=idGeneratorProvider.get("bizA"); > Kotlin DSL ``` kotlin - val cosidVersion = "1.3.2"; + val cosidVersion = "1.3.3"; implementation("me.ahoo.cosid:spring-boot-starter-cosid:${cosidVersion}") ``` @@ -323,7 +323,7 @@ IdGenerator idGenerator=idGeneratorProvider.get("bizA"); 4.0.0 demo - 1.3.2 + 1.3.3 @@ -411,7 +411,7 @@ cosid: ``` shell gradle cosid-core:jmh # or -java -jar cosid-core/build/libs/cosid-core-1.3.2-jmh.jar -bm thrpt -wi 1 -rf json -f 1 +java -jar cosid-core/build/libs/cosid-core-1.3.3-jmh.jar -bm thrpt -wi 1 -rf json -f 1 ``` ``` @@ -432,7 +432,7 @@ SnowflakeIdBenchmark.secondSnowflakeId_generate thrpt 4206843. ``` shell gradle cosid-redis:jmh # or -java -jar cosid-redis/build/libs/cosid-redis-1.3.2-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark +java -jar cosid-redis/build/libs/cosid-redis-1.3.3-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark ``` ``` @@ -450,7 +450,7 @@ RedisChainIdBenchmark.step_1000 thrpt 5 127439148.104 ± 1833743 ![RedisChainIdBenchmark-Sample](./docs/jmh/RedisChainIdBenchmark-Sample.png) ```shell -java -jar cosid-redis/build/libs/cosid-redis-1.3.2-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000 +java -jar cosid-redis/build/libs/cosid-redis-1.3.3-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000 ``` ``` @@ -475,7 +475,7 @@ RedisChainIdBenchmark.step_1000:step_1000·p1.00 sample 37.440 ``` shell gradle cosid-jdbc:jmh # or -java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.2-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark +java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.3-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark ``` ``` @@ -491,7 +491,7 @@ MySqlChainIdBenchmark.step_1000 thrpt 5 123131804.260 ± 1488004. ![MySqlChainIdBenchmark-Sample](./docs/jmh/MySqlChainIdBenchmark-Sample.png) ```shell -java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.2-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000 +java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.3-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000 ``` ``` Benchmark Mode Cnt Score Error Units diff --git a/cosid-core/src/jmh/java/me/ahoo/cosid/SegmentIdBenchmark.java b/cosid-core/src/jmh/java/me/ahoo/cosid/SegmentIdBenchmark.java index 7468c9130b..61f2b6c3d6 100644 --- a/cosid-core/src/jmh/java/me/ahoo/cosid/SegmentIdBenchmark.java +++ b/cosid-core/src/jmh/java/me/ahoo/cosid/SegmentIdBenchmark.java @@ -24,6 +24,8 @@ import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; +import java.util.UUID; + import static me.ahoo.cosid.segment.IdSegment.TIME_TO_LIVE_FOREVER; @@ -41,7 +43,12 @@ public class SegmentIdBenchmark { public void setup() { jdkId = new JdkId(); segmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock()); - segmentChainId = new SegmentChainId(TIME_TO_LIVE_FOREVER, 10000, new IdSegmentDistributor.Mock(), PrefetchWorkerExecutorService.DEFAULT); + segmentChainId = new SegmentChainId(TIME_TO_LIVE_FOREVER, 10, new IdSegmentDistributor.Mock(), PrefetchWorkerExecutorService.DEFAULT); + } + + @Benchmark + public UUID uuid_generate() { + return UUID.randomUUID(); } @Benchmark diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java index 294c3cc606..0a4f3c2143 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultIdSegment.java @@ -13,6 +13,7 @@ package me.ahoo.cosid.segment; +import com.google.common.base.Preconditions; import me.ahoo.cosid.util.Clock; import java.util.concurrent.atomic.AtomicLong; @@ -22,7 +23,7 @@ */ public class DefaultIdSegment implements IdSegment { - public static final DefaultIdSegment OVERFLOW = new DefaultIdSegment(IdSegment.SEQUENCE_OVERFLOW, 0, 0, TIME_TO_LIVE_FOREVER); + public static final DefaultIdSegment OVERFLOW = new DefaultIdSegment(IdSegment.SEQUENCE_OVERFLOW, 0, Clock.CACHE.secondTime(), TIME_TO_LIVE_FOREVER); /** * include @@ -39,6 +40,7 @@ public DefaultIdSegment(long maxId, long step) { } public DefaultIdSegment(long maxId, long step, long fetchTime, long ttl) { + Preconditions.checkArgument(ttl > 0, "ttl:[%s] must be greater than 0.", ttl); this.maxId = maxId; this.step = step; this.offset = maxId - step; diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultSegmentId.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultSegmentId.java index 3b2f81b585..a96b102dbb 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultSegmentId.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/DefaultSegmentId.java @@ -14,7 +14,6 @@ package me.ahoo.cosid.segment; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; import static me.ahoo.cosid.segment.IdSegment.TIME_TO_LIVE_FOREVER; @@ -35,7 +34,8 @@ public DefaultSegmentId(IdSegmentDistributor maxIdDistributor) { } public DefaultSegmentId(long idSegmentTtl, IdSegmentDistributor maxIdDistributor) { - Preconditions.checkArgument(idSegmentTtl > 0, Strings.lenientFormat("Illegal idSegmentTtl parameter:[%s].", idSegmentTtl)); + Preconditions.checkArgument(idSegmentTtl > 0, "idSegmentTtl:[%s] must be greater than 0.", idSegmentTtl); + this.idSegmentTtl = idSegmentTtl; this.maxIdDistributor = maxIdDistributor; } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java index 6472dbf651..c0dd8e21e0 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/IdSegmentDistributor.java @@ -57,11 +57,16 @@ default IdSegment nextIdSegment() { } default IdSegment nextIdSegment(long ttl) { + Preconditions.checkArgument(ttl > 0, "ttl:[%s] must be greater than 0.", ttl); + final long maxId = nextMaxId(); return new DefaultIdSegment(maxId, getStep(), Clock.CACHE.secondTime(), ttl); } default IdSegment nextIdSegment(int segments, long ttl) { + Preconditions.checkArgument(segments > 0, "segments:[%s] must be greater than 0.", segments); + Preconditions.checkArgument(ttl > 0, "ttl:[%s] must be greater than 0.", ttl); + final long totalStep = getStep(segments); final long maxId = nextMaxId(totalStep); final IdSegment nextIdSegment = new DefaultIdSegment(maxId, totalStep, Clock.CACHE.secondTime(), ttl); @@ -83,7 +88,7 @@ default IdSegmentChain nextIdSegmentChain(IdSegmentChain previousChain, int segm } static void ensureStep(long step) { - Preconditions.checkArgument(step > 0, "the step:[%s] can not less than 1.", step); + Preconditions.checkArgument(step > 0, "step:[%s] must be greater than 0!", step); } class Atomic implements IdSegmentDistributor { diff --git a/cosid-core/src/main/java/me/ahoo/cosid/segment/concurrent/PrefetchWorkerExecutorService.java b/cosid-core/src/main/java/me/ahoo/cosid/segment/concurrent/PrefetchWorkerExecutorService.java index 02d2eadcf5..01d98f2900 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/segment/concurrent/PrefetchWorkerExecutorService.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/segment/concurrent/PrefetchWorkerExecutorService.java @@ -13,6 +13,7 @@ package me.ahoo.cosid.segment.concurrent; +import com.google.common.base.Preconditions; import lombok.extern.slf4j.Slf4j; import java.time.Duration; @@ -39,6 +40,7 @@ public class PrefetchWorkerExecutorService { private final AtomicLong threadIdx = new AtomicLong(); public PrefetchWorkerExecutorService(Duration prefetchPeriod, int corePoolSize) { + Preconditions.checkArgument(corePoolSize > 0, "corePoolSize:[%s] must be greater than 0.", corePoolSize); this.prefetchPeriod = prefetchPeriod; this.corePoolSize = corePoolSize; this.workers = new DefaultPrefetchWorker[corePoolSize]; @@ -77,6 +79,7 @@ public void shutdown() { } public void submit(AffinityJob affinityJob) { + Preconditions.checkNotNull(affinityJob, "affinityJob can not be null!"); if (log.isInfoEnabled()) { log.info("submit - jobId:[{}].", affinityJob.getJobId()); } @@ -114,7 +117,7 @@ private DefaultPrefetchWorker chooseWorker() { public class GracefullyCloser extends Thread { @Override public void run() { - if (log.isInfoEnabled()){ + if (log.isInfoEnabled()) { log.info("Close gracefully!"); } shutdown(); diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/DefaultClockBackwardsSynchronizer.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/DefaultClockBackwardsSynchronizer.java index bd0384e4a1..8d1423e277 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/DefaultClockBackwardsSynchronizer.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/DefaultClockBackwardsSynchronizer.java @@ -13,6 +13,7 @@ package me.ahoo.cosid.snowflake; +import com.google.common.base.Preconditions; import lombok.extern.slf4j.Slf4j; import me.ahoo.cosid.CosIdException; import me.ahoo.cosid.snowflake.exception.ClockTooManyBackwardsException; @@ -35,6 +36,9 @@ public DefaultClockBackwardsSynchronizer() { } public DefaultClockBackwardsSynchronizer(int spinThreshold, int brokenThreshold) { + Preconditions.checkArgument(spinThreshold > 0, "spinThreshold:[%s] must be greater than 0!", spinThreshold); + Preconditions.checkArgument(brokenThreshold > spinThreshold, "spinThreshold:[%s] must be greater than brokenThreshold:[%s]!", spinThreshold, brokenThreshold); + this.spinThreshold = spinThreshold; this.brokenThreshold = brokenThreshold; } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/AbstractMachineIdDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/AbstractMachineIdDistributor.java index 9786bc137e..aec0bfc937 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/AbstractMachineIdDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/AbstractMachineIdDistributor.java @@ -13,6 +13,8 @@ package me.ahoo.cosid.snowflake.machine; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; import me.ahoo.cosid.snowflake.ClockBackwardsSynchronizer; @@ -43,6 +45,10 @@ public AbstractMachineIdDistributor(MachineStateStorage machineStateStorage, Clo */ @Override public int distribute(String namespace, int machineBit, InstanceId instanceId) throws MachineIdOverflowException { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkArgument(machineBit > 0, "machineBit:[%s] must be greater than 0!", machineBit); + Preconditions.checkNotNull(instanceId, "instanceId can not be null!"); + MachineState localState = machineStateStorage.get(namespace, instanceId); if (!MachineState.NOT_FOUND.equals(localState)) { clockBackwardsSynchronizer.syncUninterruptibly(localState.getLastTimeStamp()); @@ -73,6 +79,9 @@ public int distribute(String namespace, int machineBit, InstanceId instanceId) t */ @Override public void revert(String namespace, InstanceId instanceId) throws MachineIdOverflowException { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkNotNull(instanceId, "instanceId can not be null!"); + MachineState lastLocalState = machineStateStorage.get(namespace, instanceId); if (MachineState.NOT_FOUND.equals(lastLocalState)) { revert0(namespace, instanceId, lastLocalState); diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/LocalMachineStateStorage.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/LocalMachineStateStorage.java index d4c77fca53..fd6f4c8f0e 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/LocalMachineStateStorage.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/LocalMachineStateStorage.java @@ -14,6 +14,7 @@ package me.ahoo.cosid.snowflake.machine; import com.google.common.base.Charsets; +import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.io.Files; @@ -45,6 +46,9 @@ public LocalMachineStateStorage() { @Override public MachineState get(String namespace, InstanceId instanceId) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkNotNull(instanceId, "instanceId can not be null!"); + File stateFile = getStateFile(namespace, instanceId); if (log.isInfoEnabled()) { log.info("get - read from stateLocation : [{}].", stateFile.getAbsolutePath()); @@ -92,6 +96,10 @@ private File getStateFile(String namespace, InstanceId instanceId) { @Override public void set(String namespace, int machineId, InstanceId instanceId) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkArgument(machineId >= 0, "machineId:[%s] must be greater than or equal to 0!", machineId); + Preconditions.checkNotNull(instanceId, "instanceId can not be null!"); + File stateFile = getStateFile(namespace, instanceId); if (log.isInfoEnabled()) { log.info("set - write machineId:[{}] to stateLocation : [{}].", machineId, stateFile.getAbsolutePath()); @@ -118,6 +126,9 @@ public void set(String namespace, int machineId, InstanceId instanceId) { @Override public void remove(String namespace, InstanceId instanceId) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkNotNull(instanceId, "instanceId can not be null!"); + File stateFile = getStateFile(namespace, instanceId); if (log.isInfoEnabled()) { log.info("remove - stateLocation : [{}].", stateFile.getAbsolutePath()); @@ -129,6 +140,8 @@ public void remove(String namespace, InstanceId instanceId) { @Override public void clear(String namespace) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + if (log.isInfoEnabled()) { log.info("clear - namespace : [{}].", namespace); } @@ -156,12 +169,17 @@ private File[] getStateFilesOf(String namespace) { @Override public int size(String namespace) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + return getStateFilesOf(namespace).length; } @Override public boolean exists(String namespace, InstanceId instanceId) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkNotNull(instanceId, "instanceId can not be null!"); + File stateFile = getStateFile(namespace, instanceId); return stateFile.exists(); } diff --git a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/k8s/StatefulSetMachineIdDistributor.java b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/k8s/StatefulSetMachineIdDistributor.java index 40541178fc..81c597fb79 100644 --- a/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/k8s/StatefulSetMachineIdDistributor.java +++ b/cosid-core/src/main/java/me/ahoo/cosid/snowflake/machine/k8s/StatefulSetMachineIdDistributor.java @@ -35,7 +35,7 @@ public static int resolveMachineId() { String hostName = System.getenv(HOSTNAME_KEY); Preconditions.checkNotNull(hostName, "HOSTNAME can not be null."); int lastSplitIdx = hostName.lastIndexOf("-"); - + Preconditions.checkArgument(lastSplitIdx > 0, "The format of hostName:[%s] is incorrect.", hostName); String idStr = hostName.substring(lastSplitIdx + 1); if (log.isInfoEnabled()) { log.info("resolveMachineId - machineId:[{}] from Env HOSTNAME:[{}]", idStr, hostName); diff --git a/cosid-core/src/test/java/me/ahoo/cosid/MachineIdDistributorTest.java b/cosid-core/src/test/java/me/ahoo/cosid/MachineIdDistributorTest.java index 0715ed11db..5db012b1ac 100644 --- a/cosid-core/src/test/java/me/ahoo/cosid/MachineIdDistributorTest.java +++ b/cosid-core/src/test/java/me/ahoo/cosid/MachineIdDistributorTest.java @@ -30,7 +30,7 @@ class MachineIdDistributorTest { key = StatefulSetMachineIdDistributor.HOSTNAME_KEY, value = "cosid-host-6") void distribute() { - var machineId = StatefulSetMachineIdDistributor.INSTANCE.distribute("k8s", 0, InstanceId.NONE); + var machineId = StatefulSetMachineIdDistributor.INSTANCE.distribute("k8s", 1, InstanceId.NONE); Assertions.assertEquals(6, machineId); } } diff --git a/cosid-jdbc/src/main/java/me/ahoo/cosid/jdbc/JdbcIdSegmentDistributor.java b/cosid-jdbc/src/main/java/me/ahoo/cosid/jdbc/JdbcIdSegmentDistributor.java index 02e78a41c6..118703812c 100644 --- a/cosid-jdbc/src/main/java/me/ahoo/cosid/jdbc/JdbcIdSegmentDistributor.java +++ b/cosid-jdbc/src/main/java/me/ahoo/cosid/jdbc/JdbcIdSegmentDistributor.java @@ -13,6 +13,8 @@ package me.ahoo.cosid.jdbc; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; import me.ahoo.cosid.CosIdException; import me.ahoo.cosid.jdbc.exception.NotFoundMaxIdException; @@ -46,6 +48,13 @@ public JdbcIdSegmentDistributor(String namespace, String name, long step, DataSo } public JdbcIdSegmentDistributor(String namespace, String name, long step, String incrementMaxIdSql, String fetchMaxIdSql, DataSource dataSource) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "name can not be empty!"); + Preconditions.checkArgument(step > 0, "step:[%s] must be greater than 0!", step); + Preconditions.checkArgument(!Strings.isNullOrEmpty(incrementMaxIdSql), "incrementMaxIdSql can not be empty!"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(fetchMaxIdSql), "fetchMaxIdSql can not be empty!"); + Preconditions.checkNotNull(dataSource, "dataSource can not be null!"); + this.namespace = namespace; this.name = name; this.step = step; @@ -69,7 +78,6 @@ public long getStep() { return step; } - @Override public long nextMaxId(long step) { IdSegmentDistributor.ensureStep(step); diff --git a/cosid-jdbc/src/main/java/me/ahoo/cosid/jdbc/JdbcIdSegmentInitializer.java b/cosid-jdbc/src/main/java/me/ahoo/cosid/jdbc/JdbcIdSegmentInitializer.java index c24152494a..d97b419416 100644 --- a/cosid-jdbc/src/main/java/me/ahoo/cosid/jdbc/JdbcIdSegmentInitializer.java +++ b/cosid-jdbc/src/main/java/me/ahoo/cosid/jdbc/JdbcIdSegmentInitializer.java @@ -13,6 +13,8 @@ package me.ahoo.cosid.jdbc; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; import javax.sql.DataSource; @@ -46,6 +48,10 @@ public JdbcIdSegmentInitializer(DataSource dataSource) { } public JdbcIdSegmentInitializer(String initCosIdTableSql, String initIdSegmentSql, DataSource dataSource) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(initCosIdTableSql), "initCosIdTableSql can not be empty!"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(initIdSegmentSql), "initIdSegmentSql can not be empty!"); + Preconditions.checkNotNull(dataSource, "dataSource can not be null!"); + this.initCosIdTableSql = initCosIdTableSql; this.initIdSegmentSql = initIdSegmentSql; this.dataSource = dataSource; @@ -78,6 +84,9 @@ public boolean tryInitCosIdTable() { } public int initIdSegment(String segmentName, long offset) throws SQLException, SQLIntegrityConstraintViolationException { + Preconditions.checkArgument(!Strings.isNullOrEmpty(segmentName), "segmentName can not be empty!"); + Preconditions.checkArgument(offset >= 0, "offset:[%s] must be greater than or equal to 0!", offset); + if (log.isInfoEnabled()) { log.info("initIdSegment - segmentName:[{}] - offset:[{}]", segmentName, offset); } diff --git a/cosid-redis/src/main/java/me/ahoo/cosid/redis/RedisIdSegmentDistributor.java b/cosid-redis/src/main/java/me/ahoo/cosid/redis/RedisIdSegmentDistributor.java index c2ad0056be..e5f02cc193 100644 --- a/cosid-redis/src/main/java/me/ahoo/cosid/redis/RedisIdSegmentDistributor.java +++ b/cosid-redis/src/main/java/me/ahoo/cosid/redis/RedisIdSegmentDistributor.java @@ -13,6 +13,8 @@ package me.ahoo.cosid.redis; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import io.lettuce.core.ScriptOutputType; import io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands; import lombok.extern.slf4j.Slf4j; @@ -59,10 +61,15 @@ public RedisIdSegmentDistributor(String namespace, long step, Duration timeout, RedisClusterAsyncCommands redisCommands) { - this.step = step; + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "name can not be empty!"); + Preconditions.checkArgument(offset >= 0, "offset:[%s] must be greater than or equal to 0!", offset); + Preconditions.checkArgument(step > 0, "step:[%s] must be greater than 0!", step); + this.namespace = namespace; this.name = name; this.offset = offset; + this.step = step; this.timeout = timeout; this.redisCommands = redisCommands; this.adderKey = CosId.COSID + ":" + hashTag(getNamespacedName()) + ".adder"; diff --git a/cosid-spring-redis/src/main/java/me/ahoo/cosid/spring/redis/SpringRedisIdSegmentDistributor.java b/cosid-spring-redis/src/main/java/me/ahoo/cosid/spring/redis/SpringRedisIdSegmentDistributor.java index f006fd8004..7833bfd2f2 100644 --- a/cosid-spring-redis/src/main/java/me/ahoo/cosid/spring/redis/SpringRedisIdSegmentDistributor.java +++ b/cosid-spring-redis/src/main/java/me/ahoo/cosid/spring/redis/SpringRedisIdSegmentDistributor.java @@ -13,6 +13,8 @@ package me.ahoo.cosid.spring.redis; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import lombok.extern.slf4j.Slf4j; import me.ahoo.cosid.CosId; import me.ahoo.cosid.segment.IdSegmentDistributor; @@ -49,7 +51,7 @@ public class SpringRedisIdSegmentDistributor implements IdSegmentDistributor { public SpringRedisIdSegmentDistributor(String namespace, String name, StringRedisTemplate redisTemplate) { - this(namespace, name, DEFAULT_OFFSET, DEFAULT_STEP, redisTemplate); + this(namespace, name, DEFAULT_OFFSET, DEFAULT_STEP, redisTemplate); } public SpringRedisIdSegmentDistributor(String namespace, @@ -57,10 +59,15 @@ public SpringRedisIdSegmentDistributor(String namespace, long offset, long step, StringRedisTemplate redisTemplate) { - this.step = step; + Preconditions.checkArgument(!Strings.isNullOrEmpty(namespace), "namespace can not be empty!"); + Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "name can not be empty!"); + Preconditions.checkArgument(offset >= 0, "offset:[%s] must be greater than or equal to 0!", offset); + Preconditions.checkArgument(step > 0, "step:[%s] must be greater than 0!", step); + this.namespace = namespace; this.name = name; this.offset = offset; + this.step = step; this.redisTemplate = redisTemplate; this.adderKey = CosId.COSID + ":" + hashTag(getNamespacedName()) + ".adder"; } diff --git a/gradle.properties b/gradle.properties index cfdaa4456a..b9045834e5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ # group=me.ahoo.cosid -version=1.3.2 +version=1.3.3 description=Global distributed ID generator website=https://github.com/Ahoo-Wang/CosId