Skip to content

Commit

Permalink
Support cluster addslotsrange and cluster delslotsrange (#2823)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeekling authored Jan 18, 2022
1 parent 2402305 commit 18043de
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -8340,6 +8340,22 @@ public List<Map<String, Object>> clusterLinks() {
.map(BuilderFactory.ENCODED_OBJECT_MAP::build).collect(Collectors.toList());
}

@Override
public String clusterAddSlotsRange(int... ranges) {
checkIsInMultiOrPipeline();
connection.sendCommand(CLUSTER,
joinParameters(ClusterKeyword.ADDSLOTSRANGE.getRaw(), joinParameters(ranges)));
return connection.getStatusCodeReply();
}

@Override
public String clusterDelSlotsRange(int... ranges) {
checkIsInMultiOrPipeline();
connection.sendCommand(CLUSTER,
joinParameters(ClusterKeyword.DELSLOTSRANGE.getRaw(), joinParameters(ranges)));
return connection.getStatusCodeReply();
}

@Override
public String asking() {
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 @@ -314,7 +314,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;
COUNTKEYSINSLOT, SAVECONFIG, REPLICATE, LINKS, ADDSLOTSRANGE, DELSLOTSRANGE;

private final byte[] raw;

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

/**
* {@code CLUSTER SLAVES} command is deprecated since Redis 5.
*
* @deprecated Use {@link ClusterCommands#clusterReplicas(java.lang.String)}.
*/
@Deprecated
Expand All @@ -67,6 +68,7 @@ public interface ClusterCommands {

/**
* {@code resetType} can be null for default behavior.
*
* @param resetType
* @return OK
*/
Expand All @@ -82,4 +84,20 @@ public interface ClusterCommands {
* @see <a href="https://redis.io/commands/cluster-links" >CLUSTET LINKS</a>
*/
List<Map<String, Object>> clusterLinks();

/**
* Takes a list of slot ranges (specified by start and end slots) to assign to the node
*
* @param ranges slots range
* @return OK if the command was successful. Otherwise an error is returned.
*/
String clusterAddSlotsRange(int... ranges);

/**
* Takes a list of slot ranges (specified by start and end slots) to remove to the node.
*
* @param ranges slots range
* @return OK if the command was successful. Otherwise an error is returned.
*/
String clusterDelSlotsRange(int... ranges);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
Expand Down Expand Up @@ -112,6 +109,32 @@ public void clusterInfo() {
assertNotNull(info);
}

@Test
public void addAndDelSlotsRange() {
// test add
String res = node1.clusterAddSlotsRange(0, 5);
Assert.assertEquals("OK", res);

String clusterNodes = node1.clusterNodes();
Assert.assertTrue(clusterNodes.endsWith("connected 0-5\n"));

res = node1.clusterAddSlotsRange(10, 20);
Assert.assertEquals("OK", res);
clusterNodes = node1.clusterNodes();
Assert.assertTrue(clusterNodes.endsWith("connected 0-5 10-20\n"));

// test del
String resDel = node1.clusterDelSlotsRange(0, 5);
Assert.assertEquals("OK", resDel);
clusterNodes = node1.clusterNodes();
Assert.assertTrue(clusterNodes.endsWith("connected 10-20\n"));

resDel = node1.clusterDelSlotsRange(10, 20);
Assert.assertEquals("OK", resDel);
clusterNodes = node1.clusterNodes();
Assert.assertTrue(clusterNodes.endsWith("connected\n"));
}

@Test
public void clusterGetKeysInSlot() {
node1.clusterAddSlots(500);
Expand Down

0 comments on commit 18043de

Please sign in to comment.