Skip to content

Commit

Permalink
enhancement: add defensive parameter check to enhance robustness.
Browse files Browse the repository at this point in the history
add: add uuid_generate jmh-test
  • Loading branch information
Ahoo-Wang committed Jul 22, 2021
1 parent de8380b commit ecace22
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 27 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
```

Expand All @@ -328,7 +328,7 @@ In actual use, we generally do not use the same `IdGenerator` for all business s
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<cosid.version>1.3.2</cosid.version>
<cosid.version>1.3.3</cosid.version>
</properties>
<dependencies>
Expand Down Expand Up @@ -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
```

```
Expand All @@ -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
```

```
Expand All @@ -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
```

```
Expand All @@ -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
```

```
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
```

Expand All @@ -323,7 +323,7 @@ IdGenerator idGenerator=idGeneratorProvider.get("bizA");
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<cosid.version>1.3.2</cosid.version>
<cosid.version>1.3.3</cosid.version>
</properties>
<dependencies>
Expand Down Expand Up @@ -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
```

```
Expand All @@ -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
```

```
Expand All @@ -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
```

```
Expand All @@ -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
```

```
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading

0 comments on commit ecace22

Please sign in to comment.