Add support for cluster reset command. Some tests were refactored per the inclusion of the new command

This commit is contained in:
Marcos Nils
2014-09-14 15:43:18 -03:00
parent 56d1798943
commit 687716902d
7 changed files with 48 additions and 24 deletions

View File

@@ -8,6 +8,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import redis.clients.jedis.JedisCluster.Reset;
import redis.clients.util.SafeEncoder;
public class Client extends BinaryClient implements Commands {
@@ -911,6 +912,10 @@ public class Client extends BinaryClient implements Commands {
cluster(Protocol.CLUSTER_MEET, ip, String.valueOf(port));
}
public void clusterReset(Reset resetType) {
cluster(Protocol.CLUSTER_RESET, resetType.toString());
}
public void clusterAddSlots(final int... slots) {
cluster(Protocol.CLUSTER_ADDSLOTS, slots);
}

View File

@@ -2,6 +2,8 @@ package redis.clients.jedis;
import java.util.List;
import redis.clients.jedis.JedisCluster.Reset;
public interface ClusterCommands {
String clusterNodes();
@@ -40,4 +42,6 @@ public interface ClusterCommands {
String clusterFailover();
List<Object> clusterSlots();
String clusterReset(Reset resetType);
}

View File

@@ -1,14 +1,22 @@
package redis.clients.jedis;
import java.net.URI;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.JedisCluster.Reset;
import redis.clients.util.Pool;
import redis.clients.util.SafeEncoder;
import redis.clients.util.Slowlog;
import java.net.URI;
import java.util.*;
import java.util.Map.Entry;
public class Jedis extends BinaryJedis implements JedisCommands,
MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands,
BasicCommands, ClusterCommands {
@@ -3298,6 +3306,12 @@ public class Jedis extends BinaryJedis implements JedisCommands,
client.clusterMeet(ip, port);
return client.getStatusCodeReply();
}
public String clusterReset(final Reset resetType) {
checkIsInMulti();
client.clusterReset(resetType);
return client.getStatusCodeReply();
}
public String clusterAddSlots(final int... slots) {
checkIsInMulti();

View File

@@ -14,6 +14,8 @@ public class JedisCluster implements JedisCommands, BasicCommands, Closeable {
public static final short HASHSLOTS = 16384;
private static final int DEFAULT_TIMEOUT = 1;
private static final int DEFAULT_MAX_REDIRECTIONS = 5;
public static enum Reset {SOFT, HARD}
private int timeout;
private int maxRedirections;

View File

@@ -42,6 +42,7 @@ public final class Protocol {
public static final String CLUSTER_NODES = "nodes";
public static final String CLUSTER_MEET = "meet";
public static final String CLUSTER_RESET = "reset";
public static final String CLUSTER_ADDSLOTS = "addslots";
public static final String CLUSTER_DELSLOTS = "delslots";
public static final String CLUSTER_INFO = "info";

View File

@@ -31,7 +31,6 @@ public class HostAndPortUtil {
clusterHostAndPortList.add(new HostAndPort("localhost", 7382));
clusterHostAndPortList.add(new HostAndPort("localhost", 7383));
clusterHostAndPortList.add(new HostAndPort("localhost", 7384));
clusterHostAndPortList.add(new HostAndPort("localhost", 7385));
String envRedisHosts = System.getProperty("redis-hosts");
String envSentinelHosts = System.getProperty("sentinel-hosts");

View File

@@ -9,9 +9,10 @@ import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.JedisCluster.Reset;
import redis.clients.jedis.tests.HostAndPortUtil;
import redis.clients.jedis.tests.JedisTestBase;
import redis.clients.jedis.tests.utils.JedisClusterTestUtil;
public class ClusterCommandsTest extends JedisTestBase {
private static Jedis node1;
@@ -42,16 +43,8 @@ public class ClusterCommandsTest extends JedisTestBase {
public static void removeSlots() throws InterruptedException {
String[] nodes = node1.clusterNodes().split("\n");
String node1Id = nodes[0].split(" ")[0];
node1.clusterDelSlots(1, 2, 3, 4, 5, 500);
node1.clusterSetSlotNode(5000, node1Id);
node1.clusterDelSlots(5000, 10000);
node1.clusterDelSlots(3000, 3001, 3002);
node2.clusterDelSlots(4000, 4001, 4002);
node1.clusterAddSlots(6000);
node1.clusterDelSlots(6000);
waitForGossip();
node2.clusterDelSlots(6000);
node1.clusterDelSlots(6000);
node1.clusterReset(Reset.SOFT);
node2.clusterReset(Reset.SOFT);
}
private static void waitForGossip() {
@@ -63,14 +56,20 @@ public class ClusterCommandsTest extends JedisTestBase {
}
}
private static int getClusterAttribute(String clusterInfo,
String attributeName) {
for (String infoElement : clusterInfo.split("\n")) {
if (infoElement.contains(attributeName)) {
return Integer.valueOf(infoElement.split(":")[1].trim());
}
}
return 0;
@Test
public void testClusterSoftReset() {
node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
assertEquals(2, node1.clusterNodes().split("\n").length);
node1.clusterReset(Reset.SOFT);
assertEquals(1, node1.clusterNodes().split("\n").length);
}
@Test
public void testClusterHardReset() {
String nodeId = JedisClusterTestUtil.getNodeId(node1.clusterNodes());
node1.clusterReset(Reset.HARD);
String newNodeId = JedisClusterTestUtil.getNodeId(node1.clusterNodes());
assertNotEquals(nodeId, newNodeId);
}
@Test