Search in sources :

Example 6 with JedisCluster

use of redis.clients.jedis.JedisCluster in project jedis by xetorthio.

the class JedisClusterTest method testAskResponse.

@Test
public void testAskResponse() throws InterruptedException {
    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
    jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
    JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
    int slot51 = JedisClusterCRC16.getSlot("51");
    node3.clusterSetSlotImporting(slot51, JedisClusterTestUtil.getNodeId(node2.clusterNodes()));
    node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes()));
    jc.set("51", "foo");
    assertEquals("foo", jc.get("51"));
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 7 with JedisCluster

use of redis.clients.jedis.JedisCluster in project jedis by xetorthio.

the class JedisClusterTest method testMigrateToNewNode.

@Test
public void testMigrateToNewNode() throws InterruptedException {
    log.info("test migrate slot to new node");
    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
    jedisClusterNode.add(nodeInfo1);
    JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
    node4.clusterMeet(localHost, nodeInfo1.getPort());
    String node3Id = JedisClusterTestUtil.getNodeId(node3.clusterNodes());
    String node4Id = JedisClusterTestUtil.getNodeId(node4.clusterNodes());
    JedisClusterTestUtil.waitForClusterReady(node4);
    node3.clusterSetSlotMigrating(15363, node4Id);
    node4.clusterSetSlotImporting(15363, node3Id);
    try {
        node4.set("e", "e");
    } catch (JedisMovedDataException jme) {
        assertEquals(15363, jme.getSlot());
        assertEquals(new HostAndPort(localHost, nodeInfo3.getPort()), jme.getTargetNode());
    }
    try {
        node3.set("e", "e");
    } catch (JedisAskDataException jae) {
        assertEquals(15363, jae.getSlot());
        assertEquals(new HostAndPort(localHost, nodeInfo4.getPort()), jae.getTargetNode());
    }
    jc.set("e", "e");
    try {
        node4.get("e");
    } catch (JedisMovedDataException jme) {
        assertEquals(15363, jme.getSlot());
        assertEquals(new HostAndPort(localHost, nodeInfo3.getPort()), jme.getTargetNode());
    }
    try {
        node3.get("e");
    } catch (JedisAskDataException jae) {
        assertEquals(15363, jae.getSlot());
        assertEquals(new HostAndPort(localHost, nodeInfo4.getPort()), jae.getTargetNode());
    }
    assertEquals("e", jc.get("e"));
    node4.clusterSetSlotNode(15363, node4Id);
    node3.clusterSetSlotNode(15363, node4Id);
    // assertEquals("e", jc.get("e"));
    assertEquals("e", node4.get("e"));
// assertEquals("e", node3.get("e"));
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 8 with JedisCluster

use of redis.clients.jedis.JedisCluster in project jedis by xetorthio.

the class JedisClusterTest method testCloseable.

@Test
public void testCloseable() throws IOException {
    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
    jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort()));
    JedisCluster jc = null;
    try {
        jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
        jc.set("51", "foo");
    } finally {
        if (jc != null) {
            jc.close();
        }
    }
    Iterator<JedisPool> poolIterator = jc.getClusterNodes().values().iterator();
    while (poolIterator.hasNext()) {
        JedisPool pool = poolIterator.next();
        try {
            pool.getResource();
            fail("JedisCluster's internal pools should be already destroyed");
        } catch (JedisConnectionException e) {
        // ok to go...
        }
    }
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) JedisPool(redis.clients.jedis.JedisPool) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 9 with JedisCluster

use of redis.clients.jedis.JedisCluster in project jedis by xetorthio.

the class JedisClusterTest method testJedisClusterRunsWithMultithreaded.

@Test
public void testJedisClusterRunsWithMultithreaded() throws InterruptedException, ExecutionException, IOException {
    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
    jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
    final JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
    jc.set("foo", "bar");
    ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
    List<Future<String>> futures = new ArrayList<Future<String>>();
    for (int i = 0; i < 50; i++) {
        executor.submit(new Callable<String>() {

            @Override
            public String call() throws Exception {
                // random connection also does work
                return jc.get("foo");
            }
        });
    }
    for (Future<String> future : futures) {
        String value = future.get();
        assertEquals("bar", value);
    }
    jc.close();
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 10 with JedisCluster

use of redis.clients.jedis.JedisCluster in project jedis by xetorthio.

the class JedisClusterTest method testClusterCountKeysInSlot.

@Test
public void testClusterCountKeysInSlot() {
    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
    jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort()));
    JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG);
    for (int index = 0; index < 5; index++) {
        jc.set("foo{bar}" + index, "hello");
    }
    int slot = JedisClusterCRC16.getSlot("foo{bar}");
    assertEquals(DEFAULT_REDIRECTIONS, node1.clusterCountKeysInSlot(slot).intValue());
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisCluster(redis.clients.jedis.JedisCluster) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Aggregations

JedisCluster (redis.clients.jedis.JedisCluster)26 HostAndPort (redis.clients.jedis.HostAndPort)23 Test (org.junit.Test)19 HashSet (java.util.HashSet)18 LinkedHashSet (java.util.LinkedHashSet)18 Jedis (redis.clients.jedis.Jedis)7 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)6 JedisPool (redis.clients.jedis.JedisPool)5 Before (org.junit.Before)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HeartbeatInfo (com.sohu.tv.cachecloud.client.basic.heartbeat.HeartbeatInfo)1 Map (java.util.Map)1 Random (java.util.Random)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)1 RedisClusterState (org.apache.storm.redis.trident.state.RedisClusterState)1