Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROLE command #2607

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3561,6 +3561,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 @@ -3145,6 +3145,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();

long move(byte[] key, int dbIndex);

List<byte[]> configGet(byte[] pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

public interface AdvancedJedisCommands {

List<Object> role();

long move(String key, int dbIndex);

List<String> configGet(String pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
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.Protocol;
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 @@ -88,6 +91,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