Update ClusterLuaScript formatting
This commit is contained in:
parent
715d1157ad
commit
2383aaaa3d
|
@ -29,7 +29,10 @@ public class ClusterLuaScript {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(ClusterLuaScript.class);
|
private static final Logger log = LoggerFactory.getLogger(ClusterLuaScript.class);
|
||||||
|
|
||||||
public static ClusterLuaScript fromResource(final FaultTolerantRedisCluster redisCluster, final String resource, final ScriptOutputType scriptOutputType) throws IOException {
|
public static ClusterLuaScript fromResource(final FaultTolerantRedisCluster redisCluster,
|
||||||
|
final String resource,
|
||||||
|
final ScriptOutputType scriptOutputType) throws IOException {
|
||||||
|
|
||||||
try (final InputStream inputStream = LuaScript.class.getClassLoader().getResourceAsStream(resource);
|
try (final InputStream inputStream = LuaScript.class.getClassLoader().getResourceAsStream(resource);
|
||||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||||
|
|
||||||
|
@ -45,7 +48,10 @@ public class ClusterLuaScript {
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
ClusterLuaScript(final FaultTolerantRedisCluster redisCluster, final String script, final ScriptOutputType scriptOutputType) {
|
ClusterLuaScript(final FaultTolerantRedisCluster redisCluster,
|
||||||
|
final String script,
|
||||||
|
final ScriptOutputType scriptOutputType) {
|
||||||
|
|
||||||
this.redisCluster = redisCluster;
|
this.redisCluster = redisCluster;
|
||||||
this.scriptOutputType = scriptOutputType;
|
this.scriptOutputType = scriptOutputType;
|
||||||
this.script = script;
|
this.script = script;
|
||||||
|
@ -76,10 +82,12 @@ public class ClusterLuaScript {
|
||||||
final RedisAdvancedClusterCommands<byte[], byte[]> binaryCommands = connection.sync();
|
final RedisAdvancedClusterCommands<byte[], byte[]> binaryCommands = connection.sync();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return binaryCommands.evalsha(sha, scriptOutputType, keys.toArray(BYTE_ARRAY_ARRAY), args.toArray(BYTE_ARRAY_ARRAY));
|
return binaryCommands
|
||||||
|
.evalsha(sha, scriptOutputType, keys.toArray(BYTE_ARRAY_ARRAY), args.toArray(BYTE_ARRAY_ARRAY));
|
||||||
} catch (final RedisNoScriptException e) {
|
} catch (final RedisNoScriptException e) {
|
||||||
reloadScript();
|
reloadScript();
|
||||||
return binaryCommands.evalsha(sha, scriptOutputType, keys.toArray(BYTE_ARRAY_ARRAY), args.toArray(BYTE_ARRAY_ARRAY));
|
return binaryCommands
|
||||||
|
.evalsha(sha, scriptOutputType, keys.toArray(BYTE_ARRAY_ARRAY), args.toArray(BYTE_ARRAY_ARRAY));
|
||||||
}
|
}
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
log.warn("Failed to execute script", e);
|
log.warn("Failed to execute script", e);
|
||||||
|
|
|
@ -31,16 +31,22 @@ public class ClusterLuaScriptTest extends AbstractRedisClusterTest {
|
||||||
|
|
||||||
final FaultTolerantRedisCluster redisCluster = getRedisCluster();
|
final FaultTolerantRedisCluster redisCluster = getRedisCluster();
|
||||||
|
|
||||||
final ClusterLuaScript script = new ClusterLuaScript(redisCluster, "return redis.call(\"SET\", KEYS[1], ARGV[1])", ScriptOutputType.VALUE);
|
final ClusterLuaScript script = new ClusterLuaScript(redisCluster, "return redis.call(\"SET\", KEYS[1], ARGV[1])",
|
||||||
|
ScriptOutputType.VALUE);
|
||||||
|
|
||||||
assertEquals("OK", script.execute(List.of(key), List.of(value)));
|
assertEquals("OK", script.execute(List.of(key), List.of(value)));
|
||||||
assertEquals(value, redisCluster.withCluster(connection -> connection.sync().get(key)));
|
assertEquals(value, redisCluster.withCluster(connection -> connection.sync().get(key)));
|
||||||
|
|
||||||
final int slot = SlotHash.getSlot(key);
|
final int slot = SlotHash.getSlot(key);
|
||||||
|
|
||||||
final int sourcePort = redisCluster.withCluster(connection -> connection.sync().nodes(node -> node.hasSlot(slot) && node.is(RedisClusterNode.NodeFlag.UPSTREAM)).node(0).getUri().getPort());
|
final int sourcePort = redisCluster.withCluster(
|
||||||
final RedisCommands<String, String> sourceCommands = redisCluster.withCluster(connection -> connection.sync().nodes(node -> node.hasSlot(slot) && node.is(RedisClusterNode.NodeFlag.UPSTREAM)).commands(0));
|
connection -> connection.sync().nodes(node -> node.hasSlot(slot) && node.is(RedisClusterNode.NodeFlag.UPSTREAM))
|
||||||
final RedisCommands<String, String> destinationCommands = redisCluster.withCluster(connection -> connection.sync().nodes(node -> !node.hasSlot(slot) && node.is(RedisClusterNode.NodeFlag.UPSTREAM)).commands(0));
|
.node(0).getUri().getPort());
|
||||||
|
final RedisCommands<String, String> sourceCommands = redisCluster.withCluster(
|
||||||
|
connection -> connection.sync().nodes(node -> node.hasSlot(slot) && node.is(RedisClusterNode.NodeFlag.UPSTREAM))
|
||||||
|
.commands(0));
|
||||||
|
final RedisCommands<String, String> destinationCommands = redisCluster.withCluster(connection -> connection.sync()
|
||||||
|
.nodes(node -> !node.hasSlot(slot) && node.is(RedisClusterNode.NodeFlag.UPSTREAM)).commands(0));
|
||||||
|
|
||||||
destinationCommands.clusterSetSlotImporting(slot, sourceCommands.clusterMyId());
|
destinationCommands.clusterSetSlotImporting(slot, sourceCommands.clusterMyId());
|
||||||
|
|
||||||
|
@ -92,7 +98,8 @@ public class ClusterLuaScriptTest extends AbstractRedisClusterTest {
|
||||||
|
|
||||||
final FaultTolerantRedisCluster redisCluster = getRedisCluster();
|
final FaultTolerantRedisCluster redisCluster = getRedisCluster();
|
||||||
|
|
||||||
final ClusterLuaScript script = new ClusterLuaScript(redisCluster, "return redis.call(\"SET\", KEYS[1], ARGV[1])", ScriptOutputType.VALUE);
|
final ClusterLuaScript script = new ClusterLuaScript(redisCluster, "return redis.call(\"SET\", KEYS[1], ARGV[1])",
|
||||||
|
ScriptOutputType.VALUE);
|
||||||
|
|
||||||
// Remove the scripts created by the CLusterLuaScript constructor
|
// Remove the scripts created by the CLusterLuaScript constructor
|
||||||
redisCluster.useCluster(connection -> connection.sync().upstream().commands().scriptFlush());
|
redisCluster.useCluster(connection -> connection.sync().upstream().commands().scriptFlush());
|
||||||
|
@ -105,7 +112,8 @@ public class ClusterLuaScriptTest extends AbstractRedisClusterTest {
|
||||||
public void testExecuteBinary() {
|
public void testExecuteBinary() {
|
||||||
final RedisAdvancedClusterCommands<String, String> stringCommands = mock(RedisAdvancedClusterCommands.class);
|
final RedisAdvancedClusterCommands<String, String> stringCommands = mock(RedisAdvancedClusterCommands.class);
|
||||||
final RedisAdvancedClusterCommands<byte[], byte[]> binaryCommands = mock(RedisAdvancedClusterCommands.class);
|
final RedisAdvancedClusterCommands<byte[], byte[]> binaryCommands = mock(RedisAdvancedClusterCommands.class);
|
||||||
final FaultTolerantRedisCluster mockCluster = RedisClusterHelper.buildMockRedisCluster(stringCommands, binaryCommands);
|
final FaultTolerantRedisCluster mockCluster = RedisClusterHelper
|
||||||
|
.buildMockRedisCluster(stringCommands, binaryCommands);
|
||||||
|
|
||||||
final String script = "return redis.call(\"SET\", KEYS[1], ARGV[1])";
|
final String script = "return redis.call(\"SET\", KEYS[1], ARGV[1])";
|
||||||
final String sha = "abc123";
|
final String sha = "abc123";
|
||||||
|
@ -129,12 +137,14 @@ public class ClusterLuaScriptTest extends AbstractRedisClusterTest {
|
||||||
|
|
||||||
final FaultTolerantRedisCluster redisCluster = getRedisCluster();
|
final FaultTolerantRedisCluster redisCluster = getRedisCluster();
|
||||||
|
|
||||||
final ClusterLuaScript script = new ClusterLuaScript(redisCluster, "return redis.call(\"SET\", KEYS[1], ARGV[1])", ScriptOutputType.VALUE);
|
final ClusterLuaScript script = new ClusterLuaScript(redisCluster, "return redis.call(\"SET\", KEYS[1], ARGV[1])",
|
||||||
|
ScriptOutputType.VALUE);
|
||||||
|
|
||||||
// Remove the scripts created by the CLusterLuaScript constructor
|
// Remove the scripts created by the CLusterLuaScript constructor
|
||||||
redisCluster.useCluster(connection -> connection.sync().upstream().commands().scriptFlush());
|
redisCluster.useCluster(connection -> connection.sync().upstream().commands().scriptFlush());
|
||||||
|
|
||||||
assertArrayEquals("OK".getBytes(StandardCharsets.UTF_8), (byte[])script.executeBinary(List.of(key.getBytes(StandardCharsets.UTF_8)), List.of(value.getBytes(StandardCharsets.UTF_8))));
|
assertArrayEquals("OK".getBytes(StandardCharsets.UTF_8), (byte[]) script
|
||||||
|
.executeBinary(List.of(key.getBytes(StandardCharsets.UTF_8)), List.of(value.getBytes(StandardCharsets.UTF_8))));
|
||||||
assertEquals(value, redisCluster.withCluster(connection -> connection.sync().get(key)));
|
assertEquals(value, redisCluster.withCluster(connection -> connection.sync().get(key)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue