Skip to content

Commit

Permalink
ROLE command (#2607)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Jul 29, 2021
1 parent ffd6d73 commit 4b9249e
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3564,6 +3564,13 @@ public String slaveofNoOne() {
return client.getStatusCodeReply();
}

@Override
public List<Object> 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.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ public String toString() {
}
};

public static final Builder<List<Object>> ENCODED_OBJECT_LIST = new Builder<List<Object>>() {
@Override
public List<Object> build(Object data) {
return (List<Object>) SafeEncoder.encodeObject(data);
}

@Override
public String toString() {
return "List<Object>";
}
};

public static final Builder<Long> LONG = new Builder<Long>() {
@Override
public Long build(Object data) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,13 @@ public Long bitpos(final String key, final boolean value, final BitPosParams par
return client.getIntegerReply();
}

@Override
public List<Object> 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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

public interface AdvancedBinaryJedisCommands {

List<Object> roleBinary();

List<byte[]> configGet(byte[] pattern);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import redis.clients.jedis.util.Slowlog;

public interface AdvancedJedisCommands {

List<Object> role();

List<String> configGet(String pattern);

String configSet(String parameter, String value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Object> role = master.role();
assertEquals("master", role.get(0));
assertTrue(role.get(1) instanceof Long);
assertTrue(role.get(2) instanceof List);

// binary
List<Object> 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<Object> 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<Object> 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<Object> role = sentinel.role();
assertEquals("sentinel", role.get(0));
assertTrue(role.get(1) instanceof List);
assertTrue(((List) role.get(1)).contains("mymaster"));

// binary
List<Object> 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() {
Expand Down

0 comments on commit 4b9249e

Please sign in to comment.