Skip to content

Commit

Permalink
- Upgrade to CoSky 1.3.1
Browse files Browse the repository at this point in the history
- Upgrade to Spring-Boot 2.4.10
  • Loading branch information
Ahoo-Wang committed Aug 25, 2021
1 parent 9de2229 commit b90a4e0
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 50 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public interface OrderRepository {
> Kotlin DSL

``` kotlin
val cosidVersion = "1.3.13";
val cosidVersion = "1.3.14";
implementation("me.ahoo.cosid:cosid-spring-boot-starter:${cosidVersion}")
```

Expand All @@ -401,7 +401,7 @@ public interface OrderRepository {
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<cosid.version>1.3.13</cosid.version>
<cosid.version>1.3.14</cosid.version>
</properties>
<dependencies>
Expand Down Expand Up @@ -491,7 +491,7 @@ cosid:
``` shell
gradle cosid-core:jmh
# or
java -jar cosid-core/build/libs/cosid-core-1.3.13-jmh.jar -bm thrpt -wi 1 -rf json -f 1
java -jar cosid-core/build/libs/cosid-core-1.3.14-jmh.jar -bm thrpt -wi 1 -rf json -f 1
```

```
Expand All @@ -512,7 +512,7 @@ SnowflakeIdBenchmark.secondSnowflakeId_generate thrpt 4206843.
``` shell
gradle cosid-redis:jmh
# or
java -jar cosid-redis/build/libs/cosid-redis-1.3.13-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark
java -jar cosid-redis/build/libs/cosid-redis-1.3.14-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark
```

```
Expand All @@ -530,7 +530,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.13-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
java -jar cosid-redis/build/libs/cosid-redis-1.3.14-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
```

```
Expand All @@ -555,7 +555,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.13-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.14-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark
```

```
Expand All @@ -571,7 +571,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.13-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.14-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
```
```
Benchmark Mode Cnt Score Error Units
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ val libraryProjects = publishProjects - bomProjects
ext {
set("lombokVersion", "1.18.20")
set("guavaVersion", "30.0-jre")
set("springBootVersion", "2.4.7")
set("springBootVersion", "2.4.10")
set("springCloudVersion", "2020.0.3")
set("springfoxVersion", "3.0.0")
set("jmhVersion", "1.29")
set("junitPioneerVersion", "1.4.2")
set("mybatisVersion", "3.5.7")
set("mybatisBootVersion","2.1.4")
set("coskyVersion", "1.2.10")
set("coskyVersion", "1.3.1")
set("libraryProjects", libraryProjects)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public synchronized RedisIdSegmentDistributor createDistributor(int step) {
0,
step,
RedisIdSegmentDistributor.DEFAULT_TIMEOUT,
redisClient.connect().async());
redisClient.connect().reactive());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@

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 io.lettuce.core.cluster.api.reactive.RedisClusterReactiveCommands;
import lombok.extern.slf4j.Slf4j;
import me.ahoo.cosid.CosId;
import me.ahoo.cosid.segment.IdSegmentDistributor;
import me.ahoo.cosky.core.redis.RedisScripts;
import me.ahoo.cosky.core.util.Futures;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;

import static me.ahoo.cosid.redis.RedisMachineIdDistributor.hashTag;

Expand All @@ -47,12 +43,12 @@ public class RedisIdSegmentDistributor implements IdSegmentDistributor {
private final long offset;
private final long step;
private final Duration timeout;
private final RedisClusterAsyncCommands<String, String> redisCommands;
private final RedisClusterReactiveCommands<String, String> redisCommands;
private volatile long lastMaxId;

public RedisIdSegmentDistributor(String namespace,
String name,
RedisClusterAsyncCommands<String, String> redisCommands) {
RedisClusterReactiveCommands<String, String> redisCommands) {
this(namespace, name, DEFAULT_OFFSET, DEFAULT_STEP, DEFAULT_TIMEOUT, redisCommands);
}

Expand All @@ -61,7 +57,7 @@ public RedisIdSegmentDistributor(String namespace,
long offset,
long step,
Duration timeout,
RedisClusterAsyncCommands<String, String> redisCommands) {
RedisClusterReactiveCommands<String, String> redisCommands) {
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);
Expand All @@ -81,7 +77,7 @@ private void ensureOffset() {
if (log.isDebugEnabled()) {
log.debug("ensureOffset -[{}]- offset:[{}].", adderKey, offset);
}
Boolean notExists = Futures.getUnChecked(redisCommands.setnx(this.adderKey, String.valueOf(offset)), timeout);
Boolean notExists = redisCommands.setnx(this.adderKey, String.valueOf(offset)).block(timeout);
if (log.isDebugEnabled()) {
log.debug("ensureOffset -[{}]- offset:[{}] - notExists:[{}].", adderKey, offset, notExists);
}
Expand Down Expand Up @@ -118,7 +114,7 @@ public long nextMaxId(long step) {
}
final long nextMinMaxId = lastMaxId + step;

long nextMaxId = Futures.getUnChecked(redisCommands.incrby(adderKey, step), timeout);
long nextMaxId = redisCommands.incrby(adderKey, step).block(timeout);
if (log.isDebugEnabled()) {
log.debug("nextMaxId -[{}]- step:[{}] - nextMaxId:[{}].", adderKey, step, nextMaxId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
package me.ahoo.cosid.redis;

import io.lettuce.core.ScriptOutputType;
import io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands;
import io.lettuce.core.cluster.api.reactive.RedisClusterReactiveCommands;
import lombok.extern.slf4j.Slf4j;
import me.ahoo.cosid.snowflake.ClockBackwardsSynchronizer;
import me.ahoo.cosid.snowflake.machine.*;
import me.ahoo.cosid.util.Futures;
import me.ahoo.cosky.core.redis.RedisScripts;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static me.ahoo.cosid.snowflake.ClockBackwardsSynchronizer.getBackwardsTimeStamp;

Expand All @@ -37,15 +36,15 @@ public class RedisMachineIdDistributor extends AbstractMachineIdDistributor {
public static final String MACHINE_ID_REVERT_STABLE = "machine_id_revert_stable.lua";
public static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(1);
public final Duration timeout;
private final RedisClusterAsyncCommands<String, String> redisCommands;
private final RedisClusterReactiveCommands<String, String> redisCommands;

public RedisMachineIdDistributor(RedisClusterAsyncCommands<String, String> redisCommands,
public RedisMachineIdDistributor(RedisClusterReactiveCommands<String, String> redisCommands,
MachineStateStorage machineStateStorage,
ClockBackwardsSynchronizer clockBackwardsSynchronizer) {
this(DEFAULT_TIMEOUT, redisCommands, machineStateStorage, clockBackwardsSynchronizer);
}

public RedisMachineIdDistributor(Duration timeout, RedisClusterAsyncCommands<String, String> redisCommands,
public RedisMachineIdDistributor(Duration timeout, RedisClusterReactiveCommands<String, String> redisCommands,
MachineStateStorage machineStateStorage,
ClockBackwardsSynchronizer clockBackwardsSynchronizer) {
super(machineStateStorage, clockBackwardsSynchronizer);
Expand All @@ -57,24 +56,24 @@ public RedisMachineIdDistributor(Duration timeout, RedisClusterAsyncCommands<Str
@Override
protected MachineState distribute0(String namespace, int machineBit, InstanceId instanceId) {

MachineState machineState =Futures.getUnChecked(distributeAsync0(namespace, machineBit, instanceId),timeout);
MachineState machineState = distributeAsync0(namespace, machineBit, instanceId).block(timeout);
if (log.isInfoEnabled()) {
log.info("distribute0 - machineState:[{}] - instanceId:[{}] - machineBit:[{}] @ namespace:[{}].", machineState, instanceId, machineBit, namespace);
}
return machineState;
}

protected CompletableFuture<MachineState> distributeAsync0(String namespace, int machineBit, InstanceId instanceId) {
protected Mono<MachineState> distributeAsync0(String namespace, int machineBit, InstanceId instanceId) {
if (log.isInfoEnabled()) {
log.info("distributeAsync0 - instanceId:[{}] - machineBit:[{}] @ namespace:[{}].", instanceId, machineBit, namespace);
}
return RedisScripts.doEnsureScript(MACHINE_ID_DISTRIBUTE, redisCommands,
(scriptSha) -> {
String[] keys = {hashTag(namespace)};
String[] values = {instanceId.getInstanceId(), String.valueOf(maxMachineId(machineBit))};
return redisCommands.evalsha(scriptSha, ScriptOutputType.MULTI, keys, values);
return redisCommands.evalsha(scriptSha, ScriptOutputType.MULTI, keys, values).next();
}
).thenApply(distribution -> {
).map(distribution -> {
@SuppressWarnings("unchecked")
List<Long> state = (List<Long>) distribution;
int realMachineId = state.get(0).intValue();
Expand All @@ -95,7 +94,7 @@ protected CompletableFuture<MachineState> distributeAsync0(String namespace, int

@Override
protected void revert0(String namespace, InstanceId instanceId, MachineState machineState) {
Futures.getUnChecked(revertAsync0(namespace, instanceId, machineState),timeout);
revertAsync0(namespace, instanceId, machineState).block(timeout);
}

/**
Expand All @@ -106,7 +105,7 @@ protected void revert0(String namespace, InstanceId instanceId, MachineState mac
* @param machineState
* @return
*/
protected CompletableFuture<Void> revertAsync0(String namespace, InstanceId instanceId, MachineState machineState) {
protected Mono<Void> revertAsync0(String namespace, InstanceId instanceId, MachineState machineState) {
if (log.isInfoEnabled()) {
log.info("revertAsync - instanceId:[{}] @ namespace:[{}].", instanceId, namespace);
}
Expand All @@ -118,7 +117,7 @@ protected CompletableFuture<Void> revertAsync0(String namespace, InstanceId inst
return revertScriptAsync(MACHINE_ID_REVERT, namespace, instanceId, machineState);
}

private CompletableFuture<Void> revertScriptAsync(String scriptName, String namespace, InstanceId instanceId, MachineState machineState) {
private Mono<Void> revertScriptAsync(String scriptName, String namespace, InstanceId instanceId, MachineState machineState) {
return RedisScripts.doEnsureScript(scriptName, redisCommands,
(scriptSha) -> {
long lastStamp = machineState.getLastTimeStamp();
Expand All @@ -127,7 +126,7 @@ private CompletableFuture<Void> revertScriptAsync(String scriptName, String name
}
String[] keys = {hashTag(namespace)};
String[] values = {instanceId.getInstanceId(), String.valueOf(lastStamp)};
return redisCommands.evalsha(scriptSha, ScriptOutputType.INTEGER, keys, values);
return redisCommands.evalsha(scriptSha, ScriptOutputType.INTEGER, keys, values).then();
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void initRedis() {
System.out.println("--- initRedis ---");
redisClient = RedisClient.create("redis://localhost:6379");
redisConnection = redisClient.connect();
redisMaxIdDistributor = new RedisIdSegmentDistributor(UUID.randomUUID().toString(), "RedisIdGeneratorTest", 0, 100, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().async());
redisMaxIdDistributor = new RedisIdSegmentDistributor(UUID.randomUUID().toString(), "RedisIdGeneratorTest", 0, 100, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().reactive());
}

@Test
Expand Down Expand Up @@ -79,7 +79,7 @@ public void generateIfMaxIdBack() {
@Test
public void generate_offset() {
String namespace = UUID.randomUUID().toString();
RedisIdSegmentDistributor redisMaxIdDistributor_offset_10 = new RedisIdSegmentDistributor(namespace, "generate_offset", 10, 100, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().async());
RedisIdSegmentDistributor redisMaxIdDistributor_offset_10 = new RedisIdSegmentDistributor(namespace, "generate_offset", 10, 100, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().reactive());
long id = redisMaxIdDistributor_offset_10.nextMaxId();
Assertions.assertEquals(110, id);
}
Expand All @@ -90,7 +90,7 @@ public void generate_offset() {
@Test
public void concurrent_generate_step_100() {
String namespace = UUID.randomUUID().toString();
RedisIdSegmentDistributor redisMaxIdDistributor_generate_step_100 = new RedisIdSegmentDistributor(namespace, "generate_step_10", 0, 100, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().async());
RedisIdSegmentDistributor redisMaxIdDistributor_generate_step_100 = new RedisIdSegmentDistributor(namespace, "generate_step_10", 0, 100, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().reactive());
SegmentId defaultSegmentId = new DefaultSegmentId(redisMaxIdDistributor_generate_step_100);
CompletableFuture<List<Long>>[] completableFutures = new CompletableFuture[CONCURRENT_THREADS];
int threads = 0;
Expand Down Expand Up @@ -137,8 +137,8 @@ public void concurrent_generate_step_100() {
@Test
public void concurrent_generate_step_10_multi_instance() {
String namespace = UUID.randomUUID().toString();
RedisIdSegmentDistributor redisMaxIdDistributor1 = new RedisIdSegmentDistributor(namespace, "generate_step_10", 0, 10, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().async());
RedisIdSegmentDistributor redisMaxIdDistributor2 = new RedisIdSegmentDistributor(namespace, "generate_step_10", 0, 10, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().async());
RedisIdSegmentDistributor redisMaxIdDistributor1 = new RedisIdSegmentDistributor(namespace, "generate_step_10", 0, 10, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().reactive());
RedisIdSegmentDistributor redisMaxIdDistributor2 = new RedisIdSegmentDistributor(namespace, "generate_step_10", 0, 10, RedisIdSegmentDistributor.DEFAULT_TIMEOUT, redisClient.connect().reactive());
SegmentId idGenerator1 = new DefaultSegmentId(redisMaxIdDistributor1);
SegmentId idGenerator2 = new DefaultSegmentId(redisMaxIdDistributor2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void initRedis() {
System.out.println("--- initRedis ---");
redisClient = RedisClient.create("redis://localhost:6379");
redisConnection = redisClient.connect();
redisMachineIdDistributor = new RedisMachineIdDistributor(redisConnection.async(), MachineStateStorage.LOCAL, ClockBackwardsSynchronizer.DEFAULT);
redisMachineIdDistributor = new RedisMachineIdDistributor(redisConnection.reactive(), MachineStateStorage.LOCAL, ClockBackwardsSynchronizer.DEFAULT);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private SegmentId createSegmentId(String name, SegmentIdProperties.IdDefinition
idDefinition.getOffset(),
idDefinition.getStep(),
timeout,
redisConnectionFactory.getShareAsyncCommands());
redisConnectionFactory.getShareReactiveCommands());
return CosIdSegmentAutoConfiguration.createSegment(segmentIdProperties, idDefinition, redisIdSegmentDistributor, prefetchWorkerExecutorService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public CosIdRedisMachineIdDistributorAutoConfiguration(SnowflakeIdProperties sno
public RedisMachineIdDistributor redisMachineIdDistributor(RedisConnectionFactory redisConnectionFactory, MachineStateStorage localMachineState, ClockBackwardsSynchronizer clockBackwardsSynchronizer) {
Preconditions.checkNotNull(redisConnectionFactory, "redisConnectionFactory can not be null.");
Duration timeout = snowflakeIdProperties.getMachine().getDistributor().getRedis().getTimeout();
return new RedisMachineIdDistributor(timeout, redisConnectionFactory.getShareAsyncCommands(), localMachineState, clockBackwardsSynchronizer);
return new RedisMachineIdDistributor(timeout, redisConnectionFactory.getShareReactiveCommands(), localMachineState, clockBackwardsSynchronizer);
}


Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#

group=me.ahoo.cosid
version=1.3.13
version=1.3.14

description=Global distributed ID generator
website=https://github.com/Ahoo-Wang/CosId
Expand Down
14 changes: 7 additions & 7 deletions wiki/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public interface OrderRepository {
> Kotlin DSL

``` kotlin
val cosidVersion = "1.3.13";
val cosidVersion = "1.3.14";
implementation("me.ahoo.cosid:cosid-spring-boot-starter:${cosidVersion}")
```

Expand All @@ -399,7 +399,7 @@ public interface OrderRepository {
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<cosid.version>1.3.13</cosid.version>
<cosid.version>1.3.14</cosid.version>
</properties>
<dependencies>
Expand Down Expand Up @@ -487,7 +487,7 @@ cosid:
``` shell
gradle cosid-core:jmh
# or
java -jar cosid-core/build/libs/cosid-core-1.3.13-jmh.jar -bm thrpt -wi 1 -rf json -f 1
java -jar cosid-core/build/libs/cosid-core-1.3.14-jmh.jar -bm thrpt -wi 1 -rf json -f 1
```

```
Expand All @@ -508,7 +508,7 @@ SnowflakeIdBenchmark.secondSnowflakeId_generate thrpt 4206843.
``` shell
gradle cosid-redis:jmh
# or
java -jar cosid-redis/build/libs/cosid-redis-1.3.13-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark
java -jar cosid-redis/build/libs/cosid-redis-1.3.14-jmh.jar -bm thrpt -wi 1 -rf json -f 1 RedisChainIdBenchmark
```

```
Expand All @@ -526,7 +526,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.13-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
java -jar cosid-redis/build/libs/cosid-redis-1.3.14-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
```

```
Expand All @@ -551,7 +551,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.13-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.14-jmh.jar -bm thrpt -wi 1 -rf json -f 1 MySqlChainIdBenchmark
```

```
Expand All @@ -567,7 +567,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.13-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.14-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
```

```
Expand Down

0 comments on commit b90a4e0

Please sign in to comment.