Skip to content

Commit

Permalink
Enhance - Reload script after Redis crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed May 25, 2021
1 parent c1d88d6 commit dfe676f
Show file tree
Hide file tree
Showing 22 changed files with 284 additions and 174 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ consistency between process cache and Redis.
> Kotlin DSL
``` kotlin
val governVersion = "0.9.19";
val governVersion = "0.9.20";
implementation("me.ahoo.govern:spring-cloud-starter-govern-config:${governVersion}")
implementation("me.ahoo.govern:spring-cloud-starter-govern-discovery:${governVersion}")
```
Expand All @@ -33,7 +33,7 @@ consistency between process cache and Redis.
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<govern.version>0.9.19</govern.version>
<govern.version>0.9.20</govern.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -77,31 +77,31 @@ logging:
#### Option 1:Download the executable file
> Download [govern-rest-api-server](https://github.com/Ahoo-Wang/govern-service/releases/download/0.9.19/govern-rest-api-0.9.19.tar)
> Download [govern-rest-api-server](https://github.com/Ahoo-Wang/govern-service/releases/download/0.9.20/govern-rest-api-0.9.20.tar)
> tar *govern-rest-api-0.9.19.tar*
> tar *govern-rest-api-0.9.20.tar*
```shell
cd govern-rest-api-0.9.19
# Working directory: govern-rest-api-0.9.19
cd govern-rest-api-0.9.20
# Working directory: govern-rest-api-0.9.20
bin/govern-rest-api --server.port=8080 --govern.redis.uri=redis://localhost:6379
```

#### Option 2:Run On Docker

```shell
docker pull ahoowang/govern-service:0.9.19
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.19
docker pull ahoowang/govern-service:0.9.20
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.20
```

---
> MacBook Pro (M1)
>
> Please use *ahoowang/govern-service:0.9.19-armv7*
> Please use *ahoowang/govern-service:0.9.20-armv7*
```shell
docker pull ahoowang/govern-service:0.9.19-armv7
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.19-armv7
docker pull ahoowang/govern-service:0.9.20-armv7
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.20-armv7
```

#### Option 3:Run On Kubernetes
Expand All @@ -127,7 +127,7 @@ spec:
value: standalone
- name: GOVERN_REDIS_URI
value: redis://redis-uri:6379
image: ahoowang/govern-service:0.9.19
image: ahoowang/govern-service:0.9.20
name: govern-service
resources:
limits:
Expand Down Expand Up @@ -250,7 +250,9 @@ spec:
### ConfigService
``` shell
gradle config:jmh
gradle govern-config:jmh
# or
java -jar govern-config/build/libs/govern-config-0.9.20-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
```

```
Expand All @@ -274,7 +276,9 @@ RedisConfigServiceBenchmark.setConfig thrpt 103659.132
### ServiceDiscovery

``` shell
gradle discovery:jmh
gradle govern-discovery:jmh
# or
java -jar govern-discovery/build/libs/govern-discovery-0.9.20-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
```

```
Expand Down
32 changes: 18 additions & 14 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
> Kotlin DSL
``` kotlin
val governVersion = "0.9.19";
val governVersion = "0.9.20";
implementation("me.ahoo.govern:spring-cloud-starter-govern-config:${governVersion}")
implementation("me.ahoo.govern:spring-cloud-starter-govern-discovery:${governVersion}")
```
Expand All @@ -28,7 +28,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<govern.version>0.9.19</govern.version>
<govern.version>0.9.20</govern.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -72,30 +72,30 @@ logging:
#### 方式一:下载可执行文件
> 下载 [rest-api-server](https://github.com/Ahoo-Wang/govern-service/releases/download/0.9.19/govern-rest-api-0.9.19.tar)
> 下载 [rest-api-server](https://github.com/Ahoo-Wang/govern-service/releases/download/0.9.20/govern-rest-api-0.9.20.tar)
> 解压 *govern-rest-api-0.9.19.tar*
> 解压 *govern-rest-api-0.9.20.tar*
```shell
cd govern-rest-api-0.9.19
# 工作目录: govern-rest-api-0.9.19
cd govern-rest-api-0.9.20
# 工作目录: govern-rest-api-0.9.20
bin/govern-rest-api --server.port=8080 --govern.redis.uri=redis://localhost:6379
```

#### 方式二:在 Docker 中运行

```shell
docker pull ahoowang/govern-service:0.9.19
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.19
docker pull ahoowang/govern-service:0.9.20
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.20
```
---
> MacBook Pro (M1)
>
> 请使用 *ahoowang/govern-service:0.9.19-armv7*
> 请使用 *ahoowang/govern-service:0.9.20-armv7*
```shell
docker pull ahoowang/govern-service:0.9.19-armv7
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.19-armv7
docker pull ahoowang/govern-service:0.9.20-armv7
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.20-armv7
```

#### 方式三:在 Kubernetes 中运行
Expand All @@ -121,7 +121,7 @@ spec:
value: standalone
- name: GOVERN_REDIS_URI
value: redis://redis-uri:6379
image: ahoowang/govern-service:0.9.19
image: ahoowang/govern-service:0.9.20
name: govern-service
resources:
limits:
Expand Down Expand Up @@ -245,7 +245,9 @@ spec:
### ConfigService
``` shell
gradle config:jmh
gradle govern-config:jmh
# or
java -jar govern-config/build/libs/govern-config-0.9.20-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
```

```
Expand All @@ -269,7 +271,9 @@ RedisConfigServiceBenchmark.setConfig thrpt 103659.132
### ServiceDiscovery

``` shell
gradle discovery:jmh
gradle govern-discovery:jmh
# or
java -jar govern-discovery/build/libs/govern-discovery-0.9.20-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
```

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void tearDown() {
}

@Benchmark
public void getConfig() {
configService.getConfig(namespace, configId).join();
public Config getConfig() {
return configService.getConfig(namespace, configId).join();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public void tearDown() {
}

@Benchmark
public void setConfig() {
public Boolean setConfig() {
String randomConfigId = String.valueOf(atomicInteger.incrementAndGet());
configService.setConfig(namespace, randomConfigId, configData).join();
return configService.setConfig(namespace, randomConfigId, configData).join();
}

@Benchmark
public void getConfig() {
configService.getConfig(namespace, configId).join();
public Config getConfig() {
return configService.getConfig(namespace, configId).join();
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.ahoo.govern.config.redis;

import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.async.RedisScriptingAsyncCommands;
import me.ahoo.govern.core.util.RedisScripts;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

/**
* @author ahoo wang
Expand All @@ -13,14 +15,14 @@ public final class ConfigRedisScripts {
public static final String CONFIG_REMOVE = "config_remove.lua";
public static final String CONFIG_ROLLBACK = "config_rollback.lua";

public static CompletableFuture<String> loadConfigSet(RedisScriptingAsyncCommands<String, String> scriptingCommands) {
return RedisScripts.loadScript(CONFIG_SET, scriptingCommands);
public static <T> CompletableFuture<T> doConfigSet(RedisScriptingAsyncCommands<String, String> scriptingCommands, Function<String, RedisFuture<T>> doSha) {
return RedisScripts.doEnsureScript(CONFIG_SET, scriptingCommands, doSha);
}

public static CompletableFuture<String> loadConfigRemove(RedisScriptingAsyncCommands<String, String> scriptingCommands) {
return RedisScripts.loadScript(CONFIG_REMOVE, scriptingCommands);
public static <T> CompletableFuture<T> doConfigRemove(RedisScriptingAsyncCommands<String, String> scriptingCommands, Function<String, RedisFuture<T>> doSha) {
return RedisScripts.doEnsureScript(CONFIG_REMOVE, scriptingCommands, doSha);
}
public static CompletableFuture<String> loadConfigRollback(RedisScriptingAsyncCommands<String, String> scriptingCommands) {
return RedisScripts.loadScript(CONFIG_ROLLBACK, scriptingCommands);
public static <T> CompletableFuture<T> doConfigRollback(RedisScriptingAsyncCommands<String, String> scriptingCommands, Function<String, RedisFuture<T>> doSha) {
return RedisScripts.doEnsureScript(CONFIG_ROLLBACK, scriptingCommands, doSha);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.hash.Hashing;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.ScriptOutputType;
import io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -76,14 +75,11 @@ public CompletableFuture<Boolean> setConfig(String namespace, String configId, S
if (log.isInfoEnabled()) {
log.info("setConfig - configId:[{}] - hash:[{}] @ namespace:[{}].", configId, hash, namespace);
}

return ConfigRedisScripts.loadConfigSet(redisCommands)
.thenCompose(sha -> {
String[] keys = {namespace};
String[] values = {configId, data, hash};
RedisFuture<Boolean> redisFuture = redisCommands.evalsha(sha, ScriptOutputType.BOOLEAN, keys, values);
return redisFuture;
});
return ConfigRedisScripts.doConfigSet(redisCommands, sha -> {
String[] keys = {namespace};
String[] values = {configId, data, hash};
return redisCommands.evalsha(sha, ScriptOutputType.BOOLEAN, keys, values);
});
}

@Override
Expand All @@ -96,14 +92,11 @@ public CompletableFuture<Boolean> removeConfig(String namespace, String configId
if (log.isInfoEnabled()) {
log.info("removeConfig - configId:[{}] @ namespace:[{}].", configId, namespace);
}

return ConfigRedisScripts.loadConfigRemove(redisCommands)
.thenCompose(sha -> {
String[] keys = {namespace};
String[] values = {configId};
RedisFuture<Boolean> redisFuture = redisCommands.evalsha(sha, ScriptOutputType.BOOLEAN, keys, values);
return redisFuture;
});
return ConfigRedisScripts.doConfigRemove(redisCommands, sha -> {
String[] keys = {namespace};
String[] values = {configId};
return redisCommands.evalsha(sha, ScriptOutputType.BOOLEAN, keys, values);
});
}

@Override
Expand All @@ -122,14 +115,11 @@ public CompletableFuture<Boolean> rollback(String namespace, String configId, in
if (log.isInfoEnabled()) {
log.info("rollback - configId:[{}] - targetVersion:[{}] @ namespace:[{}].", configId, targetVersion, namespace);
}

return ConfigRedisScripts.loadConfigRollback(redisCommands)
.thenCompose(sha -> {
String[] keys = {namespace};
String[] values = {configId, String.valueOf(targetVersion)};
RedisFuture<Boolean> redisFuture = redisCommands.evalsha(sha, ScriptOutputType.BOOLEAN, keys, values);
return redisFuture;
});
return ConfigRedisScripts.doConfigRollback(redisCommands, sha -> {
String[] keys = {namespace};
String[] values = {configId, String.valueOf(targetVersion)};
return redisCommands.evalsha(sha, ScriptOutputType.BOOLEAN, keys, values);
});
}

private final static int HISTORY_STOP = HISTORY_SIZE - 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package me.ahoo.govern.core.redis;

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.RedisChannelHandler;
import io.lettuce.core.RedisConnectionStateListener;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import me.ahoo.govern.core.util.RedisScripts;

import java.io.Closeable;
import java.io.IOException;
import java.net.SocketAddress;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author ahoo wang
*/
@Slf4j
public class RedisScriptInitializer implements Closeable {

private final AbstractRedisClient redisClient;
private final RedisStateListener redisStateListener;

public RedisScriptInitializer(AbstractRedisClient redisClient) {
this.redisClient = redisClient;
this.redisStateListener = new RedisStateListener();
redisClient.addListener(redisStateListener);
}

/**
* Closes this stream and releases any system resources associated
* with it. If the stream is already closed then invoking this
* method has no effect.
*
* <p> As noted in {@link AutoCloseable#close()}, cases where the
* close may fail require careful attention. It is strongly advised
* to relinquish the underlying resources and to internally
* <em>mark</em> the {@code Closeable} as closed, prior to throwing
* the {@code IOException}.
*
* @throws IOException if an I/O error occurs
*/
@Override
public void close() throws IOException {
this.redisClient.removeListener(redisStateListener);
}

private static class RedisStateListener implements RedisConnectionStateListener {

private final Set<Object> connectedMap = ConcurrentHashMap.newKeySet();

/**
* Event handler for disconnection event.
*
* @param connection Source connection.
*/
@Override
public void onRedisDisconnected(RedisChannelHandler<?, ?> connection) {
}

@Override
public void onRedisConnected(RedisChannelHandler<?, ?> connection, SocketAddress socketAddress) {
var added = connectedMap.add(connection);
if (!added) {
if (log.isWarnEnabled()) {
log.warn("onRedisConnected - [{}] - [{}] - RedisScripts.clearScript.", connection, socketAddress);
}
RedisScripts.clearScript();
}
}

/**
* Event handler for exceptions.
*
* @param connection Source connection.
* @param cause Caught exception.
*/
@Override
public void onRedisExceptionCaught(RedisChannelHandler<?, ?> connection, Throwable cause) {
log.error("onRedisExceptionCaught.", cause);
}
}
}
Loading

0 comments on commit dfe676f

Please sign in to comment.