Skip to content

Commit

Permalink
add cosid-spring-redis
Browse files Browse the repository at this point in the history
fix `IdSegmentChain` performs two prefetch operations when it is first started
fix the issue of `PrefetchWorker` log information output wrong
fix
  • Loading branch information
Ahoo-Wang committed Jul 12, 2021
1 parent 224fb87 commit 4b1cffa
Show file tree
Hide file tree
Showing 32 changed files with 664 additions and 51 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ In actual use, we generally do not use the same `IdGenerator` for all business s
> Kotlin DSL
``` kotlin
val cosidVersion = "1.2.4";
val cosidVersion = "1.3.1";
implementation("me.ahoo.cosid:spring-boot-starter-cosid:${cosidVersion}")
```

Expand All @@ -326,7 +326,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.2.4</cosid.version>
<cosid.version>1.3.1</cosid.version>
</properties>

<dependencies>
Expand All @@ -343,6 +343,15 @@ In actual use, we generally do not use the same `IdGenerator` for all business s
### application.yaml

```yaml
spring:
application:
name: ${service.name:cosid-example}
datasource:
url: jdbc:mysql://localhost:3306/test_db
username: root
password: root
redis:
url: redis://localhost:6379
cosid:
namespace: ${spring.application.name}
snowflake:
Expand Down Expand Up @@ -405,7 +414,7 @@ cosid:
``` shell
gradle cosid-core:jmh
# or
java -jar cosid-core/build/libs/cosid-core-1.2.8-jmh.jar -bm thrpt -wi 1 -rf json -f 1
java -jar cosid-core/build/libs/cosid-core-1.3.1-jmh.jar -bm thrpt -wi 1 -rf json -f 1
```

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

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

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

```
Expand All @@ -485,7 +494,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.2.8-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.1-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
```
```
Benchmark Mode Cnt Score Error Units
Expand Down
23 changes: 16 additions & 7 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ IdGenerator idGenerator=idGeneratorProvider.get("bizA");
> Kotlin DSL
``` kotlin
val cosidVersion = "1.2.8";
val cosidVersion = "1.3.1";
implementation("me.ahoo.cosid:spring-boot-starter-cosid:${cosidVersion}")
```

Expand All @@ -321,7 +321,7 @@ IdGenerator idGenerator=idGeneratorProvider.get("bizA");
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<cosid.version>1.2.8</cosid.version>
<cosid.version>1.3.1</cosid.version>
</properties>

<dependencies>
Expand All @@ -338,6 +338,15 @@ IdGenerator idGenerator=idGeneratorProvider.get("bizA");
### application.yaml

```yaml
spring:
application:
name: ${service.name:cosid-example}
datasource:
url: jdbc:mysql://localhost:3306/test_db
username: root
password: root
redis:
url: redis://localhost:6379
cosid:
namespace: ${spring.application.name}
snowflake:
Expand Down Expand Up @@ -398,7 +407,7 @@ cosid:
``` shell
gradle cosid-core:jmh
# or
java -jar cosid-core/build/libs/cosid-core-1.2.8-jmh.jar -bm thrpt -wi 1 -rf json -f 1
java -jar cosid-core/build/libs/cosid-core-1.3.1-jmh.jar -bm thrpt -wi 1 -rf json -f 1
```

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

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

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

```
Expand All @@ -478,7 +487,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.2.8-jmh.jar -bm sample -wi 1 -rf json -f 1 -tu us step_1000
java -jar cosid-jdbc/build/libs/cosid-jdbc-1.3.1-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 @@ -30,6 +30,7 @@
*/
public interface IdSegmentDistributor {
int DEFAULT_SEGMENTS = 1;
int DEFAULT_OFFSET = 0;
int DEFAULT_STEP = 100;

String getNamespace();
Expand Down
24 changes: 20 additions & 4 deletions cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public synchronized void wakeup() {

public void prefetch() {

if (IdSegmentChain.ROOT_VERSION == tailChain.getVersion()) {
appendChain(tailChain, safeDistance + 1);
return;
}

long wakeupTimeGap = System.currentTimeMillis() - lastWakeupTime;
boolean hunger = wakeupTimeGap < hungerThreshold;

Expand Down Expand Up @@ -239,29 +244,40 @@ public void prefetch() {
}

forward(availableHeadChain);

final int headToTailGap = availableHeadChain.gap(tailChain);
final int safeGap = safeDistance - headToTailGap;

if (safeGap <= 0 && !hunger) {
if (log.isTraceEnabled()) {
log.trace("prefetch - {} - safeGap is less than or equal to 0, and is not hungry - headChain.version:[{}] - tailChain.version:[{}].", maxIdDistributor.getNamespacedName(), availableHeadChain.getVersion(), tailChain.getVersion());
}
return;
}

final int prefetchSegments = hunger ? this.prefetchDistance : safeGap;

appendChain(availableHeadChain, prefetchSegments);
}

private void appendChain(IdSegmentChain availableHeadChain, int prefetchSegments) {

if (log.isDebugEnabled()) {
log.debug("prefetch - {} - headChain.version:[{}] - tailChain.version:[{}] - prefetchDistance:[{}].", maxIdDistributor.getNamespacedName(), availableHeadChain.getVersion(), tailChain.getVersion(), this.prefetchDistance);
log.debug("appendChain - {} - headChain.version:[{}] - tailChain.version:[{}] - prefetchSegments:[{}].", maxIdDistributor.getNamespacedName(), availableHeadChain.getVersion(), tailChain.getVersion(), prefetchSegments);
}

try {
final IdSegmentChain preTail = tailChain;
tailChain = tailChain.ensureSetNext((preChain) -> generateNext(preChain, this.prefetchDistance)).getNext();
tailChain = tailChain.ensureSetNext((preChain) -> generateNext(preChain, prefetchSegments)).getNext();
while (tailChain.getNext() != null) {
tailChain = tailChain.getNext();
}
if (log.isDebugEnabled()) {
log.debug("prefetch - {} - restTail - tailChain.version:[{}:{}->{}] .", maxIdDistributor.getNamespacedName(), preTail.gap(preTail.getNext()), preTail.getVersion(), preTail.getNext().getVersion());
log.debug("appendChain - {} - restTail - tailChain.version:[{}:{}->{}] .", maxIdDistributor.getNamespacedName(), preTail.gap(tailChain), preTail.getVersion(), tailChain.getVersion());
}
} catch (NextIdSegmentExpiredException nextIdSegmentExpiredException) {
if (log.isWarnEnabled()) {
log.warn("prefetch - {} - gave up this next IdSegmentChain.", maxIdDistributor.getNamespacedName(), nextIdSegmentExpiredException);
log.warn("appendChain - {} - gave up this next IdSegmentChain.", maxIdDistributor.getNamespacedName(), nextIdSegmentExpiredException);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
import lombok.extern.slf4j.Slf4j;
import me.ahoo.cosid.snowflake.ClockBackwardsSynchronizer;

import java.time.Duration;

/**
* @author ahoo wang
*/
@Slf4j
public abstract class AbstractMachineIdDistributor implements MachineIdDistributor {
public static final int NOT_FOUND_LAST_STAMP = -1;
private final int DEFAULT_TIMEOUT = 1;
private final Duration timeout = Duration.ofSeconds(DEFAULT_TIMEOUT);
private final MachineStateStorage machineStateStorage;
private final ClockBackwardsSynchronizer clockBackwardsSynchronizer;

Expand Down
1 change: 0 additions & 1 deletion cosid-redis/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dependencies {
api(project(":cosid-core"))
implementation("me.ahoo.cosky:cosky-core")
api("io.lettuce:lettuce-core")
api("com.google.guava:guava")
testImplementation("org.junit-pioneer:junit-pioneer")
jmh("org.openjdk.jmh:jmh-core:${rootProject.ext.get("jmhVersion")}")
jmh("org.openjdk.jmh:jmh-generator-annprocess:${rootProject.ext.get("jmhVersion")}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class RedisIdSegmentDistributor implements IdSegmentDistributor {

public static final String REDIS_ID_GENERATE = "redis_id_generate.lua";
public static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(1);
public static final int DEFAULT_OFFSET = 0;

private final String namespace;
private final String name;
Expand Down
20 changes: 18 additions & 2 deletions cosid-rest-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,35 @@ application {
dependencies {
implementation(platform(project(":cosid-dependencies")))
implementation(project(":spring-boot-starter-cosid"))
implementation("me.ahoo.cosky:cosky-spring-cloud-core")

implementation("io.springfox:springfox-boot-starter")

implementation(project(":cosid-redis"))
//region cosid-redis
// implementation(project(":cosid-redis"))
// implementation("me.ahoo.cosky:cosky-spring-cloud-core")
// or
// implementation(project(":spring-boot-starter-cosid")){
// capabilities {
// requireCapability("me.ahoo.cosid:redis-support")
// }
// }
//endregion
//region cosid-spring-redis
implementation(project(":cosid-spring-redis"))
implementation("org.springframework.boot:spring-boot-starter-data-redis")
// or
// implementation(project(":spring-boot-starter-cosid")){
// capabilities {
// requireCapability("me.ahoo.cosid:spring-redis-support")
// }
// }

//endregion
//region cosid-jdbc
implementation(project(":cosid-jdbc"))
implementation("org.springframework.boot:spring-boot-starter-jdbc")
implementation("mysql:mysql-connector-java")
//endregion

implementation("io.springfox:springfox-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-actuator")
Expand Down
11 changes: 7 additions & 4 deletions cosid-rest-api/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
spring:
application:
name: ${service.name:cosid-example}
datasource:
url: jdbc:mysql://localhost:3306/test_db?serverTimezone=GMT%2B8&characterEncoding=utf-8
url: jdbc:mysql://localhost:3306/test_db
username: root
password: root

redis:
url: redis://localhost:6379
cosid:
namespace: ${spring.application.name}
snowflake:
Expand Down Expand Up @@ -40,7 +43,7 @@ cosid:
safe-distance: 5
prefetch-period: 1s
distributor:
type: jdbc
type: redis
share:
offset: 0
step: 100
Expand All @@ -53,5 +56,5 @@ cosid:
step: 100
logging:
level:
me.ahoo.cosid: info
me.ahoo.cosid: debug

36 changes: 36 additions & 0 deletions cosid-spring-redis/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright [2021-2021] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id("me.champeau.jmh") version "0.6.4"
}

dependencies {
api(project(":cosid-core"))
api("org.springframework.data:spring-data-redis")
testImplementation("org.junit-pioneer:junit-pioneer")
jmh("org.openjdk.jmh:jmh-core:${rootProject.ext.get("jmhVersion")}")
jmh("org.openjdk.jmh:jmh-generator-annprocess:${rootProject.ext.get("jmhVersion")}")
}

jmh {
jmhVersion.set(rootProject.ext.get("jmhVersion").toString())
warmupIterations.set(1)
iterations.set(1)
resultFormat.set("json")
benchmarkMode.set(listOf(
"thrpt"
))
// threads.set(40)
fork.set(1)
}
Loading

0 comments on commit 4b1cffa

Please sign in to comment.