From 4b9249e4d7e8c4da2c936a40d03ab90df8f71e60 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Thu, 29 Jul 2021 12:46:34 +0600 Subject: [PATCH] ROLE command (#2607) --- .../redis/clients/jedis/BinaryClient.java | 4 ++ .../java/redis/clients/jedis/BinaryJedis.java | 7 +++ .../redis/clients/jedis/BuilderFactory.java | 12 ++++ src/main/java/redis/clients/jedis/Jedis.java | 7 +++ .../java/redis/clients/jedis/Protocol.java | 2 +- .../commands/AdvancedBinaryJedisCommands.java | 2 + .../jedis/commands/AdvancedJedisCommands.java | 3 + .../tests/commands/ControlCommandsTest.java | 58 +++++++++++++++++++ 8 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index b746a6c665..eacebabb3a 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -1084,6 +1084,10 @@ public void slaveofNoOne() { sendCommand(SLAVEOF, NO.getRaw(), ONE.getRaw()); } + public void role() { + sendCommand(ROLE); + } + public void configGet(final byte[] pattern) { sendCommand(CONFIG, Keyword.GET.getRaw(), pattern); } diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index af735cca5c..a370eb049a 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -3564,6 +3564,13 @@ public String slaveofNoOne() { return client.getStatusCodeReply(); } + @Override + public List roleBinary() { + checkIsInMultiOrPipeline(); + client.role(); + return BuilderFactory.RAW_OBJECT_LIST.build(client.getOne()); + } + /** * Retrieve the configuration of a running Redis server. Not all the configuration parameters are * supported. diff --git a/src/main/java/redis/clients/jedis/BuilderFactory.java b/src/main/java/redis/clients/jedis/BuilderFactory.java index d8a370ff14..04f2d20ec2 100644 --- a/src/main/java/redis/clients/jedis/BuilderFactory.java +++ b/src/main/java/redis/clients/jedis/BuilderFactory.java @@ -68,6 +68,18 @@ public String toString() { } }; + public static final Builder> ENCODED_OBJECT_LIST = new Builder>() { + @Override + public List build(Object data) { + return (List) SafeEncoder.encodeObject(data); + } + + @Override + public String toString() { + return "List"; + } + }; + public static final Builder LONG = new Builder() { @Override public Long build(Object data) { diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index d3c52f9af7..3bedd81ba0 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -3148,6 +3148,13 @@ public Long bitpos(final String key, final boolean value, final BitPosParams par return client.getIntegerReply(); } + @Override + public List role() { + checkIsInMultiOrPipeline(); + client.role(); + return BuilderFactory.ENCODED_OBJECT_LIST.build(client.getOne()); + } + /** * Retrieve the configuration of a running Redis server. Not all the configuration parameters are * supported. diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 68f5da1ecf..e808d3260d 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -268,7 +268,7 @@ public static enum Command implements ProtocolCommand { READONLY, READWRITE, GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS, GEORADIUS_RO, GEORADIUSBYMEMBER, GEORADIUSBYMEMBER_RO, MODULE, BITFIELD, HSTRLEN, TOUCH, SWAPDB, MEMORY, XADD, XLEN, XDEL, XTRIM, XRANGE, XREVRANGE, XREAD, XACK, XGROUP, XREADGROUP, XPENDING, XCLAIM, XAUTOCLAIM, ACL, XINFO, - BITFIELD_RO, LPOS, SMISMEMBER, ZMSCORE, BZPOPMIN, BZPOPMAX, BLMOVE, LMOVE, COPY; + BITFIELD_RO, LPOS, SMISMEMBER, ZMSCORE, BZPOPMIN, BZPOPMAX, BLMOVE, LMOVE, COPY, ROLE; private final byte[] raw; diff --git a/src/main/java/redis/clients/jedis/commands/AdvancedBinaryJedisCommands.java b/src/main/java/redis/clients/jedis/commands/AdvancedBinaryJedisCommands.java index 47c01bd1fd..c245e4773c 100644 --- a/src/main/java/redis/clients/jedis/commands/AdvancedBinaryJedisCommands.java +++ b/src/main/java/redis/clients/jedis/commands/AdvancedBinaryJedisCommands.java @@ -11,6 +11,8 @@ public interface AdvancedBinaryJedisCommands { + List roleBinary(); + List configGet(byte[] pattern); /** diff --git a/src/main/java/redis/clients/jedis/commands/AdvancedJedisCommands.java b/src/main/java/redis/clients/jedis/commands/AdvancedJedisCommands.java index de9a576a0a..1db033f275 100644 --- a/src/main/java/redis/clients/jedis/commands/AdvancedJedisCommands.java +++ b/src/main/java/redis/clients/jedis/commands/AdvancedJedisCommands.java @@ -12,6 +12,9 @@ import redis.clients.jedis.util.Slowlog; public interface AdvancedJedisCommands { + + List role(); + List configGet(String pattern); String configSet(String parameter, String value); diff --git a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java index 04c77fec87..307f11b1b1 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java @@ -17,10 +17,13 @@ import org.junit.Test; import redis.clients.jedis.DebugParams; +import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisMonitor; import redis.clients.jedis.args.ClientPauseMode; import redis.clients.jedis.exceptions.JedisDataException; +import redis.clients.jedis.tests.HostAndPortUtil; +import redis.clients.jedis.tests.utils.AssertUtil; import redis.clients.jedis.util.SafeEncoder; public class ControlCommandsTest extends JedisCommandTestBase { @@ -87,6 +90,61 @@ public void readwrite() { } } + @Test + public void roleMaster() { + try (Jedis master = new Jedis(HostAndPortUtil.getRedisServers().get(0), + DefaultJedisClientConfig.builder().password("foobared").build())) { + + List role = master.role(); + assertEquals("master", role.get(0)); + assertTrue(role.get(1) instanceof Long); + assertTrue(role.get(2) instanceof List); + + // binary + List brole = master.roleBinary(); + assertArrayEquals("master".getBytes(), (byte[]) brole.get(0)); + assertTrue(brole.get(1) instanceof Long); + assertTrue(brole.get(2) instanceof List); + } + } + + @Test + public void roleSlave() { + try (Jedis slave = new Jedis(HostAndPortUtil.getRedisServers().get(4), + DefaultJedisClientConfig.builder().password("foobared").build())) { + + List role = slave.role(); + assertEquals("slave", role.get(0)); + assertEquals((long) HostAndPortUtil.getRedisServers().get(0).getPort(), role.get(2)); + assertEquals("connected", role.get(3)); + assertTrue(role.get(4) instanceof Long); + + // binary + List brole = slave.roleBinary(); + assertArrayEquals("slave".getBytes(), (byte[]) brole.get(0)); + assertEquals((long) HostAndPortUtil.getRedisServers().get(0).getPort(), brole.get(2)); + assertArrayEquals("connected".getBytes(), (byte[]) brole.get(3)); + assertTrue(brole.get(4) instanceof Long); + } + } + + @Test + public void roleSentinel() { + try (Jedis sentinel = new Jedis(HostAndPortUtil.getSentinelServers().get(0))) { + + List role = sentinel.role(); + assertEquals("sentinel", role.get(0)); + assertTrue(role.get(1) instanceof List); + assertTrue(((List) role.get(1)).contains("mymaster")); + + // binary + List brole = sentinel.roleBinary(); + assertArrayEquals("sentinel".getBytes(), (byte[]) brole.get(0)); + assertTrue(brole.get(1) instanceof List); + AssertUtil.assertCollectionContains((List) brole.get(1), "mymaster".getBytes()); + } + } + @Test public void monitor() { new Thread(new Runnable() {