Skip to content

Commit 0ac60c7

Browse files
committed
fix
test test Add support for AI.SCRIPTSTORE command fix fix fix fix fix fix fix cleanup
1 parent bb63302 commit 0ac60c7

File tree

7 files changed

+153
-10
lines changed

7 files changed

+153
-10
lines changed

.github/workflows/build.yml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,38 @@ on:
1111
jobs:
1212
build:
1313
runs-on: ubuntu-latest
14+
services:
15+
redismod:
16+
image: redislabs/redismod:edge
17+
options: >-
18+
--health-cmd "redis-cli ping"
19+
--health-interval 10s
20+
--health-timeout 5s
21+
--health-retries 5
22+
ports:
23+
- 52567:6379
24+
redisai:
25+
image: redislabs/redisai:edge-cpu-bionic
26+
options: >-
27+
--health-cmd "redis-cli ping"
28+
--health-interval 10s
29+
--health-timeout 5s
30+
--health-retries 5
31+
ports:
32+
- 52568:6379
1433
steps:
1534
- uses: actions/checkout@v2
1635
- name: Set up JDK 1.8
1736
uses: actions/setup-java@v1
1837
with:
1938
java-version: 1.8
20-
- name: Run redismod docker
21-
run: docker run --name redismod -p 52567:6379 -d redislabs/redismod:edge
22-
39+
- name: Cache Maven Repos
40+
uses: actions/cache@v2
41+
with:
42+
path: ~/.m2/repository
43+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
44+
restore-keys: |
45+
${{ runner.os }}-maven-
2346
- name: Build with Maven
24-
run: mvn clean install -DREDIS_HOST=redismod -DREDIS_PORT=52567 -Dgpg.skip --file pom.xml
25-
47+
run: mvn clean install -DREDIS_HOST=localhost -DREDIS_PORT=52567 -DREDISAI_PORT=52568 -Dgpg.skip --file pom.xml
2648
- uses: codecov/codecov-action@v1

redisai/src/main/java/io/github/dengliming/redismodule/redisai/RedisAI.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.github.dengliming.redismodule.common.util.RAssert;
2020
import io.github.dengliming.redismodule.redisai.args.SetModelArgs;
21+
import io.github.dengliming.redismodule.redisai.args.StoreScriptArgs;
2122
import io.github.dengliming.redismodule.redisai.model.Model;
2223
import io.github.dengliming.redismodule.redisai.model.Script;
2324
import io.github.dengliming.redismodule.redisai.model.Tensor;
@@ -42,6 +43,7 @@
4243
import static io.github.dengliming.redismodule.redisai.protocol.RedisCommands.AI_SCRIPTGET;
4344
import static io.github.dengliming.redismodule.redisai.protocol.RedisCommands.AI_SCRIPTRUN;
4445
import static io.github.dengliming.redismodule.redisai.protocol.RedisCommands.AI_SCRIPTSET;
46+
import static io.github.dengliming.redismodule.redisai.protocol.RedisCommands.AI_SCRIPTSTORE;
4547
import static io.github.dengliming.redismodule.redisai.protocol.RedisCommands.AI_TENSORGET;
4648
import static io.github.dengliming.redismodule.redisai.protocol.RedisCommands.AI_TENSORSET;
4749

@@ -124,8 +126,10 @@ public RFuture<Boolean> setModelAsync(String key, SetModelArgs modelArgs) {
124126
* @param key
125127
* @param device
126128
* @param source
129+
* @deprecated This command is deprecated and will not be available in future versions.
127130
* @return
128131
*/
132+
@Deprecated
129133
public boolean setScript(String key, Device device, String source) {
130134
return this.setScript(key, device, source, null);
131135
}
@@ -309,6 +313,28 @@ public RFuture<Script> getScriptAsync(String key) {
309313
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, AI_SCRIPTGET, key, Keywords.META, Keywords.SOURCE);
310314
}
311315

316+
/**
317+
* Stores a TorchScript as the value of a key
318+
*
319+
* @param key
320+
* @param args
321+
* @return
322+
*/
323+
public boolean storeScript(String key, StoreScriptArgs args) {
324+
return commandExecutor.get(storeScriptAsync(key, args));
325+
}
326+
327+
public RFuture<Boolean> storeScriptAsync(String key, StoreScriptArgs storeScriptArgs) {
328+
RAssert.notNull(key, "key must not be null");
329+
RAssert.notNull(storeScriptArgs, "storeScriptArgs must not be null");
330+
RAssert.notNull(storeScriptArgs.getDevice(), "device must not be null");
331+
332+
List<Object> args = new ArrayList<>();
333+
args.add(key);
334+
storeScriptArgs.build(args);
335+
return commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, AI_SCRIPTSTORE, args.toArray());
336+
}
337+
312338
public String getName() {
313339
return null;
314340
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2022 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.redisai.args;
18+
19+
import io.github.dengliming.redismodule.redisai.Device;
20+
import io.github.dengliming.redismodule.redisai.protocol.Keywords;
21+
22+
import java.util.List;
23+
24+
public class StoreScriptArgs {
25+
26+
private String tag;
27+
28+
private Device device;
29+
30+
private List<String> entryPoints;
31+
32+
private String script;
33+
34+
public Device getDevice() {
35+
return device;
36+
}
37+
38+
public List<String> getEntryPoints() {
39+
return entryPoints;
40+
}
41+
42+
public String getScript() {
43+
return script;
44+
}
45+
46+
public String getTag() {
47+
return tag;
48+
}
49+
50+
public StoreScriptArgs device(Device device) {
51+
this.device = device;
52+
return this;
53+
}
54+
55+
public StoreScriptArgs tag(String tag) {
56+
this.tag = tag;
57+
return this;
58+
}
59+
60+
public StoreScriptArgs script(String script) {
61+
this.script = script;
62+
return this;
63+
}
64+
65+
public StoreScriptArgs entryPoints(List<String> entryPoints) {
66+
this.entryPoints = entryPoints;
67+
return this;
68+
}
69+
70+
public void build(List<Object> args) {
71+
if (device != null) {
72+
args.add(device);
73+
}
74+
if (tag != null) {
75+
args.add(Keywords.TAG);
76+
args.add(tag);
77+
}
78+
79+
if (entryPoints != null && !entryPoints.isEmpty()) {
80+
args.add(Keywords.ENTRY_POINTS);
81+
args.add(entryPoints.size());
82+
for (String entryPoint : entryPoints) {
83+
args.add(entryPoint);
84+
}
85+
}
86+
87+
if (script != null) {
88+
args.add(Keywords.SOURCE);
89+
args.add(script);
90+
}
91+
}
92+
}

redisai/src/main/java/io/github/dengliming/redismodule/redisai/protocol/Keywords.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
*/
2222
public enum Keywords {
2323

24-
BLOB, VALUES, META, TAG, BATCHSIZE, MINBATCHSIZE, INPUTS, OUTPUTS, SOURCE, LOAD, PERSIST, RESETSTAT, BACKENDSPATH, LOADBACKEND;
24+
BLOB, VALUES, META, TAG, BATCHSIZE, MINBATCHSIZE, INPUTS, OUTPUTS, SOURCE, LOAD, PERSIST, RESETSTAT, BACKENDSPATH, LOADBACKEND, ENTRY_POINTS
2525

2626
}

redisai/src/main/java/io/github/dengliming/redismodule/redisai/protocol/RedisCommands.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ public interface RedisCommands {
4848
RedisCommand AI_INFO = new RedisCommand<>("AI.INFO", new ObjectMapReplayDecoder());
4949
RedisCommand AI_INFO_RESETSTAT = new RedisCommand<>("AI.INFO", new BooleanReplayConvertor());
5050
RedisCommand AI_CONFIG = new RedisCommand<>("AI.CONFIG", new BooleanReplayConvertor());
51+
RedisCommand AI_SCRIPTSTORE = new RedisCommand<>("AI.SCRIPTSTORE", new BooleanReplayConvertor());
5152
}

redisai/src/test/java/io/github/dengliming/redismodule/redisai/AbstractTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void init() {
3535
config.useSingleServer()
3636
.setConnectTimeout(10000)
3737
.setTimeout(10000)
38-
.setAddress("redis://" + TestSettings.host() + ":" + TestSettings.port());
38+
.setAddress("redis://" + TestSettings.host() + ":" + System.getProperty("REDISAI_PORT", TestSettings.port() + ""));
3939
redisAIClient = new RedisAIClient(config);
4040
redisAIClient.flushall();
4141
}

redisai/src/test/java/io/github/dengliming/redismodule/redisai/RedisAITest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import io.github.dengliming.redismodule.redisai.model.Model;
2121
import io.github.dengliming.redismodule.redisai.model.Script;
2222
import io.github.dengliming.redismodule.redisai.model.Tensor;
23+
import org.junit.jupiter.api.Disabled;
2324
import org.junit.jupiter.api.Test;
2425
import org.redisson.client.RedisException;
26+
2527
import java.nio.file.Files;
2628
import java.nio.file.Paths;
2729
import java.util.Arrays;
@@ -68,10 +70,11 @@ public void testModel() throws Exception {
6870
}
6971

7072
@Test
73+
@Disabled("AI.SCRIPTSET This command is deprecated and will not be available in future versions.")
7174
public void testScript() {
7275
RedisAI redisAI = getRedisAI();
7376
String key = "script1";
74-
String script = "def bar(a, b):\n" + " return a + b\n";
77+
String script = "def bar(a, b):\n return a + b\n";
7578
// Set Script
7679
assertThat(redisAI.setScript(key, Device.CPU, script)).isTrue();
7780

@@ -103,14 +106,13 @@ public void testInfo() {
103106
String script = "def bar(a, b):\n" + " return a + b\n";
104107
assertThat(redisAI.setScript(key, Device.CPU, script)).isTrue();
105108
// not exist
106-
Map<String, Object> infoMap;
107109
assertThrows(RedisException.class, () -> {
108110
// ERR cannot find run info for key
109111
redisAI.getInfo("not:exist");
110112
});
111113

112114
// first inited info
113-
infoMap = redisAI.getInfo(key);
115+
Map<String, Object> infoMap = redisAI.getInfo(key);
114116
assertThat(infoMap).isNotNull();
115117
assertThat(infoMap.get("key")).isEqualTo(key);
116118
assertThat(infoMap.get("device")).isEqualTo(Device.CPU.name());

0 commit comments

Comments
 (0)