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

Add CLUSTER SHARDS command support (#2984) #3598

Merged
merged 7 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -8826,12 +8826,20 @@ public String clusterFailover(ClusterFailoverOption failoverOption) {
}

@Override
@Deprecated
public List<Object> clusterSlots() {
checkIsInMultiOrPipeline();
connection.sendCommand(CLUSTER, ClusterKeyword.SLOTS);
return connection.getObjectMultiBulkReply();
}

@Override
public List<Object> clusterShards() {
checkIsInMultiOrPipeline();
connection.sendCommand(CLUSTER, ClusterKeyword.SHARDS);
return connection.getObjectMultiBulkReply();
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public String clusterMyId() {
checkIsInMultiOrPipeline();
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 @@ -357,7 +357,7 @@ public static enum ClusterKeyword implements Rawable {
MEET, RESET, INFO, FAILOVER, SLOTS, NODES, REPLICAS, SLAVES, MYID, ADDSLOTS, DELSLOTS,
GETKEYSINSLOT, SETSLOT, NODE, MIGRATING, IMPORTING, STABLE, FORGET, FLUSHSLOTS, KEYSLOT,
COUNTKEYSINSLOT, SAVECONFIG, REPLICATE, LINKS, ADDSLOTSRANGE, DELSLOTSRANGE, BUMPEPOCH,
MYSHARDID;
MYSHARDID, SHARDS;

private final byte[] raw;

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/redis/clients/jedis/commands/ClusterCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,24 @@ public interface ClusterCommands {

String clusterFailover(ClusterFailoverOption failoverOption);

/**
* {@code CLUSTER SLOTS} command is deprecated since Redis 7.
*
* @deprecated Use {@link ClusterCommands#clusterShards()}.
*/
@Deprecated
List<Object> clusterSlots();

/**
* {@code CLUSTER SHARDS} returns details about the shards of the cluster.
* This command replaces the {@code CLUSTER SLOTS} command from Redis 7,
* by providing a more efficient and extensible representation of the cluster.
*
* @return a list of shards, with each shard containing two objects, 'slots' and 'nodes'.
* @see <a href="https://redis.io/commands/cluster-shards/">CLUSTER SHARDS</a>
*/
List<Object> clusterShards();

String clusterReset();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,39 @@ public void clusterSlots() {
node1.clusterDelSlots(3000, 3001, 3002);
}

@Test
public void clusterShards() {
assertEquals("OK", node1.clusterAddSlots(3100, 3101, 3102));

List<Object> shards = node1.clusterShards();
assertNotNull(shards);
assertTrue(shards.size() > 0);

for (Object shardInfoObj : shards) {
assertNotNull(shardInfoObj);
List<Object> shardInfo = (List<Object>) shardInfoObj;
assertTrue(shardInfo.size() >= 4);

assertTrue(shardInfo.get(0) instanceof byte[]);
assertEquals("slots", new String((byte[]) shardInfo.get(0)));

List<Object> slots = (List<Object>) shardInfo.get(1);
for (Object slotInfoObj : slots) {
assertTrue(slotInfoObj instanceof Long);
}

assertTrue(shardInfo.get(2) instanceof byte[]);
assertEquals("nodes", new String((byte[]) shardInfo.get(2)));

List<Object> nodes = (List<Object>) shardInfo.get(3);
for (Object nodeInfoObj : nodes) {
List<Object> nodeInfo = (List<Object>) nodeInfoObj;
assertTrue(nodeInfo.size() >= 14);
}
}
node1.clusterDelSlots(3100, 3101, 3102);
}

@Test
public void clusterLinks() throws InterruptedException {
List<Map<String, Object>> links = node1.clusterLinks();
Expand Down